#include <bits/stdc++.h>
#define int long long
using namespace std;
int n,a[500001],b[500001],d[500001],res;
priority_queue <int> q;
int32_t main(){
ios_base::sync_with_stdio(NULL);cin.tie(nullptr);
cin >> n;
for (int i=1;i<=n;i++){
cin >> a[i] >> b[i];
d[i]=d[i-1]+a[i]-b[i];
}
for (int i=1;i<n;i++){
res+=max(-d[i],0LL)+max(d[i]-d[n],0LL);
d[i]=min(max(d[i],0LL),d[n]);
q.push(d[i]);
q.push(d[i]);
res+=q.top()-d[i];
q.pop();
}
cout << res;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
6 ms |
2136 KB |
Output is correct |
5 |
Correct |
13 ms |
3808 KB |
Output is correct |
6 |
Correct |
45 ms |
8372 KB |
Output is correct |
7 |
Correct |
85 ms |
16204 KB |
Output is correct |
8 |
Correct |
77 ms |
16200 KB |
Output is correct |
9 |
Correct |
73 ms |
16272 KB |
Output is correct |
10 |
Correct |
61 ms |
16240 KB |
Output is correct |
11 |
Correct |
60 ms |
16296 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
6 ms |
2136 KB |
Output is correct |
5 |
Correct |
13 ms |
3808 KB |
Output is correct |
6 |
Correct |
45 ms |
8372 KB |
Output is correct |
7 |
Correct |
85 ms |
16204 KB |
Output is correct |
8 |
Correct |
77 ms |
16200 KB |
Output is correct |
9 |
Correct |
73 ms |
16272 KB |
Output is correct |
10 |
Correct |
61 ms |
16240 KB |
Output is correct |
11 |
Correct |
60 ms |
16296 KB |
Output is correct |
12 |
Correct |
22 ms |
4376 KB |
Output is correct |
13 |
Correct |
52 ms |
11508 KB |
Output is correct |
14 |
Correct |
88 ms |
16184 KB |
Output is correct |
15 |
Correct |
77 ms |
16216 KB |
Output is correct |
16 |
Correct |
74 ms |
16264 KB |
Output is correct |
17 |
Correct |
69 ms |
16236 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
480 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
6 ms |
2136 KB |
Output is correct |
12 |
Correct |
13 ms |
3808 KB |
Output is correct |
13 |
Correct |
45 ms |
8372 KB |
Output is correct |
14 |
Correct |
85 ms |
16204 KB |
Output is correct |
15 |
Correct |
77 ms |
16200 KB |
Output is correct |
16 |
Correct |
73 ms |
16272 KB |
Output is correct |
17 |
Correct |
61 ms |
16240 KB |
Output is correct |
18 |
Correct |
60 ms |
16296 KB |
Output is correct |
19 |
Correct |
22 ms |
4376 KB |
Output is correct |
20 |
Correct |
52 ms |
11508 KB |
Output is correct |
21 |
Correct |
88 ms |
16184 KB |
Output is correct |
22 |
Correct |
77 ms |
16216 KB |
Output is correct |
23 |
Correct |
74 ms |
16264 KB |
Output is correct |
24 |
Correct |
69 ms |
16236 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
480 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
1 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
29 ms |
4424 KB |
Output is correct |
34 |
Correct |
69 ms |
11624 KB |
Output is correct |
35 |
Correct |
114 ms |
16192 KB |
Output is correct |
36 |
Correct |
105 ms |
16208 KB |
Output is correct |
37 |
Correct |
98 ms |
16256 KB |
Output is correct |
38 |
Correct |
111 ms |
16192 KB |
Output is correct |
39 |
Correct |
75 ms |
16196 KB |
Output is correct |
40 |
Correct |
83 ms |
15180 KB |
Output is correct |
41 |
Correct |
72 ms |
16196 KB |
Output is correct |
42 |
Correct |
74 ms |
16252 KB |
Output is correct |
43 |
Correct |
73 ms |
16220 KB |
Output is correct |
44 |
Correct |
76 ms |
16272 KB |
Output is correct |
45 |
Correct |
95 ms |
16224 KB |
Output is correct |
46 |
Correct |
95 ms |
16184 KB |
Output is correct |
47 |
Correct |
74 ms |
13676 KB |
Output is correct |