#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 5e5 + 5, INF = 1e18 + 7;
int n, a[N], b[N], d[N], pre[N];
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for(int i = 1; i <= n; i++) cin >> a[i] >> b[i], d[i] = a[i] - b[i], pre[i] = pre[i - 1] + d[i];
priority_queue<int> pq;
int ans = 0;
for(int i = 1; i <= n - 1; i++){
pq.push(pre[i]); pq.push(pre[i]);
ans = ans + abs(max(0LL, min(pq.top(), pre[n])) - pre[i]);
pq.pop();
}
cout << ans << '\n';
return 0;
}
/*
7
2 0
2 0
2 0
0 5
2 0
2 0
2 0
6
1 2
0 0
2 0
0 0
0 0
0 1
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6492 KB |
Output is correct |
2 |
Correct |
2 ms |
6488 KB |
Output is correct |
3 |
Correct |
3 ms |
6492 KB |
Output is correct |
4 |
Correct |
12 ms |
11744 KB |
Output is correct |
5 |
Correct |
22 ms |
16600 KB |
Output is correct |
6 |
Correct |
58 ms |
20492 KB |
Output is correct |
7 |
Correct |
119 ms |
28620 KB |
Output is correct |
8 |
Correct |
102 ms |
25288 KB |
Output is correct |
9 |
Correct |
109 ms |
24452 KB |
Output is correct |
10 |
Correct |
71 ms |
22728 KB |
Output is correct |
11 |
Correct |
76 ms |
23304 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6492 KB |
Output is correct |
2 |
Correct |
2 ms |
6488 KB |
Output is correct |
3 |
Correct |
3 ms |
6492 KB |
Output is correct |
4 |
Correct |
12 ms |
11744 KB |
Output is correct |
5 |
Correct |
22 ms |
16600 KB |
Output is correct |
6 |
Correct |
58 ms |
20492 KB |
Output is correct |
7 |
Correct |
119 ms |
28620 KB |
Output is correct |
8 |
Correct |
102 ms |
25288 KB |
Output is correct |
9 |
Correct |
109 ms |
24452 KB |
Output is correct |
10 |
Correct |
71 ms |
22728 KB |
Output is correct |
11 |
Correct |
76 ms |
23304 KB |
Output is correct |
12 |
Correct |
35 ms |
17616 KB |
Output is correct |
13 |
Correct |
81 ms |
23800 KB |
Output is correct |
14 |
Correct |
117 ms |
29128 KB |
Output is correct |
15 |
Correct |
100 ms |
25548 KB |
Output is correct |
16 |
Correct |
139 ms |
24444 KB |
Output is correct |
17 |
Correct |
71 ms |
23908 KB |
Output is correct |
18 |
Correct |
2 ms |
6492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6492 KB |
Output is correct |
2 |
Correct |
2 ms |
6488 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
6536 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
7 |
Correct |
2 ms |
6488 KB |
Output is correct |
8 |
Correct |
2 ms |
6628 KB |
Output is correct |
9 |
Correct |
2 ms |
6492 KB |
Output is correct |
10 |
Correct |
2 ms |
6492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6492 KB |
Output is correct |
2 |
Correct |
2 ms |
6488 KB |
Output is correct |
3 |
Correct |
3 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
6536 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
7 |
Correct |
2 ms |
6488 KB |
Output is correct |
8 |
Correct |
2 ms |
6628 KB |
Output is correct |
9 |
Correct |
2 ms |
6492 KB |
Output is correct |
10 |
Correct |
2 ms |
6492 KB |
Output is correct |
11 |
Correct |
2 ms |
6492 KB |
Output is correct |
12 |
Correct |
2 ms |
6492 KB |
Output is correct |
13 |
Correct |
2 ms |
6748 KB |
Output is correct |
14 |
Correct |
2 ms |
6748 KB |
Output is correct |
15 |
Correct |
2 ms |
6492 KB |
Output is correct |
16 |
Correct |
2 ms |
6492 KB |
Output is correct |
17 |
Correct |
2 ms |
6640 KB |
Output is correct |
18 |
Correct |
2 ms |
6748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6492 KB |
Output is correct |
2 |
Correct |
2 ms |
6488 KB |
Output is correct |
3 |
Correct |
3 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
6536 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
7 |
Correct |
2 ms |
6488 KB |
Output is correct |
8 |
Correct |
2 ms |
6628 KB |
Output is correct |
9 |
Correct |
2 ms |
6492 KB |
Output is correct |
10 |
Correct |
2 ms |
6492 KB |
Output is correct |
11 |
Correct |
12 ms |
11744 KB |
Output is correct |
12 |
Correct |
22 ms |
16600 KB |
Output is correct |
13 |
Correct |
58 ms |
20492 KB |
Output is correct |
14 |
Correct |
119 ms |
28620 KB |
Output is correct |
15 |
Correct |
102 ms |
25288 KB |
Output is correct |
16 |
Correct |
109 ms |
24452 KB |
Output is correct |
17 |
Correct |
71 ms |
22728 KB |
Output is correct |
18 |
Correct |
76 ms |
23304 KB |
Output is correct |
19 |
Correct |
35 ms |
17616 KB |
Output is correct |
20 |
Correct |
81 ms |
23800 KB |
Output is correct |
21 |
Correct |
117 ms |
29128 KB |
Output is correct |
22 |
Correct |
100 ms |
25548 KB |
Output is correct |
23 |
Correct |
139 ms |
24444 KB |
Output is correct |
24 |
Correct |
71 ms |
23908 KB |
Output is correct |
25 |
Correct |
2 ms |
6492 KB |
Output is correct |
26 |
Correct |
2 ms |
6748 KB |
Output is correct |
27 |
Correct |
2 ms |
6748 KB |
Output is correct |
28 |
Correct |
2 ms |
6492 KB |
Output is correct |
29 |
Correct |
2 ms |
6492 KB |
Output is correct |
30 |
Correct |
2 ms |
6640 KB |
Output is correct |
31 |
Correct |
2 ms |
6748 KB |
Output is correct |
32 |
Correct |
2 ms |
6492 KB |
Output is correct |
33 |
Correct |
30 ms |
17628 KB |
Output is correct |
34 |
Correct |
80 ms |
25100 KB |
Output is correct |
35 |
Correct |
118 ms |
28356 KB |
Output is correct |
36 |
Correct |
107 ms |
26312 KB |
Output is correct |
37 |
Correct |
107 ms |
25292 KB |
Output is correct |
38 |
Correct |
127 ms |
28160 KB |
Output is correct |
39 |
Correct |
87 ms |
24020 KB |
Output is correct |
40 |
Correct |
96 ms |
24908 KB |
Output is correct |
41 |
Correct |
78 ms |
24012 KB |
Output is correct |
42 |
Correct |
79 ms |
23240 KB |
Output is correct |
43 |
Correct |
73 ms |
22184 KB |
Output is correct |
44 |
Correct |
75 ms |
23500 KB |
Output is correct |
45 |
Correct |
127 ms |
27648 KB |
Output is correct |
46 |
Correct |
96 ms |
23768 KB |
Output is correct |
47 |
Correct |
76 ms |
22728 KB |
Output is correct |