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>
#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 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... |