import sys
import heapq as hq
input = sys.stdin.readline
n=int(input())
t=0
p=0
s=[]
ww=0
z=[]
for i in range(n):
pi,bi=list(map(int,input().split()))
p+=bi-pi
s.append(p)
if p==0:
for c in s:
ww+=abs(c)
elif p==-1:
for c in s:
if c>=0:
ww+=c
t+=c+1
else:
ww-=c
t-=c+1
z.append(ww-t)
ww=min(t,min(z)+t)
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 |
12 ms |
2900 KB |
Output is correct |
2 |
Correct |
18 ms |
2944 KB |
Output is correct |
3 |
Correct |
22 ms |
3012 KB |
Output is correct |
4 |
Correct |
95 ms |
5056 KB |
Output is correct |
5 |
Correct |
182 ms |
6956 KB |
Output is correct |
6 |
Correct |
399 ms |
12856 KB |
Output is correct |
7 |
Correct |
834 ms |
22848 KB |
Output is correct |
8 |
Correct |
780 ms |
26896 KB |
Output is correct |
9 |
Correct |
814 ms |
22740 KB |
Output is correct |
10 |
Correct |
739 ms |
13704 KB |
Output is correct |
11 |
Correct |
754 ms |
16332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
2900 KB |
Output is correct |
2 |
Correct |
18 ms |
2944 KB |
Output is correct |
3 |
Correct |
22 ms |
3012 KB |
Output is correct |
4 |
Correct |
95 ms |
5056 KB |
Output is correct |
5 |
Correct |
182 ms |
6956 KB |
Output is correct |
6 |
Correct |
399 ms |
12856 KB |
Output is correct |
7 |
Correct |
834 ms |
22848 KB |
Output is correct |
8 |
Correct |
780 ms |
26896 KB |
Output is correct |
9 |
Correct |
814 ms |
22740 KB |
Output is correct |
10 |
Correct |
739 ms |
13704 KB |
Output is correct |
11 |
Correct |
754 ms |
16332 KB |
Output is correct |
12 |
Execution timed out |
1094 ms |
8420 KB |
Time limit exceeded |
13 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
2900 KB |
Output is correct |
2 |
Correct |
18 ms |
2944 KB |
Output is correct |
3 |
Correct |
14 ms |
2900 KB |
Output is correct |
4 |
Correct |
16 ms |
2944 KB |
Output is correct |
5 |
Correct |
22 ms |
2916 KB |
Output is correct |
6 |
Correct |
21 ms |
3120 KB |
Output is correct |
7 |
Correct |
20 ms |
3012 KB |
Output is correct |
8 |
Correct |
23 ms |
3044 KB |
Output is correct |
9 |
Correct |
21 ms |
3088 KB |
Output is correct |
10 |
Incorrect |
95 ms |
3020 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
2900 KB |
Output is correct |
2 |
Correct |
18 ms |
2944 KB |
Output is correct |
3 |
Correct |
22 ms |
3012 KB |
Output is correct |
4 |
Correct |
14 ms |
2900 KB |
Output is correct |
5 |
Correct |
16 ms |
2944 KB |
Output is correct |
6 |
Correct |
22 ms |
2916 KB |
Output is correct |
7 |
Correct |
21 ms |
3120 KB |
Output is correct |
8 |
Correct |
20 ms |
3012 KB |
Output is correct |
9 |
Correct |
23 ms |
3044 KB |
Output is correct |
10 |
Correct |
21 ms |
3088 KB |
Output is correct |
11 |
Incorrect |
95 ms |
3020 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
2900 KB |
Output is correct |
2 |
Correct |
18 ms |
2944 KB |
Output is correct |
3 |
Correct |
22 ms |
3012 KB |
Output is correct |
4 |
Correct |
14 ms |
2900 KB |
Output is correct |
5 |
Correct |
16 ms |
2944 KB |
Output is correct |
6 |
Correct |
22 ms |
2916 KB |
Output is correct |
7 |
Correct |
21 ms |
3120 KB |
Output is correct |
8 |
Correct |
20 ms |
3012 KB |
Output is correct |
9 |
Correct |
23 ms |
3044 KB |
Output is correct |
10 |
Correct |
21 ms |
3088 KB |
Output is correct |
11 |
Correct |
95 ms |
5056 KB |
Output is correct |
12 |
Correct |
182 ms |
6956 KB |
Output is correct |
13 |
Correct |
399 ms |
12856 KB |
Output is correct |
14 |
Correct |
834 ms |
22848 KB |
Output is correct |
15 |
Correct |
780 ms |
26896 KB |
Output is correct |
16 |
Correct |
814 ms |
22740 KB |
Output is correct |
17 |
Correct |
739 ms |
13704 KB |
Output is correct |
18 |
Correct |
754 ms |
16332 KB |
Output is correct |
19 |
Execution timed out |
1094 ms |
8420 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |