#include<bits/stdc++.h>
using namespace std;
long long v[500100];
long long ans = 0;
priority_queue<long long> heap;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n; cin >> n;
for (int i = 1; i <= n; i++) {
int a, b; cin >> a >> b;
v[i] = a-b + v[i-1];
}
for (int i = 1; i <= n; i++) {
if (v[i] > v[n]) {ans += v[i]-v[n]; v[i] = v[n];} // v[n] can't be changed
if (v[i] < 0) {ans += 0-v[i]; v[i] = 0;} // v[i] >= 0
heap.push(v[i]);
heap.push(v[i]);
ans += heap.top() - v[i];
heap.pop();
}
cout << ans << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
512 KB |
Output is correct |
4 |
Correct |
16 ms |
1408 KB |
Output is correct |
5 |
Correct |
26 ms |
2300 KB |
Output is correct |
6 |
Correct |
73 ms |
4464 KB |
Output is correct |
7 |
Correct |
153 ms |
8428 KB |
Output is correct |
8 |
Correct |
126 ms |
8428 KB |
Output is correct |
9 |
Correct |
128 ms |
8428 KB |
Output is correct |
10 |
Correct |
102 ms |
8556 KB |
Output is correct |
11 |
Correct |
110 ms |
8556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
512 KB |
Output is correct |
4 |
Correct |
16 ms |
1408 KB |
Output is correct |
5 |
Correct |
26 ms |
2300 KB |
Output is correct |
6 |
Correct |
73 ms |
4464 KB |
Output is correct |
7 |
Correct |
153 ms |
8428 KB |
Output is correct |
8 |
Correct |
126 ms |
8428 KB |
Output is correct |
9 |
Correct |
128 ms |
8428 KB |
Output is correct |
10 |
Correct |
102 ms |
8556 KB |
Output is correct |
11 |
Correct |
110 ms |
8556 KB |
Output is correct |
12 |
Correct |
41 ms |
2428 KB |
Output is correct |
13 |
Correct |
86 ms |
6892 KB |
Output is correct |
14 |
Correct |
146 ms |
8556 KB |
Output is correct |
15 |
Correct |
134 ms |
8556 KB |
Output is correct |
16 |
Correct |
125 ms |
8556 KB |
Output is correct |
17 |
Correct |
113 ms |
8536 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
6 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
512 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
6 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
512 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
6 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
16 ms |
1408 KB |
Output is correct |
12 |
Correct |
26 ms |
2300 KB |
Output is correct |
13 |
Correct |
73 ms |
4464 KB |
Output is correct |
14 |
Correct |
153 ms |
8428 KB |
Output is correct |
15 |
Correct |
126 ms |
8428 KB |
Output is correct |
16 |
Correct |
128 ms |
8428 KB |
Output is correct |
17 |
Correct |
102 ms |
8556 KB |
Output is correct |
18 |
Correct |
110 ms |
8556 KB |
Output is correct |
19 |
Correct |
41 ms |
2428 KB |
Output is correct |
20 |
Correct |
86 ms |
6892 KB |
Output is correct |
21 |
Correct |
146 ms |
8556 KB |
Output is correct |
22 |
Correct |
134 ms |
8556 KB |
Output is correct |
23 |
Correct |
125 ms |
8556 KB |
Output is correct |
24 |
Correct |
113 ms |
8536 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
26 |
Correct |
5 ms |
384 KB |
Output is correct |
27 |
Correct |
5 ms |
384 KB |
Output is correct |
28 |
Correct |
5 ms |
384 KB |
Output is correct |
29 |
Correct |
5 ms |
384 KB |
Output is correct |
30 |
Correct |
5 ms |
384 KB |
Output is correct |
31 |
Correct |
5 ms |
384 KB |
Output is correct |
32 |
Correct |
46 ms |
2420 KB |
Output is correct |
33 |
Correct |
110 ms |
7016 KB |
Output is correct |
34 |
Correct |
184 ms |
8428 KB |
Output is correct |
35 |
Correct |
170 ms |
8556 KB |
Output is correct |
36 |
Correct |
143 ms |
8484 KB |
Output is correct |
37 |
Correct |
172 ms |
15308 KB |
Output is correct |
38 |
Correct |
118 ms |
11372 KB |
Output is correct |
39 |
Correct |
127 ms |
10988 KB |
Output is correct |
40 |
Correct |
118 ms |
10476 KB |
Output is correct |
41 |
Correct |
112 ms |
10476 KB |
Output is correct |
42 |
Correct |
120 ms |
10476 KB |
Output is correct |
43 |
Correct |
114 ms |
10476 KB |
Output is correct |
44 |
Correct |
161 ms |
15212 KB |
Output is correct |
45 |
Correct |
133 ms |
10732 KB |
Output is correct |
46 |
Correct |
114 ms |
10264 KB |
Output is correct |
47 |
Correct |
5 ms |
384 KB |
Output is correct |