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], pq.push(d[n]);
else pq.push(d[i]);
//cout<<i<<' '<<res<<'\n';
}
else
{
if (d[i]>=d[n]) res+=(d[i]-d[n]), pq.push(d[n]);
else if (d[i]>=pq.top()) pq.push(d[i]);
else if (d[i]<=0) res+=pq.top()-d[i], pq.pop();
else res+=pq.top()-d[i], pq.push(max(0ll, d[i])), pq.push(max(0ll, d[i])), pq.pop();
}
//priority_queue<ll> tmp=pq;
//cout<<"pq "<<res<<':';
//while (!tmp.empty()) cout<<tmp.top()<<' ', tmp.pop();
//cout<<'\n';
//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... |