#include <bits/stdc++.h>
using namespace std;
int N;
long long pre[500005];
long long ans;
int main() {
ios_base::sync_with_stdio(0);
cin >> N;
int a, b;
for(int i = 1; i <= N; i++) {
cin >> a >> b;
pre[i] = a - b + pre[i - 1];
}
for(int i = 1; i <= N; i++) {
if(pre[i] < 0) ans -= pre[i], pre[i] = 0;
else if(pre[i] > pre[N]) ans += pre[i] - pre[N], pre[i] = pre[N];
}
priority_queue<long long, vector<long long>> pq;
pq.emplace(pre[1]);
for(int i = 2; i <= N; i++) {
if(pre[i] >= pq.top()) {
pq.emplace(pre[i]);
}else {
ans += (pq.top() - pre[i]);
pq.emplace(pre[i]);
pq.emplace(pre[i]);
pq.pop();
}
}
cout << ans << "\n";
cin >> N;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
512 KB |
Output is correct |
4 |
Correct |
10 ms |
1792 KB |
Output is correct |
5 |
Correct |
19 ms |
2848 KB |
Output is correct |
6 |
Correct |
61 ms |
7920 KB |
Output is correct |
7 |
Correct |
131 ms |
15212 KB |
Output is correct |
8 |
Correct |
116 ms |
13420 KB |
Output is correct |
9 |
Correct |
112 ms |
12776 KB |
Output is correct |
10 |
Correct |
84 ms |
10476 KB |
Output is correct |
11 |
Correct |
81 ms |
10476 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
512 KB |
Output is correct |
4 |
Correct |
10 ms |
1792 KB |
Output is correct |
5 |
Correct |
19 ms |
2848 KB |
Output is correct |
6 |
Correct |
61 ms |
7920 KB |
Output is correct |
7 |
Correct |
131 ms |
15212 KB |
Output is correct |
8 |
Correct |
116 ms |
13420 KB |
Output is correct |
9 |
Correct |
112 ms |
12776 KB |
Output is correct |
10 |
Correct |
84 ms |
10476 KB |
Output is correct |
11 |
Correct |
81 ms |
10476 KB |
Output is correct |
12 |
Correct |
39 ms |
4212 KB |
Output is correct |
13 |
Correct |
85 ms |
10988 KB |
Output is correct |
14 |
Correct |
137 ms |
15248 KB |
Output is correct |
15 |
Correct |
105 ms |
13420 KB |
Output is correct |
16 |
Correct |
115 ms |
12780 KB |
Output is correct |
17 |
Correct |
88 ms |
10476 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
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 |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
512 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 |
2 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 |
2 ms |
512 KB |
Output is correct |
13 |
Correct |
1 ms |
512 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
512 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
512 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
512 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 |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
10 ms |
1792 KB |
Output is correct |
12 |
Correct |
19 ms |
2848 KB |
Output is correct |
13 |
Correct |
61 ms |
7920 KB |
Output is correct |
14 |
Correct |
131 ms |
15212 KB |
Output is correct |
15 |
Correct |
116 ms |
13420 KB |
Output is correct |
16 |
Correct |
112 ms |
12776 KB |
Output is correct |
17 |
Correct |
84 ms |
10476 KB |
Output is correct |
18 |
Correct |
81 ms |
10476 KB |
Output is correct |
19 |
Correct |
39 ms |
4212 KB |
Output is correct |
20 |
Correct |
85 ms |
10988 KB |
Output is correct |
21 |
Correct |
137 ms |
15248 KB |
Output is correct |
22 |
Correct |
105 ms |
13420 KB |
Output is correct |
23 |
Correct |
115 ms |
12780 KB |
Output is correct |
24 |
Correct |
88 ms |
10476 KB |
Output is correct |
25 |
Correct |
1 ms |
384 KB |
Output is correct |
26 |
Correct |
2 ms |
512 KB |
Output is correct |
27 |
Correct |
1 ms |
512 KB |
Output is correct |
28 |
Correct |
2 ms |
384 KB |
Output is correct |
29 |
Correct |
1 ms |
512 KB |
Output is correct |
30 |
Correct |
1 ms |
384 KB |
Output is correct |
31 |
Correct |
2 ms |
512 KB |
Output is correct |
32 |
Correct |
43 ms |
4212 KB |
Output is correct |
33 |
Correct |
111 ms |
11120 KB |
Output is correct |
34 |
Correct |
188 ms |
15212 KB |
Output is correct |
35 |
Correct |
173 ms |
12904 KB |
Output is correct |
36 |
Correct |
111 ms |
13420 KB |
Output is correct |
37 |
Correct |
167 ms |
15304 KB |
Output is correct |
38 |
Correct |
95 ms |
11500 KB |
Output is correct |
39 |
Correct |
115 ms |
10936 KB |
Output is correct |
40 |
Correct |
103 ms |
10640 KB |
Output is correct |
41 |
Correct |
107 ms |
10476 KB |
Output is correct |
42 |
Correct |
110 ms |
10556 KB |
Output is correct |
43 |
Correct |
108 ms |
10464 KB |
Output is correct |
44 |
Correct |
143 ms |
15252 KB |
Output is correct |
45 |
Correct |
87 ms |
10848 KB |
Output is correct |
46 |
Correct |
101 ms |
10348 KB |
Output is correct |
47 |
Correct |
1 ms |
384 KB |
Output is correct |