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 N;
long long pre[500005];
long long ans;
int main() {
ios_base::sync_with_stdio(0);
cin >> N;
int a, b;
for(int i = 1; i <= N; i++) {
cin >> a >> b;
pre[i] = a - b + pre[i - 1];
}
for(int i = 1; i <= N; i++) {
if(pre[i] < 0) ans -= pre[i], pre[i] = 0;
else if(pre[i] > pre[N]) ans += pre[i] - pre[N], pre[i] = pre[N];
}
priority_queue<long long, vector<long long>> pq;
pq.emplace(pre[1]);
for(int i = 2; i <= N; i++) {
if(pre[i] >= pq.top()) {
pq.emplace(pre[i]);
}else {
ans += (pq.top() - pre[i]);
pq.emplace(pre[i]);
pq.emplace(pre[i]);
pq.pop();
}
}
cout << ans << "\n";
cin >> N;
}
# | 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... |