This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |