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;
#define int int64_t
#define pb push_back
using pii=pair<int,int>;
const int lim=2e5+100;
const int mod=1e9+7;
signed main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#ifdef Local
freopen(".in","r",stdin);freopen(".out","w",stdout);
#endif
int n;
cin>>n;
int a[n+1],b[n+1];
for(int i=1;i<=n;i++)cin>>a[i]>>b[i];
int d[n+1];
for(int i=1;i<=n;i++)d[i]=a[i]-b[i];
int p[n+1];
p[0]=0;
for(int i=1;i<=n;i++)p[i]=p[i-1]+d[i];
priority_queue<int>pq;
int ans=0;
for(int i=1;i<=n;i++){
if(p[i]<0){
if(pq.size()){
ans+=pq.top();
pq.pop();
}
ans-=p[i];
}else{
pq.push(p[i]);
ans+=pq.top()-p[i];
pq.push(p[i]);
pq.pop();
}
}
while(p[n]<pq.top()){
ans+=pq.top()-p[n];
pq.pop();
}
cout<<ans;
}
# | 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... |