/*
* LMIO 2019 Ptatoes and Fertilizers
* a - b shows the number of fertilizers spared
* Take the prefix sum of a - b
* The problem becomes:
- An operation allows increment
or decrement an element by 1
- Special case on the leftmost element
> can't be decremented, must be non-negative
- Special case on the rightmost element
> can't be incremented
- We want to make the array non-decreasing
* This can be solved with slope trick
* A similar problem: https://codeforces.com/contest/713/problem/C
* More about slope trick: https://codeforces.com/blog/entry/77298
*/
#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <vector>
#include <iostream>
#include <iomanip>
#include <queue>
using namespace std;
#define ll long long
int n;
ll pref[500005], a, b;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n;
for(int i = 0; i < n; i ++){
cin >> a >> b;
pref[i] = a - b;
if(i > 0)
pref[i] += pref[i - 1];
}
ll ans = 0ll;
priority_queue<ll> pq;
pq.push(0ll);
for(int i = 0; i < n; i ++){
if(pref[i] < 0){
ans += 0ll - pref[i];
pref[i] = 0ll;
}else if(pref[i] > pref[n - 1]){
ans += pref[i] - pref[n - 1];
pref[i] = pref[n - 1];
}
pq.push(pref[i]);
pq.push(pref[i]);
ans += pq.top() - pref[i];
pq.pop();
}
cout << ans << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
9 ms |
1408 KB |
Output is correct |
5 |
Correct |
19 ms |
2300 KB |
Output is correct |
6 |
Correct |
66 ms |
4464 KB |
Output is correct |
7 |
Correct |
137 ms |
8556 KB |
Output is correct |
8 |
Correct |
122 ms |
8556 KB |
Output is correct |
9 |
Correct |
115 ms |
8428 KB |
Output is correct |
10 |
Correct |
92 ms |
8432 KB |
Output is correct |
11 |
Correct |
91 ms |
8428 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
9 ms |
1408 KB |
Output is correct |
5 |
Correct |
19 ms |
2300 KB |
Output is correct |
6 |
Correct |
66 ms |
4464 KB |
Output is correct |
7 |
Correct |
137 ms |
8556 KB |
Output is correct |
8 |
Correct |
122 ms |
8556 KB |
Output is correct |
9 |
Correct |
115 ms |
8428 KB |
Output is correct |
10 |
Correct |
92 ms |
8432 KB |
Output is correct |
11 |
Correct |
91 ms |
8428 KB |
Output is correct |
12 |
Correct |
36 ms |
2420 KB |
Output is correct |
13 |
Correct |
89 ms |
6892 KB |
Output is correct |
14 |
Correct |
151 ms |
8428 KB |
Output is correct |
15 |
Correct |
121 ms |
8528 KB |
Output is correct |
16 |
Correct |
113 ms |
8428 KB |
Output is correct |
17 |
Correct |
93 ms |
8428 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
512 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
432 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
512 KB |
Output is correct |
11 |
Correct |
9 ms |
1408 KB |
Output is correct |
12 |
Correct |
19 ms |
2300 KB |
Output is correct |
13 |
Correct |
66 ms |
4464 KB |
Output is correct |
14 |
Correct |
137 ms |
8556 KB |
Output is correct |
15 |
Correct |
122 ms |
8556 KB |
Output is correct |
16 |
Correct |
115 ms |
8428 KB |
Output is correct |
17 |
Correct |
92 ms |
8432 KB |
Output is correct |
18 |
Correct |
91 ms |
8428 KB |
Output is correct |
19 |
Correct |
36 ms |
2420 KB |
Output is correct |
20 |
Correct |
89 ms |
6892 KB |
Output is correct |
21 |
Correct |
151 ms |
8428 KB |
Output is correct |
22 |
Correct |
121 ms |
8528 KB |
Output is correct |
23 |
Correct |
113 ms |
8428 KB |
Output is correct |
24 |
Correct |
93 ms |
8428 KB |
Output is correct |
25 |
Correct |
1 ms |
384 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
2 ms |
432 KB |
Output is correct |
28 |
Correct |
2 ms |
384 KB |
Output is correct |
29 |
Correct |
2 ms |
384 KB |
Output is correct |
30 |
Correct |
2 ms |
384 KB |
Output is correct |
31 |
Correct |
1 ms |
384 KB |
Output is correct |
32 |
Correct |
55 ms |
2420 KB |
Output is correct |
33 |
Correct |
109 ms |
6892 KB |
Output is correct |
34 |
Correct |
196 ms |
8424 KB |
Output is correct |
35 |
Correct |
167 ms |
8548 KB |
Output is correct |
36 |
Correct |
141 ms |
8584 KB |
Output is correct |
37 |
Correct |
170 ms |
8428 KB |
Output is correct |
38 |
Correct |
105 ms |
8428 KB |
Output is correct |
39 |
Correct |
117 ms |
8172 KB |
Output is correct |
40 |
Correct |
106 ms |
8560 KB |
Output is correct |
41 |
Correct |
100 ms |
8484 KB |
Output is correct |
42 |
Correct |
100 ms |
8580 KB |
Output is correct |
43 |
Correct |
98 ms |
8428 KB |
Output is correct |
44 |
Correct |
154 ms |
8556 KB |
Output is correct |
45 |
Correct |
126 ms |
8424 KB |
Output is correct |
46 |
Correct |
102 ms |
7660 KB |
Output is correct |
47 |
Correct |
1 ms |
384 KB |
Output is correct |