#include<bits/stdc++.h>
using namespace std;
const int MAXN=5e5+5;
long long pref[MAXN];
int main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
int n,a,b;
long long ans=0;
cin >> n;
for(int i=1;i<=n;i++){
cin >> a >> b;
pref[i]=pref[i-1]+a-b;
}
for(int i=1;i<=n;i++){
if(pref[i]>pref[n]){
ans+=(pref[i]-pref[n]);
pref[i]=pref[n];
}
else if(pref[i]<0){
ans+=(-pref[i]);
pref[i]=0;
}
}
//make pref non decreasing -> Classic Slope Trick CF713C
priority_queue<long long> PQ;
for(int i=1;i<=n;i++){
PQ.push(pref[i]);PQ.push(pref[i]);
ans+=(PQ.top()-pref[i]);
PQ.pop();
}
cout << ans << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
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 |
1664 KB |
Output is correct |
5 |
Correct |
21 ms |
2932 KB |
Output is correct |
6 |
Correct |
75 ms |
7796 KB |
Output is correct |
7 |
Correct |
159 ms |
15212 KB |
Output is correct |
8 |
Correct |
130 ms |
13420 KB |
Output is correct |
9 |
Correct |
123 ms |
12784 KB |
Output is correct |
10 |
Correct |
100 ms |
10476 KB |
Output is correct |
11 |
Correct |
98 ms |
10424 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
1664 KB |
Output is correct |
5 |
Correct |
21 ms |
2932 KB |
Output is correct |
6 |
Correct |
75 ms |
7796 KB |
Output is correct |
7 |
Correct |
159 ms |
15212 KB |
Output is correct |
8 |
Correct |
130 ms |
13420 KB |
Output is correct |
9 |
Correct |
123 ms |
12784 KB |
Output is correct |
10 |
Correct |
100 ms |
10476 KB |
Output is correct |
11 |
Correct |
98 ms |
10424 KB |
Output is correct |
12 |
Correct |
40 ms |
4212 KB |
Output is correct |
13 |
Correct |
96 ms |
10992 KB |
Output is correct |
14 |
Correct |
161 ms |
15216 KB |
Output is correct |
15 |
Correct |
129 ms |
13424 KB |
Output is correct |
16 |
Correct |
120 ms |
12652 KB |
Output is correct |
17 |
Correct |
98 ms |
10476 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
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 |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
512 KB |
Output is correct |
13 |
Correct |
2 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 |
1 ms |
512 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
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 |
11 |
Correct |
10 ms |
1664 KB |
Output is correct |
12 |
Correct |
21 ms |
2932 KB |
Output is correct |
13 |
Correct |
75 ms |
7796 KB |
Output is correct |
14 |
Correct |
159 ms |
15212 KB |
Output is correct |
15 |
Correct |
130 ms |
13420 KB |
Output is correct |
16 |
Correct |
123 ms |
12784 KB |
Output is correct |
17 |
Correct |
100 ms |
10476 KB |
Output is correct |
18 |
Correct |
98 ms |
10424 KB |
Output is correct |
19 |
Correct |
40 ms |
4212 KB |
Output is correct |
20 |
Correct |
96 ms |
10992 KB |
Output is correct |
21 |
Correct |
161 ms |
15216 KB |
Output is correct |
22 |
Correct |
129 ms |
13424 KB |
Output is correct |
23 |
Correct |
120 ms |
12652 KB |
Output is correct |
24 |
Correct |
98 ms |
10476 KB |
Output is correct |
25 |
Correct |
1 ms |
384 KB |
Output is correct |
26 |
Correct |
1 ms |
512 KB |
Output is correct |
27 |
Correct |
2 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 |
1 ms |
512 KB |
Output is correct |
32 |
Correct |
45 ms |
4212 KB |
Output is correct |
33 |
Correct |
118 ms |
10988 KB |
Output is correct |
34 |
Correct |
197 ms |
15184 KB |
Output is correct |
35 |
Correct |
169 ms |
12868 KB |
Output is correct |
36 |
Correct |
150 ms |
13420 KB |
Output is correct |
37 |
Correct |
208 ms |
15212 KB |
Output is correct |
38 |
Correct |
107 ms |
11372 KB |
Output is correct |
39 |
Correct |
119 ms |
10988 KB |
Output is correct |
40 |
Correct |
106 ms |
10476 KB |
Output is correct |
41 |
Correct |
111 ms |
10472 KB |
Output is correct |
42 |
Correct |
107 ms |
10476 KB |
Output is correct |
43 |
Correct |
109 ms |
10476 KB |
Output is correct |
44 |
Correct |
160 ms |
15212 KB |
Output is correct |
45 |
Correct |
126 ms |
10732 KB |
Output is correct |
46 |
Correct |
106 ms |
10344 KB |
Output is correct |
47 |
Correct |
1 ms |
384 KB |
Output is correct |