#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 10;
int n;
long long a[N];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; ++i) {
int x, y;
cin >> x >> y;
a[i] = a[i - 1] + (x - y);
}
long long ans = 0;
for (int i = 1; i <= n; ++i) {
if (a[i] < 0) {
ans += (-a[i]);
a[i] = 0;
}
if (a[i] > a[n]) {
ans += a[i] - a[n];
a[i] = a[n];
}
}
priority_queue<int> pq;
pq.push(a[1]);
for (int i = 2; i <= n; ++i) {
ans += max(0ll, pq.top() - a[i]);
pq.push(a[i]);
pq.push(a[i]);
pq.pop();
}
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
352 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
7 ms |
1368 KB |
Output is correct |
5 |
Correct |
14 ms |
2384 KB |
Output is correct |
6 |
Correct |
47 ms |
6732 KB |
Output is correct |
7 |
Correct |
105 ms |
13140 KB |
Output is correct |
8 |
Correct |
86 ms |
11312 KB |
Output is correct |
9 |
Correct |
92 ms |
10684 KB |
Output is correct |
10 |
Correct |
68 ms |
8268 KB |
Output is correct |
11 |
Correct |
66 ms |
8332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
352 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
7 ms |
1368 KB |
Output is correct |
5 |
Correct |
14 ms |
2384 KB |
Output is correct |
6 |
Correct |
47 ms |
6732 KB |
Output is correct |
7 |
Correct |
105 ms |
13140 KB |
Output is correct |
8 |
Correct |
86 ms |
11312 KB |
Output is correct |
9 |
Correct |
92 ms |
10684 KB |
Output is correct |
10 |
Correct |
68 ms |
8268 KB |
Output is correct |
11 |
Correct |
66 ms |
8332 KB |
Output is correct |
12 |
Correct |
29 ms |
3604 KB |
Output is correct |
13 |
Correct |
75 ms |
8908 KB |
Output is correct |
14 |
Correct |
109 ms |
13104 KB |
Output is correct |
15 |
Correct |
89 ms |
11216 KB |
Output is correct |
16 |
Correct |
86 ms |
10596 KB |
Output is correct |
17 |
Correct |
74 ms |
8312 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
352 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
352 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
2 ms |
344 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
344 KB |
Output is correct |
18 |
Correct |
2 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
352 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
7 ms |
1368 KB |
Output is correct |
12 |
Correct |
14 ms |
2384 KB |
Output is correct |
13 |
Correct |
47 ms |
6732 KB |
Output is correct |
14 |
Correct |
105 ms |
13140 KB |
Output is correct |
15 |
Correct |
86 ms |
11312 KB |
Output is correct |
16 |
Correct |
92 ms |
10684 KB |
Output is correct |
17 |
Correct |
68 ms |
8268 KB |
Output is correct |
18 |
Correct |
66 ms |
8332 KB |
Output is correct |
19 |
Correct |
29 ms |
3604 KB |
Output is correct |
20 |
Correct |
75 ms |
8908 KB |
Output is correct |
21 |
Correct |
109 ms |
13104 KB |
Output is correct |
22 |
Correct |
89 ms |
11216 KB |
Output is correct |
23 |
Correct |
86 ms |
10596 KB |
Output is correct |
24 |
Correct |
74 ms |
8312 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
2 ms |
344 KB |
Output is correct |
27 |
Correct |
2 ms |
384 KB |
Output is correct |
28 |
Correct |
2 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
2 ms |
344 KB |
Output is correct |
31 |
Correct |
2 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
37 ms |
3520 KB |
Output is correct |
34 |
Correct |
86 ms |
8872 KB |
Output is correct |
35 |
Correct |
137 ms |
13120 KB |
Output is correct |
36 |
Correct |
124 ms |
10732 KB |
Output is correct |
37 |
Correct |
114 ms |
11340 KB |
Output is correct |
38 |
Correct |
136 ms |
13108 KB |
Output is correct |
39 |
Correct |
83 ms |
9312 KB |
Output is correct |
40 |
Correct |
103 ms |
8904 KB |
Output is correct |
41 |
Correct |
82 ms |
8276 KB |
Output is correct |
42 |
Correct |
85 ms |
8328 KB |
Output is correct |
43 |
Correct |
85 ms |
8336 KB |
Output is correct |
44 |
Correct |
89 ms |
8332 KB |
Output is correct |
45 |
Correct |
113 ms |
13144 KB |
Output is correct |
46 |
Correct |
104 ms |
8624 KB |
Output is correct |
47 |
Correct |
86 ms |
8160 KB |
Output is correct |