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

if any(a>b for a,b in zip(A,B)):
    print(-1)
    exit()

def pb(i):
    return i-1 if i-1 >= 0 else N-1
def nx(i):
    return i+1 if i+1 < N else 0

hq = []
for i,(a,b) in enumerate(zip(A,B)):
    if a <= b - B[pb(i)] - B[nx(i)]:
        hq.append((-b,i))
heapq.heapify(hq)

ans = 0
while hq:
    _,i = heapq.heappop(hq)
    if B[i] == A[i]: continue
    l = B[pb(i)]
    r = B[nx(i)]
    if B[i]-l-r < A[i]: continue
    k = (B[i]-A[i]) // (l+r)
    ans += k
    B[i] -= k*(l+r)
    if A[i] <= B[i] - l - r:
        heapq.heappush(hq, (-B[i],i))
    ll = B[pb(pb(i))]
    rr = B[nx(nx(i))]
    if A[nx(i)] <= r - B[i] - rr:
        heapq.heappush(hq, (-r,nx(i)))
    if A[pb(i)] <= l - B[i] - ll:
        heapq.heappush(hq, (-l,pb(i)))

if all(a==b for a,b in zip(A,B)):
    print(ans)
else:
    print(-1)

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

if any(a>b for a,b in zip(A,B)):
    print(-1)
    exit()

def pb(i):
    return i-1 if i-1 >= 0 else N-1
def nx(i):
    return i+1 if i+1 < N else 0

hq = []
for i,(a,b) in enumerate(zip(A,B)):
    if a <= b - B[pb(i)] - B[nx(i)]:
        hq.append((-b,i))
heapq.heapify(hq)

ans = 0
while hq:
    _,i = heapq.heappop(hq)
    if B[i] == A[i]: continue
    l = B[pb(i)]
    r = B[nx(i)]
    if B[i]-l-r < A[i]: continue
    k = (B[i]-A[i]) // (l+r)
    ans += k
    B[i] -= k*(l+r)
    ll = B[pb(pb(i))]
    rr = B[nx(nx(i))]
    if A[nx(i)] <= r - B[i] - rr:
        heapq.heappush(hq, (-r,nx(i)))
    if A[pb(i)] <= l - B[i] - ll:
        heapq.heappush(hq, (-l,pb(i)))

if all(a==b for a,b in zip(A,B)):
    print(ans)
else:
    print(-1)

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

if any(a>b for a,b in zip(A,B)):
    print(-1)
    exit()

hq = []
for i,(a,b) in enumerate(zip(A,B)):
    if a <= b - B[(i-1)%N] - B[(i+1)%N]:
        hq.append((-b,i))
heapq.heapify(hq)

ans = 0
while hq:
    _,i = heapq.heappop(hq)
    a = A[i]
    if B[i] == a: continue
    l = B[(i-1)%N]
    r = B[(i+1)%N]
    if B[i]-l-r < a: continue
    k = (B[i]-a) // (l+r)
    ans += k
    B[i] -= k*(l+r)
    nb = B[i]
    if a <= nb - l - r:
        heapq.heappush(hq, (-nb,i))
    ll = B[(i-2)%N]
    rr = B[(i+2)%N]
    if A[(i+1)%N] <= r - nb - rr:
        heapq.heappush(hq, (-r,(i+1)%N))
    if A[(i-1)%N] <= l - nb - ll:
        heapq.heappush(hq, (-l,(i-1)%N))

if all(a==b for a,b in zip(A,B)):
    print(ans)
else:
    print(-1)

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

if any(a>b for a,b in zip(A,B)):
    print(-1)
    exit()

def pb(i):
    return i-1 if i-1 >= 0 else N-1
def nx(i):
    return i+1 if i+1 < N else 0

hq = []
for i,(a,b) in enumerate(zip(A,B)):
    if a <= b - B[pb(i)] - B[nx(i)]:
        hq.append((-b,i))
heapq.heapify(hq)

