Submission #254500

#TimeUsernameProblemLanguageResultExecution timeMemory
254500nandonathanielPotatoes and fertilizers (LMIO19_bulves)C++14
100 / 100
208 ms15216 KiB
#include<bits/stdc++.h> using namespace std; const int MAXN=5e5+5; long long pref[MAXN]; int main(){ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); int n,a,b; long long ans=0; cin >> n; for(int i=1;i<=n;i++){ cin >> a >> b; pref[i]=pref[i-1]+a-b; } for(int i=1;i<=n;i++){ if(pref[i]>pref[n]){ ans+=(pref[i]-pref[n]); pref[i]=pref[n]; } else if(pref[i]<0){ ans+=(-pref[i]); pref[i]=0; } } //make pref non decreasing -> Classic Slope Trick CF713C priority_queue<long long> PQ; for(int i=1;i<=n;i++){ PQ.push(pref[i]);PQ.push(pref[i]); ans+=(PQ.top()-pref[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...