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)
w=[]
for c in s:
if c<0:
ww-=c
c=0
elif c>p:
ww+=c-p
c=p
hq.heappush(w,-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 |
21 ms |
2900 KB |
Output is correct |
3 |
Correct |
20 ms |
2992 KB |
Output is correct |
4 |
Correct |
137 ms |
5476 KB |
Output is correct |
5 |
Correct |
268 ms |
7828 KB |
Output is correct |
6 |
Correct |
637 ms |
15040 KB |
Output is correct |
7 |
Execution timed out |
1085 ms |
25476 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
2900 KB |
Output is correct |
2 |
Correct |
21 ms |
2900 KB |
Output is correct |
3 |
Correct |
20 ms |
2992 KB |
Output is correct |
4 |
Correct |
137 ms |
5476 KB |
Output is correct |
5 |
Correct |
268 ms |
7828 KB |
Output is correct |
6 |
Correct |
637 ms |
15040 KB |
Output is correct |
7 |
Execution timed out |
1085 ms |
25476 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
2900 KB |
Output is correct |
2 |
Correct |
21 ms |
2900 KB |
Output is correct |
3 |
Correct |
14 ms |
2892 KB |
Output is correct |
4 |
Correct |
16 ms |
2896 KB |
Output is correct |
5 |
Correct |
22 ms |
2892 KB |
Output is correct |
6 |
Correct |
24 ms |
3112 KB |
Output is correct |
7 |
Correct |
20 ms |
3052 KB |
Output is correct |
8 |
Correct |
21 ms |
3020 KB |
Output is correct |
9 |
Correct |
21 ms |
3020 KB |
Output is correct |
10 |
Correct |
21 ms |
2960 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
2900 KB |
Output is correct |
2 |
Correct |
21 ms |
2900 KB |
Output is correct |
3 |
Correct |
20 ms |
2992 KB |
Output is correct |
4 |
Correct |
14 ms |
2892 KB |
Output is correct |
5 |
Correct |
16 ms |
2896 KB |
Output is correct |
6 |
Correct |
22 ms |
2892 KB |
Output is correct |
7 |
Correct |
24 ms |
3112 KB |
Output is correct |
8 |
Correct |
20 ms |
3052 KB |
Output is correct |
9 |
Correct |
21 ms |
3020 KB |
Output is correct |
10 |
Correct |
21 ms |
3020 KB |
Output is correct |
11 |
Correct |
21 ms |
2960 KB |
Output is correct |
12 |
Correct |
18 ms |
3028 KB |
Output is correct |
13 |
Correct |
23 ms |
3284 KB |
Output is correct |
14 |
Correct |
21 ms |
3156 KB |
Output is correct |
15 |
Correct |
22 ms |
3132 KB |
Output is correct |
16 |
Correct |
20 ms |
3180 KB |
Output is correct |
17 |
Correct |
20 ms |
3156 KB |
Output is correct |
18 |
Correct |
22 ms |
3116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
2900 KB |
Output is correct |
2 |
Correct |
21 ms |
2900 KB |
Output is correct |
3 |
Correct |
20 ms |
2992 KB |
Output is correct |
4 |
Correct |
14 ms |
2892 KB |
Output is correct |
5 |
Correct |
16 ms |
2896 KB |
Output is correct |
6 |
Correct |
22 ms |
2892 KB |
Output is correct |
7 |
Correct |
24 ms |
3112 KB |
Output is correct |
8 |
Correct |
20 ms |
3052 KB |
Output is correct |
9 |
Correct |
21 ms |
3020 KB |
Output is correct |
10 |
Correct |
21 ms |
3020 KB |
Output is correct |
11 |
Correct |
137 ms |
5476 KB |
Output is correct |
12 |
Correct |
268 ms |
7828 KB |
Output is correct |
13 |
Correct |
637 ms |
15040 KB |
Output is correct |
14 |
Execution timed out |
1085 ms |
25476 KB |
Time limit exceeded |
15 |
Halted |
0 ms |
0 KB |
- |