Buggy python submission (verdict: Time Limit Exceeded):
from heapq import heappush,heappop
import sys
input=sys.stdin.readline

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

count=0
while lst:
    h,i=heappop(lst)
    h=-h
    x,z=B[i-1],B[(i+1)%N]
    if (h-A[i])%(x+z)==0:
        num=(h-A[i])//(x+z)
        count+=num
        B[i]=A[i]
        continue
    else:
        num=h//(x+z)
        h=h%(x+z)
        count+=num
        if num==0:
            print(-1)
            sys.exit()
        B[i]=h    
        if A[i]>h:
            print(-1)
            sys.exit()
        heappush(lst,[-h,i])

if A==B:
    print(count)
else:
    print(-1)

Fixed submission:
from heapq import heappush,heappop
import sys
input=sys.stdin.readline

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

count=0
while lst:
    h,i=heappop(lst)
    h=-h
    x,z=B[i-1],B[(i+1)%N]
    kkk=x+z
    if (h-A[i])%kkk==0:
        count+=(h-A[i])//kkk
        B[i]=A[i]
        continue
    elif kkk>=h:
        print(-1)
        sys.exit()
    else:
        count+=h//kkk
        h=h%kkk
        B[i]=h    
        if A[i]>h:
            print(-1)
            sys.exit()
        heappush(lst,(-h,i))

if A==B:
    print(count)
else:
    print(-1)

Buggy python submission (verdict: Time Limit Exceeded):
from heapq import heappush,heappop
import sys

N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
A=[A[-1]]+A+[A[0]]
B=[B[-1]]+B+[B[0]]
lst=[]
for i in range(1,N+1):
    heappush(lst,[-B[i],i])

count=0
while lst:
    h,i=heappop(lst)
    h=-h
    if A[i]>=h:
        continue
    h-=B[i-1]+B[i+1]
    count+=1
    B[i]=h
    if i==1:
        B[N+1]=h
    if i==N:
        B[0]=h
    if A[i]<h:
        heappush(lst,[-h,i])
    if h<=0:
        print(-1)
        sys.exit()

for i in range(1,N+1):
    if A[i]!=B[i]:
        print(-1)
        break
else:
    print(count)

Fixed submission:
from heapq import heappush,heappop
import sys
input=sys.stdin.readline

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

count=0
while lst:
    h,i=heappop(lst)
    h=-h
    x,z=B[i-1],B[(i+1)%N]
    kkk=x+z
    if (h-A[i])%kkk==0:
        count+=(h-A[i])//kkk
        B[i]=A[i]
        continue
    elif kkk>=h:
        print(-1)
        sys.exit()
    else:
        count+=h//kkk
        h=h%kkk
        B[i]=h    
        if A[i]>h:
            print(-1)
            sys.exit()
        heappush(lst,(-h,i))

if A==B:
    print(count)
else:
    print(-1)

Buggy python submission (verdict: Time Limit Exceeded):
from heapq import heappop,heappush
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
lst=[]
for i in range(n):
    if a[i]==b[i]:
        continue
    heappush(lst,[-b[i],i])
count=0
for _ in range(10**9):
    if not lst:
        print(count)
        break
    c=heappop(lst)
    num, j=c
    i,k=(j-1)%n,(j+1)%n
    num*=(-1)
    if (num-a[j])%(b[i]+b[k])==0:
        count+=(num-a[j])//(b[i]+b[k])
        b[j]=a[j]
        continue
    if num//(b[i]+b[k])==0:
        print(-1)
        break
    count+=num//(b[i]+b[k])
    num%=(b[i]+b[k])
    if num>a[j]:
        heappush(lst,[-num, j])
        b[j]=num
    elif num==a[j]:
        b[j]=num
        continue
    else:
        print(-1)
        break

Fixed submission:
from heapq import heappush,heappop
import sys
input=sys.stdin.readline

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

count=0
while lst:
    h,i=heappop(lst)
    h=-h
    x,z=B[i-1],B[(i+1)%N]
    kkk=x+z
    if (h-A[i])%kkk==0:
        count+=(h-A[i])//kkk
        B[i]=A[i]
        continue
    elif kkk>=h:
        print(-1)
        sys.exit()
    else:
        count+=h//kkk
        h=h%kkk
        B[i]=h    
        if A[i]>h:
            print(-1)
            sys.exit()
        heappush(lst,(-h,i))

if A==B:
    print(count)
else:
    print(-1)

Buggy python submission (verdict: Time Limit Exceeded):
from heapq import heappop,heappush
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
lst=[]
for i in range(n):
    heappush(lst,[-b[i],i])
