Submission #680707

#TimeUsernameProblemLanguageResultExecution timeMemory
680707PoonYaPatPotatoes and fertilizers (LMIO19_bulves)C++14
100 / 100
211 ms23056 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; int n; ll a[500001],b[500001],d[500001],ans; priority_queue<ll> q; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin>>n; for (int i=1; i<=n; ++i) { cin>>a[i]>>b[i]; d[i]=d[i-1]+a[i]-b[i]; } if (d[1]>=0) q.push(d[1]), ans+=d[1]; else q.push(0), ans-=d[1]; for (int i=2; i<=n; ++i) { if (d[i]>=q.top()) q.push(d[i]), ans+=d[i]; else { if (d[i]<0) ans-=d[i], d[i]=0; ans+=d[i]; q.push(d[i]); q.push(d[i]); q.pop(); } } //now ans is at x=Y-intersect(0), then it will be moved to x=d[n] while (!q.empty()) ans-=min(d[n],q.top()), q.pop(); cout<<ans; }
#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...