Buggy python submission (verdict: Time Limit Exceeded):
import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__

def unable():
    print(-1)
    exit()

n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

from heapq import heappush, heappop
h = []
for i in range(n):
    if a[i]>b[i]:
        unable()
    else:
        heappush(h, (-b[i], i))

ans = 0
while h:
    _, i = heappop(h)
    cnt, mod = divmod(b[i]-a[i],b[i-1]+b[(i+1)%n])
    if cnt == 0:
        if mod != 0:
            unable()
    else:
        ans += cnt
        b[i] = a[i]+mod
        heappush(h, (-b[i], i))
print(ans)

Fixed submission:
import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__

def unable():
    print(-1)
    exit()

n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

from heapq import heappush, heappop
h = []
base = 10**6
for i in range(n):
    if a[i]>b[i]:
        unable()
    else:
        heappush(h, -(b[i]*base+i))

ans = 0
while h:
    k = heappop(h)
    i = -k%base
    cnt, mod = divmod(b[i]-a[i],b[i-1]+b[(i+1)%n])
    if cnt == 0:
        if mod != 0:
            unable()
    else:
        ans += cnt
        b[i] = a[i]+mod
        heappush(h, -(b[i]*base+i))
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__

def unable():
    print(-1)
    exit()

n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

from heapq import heappop, heappush
done = set()
heap = []
for i in range(n):
    if a[i] > b[i]:
        unable()
    elif a[i] == b[i]:
        done.add(i)
    elif b[i] >= b[i-1]+b[(i+1)%n]:
        heappush(heap, (-b[i],i))

ans = 0
while heap and len(done) != n:
    _, cur = heappop(heap)
    prv = (cur-1)%n
    nxt = (cur+1)%n
    base = b[prv]+b[nxt]
    cnt1, mod = divmod(b[cur], base)
    if cnt1 == 0:
        if b[cur] == a[cur]:
            done.add(cur)
        continue
    if mod > a[cur]:
        ans += cnt1
        b[cur] = mod
    else:
        cnt2, mod = divmod(b[cur]-a[cur], base)
        if mod != 0:
            unable()
        ans += cnt2
        done.add(cur)
        b[cur] = a[cur]
    if prv not in done:
        heappush(heap, (-b[prv], prv))
    if nxt not in done:
        heappush(heap, (-b[nxt], nxt))

if len(done)==n:
    print(ans)
else:
    unable()


Fixed submission:
import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__

def unable():
    print(-1)
    exit()

n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

from heapq import heappush, heappop
h = []
base = 10**6
for i in range(n):
    if a[i]>b[i]:
        unable()
    else:
        heappush(h, -(b[i]*base+i))

ans = 0
while h:
    k = heappop(h)
    i = -k%base
    cnt, mod = divmod(b[i]-a[i],b[i-1]+b[(i+1)%n])
    if cnt == 0:
        if mod != 0:
            unable()
    else:
        ans += cnt
        b[i] = a[i]+mod
        heappush(h, -(b[i]*base+i))
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
def unable():
    print(-1)
    exit()

n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

if any(ai>bi for ai, bi in zip(a,b)):
    unable()

from heapq import heappop, heappush
heap = []
for i, bi in enumerate(b):
    heappush(heap, (-bi, i))

ans = 0
done = set()
while heap and len(done) != n:
    _, cur = heappop(heap)
    prv = (cur-1)%n
    nxt = (cur+1)%n
    base = b[prv]+b[nxt]
    cnt1, mod = divmod(b[cur], base)
    if cnt1 == 0:
        if b[cur] == a[cur]:
            done.add(cur)
        continue
    if mod > a[cur]:
        ans += cnt1
        b[cur] = mod
    else:
        cnt2, mod = divmod(b[cur]-a[cur], base)
        if mod != 0:
            unable()
        ans += cnt2
        done.add(cur)
        b[cur] = a[cur]
    heappush(heap, (-b[prv], prv))
    heappush(heap, (-b[nxt], nxt))

if len(done)==n:
    print(ans)
else:
    unable()


Fixed submission:
import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__

def unable():
    print(-1)
    exit()

n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

from heapq import heappush, heappop
h = []
base = 10**6
for i in range(n):
    if a[i]>b[i]:
        unable()
    else:
        heappush(h, -(b[i]*base+i))

ans = 0
while h:
    k = heappop(h)
    i = -k%base
    cnt, mod = divmod(b[i]-a[i],b[i-1]+b[(i+1)%n])
    if cnt == 0:
        if mod != 0:
            unable()
    else:
        ans += cnt
        b[i] = a[i]+mod
        heappush(h, -(b[i]*base+i))
print(ans)