Submission #940896

#TimeUsernameProblemLanguageResultExecution timeMemory
94089612345678Potatoes and fertilizers (LMIO19_bulves)C++17
24 / 100
73 ms18772 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long const int nx=5e5+5; ll n, a[nx], b[nx], d[nx], res; priority_queue<ll> pq; int main() { cin.tie(NULL)->sync_with_stdio(false); cin>>n; for (int i=1; i<=n; i++) cin>>a[i]>>b[i], d[i]=d[i-1]+a[i]-b[i]; for (int i=1; i<n; i++) { //cout<<"val "<<i<<' '<<d[i]<<' '<<d[n]<<'\n'; if (pq.empty()) { if (d[i]<=0) res-=d[i]; else if (d[i]>=d[n]) res+=d[i]-d[n]; else pq.push(d[i]); //cout<<i<<' '<<res<<'\n'; } else { if (d[i]>=d[n]) res+=(d[i]-d[n]), pq.push(min(d[n], d[i])); else if (d[i]>=pq.top()) pq.push(d[i]); else if (d[i]<=0) res+=pq.top()-d[i], pq.pop(); else res+=pq.top()-d[i], pq.push(max(0ll, d[i])), pq.push(max(0ll, d[i])), pq.pop(); } //priority_queue<ll> tmp=pq; //cout<<"pq "<<res<<':'; //while (!tmp.empty()) cout<<tmp.top()<<' ', tmp.pop(); //cout<<'\n'; //cout<<i<<' '<<res<<'\n'; } cout<<res; }
#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...