Buggy python submission (verdict: Time Limit Exceeded):
import sys
import heapq

N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
Q = []
for i in range(N):
    heapq.heappush(Q, (-B[i], i))
ans = 0
cnt = 0

while len(Q) != 0:
    _, i = heapq.heappop(Q)

    if (B[i] - A[i]) % (B[(i-1) % N] + B[(i+1) % N]) == 0:
        ans += (B[i] - A[i]) // (B[(i-1) % N] + B[(i+1) % N])
        cnt += 1
        B[i] = A[i]
    else:
        tmp = (B[i] - 1) // (B[(i-1) % N] + B[(i+1) % N])
        B[i] -= tmp * (B[(i-1) % N] + B[(i+1) % N])
        ans += tmp
        if B[i] < A[i]:
            print(-1)
            sys.exit()
        heapq.heappush(Q, (-B[i], i))
    
    if cnt == N:
        print(ans)
        sys.exit()

print(-1)

Fixed submission:
import sys
import heapq

N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
Q = []
for i in range(N):
    heapq.heappush(Q, (-B[i], i))
ans = 0
cnt = 0

while len(Q) != 0:
    _, i = heapq.heappop(Q)

    if (B[i] - A[i]) % (B[(i-1) % N] + B[(i+1) % N]) == 0:
        ans += (B[i] - A[i]) // (B[(i-1) % N] + B[(i+1) % N])
        cnt += 1
        B[i] = A[i]
    else:
        tmp = (B[i] - 1) // (B[(i-1) % N] + B[(i+1) % N])
        
        if tmp == 0:
            print(-1)
            sys.exit()
        
        B[i] -= tmp * (B[(i-1) % N] + B[(i+1) % N])
        ans += tmp
        heapq.heappush(Q, (-B[i], i))
    
    if cnt == N:
        print(ans)
        sys.exit()

print(-1)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import heapq

N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
Q = []
for i in range(N):
    heapq.heappush(Q, (-B[i], i))
ans = 0
cnt = 0

while len(Q) != 0:
    _, i = heapq.heappop(Q)

    if (B[i] - A[i]) % (B[(i-1) % N] + B[(i+1) % N]) == 0:
        ans += (B[i] - A[i]) // (B[(i-1) % N] + B[(i+1) % N])
        cnt += 1
        B[i] = A[i]
    else:
        tmp = (B[i] - 1) // (B[(i-1) % N] + B[(i+1) % N])
        B[i] -= tmp * (B[(i-1) % N] + B[(i+1) % N])
        ans += tmp
        heapq.heappush(Q, (-B[i], i))
    
    if cnt == N:
        print(ans)
        sys.exit()

print(-1)

Fixed submission:
import sys
import heapq

N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
Q = []
for i in range(N):
    heapq.heappush(Q, (-B[i], i))
ans = 0
cnt = 0

while len(Q) != 0:
    _, i = heapq.heappop(Q)

    if (B[i] - A[i]) % (B[(i-1) % N] + B[(i+1) % N]) == 0:
        ans += (B[i] - A[i]) // (B[(i-1) % N] + B[(i+1) % N])
        cnt += 1
        B[i] = A[i]
    else:
        tmp = (B[i] - 1) // (B[(i-1) % N] + B[(i+1) % N])
        
        if tmp == 0:
            print(-1)
            sys.exit()
        
        B[i] -= tmp * (B[(i-1) % N] + B[(i+1) % N])
        ans += tmp
        heapq.heappush(Q, (-B[i], i))
    
    if cnt == N:
        print(ans)
        sys.exit()

print(-1)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import heapq

N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
Q = []
for i in range(N):
    heapq.heappush(Q, (-B[i], i))
ans = 0
cnt = 0

while len(Q) != 0:
    _, i = heapq.heappop(Q)

    if (B[i] - A[i]) % (B[(i-1) % N] + B[(i+1) % N]) == 0:
        ans += (B[i] - A[i]) // (B[(i-1) % N] + B[(i+1) % N])
        cnt += 1
        B[i] = A[i]
    else:
        tmp = (B[i] - 1) // (B[(i-1) % N] + B[(i+1) % N])
        B[i] -= tmp * (B[(i-1) % N] + B[(i+1) % N])
        ans += tmp
        heapq.heappush(Q, (-B[i], i))
    
    if cnt == N:
        print(ans)
        sys.exit()

