This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define X first
#define Y second
#define MP make_pair
using namespace std;
const int N = 5e5 + 1;
const ll INF = 1e12 + 1;
int n;
ll a[N], b[N], dif[N];
int main () {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for(int i = 1;i <= n;i++)
cin >> a[i] >> b[i], dif[i] = dif[i - 1] + a[i] - b[i];
ll ans = 0;
priority_queue<ll> dp;
for(int i = 1;i <= n;i++) {
ans += abs(dif[i]);
if(dif[i] < 0)
dif[i] = 0;
dp.push(dif[i]);
dp.push(dif[i]);
dp.pop();
}
for(;!dp.empty();) {
// cerr << dp.top() << " " << dif[n] << "\n";
ans -= min(dp.top(), dif[n]);
dp.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... |