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 N = 5e5 + 5;
long long a[N], b[N];
signed main(){
ios::sync_with_stdio(0); cin.tie(0);
int n; cin >> n;
long long res = 0;
for(int i=1;i<=n;i++){
cin >> a[i] >> b[i];
a[i] -= b[i];
a[i] += a[i-1];
}
long long mx = a[n]; assert(0 <= mx);
for(int i=1;i<=n;i++){
if(a[i] < 0){
res -= a[i];
a[i] = 0;
}
if(a[i] > mx){
res += (a[i] - mx);
a[i] = mx;
}
}
priority_queue<long long> q;
for(int i=1;i<=n;i++){
q.push(a[i]);
if(a[i] < q.top()){
res += (q.top() - a[i]);
q.pop(); q.push(a[i]);
}
}
cout<<res<<"\n";
}
# | 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... |