print(-1)

Fixed submission:
import sys
import heapq

N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
Q = []
for i in range(N):
    heapq.heappush(Q, (-B[i], i))
ans = 0
cnt = 0

while len(Q) != 0:
    _, i = heapq.heappop(Q)

    if (B[i] - A[i]) % (B[(i-1) % N] + B[(i+1) % N]) == 0:
        ans += (B[i] - A[i]) // (B[(i-1) % N] + B[(i+1) % N])
        cnt += 1
        B[i] = A[i]
    else:
        tmp = (B[i] - 1) // (B[(i-1) % N] + B[(i+1) % N])
        
        if tmp == 0:
            print(-1)
            sys.exit()
        
        B[i] -= tmp * (B[(i-1) % N] + B[(i+1) % N])
        ans += tmp
        heapq.heappush(Q, (-B[i], i))
    
    if cnt == N:
        print(ans)
        sys.exit()

print(-1)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import heapq

N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
Q = []
for i in range(N):
    heapq.heappush(Q, (-B[i], i))
cnt = 0

while len(Q) != 0:
    _, i = heapq.heappop(Q)

    if (B[i] - A[i]) % (B[(i-1) % N] + B[(i+1) % N]) == 0:
        cnt += (B[i] - A[i]) // (B[(i-1) % N] + B[(i+1) % N])
        B[i] = A[i]
    else:
        tmp = (B[i] - 1) // (B[(i-1) % N] + B[(i+1) % N])
        B[i] -= tmp * (B[(i-1) % N] + B[(i+1) % N])
        cnt += tmp
        heapq.heappush(Q, (-B[i], i))
    
    if A == B:
        print(cnt)
        sys.exit()

print(-1)

Fixed submission:
import sys
import heapq

N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
Q = []
for i in range(N):
    heapq.heappush(Q, (-B[i], i))
ans = 0
cnt = 0

while len(Q) != 0:
    _, i = heapq.heappop(Q)

    if (B[i] - A[i]) % (B[(i-1) % N] + B[(i+1) % N]) == 0:
        ans += (B[i] - A[i]) // (B[(i-1) % N] + B[(i+1) % N])
        cnt += 1
        B[i] = A[i]
    else:
        tmp = (B[i] - 1) // (B[(i-1) % N] + B[(i+1) % N])
        
        if tmp == 0:
            print(-1)
            sys.exit()
        
        B[i] -= tmp * (B[(i-1) % N] + B[(i+1) % N])
        ans += tmp
        heapq.heappush(Q, (-B[i], i))
    
    if cnt == N:
        print(ans)
        sys.exit()

print(-1)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import heapq

N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
Q = []
for i in range(N):
    heapq.heappush(Q, (-B[i], i))
cnt = 0

while len(Q) != 0:
    _, i = heapq.heappop(Q)

    if (B[i] - A[i]) % (B[(i-1) % N] + B[(i+1) % N]) == 0:
        cnt += (B[i] - A[i]) // (B[(i-1) % N] + B[(i+1) % N])
        B[i] = A[i]
    else:
        tmp = (B[i] - 1) // (B[(i-1) % N] + B[(i+1) % N])
        B[i] -= tmp * (B[(i-1) % N] + B[(i+1) % N])
        cnt += tmp
        heapq.heappush(Q, (-B[i], i))
    
    if A == B:
        print(cnt)
        sys.exit()

print(-1)

Fixed submission:
import sys
import heapq

N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
Q = []
for i in range(N):
    heapq.heappush(Q, (-B[i], i))
ans = 0
cnt = 0

while len(Q) != 0:
    _, i = heapq.heappop(Q)

    if (B[i] - A[i]) % (B[(i-1) % N] + B[(i+1) % N]) == 0:
        ans += (B[i] - A[i]) // (B[(i-1) % N] + B[(i+1) % N])
        cnt += 1
        B[i] = A[i]
    else:
        tmp = (B[i] - 1) // (B[(i-1) % N] + B[(i+1) % N])
        
        if tmp == 0:
            print(-1)
            sys.exit()
        
        B[i] -= tmp * (B[(i-1) % N] + B[(i+1) % N])
        ans += tmp
        heapq.heappush(Q, (-B[i], i))
    
    if cnt == N:
        print(ans)
        sys.exit()

print(-1)