This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
n = int(input())
a = [0] * n
b = [0] * n
for i in range(n):
x, y = map(int, input().split())
a[i] = x
b[i] = y
# subtask 1 only
ans = 0
extra = 0
suffix = []
for i in range(n):
ans += abs(extra)
extra += a[i] - b[i]
suffix.append(-1 if extra > 0 else 1)
if sum(a) == sum(b) + 1:
# subtask 2
current = ans
suffix.pop()
total = 0
while len(suffix) > 0:
total += suffix.pop()
ans = min(ans, current + total)
print(ans)
# | 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... |