ans = 0
while hq:
    _,i = heapq.heappop(hq)
    if B[i] == A[i]: continue
    l = B[pb(i)]
    r = B[nx(i)]
    if B[i]-l-r < A[i]: continue
    k = (B[i]-A[i]) // (l+r)
    ans += k
    B[i] -= k*(l+r)
    ll = B[pb(pb(i))]
    rr = B[nx(nx(i))]
    if A[nx(i)] <= r - B[i] - rr:
        heapq.heappush(hq, (-r,nx(i)))
    if A[pb(i)] <= l - B[i] - ll:
        heapq.heappush(hq, (-l,pb(i)))

if all(a==b for a,b in zip(A,B)):
    print(ans)
else:
    print(-1)

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

if any(a>b for a,b in zip(A,B)):
    print(-1)
    exit()

hq = []
for i,(a,b) in enumerate(zip(A,B)):
    if a <= b - B[(i-1)%N] - B[(i+1)%N]:
        hq.append((-b,i))
heapq.heapify(hq)

ans = 0
while hq:
    _,i = heapq.heappop(hq)
    if B[i] == A[i]: continue
    l = B[(i-1)%N]
    r = B[(i+1)%N]
    if B[i]-l-r < A[i]: continue
    k = (B[i]-A[i]) // (l+r)
    ans += k
    B[i] -= k*(l+r)
    if A[i] <= B[i] - l - r:
        heapq.heappush(hq, (-B[i],i))
    ll = B[(i-2)%N]
    rr = B[(i+2)%N]
    if A[(i+1)%N] <= r - B[i] - rr:
        heapq.heappush(hq, (-r,(i+1)%N))
    if A[(i-1)%N] <= l - B[i] - ll:
        heapq.heappush(hq, (-l,(i-1)%N))

if all(a==b for a,b in zip(A,B)):
    print(ans)
else:
    print(-1)

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

if any(a>b for a,b in zip(A,B)):
    print(-1)
    exit()

def pb(i):
    return i-1 if i-1 >= 0 else N-1
def nx(i):
    return i+1 if i+1 < N else 0

hq = []
for i,(a,b) in enumerate(zip(A,B)):
    if a <= b - B[pb(i)] - B[nx(i)]:
        hq.append((-b,i))
heapq.heapify(hq)

ans = 0
while hq:
    _,i = heapq.heappop(hq)
    if B[i] == A[i]: continue
    l = B[pb(i)]
    r = B[nx(i)]
    if B[i]-l-r < A[i]: continue
    k = (B[i]-A[i]) // (l+r)
    ans += k
    B[i] -= k*(l+r)
    ll = B[pb(pb(i))]
    rr = B[nx(nx(i))]
    if A[nx(i)] <= r - B[i] - rr:
        heapq.heappush(hq, (-r,nx(i)))
    if A[pb(i)] <= l - B[i] - ll:
        heapq.heappush(hq, (-l,pb(i)))

if all(a==b for a,b in zip(A,B)):
    print(ans)
else:
    print(-1)

Buggy python submission (verdict: Runtime Error):
import heapq
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))

if any(a>b for a,b in zip(A,B)):
    print(-1)
    exit()

cnt = 0
for a,b in zip(A,B):
    if a != b:
        cnt += 1
if cnt == 0:
    print(0)
    exit()

hq = [(-a,i) for i,a in enumerate(B)]
heapq.heapify(hq)

ans = 0
while hq or cnt:
    _,i = heapq.heappop(hq)
    l = B[(i-1)%N]
    r = B[(i+1)%N]
    if B[i]-l-r < A[i]: continue
    k = (B[i]-A[i]) // (l+r)
    ans += k
    B[i] -= k*(l+r)
    if A[i] == B[i]:
        cnt -= 1
        continue
    heapq.heappush(hq, (-B[i],i))

if all(a==b for a,b in zip(A,B)):
    print(ans)
else:
    print(-1)

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

if any(a>b for a,b in zip(A,B)):
    print(-1)
    exit()

def pb(i):
    return i-1 if i-1 >= 0 else N-1
def nx(i):
    return i+1 if i+1 < N else 0

hq = []
for i,(a,b) in enumerate(zip(A,B)):
    if a <= b - B[pb(i)] - B[nx(i)]:
        hq.append((-b,i))
heapq.heapify(hq)

