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

N = int(input())
A = list(map(int, input().split()))
Temp = input().split()
B = []
for i in range(N):
    heapq.heappush(B, (-int(Temp[i]), i))
now = list(map(int, Temp))
complete = [False]*N
fin = [True]*N
ans = 0
while True:
    v, i = heapq.heappop(B)
    if A[i] == now[i]:
        complete[i] = True

    if not complete[i]:
        if i == 0:
            nv = v + now[-1] + now[1]
        elif i == N-1:
            nv = v + now[0] + now[-2]
        else:
            nv = v + now[i-1] + now[i+1]
        if nv <= -A[i]:
            heapq.heappush(B, (nv, i))
            now[i] = -nv
            ans += 1
    if not B:
        if complete == fin:
            break
        else:
            print(-1)
            exit()
print(ans)

Fixed submission:
import heapq

N = int(input())
A = list(map(int, input().split()))
B = input().split()
C = []
for i in range(N):
    heapq.heappush(C, (A[i] - int(B[i]), i))
B = list(map(int, B))
ans = 0
while True:
    v, i = heapq.heappop(C)
    if v == 0:
        break

    nv = B[i-1] + B[(i+1) % N]
    n = -v // nv
    if n <= 0:
        print(-1)
        exit()
    v += n * nv
    B[i] -= n * nv
    heapq.heappush(C, (v, i))
    ans += n
print(ans)

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

N = int(input())
A = list(map(int, input().split()))
Temp = input().split()
B = []
for i in range(N):
    heapq.heappush(B, (-int(Temp[i]), i))
now = list(map(int, Temp))
complete = [False]*N
fin = [True]*N
ans = 0
while True:
    v, i = heapq.heappop(B)
    if A[i] == now[i]:
        complete[i] = True

    if not complete[i]:
        if i == 0:
            nv = v + now[-1] + now[1]
        elif i == N-1:
            nv = v + now[0] + now[-2]
        else:
            nv = v + now[i-1] + now[i+1]
        if nv <= -A[i]:
            heapq.heappush(B, (nv, i))
            now[i] = -nv
            ans += 1
    if not B:
        if complete == fin:
            break
        else:
            print(-1)
            exit()
print(ans)

Fixed submission:
import heapq

N = int(input())
A = list(map(int, input().split()))
B = input().split()
C = []
for i in range(N):
    heapq.heappush(C, (A[i] - int(B[i]), i))
B = list(map(int, B))
ans = 0
while True:
    v, i = heapq.heappop(C)
    if v == 0:
        break

    nv = B[i-1] + B[(i+1) % N]
    n = -v // nv
    if n <= 0:
        print(-1)
        exit()
    v += n * nv
    B[i] -= n * nv
    heapq.heappush(C, (v, i))
    ans += n
print(ans)

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

N = int(input())
A = list(map(int, input().split()))
Temp = input().split()
B = []
for i in range(N):
    heapq.heappush(B, (-int(Temp[i]), i))
now = list(map(int, Temp))
complete = [False]*N
fin = [True]*N
ans = 0
while True:
    v, i = heapq.heappop(B)
    if A[i] == now[i]:
        complete[i] = True

    if not complete[i]:
        if i == 0:
            nv = v + now[-1] + now[1]
        elif i == N-1:
            nv = v + now[0] + now[-2]
        else:
            nv = v + now[i-1] + now[i+1]
        if nv <= -A[i]:
            heapq.heappush(B, (nv, i))
            now[i] = -nv
            ans += 1
    else:
        if complete == fin:
            break
    if not B:
        print(-1)
        exit()
print(ans)

Fixed submission:
import heapq

N = int(input())
A = list(map(int, input().split()))
B = input().split()
C = []
for i in range(N):
    heapq.heappush(C, (A[i] - int(B[i]), i))
B = list(map(int, B))
ans = 0
while True:
    v, i = heapq.heappop(C)
    if v == 0:
        break

    nv = B[i-1] + B[(i+1) % N]
    n = -v // nv
    if n <= 0:
        print(-1)
        exit()
    v += n * nv
    B[i] -= n * nv
    heapq.heappush(C, (v, i))
    ans += n
print(ans)