Submission #154216

#TimeUsernameProblemLanguageResultExecution timeMemory
154216AKaan37Potatoes and fertilizers (LMIO19_bulves)C++17
100 / 100
250 ms15340 KiB
#include <iostream> #include <queue> using namespace std; const int nmax=500005; priority_queue<long long> pq; long long c[nmax]; long long ans; int n,i,j,a,b; int main() { ios_base::sync_with_stdio(false); cin>>n; for(i=1;i<=n;i++) { cin>>a>>b; long long dif=a-b; c[i]=(c[i-1]+dif); } for(i=1;i<=n;i++) { if(c[i]<0) ans-=c[i],c[i]=0; if(c[i]>c[n]) ans+=(c[i]-c[n]),c[i]=c[n]; } for(i=1;i<=n;i++) { pq.push(c[i]); if(pq.top()>c[i]) { pq.push(c[i]); ans+=1LL*pq.top()-c[i]; pq.pop(); } } cout<<ans; 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...