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;
const int Nmax = 5e5 + 10;
int n, dif[Nmax];
priority_queue <int> opt;
long long ans;
int main() {
ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n;
for (int i = 1; i <= n; ++i) {
int a, b;
cin >> a >> b;
dif[i] = dif[i - 1] + a - b;
}
for (int i = 1; i < n; ++i) {
if (dif[i] < 0) ans -= dif[i], dif[i] = 0;
opt.push(dif[i]), opt.push(dif[i]);
opt.pop();
ans += dif[i];
}
while (opt.size()) ans -= min(dif[n], opt.top()), opt.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... |