ans = 0
while hq:
    _,i = heapq.heappop(hq)
    if B[i] == A[i]: continue
    l = B[pb(i)]
    r = B[nx(i)]
    if B[i]-l-r < A[i]: continue
    k = (B[i]-A[i]) // (l+r)
    ans += k
    B[i] -= k*(l+r)
    ll = B[pb(pb(i))]
    rr = B[nx(nx(i))]
    if A[nx(i)] <= r - B[i] - rr:
        heapq.heappush(hq, (-r,nx(i)))
    if A[pb(i)] <= l - B[i] - ll:
        heapq.heappush(hq, (-l,pb(i)))

if all(a==b for a,b in zip(A,B)):
    print(ans)
else:
    print(-1)

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

if any(a>b for a,b in zip(A,B)):
    print(-1)
    exit()

hq = [(-a,i) for i,a in enumerate(B)]
heapq.heapify(hq)

ans = 0
while hq:
    _,i = heapq.heappop(hq)
    if B[i] == A[i]: continue
    l = B[(i-1)%N]
    r = B[(i+1)%N]
    if B[i]-l-r < A[i]: continue
    k = (B[i]-A[i]) // (l+r)
    ans += k
    B[i] -= k*(l+r)
    heapq.heappush(hq, (-B[i],i))

if all(a==b for a,b in zip(A,B)):
    print(ans)
else:
    print(-1)

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

if any(a>b for a,b in zip(A,B)):
    print(-1)
    exit()

def pb(i):
    return i-1 if i-1 >= 0 else N-1
def nx(i):
    return i+1 if i+1 < N else 0

hq = []
for i,(a,b) in enumerate(zip(A,B)):
    if a <= b - B[pb(i)] - B[nx(i)]:
        hq.append((-b,i))
heapq.heapify(hq)

ans = 0
while hq:
    _,i = heapq.heappop(hq)
    if B[i] == A[i]: continue
    l = B[pb(i)]
    r = B[nx(i)]
    if B[i]-l-r < A[i]: continue
    k = (B[i]-A[i]) // (l+r)
    ans += k
    B[i] -= k*(l+r)
    ll = B[pb(pb(i))]
    rr = B[nx(nx(i))]
    if A[nx(i)] <= r - B[i] - rr:
        heapq.heappush(hq, (-r,nx(i)))
    if A[pb(i)] <= l - B[i] - ll:
        heapq.heappush(hq, (-l,pb(i)))

if all(a==b for a,b in zip(A,B)):
    print(ans)
else:
    print(-1)

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

if any(a>b for a,b in zip(A,B)):
    print(-1)
    exit()

hq = [(-a,i) for i,a in enumerate(B)]
heapq.heapify(hq)

ans = 0
while hq:
    _,i = heapq.heappop(hq)
    if B[i] == A[i]: continue
    l = B[(i-1)%N]
    r = B[(i+1)%N]
    if B[i]-l-r < A[i]: continue
    k = (B[i]-A[i]) // (l+r)
    ans += k
    B[i] -= k*(l+r)
    heapq.heappush(hq, (-B[i],i))

if all(a==b for a,b in zip(A,B)):
    print(ans)
else:
    print(-1)

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

if any(a>b for a,b in zip(A,B)):
    print(-1)
    exit()

def pb(i):
    return i-1 if i-1 >= 0 else N-1
def nx(i):
    return i+1 if i+1 < N else 0

hq = []
for i,(a,b) in enumerate(zip(A,B)):
    if a <= b - B[pb(i)] - B[nx(i)]:
        hq.append((-b,i))
heapq.heapify(hq)

ans = 0
while hq:
    _,i = heapq.heappop(hq)
    if B[i] == A[i]: continue
    l = B[pb(i)]
    r = B[nx(i)]
    if B[i]-l-r < A[i]: continue
    k = (B[i]-A[i]) // (l+r)
    ans += k
    B[i] -= k*(l+r)
    ll = B[pb(pb(i))]
    rr = B[nx(nx(i))]
    if A[nx(i)] <= r - B[i] - rr:
        heapq.heappush(hq, (-r,nx(i)))
    if A[pb(i)] <= l - B[i] - ll:
        heapq.heappush(hq, (-l,pb(i)))

if all(a==b for a,b in zip(A,B)):
    print(ans)
else:
    print(-1)