Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))


ans = 0
f = True
while f:
    f = False
    for i in range(N):
        a,b,c = B[(i-1)%N],B[i],B[(i+1)%N]
        if b > a+c and b > A[i]:
            ans += (b-A[i])//(a+c)
            B[i] = A[i]//(a+c)*(a+c)+B[i]%(a+c) 
            f = True

if A != B:
    ans = -1
print(ans)

Fixed submission:
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))


ans = 0
f = True
while f:
    f = False
    for i in range(N):
        a,b,c = B[(i-1)%N],B[i],B[(i+1)%N]
        if b > a+c and b > A[i]:
            if (b-A[i])%(a+c)==0:
                B[i] = A[i]
                ans += (b-A[i])//(a+c)
            else:
                ans += B[i]//(a+c)
                B[i] %= a+c
            f = True

if A != B:
    ans = -1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import heapq
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
h = [[-B[i],i] for i in range(N) if B[i] != A[i]]
heapq.heapify(h)

ans = 0
while h:
    b,i = heapq.heappop(h)
    b *= -1
    a = B[(i-1)%N]
    c = B[(i+1)%N]
    if a+c > b or b != B[i]:
        continue
    if b < A[i]:
        ans = -1
        break
    if (b-A[i])%(a+c)==0:
        ans += (b-A[i])//(a+c)
        B[i] = A[i]
    else:
        ans += b//(a+c)
        b %= a+c
        B[i] = b
        heapq.heappush(h,[-b,i])
        if a != A[(i-1)%N]:
            heapq.heappush(h,[-a,(i-1)%N])
        if c != A[(i+1)%N]:
            heapq.heappush(h,[-c,(i+1)%N])
if A != B:
    ans = -1
print(ans)

Fixed submission:
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))


ans = 0
f = True
while f:
    f = False
    for i in range(N):
        a,b,c = B[(i-1)%N],B[i],B[(i+1)%N]
        if b > a+c and b > A[i]:
            if (b-A[i])%(a+c)==0:
                B[i] = A[i]
                ans += (b-A[i])//(a+c)
            else:
                ans += B[i]//(a+c)
                B[i] %= a+c
            f = True

if A != B:
    ans = -1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import heapq
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
h = [[-B[i],i] for i in range(N) if B[i] != A[i]]
heapq.heapify(h)

ans = 0
while h:
    b,i = heapq.heappop(h)
    b *= -1
    a = B[(i-1)%N]
    c = B[(i+1)%N]
    if a+c > b or b != B[i]:
        continue
    if b < A[i]:
        ans = -1
        break
    if (b-A[i])%(a+c)==0:
        ans += (b-A[i])//(a+c)
        B[i] = A[i]
    else:
        ans += b//(a+c)
        b %= a+c
        B[i] = b
        heapq.heappush(h,[-b,i])
        if a != A[(i-1)%N]:
            heapq.heappush(h,[-a,(i-1)%N])
        if c != A[(i+1)%N]:
            heapq.heappush(h,[-c,(i+1)%N])
if A != B:
    ans = -1
print(ans)

Fixed submission:
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))


ans = 0
f = True
while f:
    f = False
    for i in range(N):
        a,b,c = B[(i-1)%N],B[i],B[(i+1)%N]
        if b > a+c and b > A[i]:
            if (b-A[i])%(a+c)==0:
                B[i] = A[i]
                ans += (b-A[i])//(a+c)
            else:
                ans += B[i]//(a+c)
                B[i] %= a+c
            f = True

if A != B:
    ans = -1
print(ans)