Submission #1091517

#TimeUsernameProblemLanguageResultExecution timeMemory
1091517vjudge1Potatoes and fertilizers (LMIO19_bulves)Cpython 3
24 / 100
1038 ms53296 KiB
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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...