This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
import sys
import heapq as hq
input = sys.stdin.readline
n=int(input())
p=0
s=[]
z=0
for i in range(n):
a,b=list(map(int,input().split()))
p+=b-a
s.append(p)
w=[]
for c in s:
if c>0:
z+=c
c=0
elif c<p:
z-=c-p
c=p
hq.heappush(w,c)
if w[0]<c:
hq.heappush(w,c)
z+=abs(hq.heappop(w)-c)
print(z)
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |