#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 |
1 |
Correct |
1 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 |
12 ms |
1536 KB |
Output is correct |
5 |
Correct |
20 ms |
2292 KB |
Output is correct |
6 |
Correct |
75 ms |
4464 KB |
Output is correct |
7 |
Correct |
156 ms |
8524 KB |
Output is correct |
8 |
Correct |
119 ms |
8428 KB |
Output is correct |
9 |
Correct |
120 ms |
8572 KB |
Output is correct |
10 |
Correct |
95 ms |
8556 KB |
Output is correct |
11 |
Correct |
99 ms |
8428 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
12 ms |
1536 KB |
Output is correct |
5 |
Correct |
20 ms |
2292 KB |
Output is correct |
6 |
Correct |
75 ms |
4464 KB |
Output is correct |
7 |
Correct |
156 ms |
8524 KB |
Output is correct |
8 |
Correct |
119 ms |
8428 KB |
Output is correct |
9 |
Correct |
120 ms |
8572 KB |
Output is correct |
10 |
Correct |
95 ms |
8556 KB |
Output is correct |
11 |
Correct |
99 ms |
8428 KB |
Output is correct |
12 |
Correct |
39 ms |
2420 KB |
Output is correct |
13 |
Correct |
91 ms |
6896 KB |
Output is correct |
14 |
Correct |
158 ms |
8556 KB |
Output is correct |
15 |
Correct |
124 ms |
8428 KB |
Output is correct |
16 |
Correct |
120 ms |
8524 KB |
Output is correct |
17 |
Correct |
96 ms |
8556 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
1 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 |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
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 |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 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 |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 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 |
1 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 |
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 |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
12 ms |
1536 KB |
Output is correct |
12 |
Correct |
20 ms |
2292 KB |
Output is correct |
13 |
Correct |
75 ms |
4464 KB |
Output is correct |
14 |
Correct |
156 ms |
8524 KB |
Output is correct |
15 |
Correct |
119 ms |
8428 KB |
Output is correct |
16 |
Correct |
120 ms |
8572 KB |
Output is correct |
17 |
Correct |
95 ms |
8556 KB |
Output is correct |
18 |
Correct |
99 ms |
8428 KB |
Output is correct |
19 |
Correct |
39 ms |
2420 KB |
Output is correct |
20 |
Correct |
91 ms |
6896 KB |
Output is correct |
21 |
Correct |
158 ms |
8556 KB |
Output is correct |
22 |
Correct |
124 ms |
8428 KB |
Output is correct |
23 |
Correct |
120 ms |
8524 KB |
Output is correct |
24 |
Correct |
96 ms |
8556 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 |
384 KB |
Output is correct |
28 |
Correct |
1 ms |
384 KB |
Output is correct |
29 |
Correct |
1 ms |
384 KB |
Output is correct |
30 |
Correct |
1 ms |
384 KB |
Output is correct |
31 |
Correct |
1 ms |
384 KB |
Output is correct |
32 |
Correct |
45 ms |
2524 KB |
Output is correct |
33 |
Correct |
117 ms |
7020 KB |
Output is correct |
34 |
Correct |
191 ms |
8556 KB |
Output is correct |
35 |
Correct |
163 ms |
8556 KB |
Output is correct |
36 |
Correct |
139 ms |
8556 KB |
Output is correct |
37 |
Correct |
183 ms |
15212 KB |
Output is correct |
38 |
Correct |
104 ms |
11500 KB |
Output is correct |
39 |
Correct |
148 ms |
10988 KB |
Output is correct |
40 |
Correct |
109 ms |
10476 KB |
Output is correct |
41 |
Correct |
110 ms |
10476 KB |
Output is correct |
42 |
Correct |
110 ms |
10480 KB |
Output is correct |
43 |
Correct |
109 ms |
10476 KB |
Output is correct |
44 |
Correct |
162 ms |
15184 KB |
Output is correct |
45 |
Correct |
127 ms |
10732 KB |
Output is correct |
46 |
Correct |
106 ms |
10348 KB |
Output is correct |
47 |
Correct |
1 ms |
384 KB |
Output is correct |