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+=pi-bi
s.append(p)
if p==0:
for c in s:
ww+=abs(c)
elif p==1:
for c in s:
if c>=1:
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)
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
18220 KB |
Output is correct |
2 |
Correct |
54 ms |
20080 KB |
Output is correct |
3 |
Correct |
56 ms |
19744 KB |
Output is correct |
4 |
Correct |
70 ms |
23312 KB |
Output is correct |
5 |
Correct |
90 ms |
25560 KB |
Output is correct |
6 |
Correct |
152 ms |
26296 KB |
Output is correct |
7 |
Correct |
256 ms |
32056 KB |
Output is correct |
8 |
Correct |
243 ms |
29652 KB |
Output is correct |
9 |
Correct |
238 ms |
29364 KB |
Output is correct |
10 |
Correct |
217 ms |
29204 KB |
Output is correct |
11 |
Correct |
204 ms |
29232 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
18220 KB |
Output is correct |
2 |
Correct |
54 ms |
20080 KB |
Output is correct |
3 |
Correct |
56 ms |
19744 KB |
Output is correct |
4 |
Correct |
70 ms |
23312 KB |
Output is correct |
5 |
Correct |
90 ms |
25560 KB |
Output is correct |
6 |
Correct |
152 ms |
26296 KB |
Output is correct |
7 |
Correct |
256 ms |
32056 KB |
Output is correct |
8 |
Correct |
243 ms |
29652 KB |
Output is correct |
9 |
Correct |
238 ms |
29364 KB |
Output is correct |
10 |
Correct |
217 ms |
29204 KB |
Output is correct |
11 |
Correct |
204 ms |
29232 KB |
Output is correct |
12 |
Correct |
107 ms |
28344 KB |
Output is correct |
13 |
Correct |
191 ms |
32552 KB |
Output is correct |
14 |
Correct |
265 ms |
40652 KB |
Output is correct |
15 |
Correct |
251 ms |
41632 KB |
Output is correct |
16 |
Correct |
242 ms |
40888 KB |
Output is correct |
17 |
Correct |
228 ms |
36264 KB |
Output is correct |
18 |
Correct |
57 ms |
20008 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
18220 KB |
Output is correct |
2 |
Correct |
54 ms |
20080 KB |
Output is correct |
3 |
Correct |
57 ms |
20008 KB |
Output is correct |
4 |
Correct |
38 ms |
18220 KB |
Output is correct |
5 |
Correct |
66 ms |
20020 KB |
Output is correct |
6 |
Correct |
81 ms |
22344 KB |
Output is correct |
7 |
Correct |
88 ms |
22268 KB |
Output is correct |
8 |
Correct |
75 ms |
21640 KB |
Output is correct |
9 |
Correct |
110 ms |
25252 KB |
Output is correct |
10 |
Correct |
55 ms |
20372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
18220 KB |
Output is correct |
2 |
Correct |
54 ms |
20080 KB |
Output is correct |
3 |
Correct |
56 ms |
19744 KB |
Output is correct |
4 |
Correct |
38 ms |
18220 KB |
Output is correct |
5 |
Correct |
66 ms |
20020 KB |
Output is correct |
6 |
Correct |
81 ms |
22344 KB |
Output is correct |
7 |
Correct |
88 ms |
22268 KB |
Output is correct |
8 |
Correct |
75 ms |
21640 KB |
Output is correct |
9 |
Correct |
110 ms |
25252 KB |
Output is correct |
10 |
Correct |
55 ms |
20372 KB |
Output is correct |
11 |
Correct |
57 ms |
20008 KB |
Output is correct |
12 |
Correct |
86 ms |
21800 KB |
Output is correct |
13 |
Correct |
110 ms |
24620 KB |
Output is correct |
14 |
Correct |
103 ms |
23136 KB |
Output is correct |
15 |
Correct |
111 ms |
24440 KB |
Output is correct |
16 |
Correct |
74 ms |
21616 KB |
Output is correct |
17 |
Correct |
92 ms |
23300 KB |
Output is correct |
18 |
Correct |
91 ms |
22592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
18220 KB |
Output is correct |
2 |
Correct |
54 ms |
20080 KB |
Output is correct |
3 |
Correct |
56 ms |
19744 KB |
Output is correct |
4 |
Correct |
38 ms |
18220 KB |
Output is correct |
5 |
Correct |
66 ms |
20020 KB |
Output is correct |
6 |
Correct |
81 ms |
22344 KB |
Output is correct |
7 |
Correct |
88 ms |
22268 KB |
Output is correct |
8 |
Correct |
75 ms |
21640 KB |
Output is correct |
9 |
Correct |
110 ms |
25252 KB |
Output is correct |
10 |
Correct |
55 ms |
20372 KB |
Output is correct |
11 |
Correct |
70 ms |
23312 KB |
Output is correct |
12 |
Correct |
90 ms |
25560 KB |
Output is correct |
13 |
Correct |
152 ms |
26296 KB |
Output is correct |
14 |
Correct |
256 ms |
32056 KB |
Output is correct |
15 |
Correct |
243 ms |
29652 KB |
Output is correct |
16 |
Correct |
238 ms |
29364 KB |
Output is correct |
17 |
Correct |
217 ms |
29204 KB |
Output is correct |
18 |
Correct |
204 ms |
29232 KB |
Output is correct |
19 |
Correct |
107 ms |
28344 KB |
Output is correct |
20 |
Correct |
191 ms |
32552 KB |
Output is correct |
21 |
Correct |
265 ms |
40652 KB |
Output is correct |
22 |
Correct |
251 ms |
41632 KB |
Output is correct |
23 |
Correct |
242 ms |
40888 KB |
Output is correct |
24 |
Correct |
228 ms |
36264 KB |
Output is correct |
25 |
Correct |
86 ms |
21800 KB |
Output is correct |
26 |
Correct |
110 ms |
24620 KB |
Output is correct |
27 |
Correct |
103 ms |
23136 KB |
Output is correct |
28 |
Correct |
111 ms |
24440 KB |
Output is correct |
29 |
Correct |
74 ms |
21616 KB |
Output is correct |
30 |
Correct |
92 ms |
23300 KB |
Output is correct |
31 |
Correct |
91 ms |
22592 KB |
Output is correct |
32 |
Correct |
57 ms |
20008 KB |
Output is correct |
33 |
Correct |
275 ms |
31440 KB |
Output is correct |
34 |
Correct |
399 ms |
36100 KB |
Output is correct |
35 |
Correct |
580 ms |
46716 KB |
Output is correct |
36 |
Correct |
538 ms |
53204 KB |
Output is correct |
37 |
Correct |
309 ms |
42796 KB |
Output is correct |
38 |
Correct |
527 ms |
51972 KB |
Output is correct |
39 |
Correct |
372 ms |
47548 KB |
Output is correct |
40 |
Correct |
398 ms |
42152 KB |
Output is correct |
41 |
Correct |
373 ms |
42400 KB |
Output is correct |
42 |
Correct |
381 ms |
42508 KB |
Output is correct |
43 |
Correct |
403 ms |
41536 KB |
Output is correct |
44 |
Correct |
409 ms |
41668 KB |
Output is correct |
45 |
Correct |
453 ms |
48628 KB |
Output is correct |
46 |
Correct |
315 ms |
43244 KB |
Output is correct |
47 |
Correct |
458 ms |
44328 KB |
Output is correct |