#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 5e5+5, MAX = 3e4+5, ZERO = 3e4, INF = 1e18;
ll n, delta;
ll val[N], pref[N];
void read() {
cin >> n;
for(int i = 1; i <= n; i++) {
ll a, b; cin >> a >> b;
val[i] = a - b;
}
}
void solve() {
pref[0] = 0;
for(int i = 1; i <= n; i++) {
pref[i] = pref[i-1] + val[i];
}
delta = pref[n];
ll ans = 0, x;
priority_queue<ll> pq;
pq.push(0);
for(int i = 1; i <= n; i++) {
x = pref[i];
if(x < 0) {
ans += -x;
x = 0;
} else if(x > delta) {
ans += x - delta;
x = delta;
}
pq.push(x);
pq.push(x);
ans += pq.top() - x;
pq.pop();
}
cout << ans << '\n';
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
read();
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
1 ms |
512 KB |
Output is correct |
4 |
Correct |
10 ms |
2176 KB |
Output is correct |
5 |
Correct |
20 ms |
3708 KB |
Output is correct |
6 |
Correct |
73 ms |
9840 KB |
Output is correct |
7 |
Correct |
147 ms |
19172 KB |
Output is correct |
8 |
Correct |
122 ms |
17260 KB |
Output is correct |
9 |
Correct |
120 ms |
16620 KB |
Output is correct |
10 |
Correct |
86 ms |
14316 KB |
Output is correct |
11 |
Correct |
96 ms |
14316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
1 ms |
512 KB |
Output is correct |
4 |
Correct |
10 ms |
2176 KB |
Output is correct |
5 |
Correct |
20 ms |
3708 KB |
Output is correct |
6 |
Correct |
73 ms |
9840 KB |
Output is correct |
7 |
Correct |
147 ms |
19172 KB |
Output is correct |
8 |
Correct |
122 ms |
17260 KB |
Output is correct |
9 |
Correct |
120 ms |
16620 KB |
Output is correct |
10 |
Correct |
86 ms |
14316 KB |
Output is correct |
11 |
Correct |
96 ms |
14316 KB |
Output is correct |
12 |
Correct |
39 ms |
5108 KB |
Output is correct |
13 |
Correct |
88 ms |
13292 KB |
Output is correct |
14 |
Correct |
157 ms |
19304 KB |
Output is correct |
15 |
Correct |
123 ms |
17260 KB |
Output is correct |
16 |
Correct |
117 ms |
16620 KB |
Output is correct |
17 |
Correct |
103 ms |
14444 KB |
Output is correct |
18 |
Correct |
1 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
512 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 |
512 KB |
Output is correct |
7 |
Correct |
1 ms |
512 KB |
Output is correct |
8 |
Correct |
2 ms |
512 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
1 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
512 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 |
512 KB |
Output is correct |
8 |
Correct |
1 ms |
512 KB |
Output is correct |
9 |
Correct |
2 ms |
512 KB |
Output is correct |
10 |
Correct |
1 ms |
512 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
536 KB |
Output is correct |
13 |
Correct |
1 ms |
512 KB |
Output is correct |
14 |
Correct |
1 ms |
512 KB |
Output is correct |
15 |
Correct |
1 ms |
512 KB |
Output is correct |
16 |
Correct |
1 ms |
512 KB |
Output is correct |
17 |
Correct |
1 ms |
512 KB |
Output is correct |
18 |
Correct |
1 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
512 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 |
512 KB |
Output is correct |
8 |
Correct |
1 ms |
512 KB |
Output is correct |
9 |
Correct |
2 ms |
512 KB |
Output is correct |
10 |
Correct |
1 ms |
512 KB |
Output is correct |
11 |
Correct |
10 ms |
2176 KB |
Output is correct |
12 |
Correct |
20 ms |
3708 KB |
Output is correct |
13 |
Correct |
73 ms |
9840 KB |
Output is correct |
14 |
Correct |
147 ms |
19172 KB |
Output is correct |
15 |
Correct |
122 ms |
17260 KB |
Output is correct |
16 |
Correct |
120 ms |
16620 KB |
Output is correct |
17 |
Correct |
86 ms |
14316 KB |
Output is correct |
18 |
Correct |
96 ms |
14316 KB |
Output is correct |
19 |
Correct |
39 ms |
5108 KB |
Output is correct |
20 |
Correct |
88 ms |
13292 KB |
Output is correct |
21 |
Correct |
157 ms |
19304 KB |
Output is correct |
22 |
Correct |
123 ms |
17260 KB |
Output is correct |
23 |
Correct |
117 ms |
16620 KB |
Output is correct |
24 |
Correct |
103 ms |
14444 KB |
Output is correct |
25 |
Correct |
1 ms |
384 KB |
Output is correct |
26 |
Correct |
2 ms |
536 KB |
Output is correct |
27 |
Correct |
1 ms |
512 KB |
Output is correct |
28 |
Correct |
1 ms |
512 KB |
Output is correct |
29 |
Correct |
1 ms |
512 KB |
Output is correct |
30 |
Correct |
1 ms |
512 KB |
Output is correct |
31 |
Correct |
1 ms |
512 KB |
Output is correct |
32 |
Correct |
45 ms |
5108 KB |
Output is correct |
33 |
Correct |
108 ms |
13420 KB |
Output is correct |
34 |
Correct |
178 ms |
19176 KB |
Output is correct |
35 |
Correct |
154 ms |
16748 KB |
Output is correct |
36 |
Correct |
142 ms |
17256 KB |
Output is correct |
37 |
Correct |
197 ms |
19212 KB |
Output is correct |
38 |
Correct |
120 ms |
15340 KB |
Output is correct |
39 |
Correct |
126 ms |
14572 KB |
Output is correct |
40 |
Correct |
102 ms |
14468 KB |
Output is correct |
41 |
Correct |
107 ms |
14444 KB |
Output is correct |
42 |
Correct |
102 ms |
14316 KB |
Output is correct |
43 |
Correct |
103 ms |
14316 KB |
Output is correct |
44 |
Correct |
202 ms |
19180 KB |
Output is correct |
45 |
Correct |
128 ms |
14700 KB |
Output is correct |
46 |
Correct |
104 ms |
13308 KB |
Output is correct |
47 |
Correct |
1 ms |
512 KB |
Output is correct |