#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main()
{
int n;
cin >> n;
vector<ll> a(n), b(n);
for(int i = 0; i < n; i ++)
cin >> a[i] >> b[i];
ll c = 0;
for(int i = 0; i < n; i ++)
c += a[i] - b[i];
ll cost = 0;
priority_queue<ll> pq;
ll d = 0;
for(int i = 0; i < n; i ++)
{
d += a[i] - b[i];
ll e = d;
if(e < 0)
{
cost -= e;
e = 0;
}
else if(e > c)
{
cost += e - c;
e = c;
}
pq.push(e);
pq.push(e);
cost += pq.top() - e;
pq.pop();
}
cout << cost;
}
# | 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... |