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<=-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 |
12 ms |
2900 KB |
Output is correct |
2 |
Correct |
25 ms |
2848 KB |
Output is correct |
3 |
Correct |
19 ms |
3056 KB |
Output is correct |
4 |
Correct |
93 ms |
5016 KB |
Output is correct |
5 |
Correct |
177 ms |
6988 KB |
Output is correct |
6 |
Correct |
383 ms |
13052 KB |
Output is correct |
7 |
Correct |
761 ms |
22948 KB |
Output is correct |
8 |
Correct |
773 ms |
26680 KB |
Output is correct |
9 |
Correct |
754 ms |
23060 KB |
Output is correct |
10 |
Correct |
696 ms |
13768 KB |
Output is correct |
11 |
Correct |
694 ms |
16316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
2900 KB |
Output is correct |
2 |
Correct |
25 ms |
2848 KB |
Output is correct |
3 |
Correct |
19 ms |
3056 KB |
Output is correct |
4 |
Correct |
93 ms |
5016 KB |
Output is correct |
5 |
Correct |
177 ms |
6988 KB |
Output is correct |
6 |
Correct |
383 ms |
13052 KB |
Output is correct |
7 |
Correct |
761 ms |
22948 KB |
Output is correct |
8 |
Correct |
773 ms |
26680 KB |
Output is correct |
9 |
Correct |
754 ms |
23060 KB |
Output is correct |
10 |
Correct |
696 ms |
13768 KB |
Output is correct |
11 |
Correct |
694 ms |
16316 KB |
Output is correct |
12 |
Correct |
249 ms |
12916 KB |
Output is correct |
13 |
Correct |
579 ms |
26724 KB |
Output is correct |
14 |
Correct |
954 ms |
42568 KB |
Output is correct |
15 |
Correct |
924 ms |
46592 KB |
Output is correct |
16 |
Correct |
863 ms |
42560 KB |
Output is correct |
17 |
Correct |
801 ms |
26772 KB |
Output is correct |
18 |
Correct |
19 ms |
3028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
2900 KB |
Output is correct |
2 |
Correct |
25 ms |
2848 KB |
Output is correct |
3 |
Correct |
19 ms |
3028 KB |
Output is correct |
4 |
Correct |
13 ms |
2900 KB |
Output is correct |
5 |
Correct |
17 ms |
2980 KB |
Output is correct |
6 |
Correct |
17 ms |
2972 KB |
Output is correct |
7 |
Correct |
23 ms |
3080 KB |
Output is correct |
8 |
Correct |
21 ms |
3028 KB |
Output is correct |
9 |
Correct |
20 ms |
3076 KB |
Output is correct |
10 |
Correct |
26 ms |
3084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
2900 KB |
Output is correct |
2 |
Correct |
25 ms |
2848 KB |
Output is correct |
3 |
Correct |
19 ms |
3056 KB |
Output is correct |
4 |
Correct |
13 ms |
2900 KB |
Output is correct |
5 |
Correct |
17 ms |
2980 KB |
Output is correct |
6 |
Correct |
17 ms |
2972 KB |
Output is correct |
7 |
Correct |
23 ms |
3080 KB |
Output is correct |
8 |
Correct |
21 ms |
3028 KB |
Output is correct |
9 |
Correct |
20 ms |
3076 KB |
Output is correct |
10 |
Correct |
26 ms |
3084 KB |
Output is correct |
11 |
Correct |
19 ms |
3028 KB |
Output is correct |
12 |
Correct |
19 ms |
3020 KB |
Output is correct |
13 |
Correct |
21 ms |
2996 KB |
Output is correct |
14 |
Correct |
20 ms |
3092 KB |
Output is correct |
15 |
Correct |
20 ms |
3016 KB |
Output is correct |
16 |
Correct |
19 ms |
3028 KB |
Output is correct |
17 |
Correct |
20 ms |
3028 KB |
Output is correct |
18 |
Correct |
21 ms |
3076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
2900 KB |
Output is correct |
2 |
Correct |
25 ms |
2848 KB |
Output is correct |
3 |
Correct |
19 ms |
3056 KB |
Output is correct |
4 |
Correct |
13 ms |
2900 KB |
Output is correct |
5 |
Correct |
17 ms |
2980 KB |
Output is correct |
6 |
Correct |
17 ms |
2972 KB |
Output is correct |
7 |
Correct |
23 ms |
3080 KB |
Output is correct |
8 |
Correct |
21 ms |
3028 KB |
Output is correct |
9 |
Correct |
20 ms |
3076 KB |
Output is correct |
10 |
Correct |
26 ms |
3084 KB |
Output is correct |
11 |
Correct |
93 ms |
5016 KB |
Output is correct |
12 |
Correct |
177 ms |
6988 KB |
Output is correct |
13 |
Correct |
383 ms |
13052 KB |
Output is correct |
14 |
Correct |
761 ms |
22948 KB |
Output is correct |
15 |
Correct |
773 ms |
26680 KB |
Output is correct |
16 |
Correct |
754 ms |
23060 KB |
Output is correct |
17 |
Correct |
696 ms |
13768 KB |
Output is correct |
18 |
Correct |
694 ms |
16316 KB |
Output is correct |
19 |
Correct |
249 ms |
12916 KB |
Output is correct |
20 |
Correct |
579 ms |
26724 KB |
Output is correct |
21 |
Correct |
954 ms |
42568 KB |
Output is correct |
22 |
Correct |
924 ms |
46592 KB |
Output is correct |
23 |
Correct |
863 ms |
42560 KB |
Output is correct |
24 |
Correct |
801 ms |
26772 KB |
Output is correct |
25 |
Correct |
19 ms |
3020 KB |
Output is correct |
26 |
Correct |
21 ms |
2996 KB |
Output is correct |
27 |
Correct |
20 ms |
3092 KB |
Output is correct |
28 |
Correct |
20 ms |
3016 KB |
Output is correct |
29 |
Correct |
19 ms |
3028 KB |
Output is correct |
30 |
Correct |
20 ms |
3028 KB |
Output is correct |
31 |
Correct |
21 ms |
3076 KB |
Output is correct |
32 |
Correct |
19 ms |
3028 KB |
Output is correct |
33 |
Correct |
371 ms |
8956 KB |
Output is correct |
34 |
Correct |
942 ms |
17240 KB |
Output is correct |
35 |
Execution timed out |
1087 ms |
24592 KB |
Time limit exceeded |
36 |
Halted |
0 ms |
0 KB |
- |