Submission #988301

#TimeUsernameProblemLanguageResultExecution timeMemory
988301pemguimnPotatoes and fertilizers (LMIO19_bulves)C++14
100 / 100
139 ms29128 KiB
#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 - 1; i++){
        pq.push(pre[i]); pq.push(pre[i]);
        ans = ans + abs(max(0LL, min(pq.top(), pre[n])) - pre[i]);
        pq.pop();
    }

    cout << ans << '\n';
    return 0;
}

/*
7
2 0
2 0
2 0
0 5
2 0
2 0
2 0

6
1 2
0 0
2 0
0 0
0 0
0 1

*/

#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...