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>
#define int long long
using namespace std;
const int N = 5e5 + 5, INF = 1e18 + 7;
int n, a[N], b[N], d[N], pre[N];
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for(int i = 1; i <= n; i++) cin >> a[i] >> b[i], d[i] = a[i] - b[i], pre[i] = pre[i - 1] + d[i];
priority_queue<int> pq;
int ans = 0;
for(int i = 1; i <= n; i++){
if(pre[i] < 0) ans -= pre[i], pre[i] = 0;
pq.push(pre[i]); pq.push(pre[i]);
ans += pq.top() - pre[i];
pq.pop();
}
int x = pq.top(), slope = 1; pq.pop();
while(pq.size()){
int u = pq.top(); pq.pop();
if(u <= pre[n]){
ans += slope * (x - pre[n]); break;
} else{
ans += slope * (x - u);
}
x = u;
slope++;
}
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... |