#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<int> 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 |
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 |
10 ms |
1152 KB |
Output is correct |
5 |
Correct |
20 ms |
1784 KB |
Output is correct |
6 |
Correct |
103 ms |
3444 KB |
Output is correct |
7 |
Correct |
147 ms |
6384 KB |
Output is correct |
8 |
Correct |
130 ms |
11376 KB |
Output is correct |
9 |
Correct |
116 ms |
10608 KB |
Output is correct |
10 |
Correct |
98 ms |
8432 KB |
Output is correct |
11 |
Correct |
99 ms |
8432 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 |
10 ms |
1152 KB |
Output is correct |
5 |
Correct |
20 ms |
1784 KB |
Output is correct |
6 |
Correct |
103 ms |
3444 KB |
Output is correct |
7 |
Correct |
147 ms |
6384 KB |
Output is correct |
8 |
Correct |
130 ms |
11376 KB |
Output is correct |
9 |
Correct |
116 ms |
10608 KB |
Output is correct |
10 |
Correct |
98 ms |
8432 KB |
Output is correct |
11 |
Correct |
99 ms |
8432 KB |
Output is correct |
12 |
Correct |
39 ms |
3704 KB |
Output is correct |
13 |
Correct |
92 ms |
8944 KB |
Output is correct |
14 |
Correct |
155 ms |
13224 KB |
Output is correct |
15 |
Correct |
118 ms |
11380 KB |
Output is correct |
16 |
Correct |
126 ms |
10612 KB |
Output is correct |
17 |
Correct |
100 ms |
8432 KB |
Output is correct |
18 |
Correct |
2 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 |
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 |
2 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 |
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 |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
2 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 |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
2 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 |
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 |
11 |
Correct |
10 ms |
1152 KB |
Output is correct |
12 |
Correct |
20 ms |
1784 KB |
Output is correct |
13 |
Correct |
103 ms |
3444 KB |
Output is correct |
14 |
Correct |
147 ms |
6384 KB |
Output is correct |
15 |
Correct |
130 ms |
11376 KB |
Output is correct |
16 |
Correct |
116 ms |
10608 KB |
Output is correct |
17 |
Correct |
98 ms |
8432 KB |
Output is correct |
18 |
Correct |
99 ms |
8432 KB |
Output is correct |
19 |
Correct |
39 ms |
3704 KB |
Output is correct |
20 |
Correct |
92 ms |
8944 KB |
Output is correct |
21 |
Correct |
155 ms |
13224 KB |
Output is correct |
22 |
Correct |
118 ms |
11380 KB |
Output is correct |
23 |
Correct |
126 ms |
10612 KB |
Output is correct |
24 |
Correct |
100 ms |
8432 KB |
Output is correct |
25 |
Correct |
1 ms |
384 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
2 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 |
2 ms |
384 KB |
Output is correct |
32 |
Correct |
46 ms |
3708 KB |
Output is correct |
33 |
Correct |
119 ms |
8944 KB |
Output is correct |
34 |
Correct |
192 ms |
13204 KB |
Output is correct |
35 |
Correct |
163 ms |
10732 KB |
Output is correct |
36 |
Incorrect |
146 ms |
11404 KB |
Output isn't correct |
37 |
Halted |
0 ms |
0 KB |
- |