Source Code
n = int(input())
a = list(map(int, input().strip().split()))
b = list(map(int, input().strip().split()))

if sum(a) > sum(b):
    print(-1)
    exit()

h = 0
ans = -1
lowest = n + 1
for i in range(n):
    ans += 1
    h += b[i]
    shots = min(h, a[i])
    h -= shots
    a[i] -= shots
    if a[i] != 0:
        lowest = min(lowest, i)
    ans += shots

if sum(a) != 0:
    for i in range(n - 2, lowest - 1, -1):
        ans += 1
        shots = min(h, a[i])
        h -= shots
        a[i] -= shots
        ans += shots

print(ans)
Copy
Shooting Balloons m-salti
Python 3
389 ms
49.9 MB
Wrong Answer