#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n;
ll a[500001],b[500001],d[500001],ans;
priority_queue<ll> q;
int 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]=d[i-1]+a[i]-b[i];
}
if (d[1]>=0) q.push(d[1]), ans+=d[1];
else q.push(0), ans-=d[1];
for (int i=2; i<=n; ++i) {
if (d[i]>=q.top()) q.push(d[i]), ans+=d[i];
else {
if (d[i]<0) ans-=d[i], d[i]=0;
ans+=d[i];
q.push(d[i]); q.push(d[i]); q.pop();
}
}
//now ans is at x=Y-intersect(0), then it will be moved to x=d[n]
while (!q.empty()) ans-=min(d[n],q.top()), q.pop();
cout<<ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
14 ms |
2404 KB |
Output is correct |
5 |
Correct |
27 ms |
4416 KB |
Output is correct |
6 |
Correct |
96 ms |
11720 KB |
Output is correct |
7 |
Correct |
134 ms |
23056 KB |
Output is correct |
8 |
Correct |
183 ms |
21140 KB |
Output is correct |
9 |
Correct |
125 ms |
20512 KB |
Output is correct |
10 |
Correct |
94 ms |
18228 KB |
Output is correct |
11 |
Correct |
96 ms |
18192 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
14 ms |
2404 KB |
Output is correct |
5 |
Correct |
27 ms |
4416 KB |
Output is correct |
6 |
Correct |
96 ms |
11720 KB |
Output is correct |
7 |
Correct |
134 ms |
23056 KB |
Output is correct |
8 |
Correct |
183 ms |
21140 KB |
Output is correct |
9 |
Correct |
125 ms |
20512 KB |
Output is correct |
10 |
Correct |
94 ms |
18228 KB |
Output is correct |
11 |
Correct |
96 ms |
18192 KB |
Output is correct |
12 |
Correct |
51 ms |
6076 KB |
Output is correct |
13 |
Correct |
110 ms |
15672 KB |
Output is correct |
14 |
Correct |
128 ms |
23012 KB |
Output is correct |
15 |
Correct |
138 ms |
21188 KB |
Output is correct |
16 |
Correct |
174 ms |
20492 KB |
Output is correct |
17 |
Correct |
84 ms |
18160 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 |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
0 ms |
328 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 |
472 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
0 ms |
328 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 |
472 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
2 ms |
468 KB |
Output is correct |
14 |
Correct |
3 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
392 KB |
Output is correct |
18 |
Correct |
1 ms |
472 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
0 ms |
328 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 |
472 KB |
Output is correct |
11 |
Correct |
14 ms |
2404 KB |
Output is correct |
12 |
Correct |
27 ms |
4416 KB |
Output is correct |
13 |
Correct |
96 ms |
11720 KB |
Output is correct |
14 |
Correct |
134 ms |
23056 KB |
Output is correct |
15 |
Correct |
183 ms |
21140 KB |
Output is correct |
16 |
Correct |
125 ms |
20512 KB |
Output is correct |
17 |
Correct |
94 ms |
18228 KB |
Output is correct |
18 |
Correct |
96 ms |
18192 KB |
Output is correct |
19 |
Correct |
51 ms |
6076 KB |
Output is correct |
20 |
Correct |
110 ms |
15672 KB |
Output is correct |
21 |
Correct |
128 ms |
23012 KB |
Output is correct |
22 |
Correct |
138 ms |
21188 KB |
Output is correct |
23 |
Correct |
174 ms |
20492 KB |
Output is correct |
24 |
Correct |
84 ms |
18160 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
2 ms |
468 KB |
Output is correct |
27 |
Correct |
3 ms |
468 KB |
Output is correct |
28 |
Correct |
2 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
392 KB |
Output is correct |
31 |
Correct |
1 ms |
472 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
42 ms |
6136 KB |
Output is correct |
34 |
Correct |
114 ms |
15556 KB |
Output is correct |
35 |
Correct |
211 ms |
23016 KB |
Output is correct |
36 |
Correct |
199 ms |
20568 KB |
Output is correct |
37 |
Correct |
141 ms |
21168 KB |
Output is correct |
38 |
Correct |
184 ms |
23032 KB |
Output is correct |
39 |
Correct |
118 ms |
19180 KB |
Output is correct |
40 |
Correct |
160 ms |
17952 KB |
Output is correct |
41 |
Correct |
98 ms |
18232 KB |
Output is correct |
42 |
Correct |
90 ms |
18216 KB |
Output is correct |
43 |
Correct |
95 ms |
18208 KB |
Output is correct |
44 |
Correct |
112 ms |
18196 KB |
Output is correct |
45 |
Correct |
183 ms |
22944 KB |
Output is correct |
46 |
Correct |
93 ms |
18484 KB |
Output is correct |
47 |
Correct |
118 ms |
16376 KB |
Output is correct |