import sys
import heapq as hq
input = sys.stdin.readline
n=int(input())
t=0
p=0
s=[]
ww=0
for i in range(n):
pi,bi=list(map(int,input().split()))
p+=pi-bi
s.append(p)
if p==0:
for c in s:
ww+=abs(c)
else:
w=[]
for c in s:
if c<0:
ww-=c
c=0
elif c>p:
ww+=c-p
c=p
hq.heappush(w,-c)
if w[0]<-c:
hq.heappush(w,-c)
ww+=abs(-hq.heappop(w)-c)
print(ww)
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
2900 KB |
Output is correct |
2 |
Correct |
17 ms |
2952 KB |
Output is correct |
3 |
Correct |
18 ms |
2980 KB |
Output is correct |
4 |
Correct |
97 ms |
4968 KB |
Output is correct |
5 |
Correct |
173 ms |
6948 KB |
Output is correct |
6 |
Correct |
401 ms |
12972 KB |
Output is correct |
7 |
Correct |
817 ms |
22868 KB |
Output is correct |
8 |
Correct |
789 ms |
26668 KB |
Output is correct |
9 |
Correct |
852 ms |
22700 KB |
Output is correct |
10 |
Correct |
724 ms |
13644 KB |
Output is correct |
11 |
Correct |
712 ms |
16332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
2900 KB |
Output is correct |
2 |
Correct |
17 ms |
2952 KB |
Output is correct |
3 |
Correct |
18 ms |
2980 KB |
Output is correct |
4 |
Correct |
97 ms |
4968 KB |
Output is correct |
5 |
Correct |
173 ms |
6948 KB |
Output is correct |
6 |
Correct |
401 ms |
12972 KB |
Output is correct |
7 |
Correct |
817 ms |
22868 KB |
Output is correct |
8 |
Correct |
789 ms |
26668 KB |
Output is correct |
9 |
Correct |
852 ms |
22700 KB |
Output is correct |
10 |
Correct |
724 ms |
13644 KB |
Output is correct |
11 |
Correct |
712 ms |
16332 KB |
Output is correct |
12 |
Correct |
264 ms |
8912 KB |
Output is correct |
13 |
Correct |
613 ms |
17128 KB |
Output is correct |
14 |
Execution timed out |
1027 ms |
26872 KB |
Time limit exceeded |
15 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
2900 KB |
Output is correct |
2 |
Correct |
17 ms |
2952 KB |
Output is correct |
3 |
Correct |
13 ms |
2900 KB |
Output is correct |
4 |
Correct |
17 ms |
3288 KB |
Output is correct |
5 |
Correct |
16 ms |
2936 KB |
Output is correct |
6 |
Correct |
27 ms |
3092 KB |
Output is correct |
7 |
Correct |
22 ms |
2964 KB |
Output is correct |
8 |
Correct |
28 ms |
3048 KB |
Output is correct |
9 |
Correct |
20 ms |
3156 KB |
Output is correct |
10 |
Correct |
21 ms |
2964 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
2900 KB |
Output is correct |
2 |
Correct |
17 ms |
2952 KB |
Output is correct |
3 |
Correct |
18 ms |
2980 KB |
Output is correct |
4 |
Correct |
13 ms |
2900 KB |
Output is correct |
5 |
Correct |
17 ms |
3288 KB |
Output is correct |
6 |
Correct |
16 ms |
2936 KB |
Output is correct |
7 |
Correct |
27 ms |
3092 KB |
Output is correct |
8 |
Correct |
22 ms |
2964 KB |
Output is correct |
9 |
Correct |
28 ms |
3048 KB |
Output is correct |
10 |
Correct |
20 ms |
3156 KB |
Output is correct |
11 |
Correct |
21 ms |
2964 KB |
Output is correct |
12 |
Correct |
18 ms |
3028 KB |
Output is correct |
13 |
Correct |
21 ms |
3132 KB |
Output is correct |
14 |
Correct |
21 ms |
3156 KB |
Output is correct |
15 |
Correct |
20 ms |
3152 KB |
Output is correct |
16 |
Correct |
19 ms |
3132 KB |
Output is correct |
17 |
Correct |
19 ms |
3052 KB |
Output is correct |
18 |
Correct |
22 ms |
3060 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
2900 KB |
Output is correct |
2 |
Correct |
17 ms |
2952 KB |
Output is correct |
3 |
Correct |
18 ms |
2980 KB |
Output is correct |
4 |
Correct |
13 ms |
2900 KB |
Output is correct |
5 |
Correct |
17 ms |
3288 KB |
Output is correct |
6 |
Correct |
16 ms |
2936 KB |
Output is correct |
7 |
Correct |
27 ms |
3092 KB |
Output is correct |
8 |
Correct |
22 ms |
2964 KB |
Output is correct |
9 |
Correct |
28 ms |
3048 KB |
Output is correct |
10 |
Correct |
20 ms |
3156 KB |
Output is correct |
11 |
Correct |
97 ms |
4968 KB |
Output is correct |
12 |
Correct |
173 ms |
6948 KB |
Output is correct |
13 |
Correct |
401 ms |
12972 KB |
Output is correct |
14 |
Correct |
817 ms |
22868 KB |
Output is correct |
15 |
Correct |
789 ms |
26668 KB |
Output is correct |
16 |
Correct |
852 ms |
22700 KB |
Output is correct |
17 |
Correct |
724 ms |
13644 KB |
Output is correct |
18 |
Correct |
712 ms |
16332 KB |
Output is correct |
19 |
Correct |
264 ms |
8912 KB |
Output is correct |
20 |
Correct |
613 ms |
17128 KB |
Output is correct |
21 |
Execution timed out |
1027 ms |
26872 KB |
Time limit exceeded |
22 |
Halted |
0 ms |
0 KB |
- |