#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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
328 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
472 KB |
Output is correct |
4 |
Correct |
11 ms |
2432 KB |
Output is correct |
5 |
Correct |
17 ms |
4436 KB |
Output is correct |
6 |
Correct |
42 ms |
11764 KB |
Output is correct |
7 |
Correct |
86 ms |
22984 KB |
Output is correct |
8 |
Correct |
76 ms |
21184 KB |
Output is correct |
9 |
Correct |
73 ms |
20440 KB |
Output is correct |
10 |
Correct |
60 ms |
18180 KB |
Output is correct |
11 |
Correct |
59 ms |
18244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
328 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
472 KB |
Output is correct |
4 |
Correct |
11 ms |
2432 KB |
Output is correct |
5 |
Correct |
17 ms |
4436 KB |
Output is correct |
6 |
Correct |
42 ms |
11764 KB |
Output is correct |
7 |
Correct |
86 ms |
22984 KB |
Output is correct |
8 |
Correct |
76 ms |
21184 KB |
Output is correct |
9 |
Correct |
73 ms |
20440 KB |
Output is correct |
10 |
Correct |
60 ms |
18180 KB |
Output is correct |
11 |
Correct |
59 ms |
18244 KB |
Output is correct |
12 |
Correct |
23 ms |
6100 KB |
Output is correct |
13 |
Correct |
54 ms |
15564 KB |
Output is correct |
14 |
Correct |
88 ms |
22952 KB |
Output is correct |
15 |
Correct |
75 ms |
21152 KB |
Output is correct |
16 |
Correct |
72 ms |
20412 KB |
Output is correct |
17 |
Correct |
71 ms |
18232 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
328 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
464 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
328 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
472 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
464 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 |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
464 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
328 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
472 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
464 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 |
11 ms |
2432 KB |
Output is correct |
12 |
Correct |
17 ms |
4436 KB |
Output is correct |
13 |
Correct |
42 ms |
11764 KB |
Output is correct |
14 |
Correct |
86 ms |
22984 KB |
Output is correct |
15 |
Correct |
76 ms |
21184 KB |
Output is correct |
16 |
Correct |
73 ms |
20440 KB |
Output is correct |
17 |
Correct |
60 ms |
18180 KB |
Output is correct |
18 |
Correct |
59 ms |
18244 KB |
Output is correct |
19 |
Correct |
23 ms |
6100 KB |
Output is correct |
20 |
Correct |
54 ms |
15564 KB |
Output is correct |
21 |
Correct |
88 ms |
22952 KB |
Output is correct |
22 |
Correct |
75 ms |
21152 KB |
Output is correct |
23 |
Correct |
72 ms |
20412 KB |
Output is correct |
24 |
Correct |
71 ms |
18232 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
464 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
468 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 |
6052 KB |
Output is correct |
34 |
Correct |
69 ms |
15648 KB |
Output is correct |
35 |
Correct |
116 ms |
22960 KB |
Output is correct |
36 |
Correct |
106 ms |
20536 KB |
Output is correct |
37 |
Correct |
98 ms |
21164 KB |
Output is correct |
38 |
Correct |
113 ms |
23076 KB |
Output is correct |
39 |
Correct |
74 ms |
19168 KB |
Output is correct |
40 |
Correct |
84 ms |
17980 KB |
Output is correct |
41 |
Correct |
72 ms |
18152 KB |
Output is correct |
42 |
Correct |
72 ms |
18224 KB |
Output is correct |
43 |
Correct |
73 ms |
18244 KB |
Output is correct |
44 |
Correct |
77 ms |
18260 KB |
Output is correct |
45 |
Correct |
96 ms |
22980 KB |
Output is correct |
46 |
Correct |
94 ms |
18500 KB |
Output is correct |
47 |
Correct |
74 ms |
16324 KB |
Output is correct |