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()

s = sum(a)

h = 0
ans = sum(a) + n - 1

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


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

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