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;
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int N;
cin >> N;
vector<long long> sum(N);
for (int i = 0; i < N; i++) {
int a, b;
cin >> a >> b;
sum[i] = a - b;
if (i > 0) {
sum[i] += sum[i - 1];
}
}
// We want sum[] to be non-decreasing
// An operation: choose i, then sum[i] += 1 or sum[i] -= 1
// sum[0] cannot be decreased (-1 operation) and must be >= 0
// sum[N - 1] cannot be increased (+1 operation)
// This is classic Slope Trick
long long ans = 0;
priority_queue<long long> pq;
pq.emplace(0);
for (int i = 0; i < N; i++) {
if (sum[i] < 0) {
ans += 0 - sum[i];
sum[i] = 0;
} else if (sum[i] > sum[N - 1]) {
ans += sum[i] - sum[N - 1];
sum[i] = sum[N - 1];
}
pq.emplace(sum[i]);
pq.emplace(sum[i]);
ans += pq.top() - sum[i];
pq.pop();
}
cout << ans << "\n";
return 0;
}
# | 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... |