Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

i = 0
ans = 0
mul = 0
while i < n:
    if b[i]-b[(i-1)%n]-b[(i+1)%n] >= a[i]:
        mul = (b[i]-a[i])//(b[(i-1)%n]+b[(i+1)%n])
        b[i] =  b[i]-(b[(i-1)%n]+b[(i+1)%n])*mul
        ans += mul
        i = 0
    else:
        i += 1
if a==b:
    print(ans)
else:
    print(-1)

Fixed submission:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
 
i = 0
ans = 0
chg = 1
mul = 0
while chg == 1:
    chg = 0
    for i in range(n):
        if b[i]-b[i-1]-b[(i+1)%n] >= a[i]:
            mul = (b[i]-a[i])//(b[i-1]+b[(i+1)%n])
            b[i] -= (b[i-1]+b[(i+1)%n])*mul
            ans += mul
            chg = 1
print(ans if a==b else -1)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

i = 0
ans = 0
while i < n:
    if b[i]-b[(i-1)%n]-b[(i+1)%n] >= a[i]:
        while b[i]-b[(i-1)%n]-b[(i+1)%n] >= a[i]:
            b[i] =  b[i]-b[(i-1)%n]-b[(i+1)%n]
            ans += 1
        i = 0
    else:
        i += 1
if a==b:
    print(ans)
else:
    print(-1)

Fixed submission:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
 
i = 0
ans = 0
chg = 1
mul = 0
while chg == 1:
    chg = 0
    for i in range(n):
        if b[i]-b[i-1]-b[(i+1)%n] >= a[i]:
            mul = (b[i]-a[i])//(b[i-1]+b[(i+1)%n])
            b[i] -= (b[i-1]+b[(i+1)%n])*mul
            ans += mul
            chg = 1
print(ans if a==b else -1)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

i = 0
ans = 0
while i < n:
    if b[i]-b[(i-1)%n]-b[(i+1)%n] >= a[i]:
        b[i] =  b[i]-b[(i-1)%n]-b[(i+1)%n]
        ans += 1
        i = 0
    else:
        i += 1
if a==b:
    print(ans)
else:
    print(-1)

Fixed submission:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
 
i = 0
ans = 0
chg = 1
mul = 0
while chg == 1:
    chg = 0
    for i in range(n):
        if b[i]-b[i-1]-b[(i+1)%n] >= a[i]:
            mul = (b[i]-a[i])//(b[i-1]+b[(i+1)%n])
            b[i] -= (b[i-1]+b[(i+1)%n])*mul
            ans += mul
            chg = 1
print(ans if a==b else -1)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

i = 0
ans = 0
while i < n:
    if b[i]-b[(i-1)%n]-b[(i+1)%n] >= a[i]:
        b[i] =  b[i]-b[(i-1)%n]-b[(i+1)%n]
        ans += 1
        i = 0
    else:
        i += 1
if a==b:
    print(ans)
else:
    print(-1)

Fixed submission:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
 
i = 0
ans = 0
chg = 1
mul = 0
while chg == 1:
    chg = 0
    for i in range(n):
        if b[i]-b[i-1]-b[(i+1)%n] >= a[i]:
            mul = (b[i]-a[i])//(b[i-1]+b[(i+1)%n])
            b[i] -= (b[i-1]+b[(i+1)%n])*mul
            ans += mul
            chg = 1
print(ans if a==b else -1)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

i = 0
ans = 0
while i < n:
    if b[i]-b[(i-1)%n]-b[(i+1)%n] >= a[i]:
        b[i] =  b[i]-b[(i-1)%n]-b[(i+1)%n]
        ans += 1
        if a[i] == b[i] and a[(i+1)%n] == b[(i+1)%n] and a[(i-1)%n] == b[(i-1)%n]:
            if a == b:
                print(ans)
                exit()
        i = 0
    else:
        i += 1
print(-1)

Fixed submission:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
 
i = 0
ans = 0
chg = 1
mul = 0
while chg == 1:
    chg = 0
    for i in range(n):
        if b[i]-b[i-1]-b[(i+1)%n] >= a[i]:
            mul = (b[i]-a[i])//(b[i-1]+b[(i+1)%n])
            b[i] -= (b[i-1]+b[(i+1)%n])*mul
            ans += mul
            chg = 1
print(ans if a==b else -1)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

i = 0
ans = 0
while i < n:
    if b[i]-b[(i-1)%n]-b[(i+1)%n] >= a[i]:
        b[i] =  b[i]-b[(i-1)%n]-b[(i+1)%n]
        ans += 1
        if a == b:
            print(ans)
            exit()
        i = 0
    else:
        i += 1
print(-1)

Fixed submission:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
 
i = 0
ans = 0
chg = 1
mul = 0
while chg == 1:
    chg = 0
    for i in range(n):
        if b[i]-b[i-1]-b[(i+1)%n] >= a[i]:
            mul = (b[i]-a[i])//(b[i-1]+b[(i+1)%n])
            b[i] -= (b[i-1]+b[(i+1)%n])*mul
            ans += mul
            chg = 1
print(ans if a==b else -1)