Submission #144666

#TimeUsernameProblemLanguageResultExecution timeMemory
144666brcodePotatoes and fertilizers (LMIO19_bulves)C++14
100 / 100
727 ms19400 KiB
#include <iostream> #include <queue> using namespace std; const long long MAXN = 5e5+5; long long ans; long long pref[MAXN]; long long c[MAXN]; long long n; priority_queue<long long> pq; int main(){ cin>>n; for(long long i=1;i<=n;i++){ long long a,b; cin>>a>>b; c[i] = a-b; pref[i] = pref[i-1]+c[i]; } for(long long i=1;i<=n;i++){ if(pref[i]<0){ ans+=abs(pref[i]); pref[i] = 0; } if(pref[i]>pref[n]){ ans+=pref[i]-pref[n]; pref[i]=pref[n]; } } for(long long i=1;i<=n;i++){ if(!pq.empty() && pq.top()>pref[i]){ ans+=pq.top()-pref[i]; pq.pop(); pq.push(pref[i]); } pq.push(pref[i]); } cout<<ans<<endl; }
#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...