1/*2bruce code3#include <bits/stdc++.h>4usingnamespace std;5typedeflonglong ll;6constintMM=2e5+5;7intN, M, a[MM]; string s; ll ans, loss;8intmain(){9 ios::sync_with_stdio(0); cin.tie(0);10 cin >>N>>M>> s;11for(int i=0; i<N; i++) {12 cin >> a[i]; ans += a[i];13 }14for(int i=0; i<N; i++) {15if(s[i] =='R'&& s[(i+1)%N] =='L') {16 ll sum =0;17for(int j=(i-1+N)%N; s[j]=='R'; j=(j-1+N)%N){18 sum += a[j];19 }20 loss +=min((ll)M, sum); sum =0;21for(int j=(i+2)%N; s[j]=='L'; j=(j+1)%N){22 sum += a[j];23 }24 loss +=min((ll)M, sum);25 }26 }27 cout << ans - loss <<"\n";28}*/29#include <bits/stdc++.h>30usingnamespace std;31constintMAXN=200000+5;32int n;33longlong m;34longlong cap[MAXN];35int indeg[MAXN], nxtCow[MAXN], vis[MAXN];36intmain() {37 ios::sync_with_stdio(0);38 cin.tie(0);39 cin >> n >> m;40string s;41 cin >> s;42 s ="a"+ s;// 1-index43longlong total =0;44for (int i =1; i <= n; i++) {45 cin >> cap[i];46 total += cap[i];47 }48// Build directed graph49for (int i =1; i <= n; i++) {50int j;51if (s[i] =='L') {52 j = (i ==1? n : i -1); // wrap around left53 } else {54 j = (i == n ?1: i +1); // wrap around right55 }56 nxtCow[i] = j;57 indeg[j]++; // j has one more incoming edge58 }5960// Start from all cows with indegree 0 (net givers)61for (int i =1; i <= n; i++) {62if (!vis[i] && indeg[i] ==0) {63vector<int> path;64int cur = i;65// Follow the chain until we reach a visited node(cycle)66while (!vis[cur]) {67 vis[cur] =1;68 path.push_back(cur);69 cur = nxtCow[cur];70 }71// All nodes in path before the first occurrence of cur are non-cycle72longlong nonCycleSum =0;73for (int x : path) {74if (x == cur) break;75 nonCycleSum += cap[x];76 }77// This chain can lose at most min(m, sum of its non-cycle milk)78 total -=min(m, nonCycleSum);79 }80 }81 cout << total <<'\n';82return0;83}84
☕
Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.
Python records executed lines and locals automatically. For selected values in any language, add // @trace i, total on its own valid line; Python uses # @trace i, total.
StatusReady
Output
No run yet.
Diagnostics
No diagnostics yet.
Each run is isolated and has strict limits. Passing one test does not guarantee the judge will accept the solution.