Submission #257611

#TimeUsernameProblemLanguageResultExecution timeMemory
257611NightlightPotatoes and fertilizers (LMIO19_bulves)C++14
100 / 100
188 ms15304 KiB
#include <bits/stdc++.h> using namespace std; int N; long long pre[500005]; long long ans; int main() { ios_base::sync_with_stdio(0); cin >> N; int a, b; for(int i = 1; i <= N; i++) { cin >> a >> b; pre[i] = a - b + pre[i - 1]; } for(int i = 1; i <= N; i++) { if(pre[i] < 0) ans -= pre[i], pre[i] = 0; else if(pre[i] > pre[N]) ans += pre[i] - pre[N], pre[i] = pre[N]; } priority_queue<long long, vector<long long>> pq; pq.emplace(pre[1]); for(int i = 2; i <= N; i++) { if(pre[i] >= pq.top()) { pq.emplace(pre[i]); }else { ans += (pq.top() - pre[i]); pq.emplace(pre[i]); pq.emplace(pre[i]); pq.pop(); } } cout << ans << "\n"; cin >> N; }
#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...