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 ll long long
const int nx=5e5+5;
ll n, a[nx], b[nx], d[nx], res;
priority_queue<ll> pq;
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n;
for (int i=1; i<=n; i++) cin>>a[i]>>b[i], d[i]=d[i-1]+a[i]-b[i];
for (int i=1; i<n; i++)
{
//cout<<"val "<<i<<' '<<d[i]<<' '<<d[n]<<'\n';
if (pq.empty())
{
if (d[i]<=0) res-=d[i];
else if (d[i]>=d[n]) res+=d[i]-d[n];
else pq.push(d[i]);
//cout<<i<<' '<<res<<'\n';
continue;
}
if (d[i]>=d[n]) res+=(d[i]-d[n]), pq.push(min(d[n], d[i]));
else if (d[i]>=pq.top()) pq.push(d[i]);
else res+=pq.top()-d[i], pq.push(max(0ll, d[i])), pq.push(max(0ll, d[i])), pq.pop();
//cout<<i<<' '<<res<<'\n';
}
cout<<res;
}
# | 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... |