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;
typedef long long ll;
int n;
ll a[500001],b[500001],d[500001],ans;
priority_queue<ll> q;
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin>>n;
for (int i=1; i<=n; ++i) {
cin>>a[i]>>b[i];
d[i]=d[i-1]+a[i]-b[i];
}
if (d[1]>=0) q.push(d[1]), ans+=d[1];
else q.push(0), ans-=d[1];
for (int i=2; i<=n; ++i) {
if (d[i]>=q.top()) q.push(d[i]), ans+=d[i];
else {
if (d[i]<0) ans-=d[i], d[i]=0;
ans+=d[i];
q.push(d[i]); q.push(d[i]); q.pop();
}
}
//now ans is at x=Y-intersect(0), then it will be moved to x=d[n]
while (!q.empty()) ans-=min(d[n],q.top()), q.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... |