Submission #253198

#TimeUsernameProblemLanguageResultExecution timeMemory
253198rama_pangPotatoes and fertilizers (LMIO19_bulves)C++14
100 / 100
191 ms15212 KiB
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); int N; cin >> N; vector<long long> sum(N); for (int i = 0; i < N; i++) { int a, b; cin >> a >> b; sum[i] = a - b; if (i > 0) { sum[i] += sum[i - 1]; } } // We want sum[] to be non-decreasing // An operation: choose i, then sum[i] += 1 or sum[i] -= 1 // sum[0] cannot be decreased (-1 operation) and must be >= 0 // sum[N - 1] cannot be increased (+1 operation) // This is classic Slope Trick long long ans = 0; priority_queue<long long> pq; pq.emplace(0); for (int i = 0; i < N; i++) { if (sum[i] < 0) { ans += 0 - sum[i]; sum[i] = 0; } else if (sum[i] > sum[N - 1]) { ans += sum[i] - sum[N - 1]; sum[i] = sum[N - 1]; } pq.emplace(sum[i]); pq.emplace(sum[i]); ans += pq.top() - sum[i]; pq.pop(); } cout << ans << "\n"; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...