Submission #836449

#TimeUsernameProblemLanguageResultExecution timeMemory
836449uriegPotatoes and fertilizers (LMIO19_bulves)C++17
100 / 100
158 ms15228 KiB
#include<bits/stdc++.h> #define rall(x) x.rbegin(), x.rend() #define fi first #define se second #define ve vector typedef long long ll; using namespace std; int main(){ ios_base::sync_with_stdio(0);cin.tie(0); int n; ll a,b ; priority_queue<ll>q; long long ans = 0; cin>>n; vector<ll>d(n); for(int i = 0;i<n;i++){ cin>>a>>b; d[i] = a-b; if(i)d[i] += d[i-1]; } if(d[0] < 0){ ans += -d[0]; d[0]=0; } q.push(d[0]); for(int i = 1; i<n-1; i++){ if(d[i]<0){ ans += -d[i]; d[i]=0; } q.push(d[i]); if(q.top()>d[i]){ ans += q.top()-d[i]; q.push(d[i]); q.pop(); } } if(n>1){ while(q.size()>0 && q.top()>d[n-1]){ ans += q.top()-d[n-1]; q.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...