This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
from sys import stdin
bestScore = 0
N = int(input())
arts = []
INF = 10**18
for _ in range(N):
a, b = map(int, stdin.readline().split())
arts.append((a, b))
arts.sort(key=lambda x: x[0])
A, B = [], []
for a, b in arts:
A.append(a)
B.append(b)
prefs = [0]
for i in range(N):
prefs.append(prefs[-1] + B[i])
prefs_orig = prefs.copy()
for i in range(N):
prefs[i+1] -= A[i]
suffix_maxs = [-INF]*(N+1)
suffix_maxs[-1] = prefs[-1]
for i in range(N-1, -1, -1):
suffix_maxs[i] = max(suffix_maxs[i+1], prefs[i])
mx = max(B)
for st in range(N):
bestmx = suffix_maxs[st+1]
mx = max(mx, bestmx-prefs_orig[st]+A[st])
print(mx)
# | 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... |