count=0
while lst:
    c=heappop(lst)
    num, j=c
    if -num==a[j]:
        continue
    if j==0:
        i=n-1
        k=1
    elif j==n-1:
        i=n-2
        k=0
    else:
        i=j-1
        k=j+1
    num*=(-1)
    if (num-a[j])%(b[i]+b[k])==0:
        count+=(num-a[j])//(b[i]+b[k])
        b[j]=a[j]
        continue
    if num//(b[i]+b[k])==0:
        print(-1)
        break
    count+=num//(b[i]+b[k])
    num%=(b[i]+b[k])
    if num>a[j]:
        heappush(lst,[-num, j])
        b[j]=num
    elif num==a[j]:
        b[j]=num
        continue
    else:
        print(-1)
        break
else:
    print(count)

Fixed submission:
from heapq import heappush,heappop
import sys
input=sys.stdin.readline

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

count=0
while lst:
    h,i=heappop(lst)
    h=-h
    x,z=B[i-1],B[(i+1)%N]
    kkk=x+z
    if (h-A[i])%kkk==0:
        count+=(h-A[i])//kkk
        B[i]=A[i]
        continue
    elif kkk>=h:
        print(-1)
        sys.exit()
    else:
        count+=h//kkk
        h=h%kkk
        B[i]=h    
        if A[i]>h:
            print(-1)
            sys.exit()
        heappush(lst,(-h,i))

if A==B:
    print(count)
else:
    print(-1)

Buggy python submission (verdict: Time Limit Exceeded):
from heapq import heappop,heappush
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
lst=[]
for i in range(n):
    heappush(lst,[-b[i],i])
count=0
while lst:
    c=heappop(lst)
    num, j=c
    if -num==a[j]:
        continue
    if j==0:
        i=n-1
        k=1
    elif j==n-1:
        i=n-2
        k=0
    else:
        i=j-1
        k=j+1
    num*=(-1)
    if (num-a[j])%(b[i]+b[k])==0:
        count+=(num-a[j])//(b[i]+b[k])
        b[j]=a[j]
        continue
    if num//(b[i]+b[k])==0:
        print(-1)
        break
    count+=num//(b[i]+b[k])
    num%=(b[i]+b[k])
    if num>a[j]:
        heappush(lst,[-num, j])
        b[j]=num
    elif num==a[j]:
        b[j]=num
        continue
    else:
        print(-1)
        break
else:
    print(count)

Fixed submission:
from heapq import heappush,heappop
import sys
input=sys.stdin.readline

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

count=0
while lst:
    h,i=heappop(lst)
    h=-h
    x,z=B[i-1],B[(i+1)%N]
    kkk=x+z
    if (h-A[i])%kkk==0:
        count+=(h-A[i])//kkk
        B[i]=A[i]
        continue
    elif kkk>=h:
        print(-1)
        sys.exit()
    else:
        count+=h//kkk
        h=h%kkk
        B[i]=h    
        if A[i]>h:
            print(-1)
            sys.exit()
        heappush(lst,(-h,i))

if A==B:
    print(count)
else:
    print(-1)

Buggy python submission (verdict: Time Limit Exceeded):
from heapq import heappop,heappush
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
lst=[]
for i in range(n):
    heappush(lst,[-b[i],i])
count=0
while lst:
    c=heappop(lst)
    num, j=c
    if -num==a[j]:
        continue
    count+=1
    if j==0:
        i=n-1
        k=1
    elif j==n-1:
        i=n-2
        k=0
    else:
        i=j-1
        k=j+1
    num*=(-1)
    num-=(b[i]+b[k])
    if num>a[j]:
        heappush(lst,[-num, j])
        b[j]=num
    elif num==a[j]:
        b[j]=num
        continue
    else:
        print(-1)
        break
else:
    print(count)

Fixed submission:
from heapq import heappush,heappop
import sys
input=sys.stdin.readline

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

count=0
while lst:
    h,i=heappop(lst)
    h=-h
    x,z=B[i-1],B[(i+1)%N]
    kkk=x+z
    if (h-A[i])%kkk==0:
        count+=(h-A[i])//kkk
        B[i]=A[i]
        continue
    elif kkk>=h:
        print(-1)
        sys.exit()
    else:
        count+=h//kkk
        h=h%kkk
        B[i]=h    
        if A[i]>h:
            print(-1)
            sys.exit()
        heappush(lst,(-h,i))

if A==B:
    print(count)
else:
    print(-1)