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<bits/stdc++.h>
using namespace std;
const int MAXN=5e5+5;
long long pref[MAXN];
int main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
int n,a,b;
long long ans=0;
cin >> n;
for(int i=1;i<=n;i++){
cin >> a >> b;
pref[i]=pref[i-1]+a-b;
}
for(int i=1;i<=n;i++){
if(pref[i]>pref[n]){
ans+=(pref[i]-pref[n]);
pref[i]=pref[n];
}
else if(pref[i]<0){
ans+=(-pref[i]);
pref[i]=0;
}
}
//make pref non decreasing -> Classic Slope Trick CF713C
priority_queue<long long> PQ;
for(int i=1;i<=n;i++){
PQ.push(pref[i]);PQ.push(pref[i]);
ans+=(PQ.top()-pref[i]);
PQ.pop();
}
cout << ans << '\n';
return 0;
}
# | 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... |