Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]
from heapq import heappop, heappush
def main():
  push = lambda x,i: heappush(BB,(-x,i))
  BB=[]

  for i,b in enumerate(B):
    push(b,i)

  ans=0
  while BB:
    b,ind=heappop(BB)
    b,ai=-b,A[ind]
    if b<ai:
      ans=-1
      break
    d=B[(ind-1)%n]+B[(ind+1)%n]
    dd=(b-ai)//d
    if dd==0:
      if b==ai:
        continue
      ans=-1
      break
    ans+=dd
    B[ind]-=d*dd
    push(b-d*dd,ind)
  
  print(ans)
main()

Fixed submission:
n=int(input())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]

from heapq import heappop, heappush
push = lambda x,i: heappush(BB,(-x,i))
BB=[]

for i,b in enumerate(B):
  push(b,i)

ans=0
while BB:
  b,ind=heappop(BB)
  b,ai=-b,A[ind]
  if b<ai:
    ans=-1
    break
  d=B[(ind-1)%n]+B[(ind+1)%n]
  dd=(b-ai)//d
  if dd==0:
    if b==ai:
      continue
    ans=-1
    break
  ans+=dd
  B[ind]-=d*dd
  push(b-d*dd,ind)
  
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]
from heapq import heappop, heappush
def main():
  push = lambda x,i: heappush(BB,(-x,i))
  BB=[]

  for i,b in enumerate(B):
    push(b,i)

  ans=0
  while BB:
    b,ind=heappop(BB)
    b,ai=-b,A[ind]
    if b<ai:
      ans=-1
      break
    d=B[(ind-1)%n]+B[(ind+1)%n]
    dd=(b-ai)//d
    if dd==0:
      if b==ai:
        continue
      ans=-1
      break
    ans+=dd
    B[ind]-=d*dd
    push(b-d*dd,ind)
  
  print(ans)
main()

Fixed submission:
n=int(input())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]

from heapq import heappop, heappush
push = lambda x,i: heappush(BB,(-x,i))
BB=[]

for i,b in enumerate(B):
  push(b,i)

ans=0
while BB:
  b,ind=heappop(BB)
  b,ai=-b,A[ind]
  if b<ai:
    ans=-1
    break
  d=B[(ind-1)%n]+B[(ind+1)%n]
  dd=(b-ai)//d
  if dd==0:
    if b==ai:
      continue
    ans=-1
    break
  ans+=dd
  B[ind]-=d*dd
  push(b-d*dd,ind)
  
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]

from heapq import heappop, heappush
push = lambda x,i: heappush(BB,(-x,i))
BB=[]

for i in range(n):
  push(B[i],i)

ans=0
chk=0
while chk<n:
  b,ind=heappop(BB)
  b,ai=-b,A[ind]
  if b<ai:
    ans=-1
    break
  d=B[(ind-1)%n]+B[(ind+1)%n]
  dd=(b-ai)//d
  if dd==0:
    if b==ai:
      chk+=1
      continue
    ans=-1
    break
  ans+=dd
  B[ind]-=d*dd
  push(b-d*dd,ind)
  
print(ans)


Fixed submission:
n=int(input())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]

from heapq import heappop, heappush
push = lambda x,i: heappush(BB,(-x,i))
BB=[]

for i,b in enumerate(B):
  push(b,i)

ans=0
while BB:
  b,ind=heappop(BB)
  b,ai=-b,A[ind]
  if b<ai:
    ans=-1
    break
  d=B[(ind-1)%n]+B[(ind+1)%n]
  dd=(b-ai)//d
  if dd==0:
    if b==ai:
      continue
    ans=-1
    break
  ans+=dd
  B[ind]-=d*dd
  push(b-d*dd,ind)
  
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]

from heapq import heappop, heappush
push = lambda x,i: heappush(BB,(-x,i))
BB=[]

for i in range(n):
  push(B[i],i)

ans=0
while BB:
  b,ind=heappop(BB)
  b,ai=-b,A[ind]
  if b<ai:
    ans=-1
    break
  d=B[(ind-1)%n]+B[(ind+1)%n]
  dd=(b-ai)//d
  if dd==0:
    if b==ai:
      continue
    ans=-1
    break
  ans+=dd
  B[ind]-=d*dd
  push(b-d*dd,ind)
  
print(ans)


Fixed submission:
n=int(input())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]

from heapq import heappop, heappush
push = lambda x,i: heappush(BB,(-x,i))
BB=[]

for i,b in enumerate(B):
  push(b,i)

ans=0
while BB:
  b,ind=heappop(BB)
  b,ai=-b,A[ind]
  if b<ai:
    ans=-1
    break
  d=B[(ind-1)%n]+B[(ind+1)%n]
  dd=(b-ai)//d
  if dd==0:
    if b==ai:
      continue
    ans=-1
    break
  ans+=dd
  B[ind]-=d*dd
  push(b-d*dd,ind)
  
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]

from heapq import heappop, heappush
push = lambda x,i: heappush(BB,(-x,i))
BB=[]

for i in range(n):
  push(B[i],i)

ans=0
while BB:
  b,ind=heappop(BB)
  b,ai=-b,A[ind]
  if b<ai:
    ans=-1
    break
  d=B[(ind-1)%n]+B[(ind+1)%n]
  dd=(b-ai)//d
  if dd==0:
    if b==ai:
      continue
    ans=-1
    break
  ans+=dd
  B[ind]-=d*dd
  push(B[ind],ind)
  
print(ans)


Fixed submission:
n=int(input())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]

from heapq import heappop, heappush
push = lambda x,i: heappush(BB,(-x,i))
BB=[]

for i,b in enumerate(B):
  push(b,i)

ans=0
while BB:
  b,ind=heappop(BB)
  b,ai=-b,A[ind]
  if b<ai:
    ans=-1
    break
  d=B[(ind-1)%n]+B[(ind+1)%n]
  dd=(b-ai)//d
  if dd==0:
    if b==ai:
      continue
    ans=-1
    break
  ans+=dd
  B[ind]-=d*dd
  push(b-d*dd,ind)
  
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]

from heapq import heappop, heappush
push = lambda x,i: heappush(BB,(-x,i))
BB=[]

for i,b in enumerate(B):
  push(b,i)

ans=0
while BB:
  b,ind=heappop(BB)
  b,ai=-b,A[ind]
  if b<ai:
    ans=-1
    break
  if ind==0:
    d=B[1]+B[-1]
  elif ind==n-1:
    d=B[-2]+B[0]
  else:
    d=B[ind-1]+B[ind+1]
  dd=(b-ai)//d
  if dd==0:
    if b==ai:
      continue
    ans=-1
    break
  ans+=dd
  B[ind]-=d*dd
  push(B[ind],ind)
  
print(ans)


Fixed submission:
n=int(input())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]

from heapq import heappop, heappush
push = lambda x,i: heappush(BB,(-x,i))
BB=[]

for i,b in enumerate(B):
  push(b,i)

ans=0
while BB:
  b,ind=heappop(BB)
  b,ai=-b,A[ind]
  if b<ai:
    ans=-1
    break
  d=B[(ind-1)%n]+B[(ind+1)%n]
  dd=(b-ai)//d
  if dd==0:
    if b==ai:
      continue
    ans=-1
    break
  ans+=dd
  B[ind]-=d*dd
  push(b-d*dd,ind)
  
print(ans)

Buggy python submission (verdict: Runtime Error):
n=int(input())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]

from heapq import heappop, heappush
push = lambda x,i: heappush(BB,(-x,i))
BB=[]

for i,b in enumerate(B):
  push(b,i)

ans=0
while BB:
  b,ind=heappop(BB)
  b,ai=-b,A[ind]
  if b<ai:
    ans=-1
    break
  if ind==0:
    d=B[1]+B[-1]
  elif ind==n-1:
    d=B[-2]+B[0]
  else:
    d=B[ind-1]+B[ind+1)]
  dd=(b-ai)//d
  if dd==0:
    if b==ai:
      continue
    ans=-1
    break
  ans+=dd
  B[ind]-=d*dd
  push(B[ind],ind)
  
print(ans)


Fixed submission:
n=int(input())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]

from heapq import heappop, heappush
push = lambda x,i: heappush(BB,(-x,i))
BB=[]

for i,b in enumerate(B):
  push(b,i)

ans=0
while BB:
  b,ind=heappop(BB)
  b,ai=-b,A[ind]
  if b<ai:
    ans=-1
    break
  d=B[(ind-1)%n]+B[(ind+1)%n]
  dd=(b-ai)//d
  if dd==0:
    if b==ai:
      continue
    ans=-1
    break
  ans+=dd
  B[ind]-=d*dd
  push(b-d*dd,ind)
  
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):

n=int(input())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]

from heapq import heappop, heappush
push = lambda x,i: heappush(BB,(-x,i))
BB=[]

for i,b in enumerate(B):
  push(b,i)

ans=0
while BB:
  b,ind=heappop(BB)
  b=-b
  if b<A[ind]:
    ans=-1
    break
  d=B[(ind-1)%n]+B[(ind+1)%n]
  dd=(b-A[ind])//d
  if dd==0:
    if b==A[ind]:
      continue
    ans=-1
    break
  ans+=dd
  B[ind]-=d*dd
  push(B[ind],ind)
  
print(ans)


Fixed submission:
n=int(input())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]

from heapq import heappop, heappush
push = lambda x,i: heappush(BB,(-x,i))
BB=[]

for i,b in enumerate(B):
  push(b,i)

ans=0
while BB:
  b,ind=heappop(BB)
  b,ai=-b,A[ind]
  if b<ai:
    ans=-1
    break
  d=B[(ind-1)%n]+B[(ind+1)%n]
  dd=(b-ai)//d
  if dd==0:
    if b==ai:
      continue
    ans=-1
    break
  ans+=dd
  B[ind]-=d*dd
  push(b-d*dd,ind)
  
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):

n=int(input())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]

from heapq import heappop, heappush
push = lambda x,i: heappush(BB,(-x,i))
BB=[]

for i,b in enumerate(B):
  push(b,i)

ans=0
while BB:
  b,ind=heappop(BB)
  b,ai=-b,A[ind]
  if b<ai:
    ans=-1
    break
  d=B[(ind-1)%n]+B[(ind+1)%n]
  dd=(b-ai)//d
  if dd==0:
    if b==ai:
      continue
    ans=-1
    break
  ans+=dd
  B[ind]-=d*dd
  push(B[ind],ind)
  
print(ans)


Fixed submission:
n=int(input())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]

from heapq import heappop, heappush
push = lambda x,i: heappush(BB,(-x,i))
BB=[]

for i,b in enumerate(B):
  push(b,i)

ans=0
while BB:
  b,ind=heappop(BB)
  b,ai=-b,A[ind]
  if b<ai:
    ans=-1
    break
  d=B[(ind-1)%n]+B[(ind+1)%n]
  dd=(b-ai)//d
  if dd==0:
    if b==ai:
      continue
    ans=-1
    break
  ans+=dd
  B[ind]-=d*dd
  push(b-d*dd,ind)
  
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
n=int(input())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]

from heapq import heappop, heappush
push = lambda x,i: heappush(BB,(-x,i))
BB=[]

for i,b in enumerate(B):
  push(b,i)

ans=0
while BB:
  b,ind=heappop(BB)
  b,ai=-b,A[ind]
  if b<ai:
    ans=-1
    break
  d=B[(ind-1)%n]+B[(ind+1)%n]
  dd=(b-ai)//d
  if dd==0:
    if b==ai:
      continue
    ans=-1
    break
  ans+=dd
  B[ind]-=d*dd
  push(B[ind],ind)
  
print(ans)


Fixed submission:
n=int(input())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]

from heapq import heappop, heappush
push = lambda x,i: heappush(BB,(-x,i))
BB=[]

for i,b in enumerate(B):
  push(b,i)

ans=0
while BB:
  b,ind=heappop(BB)
  b,ai=-b,A[ind]
  if b<ai:
    ans=-1
    break
  d=B[(ind-1)%n]+B[(ind+1)%n]
  dd=(b-ai)//d
  if dd==0:
    if b==ai:
      continue
    ans=-1
    break
  ans+=dd
  B[ind]-=d*dd
  push(b-d*dd,ind)
  
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
n=int(input())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]

from heapq import heappop, heappush
push = lambda x,i: heappush(BB,(-x,i))
BB=[]

for i,b in enumerate(B):
  push(b,i)

ans=0
while BB:
  b,ind=heappop(BB)
  b,ai=-b,A[ind]
  if b<ai:
    ans=-1
    break
  d=B[(ind-1)%n]+B[(ind+1)%n]
  dd=(b-ai)//d
  if dd==0:
    if b==ai:
      continue
    else:
      ans=-1
      break
  else:
    ans+=dd
    B[ind]-=d*dd
    push(B[ind],ind)
  
print(ans)


Fixed submission:
n=int(input())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]

from heapq import heappop, heappush
push = lambda x,i: heappush(BB,(-x,i))
BB=[]

for i,b in enumerate(B):
  push(b,i)

ans=0
while BB:
  b,ind=heappop(BB)
  b,ai=-b,A[ind]
  if b<ai:
    ans=-1
    break
  d=B[(ind-1)%n]+B[(ind+1)%n]
  dd=(b-ai)//d
  if dd==0:
    if b==ai:
      continue
    ans=-1
    break
  ans+=dd
  B[ind]-=d*dd
  push(b-d*dd,ind)
  
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]

from heapq import heappop, heappush
push = lambda x,i: heappush(BB,(-x,i))
BB=[]

for i,b in enumerate(B):
  push(b,i)

ans=0
while BB:
  b,ind=heappop(BB)
  b,ai=-b,A[ind]
  if b<ai:
    ans=-1
    break
  d=B[(ind-1)%n]+B[(ind+1)%n]
  dd=(b-ai)//d
  if dd==0:
    if b==ai:
      continue
    else:
      ans=-1
      break
  else:
    ans+=dd
    B[ind]-=d*dd
    push(B[ind],ind)
  
print(ans)


Fixed submission:
n=int(input())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]

from heapq import heappop, heappush
push = lambda x,i: heappush(BB,(-x,i))
BB=[]

for i,b in enumerate(B):
  push(b,i)

ans=0
while BB:
  b,ind=heappop(BB)
  b,ai=-b,A[ind]
  if b<ai:
    ans=-1
    break
  d=B[(ind-1)%n]+B[(ind+1)%n]
  dd=(b-ai)//d
  if dd==0:
    if b==ai:
      continue
    ans=-1
    break
  ans+=dd
  B[ind]-=d*dd
  push(b-d*dd,ind)
  
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]

from heapq import heappop, heappush
push = lambda x,i: heappush(BB,(-x,i))
BB=[]

for i,b in enumerate(B):
  push(b,i)

ans=0
while BB:
  b,ind=heappop(BB)
  b=-b
  d=B[(ind-1)%n]+B[(ind+1)%n]
  dd=(b-A[ind])//d
  if dd==0:
    if b!=A[ind]:
      ans=-1
      break
  else:
    ans+=dd
    b-=d*dd
    B[ind]=b
    push(b,ind)
  
print(ans)


Fixed submission:
n=int(input())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]

from heapq import heappop, heappush
push = lambda x,i: heappush(BB,(-x,i))
BB=[]

for i,b in enumerate(B):
  push(b,i)

ans=0
while BB:
  b,ind=heappop(BB)
  b,ai=-b,A[ind]
  if b<ai:
    ans=-1
    break
  d=B[(ind-1)%n]+B[(ind+1)%n]
  dd=(b-ai)//d
  if dd==0:
    if b==ai:
      continue
    ans=-1
    break
  ans+=dd
  B[ind]-=d*dd
  push(b-d*dd,ind)
  
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]

from heapq import heappop, heappush
push = lambda x,i: heappush(BB,(-x,i))
BB=[]

for i,b in enumerate(B):
  push(b,i)

ans=0
while BB:
  b,ind=heappop(BB)
  b=-b
  d=B[(ind-1)%n]+B[(ind+1)%n]
  dd=(b-A[ind])//d
  if dd==0:
    if b!=A[ind]:
      ans=-1
      break
  else:
    ans+=dd
    b-=d*dd
    B[ind]=b
    push(b,ind)
  
print(ans)


Fixed submission:
n=int(input())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]

from heapq import heappop, heappush
push = lambda x,i: heappush(BB,(-x,i))
BB=[]

for i,b in enumerate(B):
  push(b,i)

ans=0
while BB:
  b,ind=heappop(BB)
  b,ai=-b,A[ind]
  if b<ai:
    ans=-1
    break
  d=B[(ind-1)%n]+B[(ind+1)%n]
  dd=(b-ai)//d
  if dd==0:
    if b==ai:
      continue
    ans=-1
    break
  ans+=dd
  B[ind]-=d*dd
  push(b-d*dd,ind)
  
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]

from heapq import heappop, heappush
push = lambda x,i: heappush(BB,(-x,i))
BB=[]

for i,b in enumerate(B):
  push(b,i)

ans=0
while BB:
  b,ind=heappop(BB)
  b=-b
  d=B[(ind-1)%n]+B[(ind+1)%n]
  dd=(b-A[ind])//d
  if dd==0:
    if b!=A[ind]:
      break
  else:
    ans+=dd
    b-=d*dd
    B[ind]=b
    push(b,ind)
  
print(ans if A==B else -1)


Fixed submission:
n=int(input())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]

from heapq import heappop, heappush
push = lambda x,i: heappush(BB,(-x,i))
BB=[]

for i,b in enumerate(B):
  push(b,i)

ans=0
while BB:
  b,ind=heappop(BB)
  b,ai=-b,A[ind]
  if b<ai:
    ans=-1
    break
  d=B[(ind-1)%n]+B[(ind+1)%n]
  dd=(b-ai)//d
  if dd==0:
    if b==ai:
      continue
    ans=-1
    break
  ans+=dd
  B[ind]-=d*dd
  push(b-d*dd,ind)
  
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]
ma=max(A)
import heapq
BB=[]
Chk=[False]*n
for i in range(n):
  if A[i]==B[i]:
    Chk[i]=True
for i in range(n):
  BB.append([-B[i],i])
heapq.heapify(BB)
ans=0
while BB:
  b,ind=heapq.heappop(BB)
  if Chk[ind]:
    continue
  else:
    if ind==0:
      d=B[-1]+B[1]
    elif ind==n-1:
      d=B[-2]+B[0]
    else:
      d=B[ind-1]+B[ind+1]
    if d>=-b:
      break
    else:
      b_next=max(d,A[ind])
      ans+=(-b-b_next-1)//d+1
      b+=d*((-b-b_next-1)//d+1)
      B[ind]=-b
      if b>0:
        break
      elif A[ind]==-b:
        Chk[ind]=True
      else:
        heapq.heappush(BB,[b,ind])
  
print(-1 if BB else ans)


Fixed submission:
n=int(input())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]

from heapq import heappop, heappush
push = lambda x,i: heappush(BB,(-x,i))
BB=[]

for i,b in enumerate(B):
  push(b,i)

ans=0
while BB:
  b,ind=heappop(BB)
  b,ai=-b,A[ind]
  if b<ai:
    ans=-1
    break
  d=B[(ind-1)%n]+B[(ind+1)%n]
  dd=(b-ai)//d
  if dd==0:
    if b==ai:
      continue
    ans=-1
    break
  ans+=dd
  B[ind]-=d*dd
  push(b-d*dd,ind)
  
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]
ma=max(A)
import heapq
BB=[]
Chk=[False]*n
for i in range(n):
  if A[i]==B[i]:
    Chk[i]=True
for i in range(n):
  BB.append([-B[i],i])
heapq.heapify(BB)
ans=0
while BB:
  b,ind=heapq.heappop(BB)
  if Chk[ind]:
    continue
  else:
    if ind==0:
      d=B[-1]+B[1]
    elif ind==n-1:
      d=B[-2]+B[0]
    else:
      d=B[ind-1]+B[ind+1]
    if d>=-b:
      break
    else:
      b_next=max(d,A[ind])
      ans+=(-b-b_next-1)//d+1
      b+=d*((-b-b_next-1)//d+1)
      B[ind]=-b
      if b>0:
        break
      elif A[ind]==-b:
        Chk[ind]=True
      else:
        heapq.heappush(BB,[b,ind])
  
print(ans if A==B else -1)


Fixed submission:
n=int(input())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]

from heapq import heappop, heappush
push = lambda x,i: heappush(BB,(-x,i))
BB=[]

for i,b in enumerate(B):
  push(b,i)

ans=0
while BB:
  b,ind=heappop(BB)
  b,ai=-b,A[ind]
  if b<ai:
    ans=-1
    break
  d=B[(ind-1)%n]+B[(ind+1)%n]
  dd=(b-ai)//d
  if dd==0:
    if b==ai:
      continue
    ans=-1
    break
  ans+=dd
  B[ind]-=d*dd
  push(b-d*dd,ind)
  
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]
ma=max(A)
import heapq
BB=[]
Chk=[False]*n
for i in range(n):
  if A[i]==B[i]:
    Chk[i]=True
for i in range(n):
  BB.append([-B[i],i])
heapq.heapify(BB)
ans=0
while BB:
  b,ind=heapq.heappop(BB)
  if Chk[ind]:
    continue
  else:
    if ind==0:
      d=B[-1]+B[1]
      b_next=max(B[-1],B[1])
    elif ind==n-1:
      d=B[-2]+B[0]
      b_next=max(B[-2],B[0])
    else:
      d=B[ind-1]+B[ind+1]
      b_next=max(B[ind-1],B[ind+1])
    b_next=max(b_next,A[ind])
    ans+=(-b-b_next-1)//d+1
    b+=d*((-b-b_next-1)//d+1)
    B[ind]=-b
    if b>0:
      break
    elif A[ind]==-b:
      Chk[ind]=True
    else:
      heapq.heappush(BB,[b,ind])
  
print(ans if A==B else -1)


Fixed submission:
n=int(input())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]

from heapq import heappop, heappush
push = lambda x,i: heappush(BB,(-x,i))
BB=[]

for i,b in enumerate(B):
  push(b,i)

ans=0
while BB:
  b,ind=heappop(BB)
  b,ai=-b,A[ind]
  if b<ai:
    ans=-1
    break
  d=B[(ind-1)%n]+B[(ind+1)%n]
  dd=(b-ai)//d
  if dd==0:
    if b==ai:
      continue
    ans=-1
    break
  ans+=dd
  B[ind]-=d*dd
  push(b-d*dd,ind)
  
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]
ma=max(A)
import heapq
BB=[]
Chk=[False]*n
for i in range(n):
  if A[i]==B[i]:
    Chk[i]=True
for i in range(n):
  BB.append([-B[i],i])
heapq.heapify(BB)
ans=0
while BB:
  b,ind=heapq.heappop(BB)
  if Chk[ind]:
    continue
  else:
    if ind==0:
      d=B[-1]+B[1]
      b_next=max(B[-1],B[1])
    elif ind==n-1:
      d=B[-2]+B[0]
      b_next=max(B[-2],B[0])
    else:
      d=B[ind-1]+B[ind+1]
      b_next=max(B[ind-1],B[ind+1])
    b_next=max(b_next,A[ind])
    ans+=(-b-b_next-1)//d+1
    b+=d*((-b-b_next-1)//d+1)
    B[ind]=-b
    if b>0:
      break
    elif A[ind]==-b:
      Chk[ind]=True
    else:
      heapq.heappush(BB,[b,ind])
  
print(ans if A==B else -1)


Fixed submission:
n=int(input())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]

from heapq import heappop, heappush
push = lambda x,i: heappush(BB,(-x,i))
BB=[]

for i,b in enumerate(B):
  push(b,i)

ans=0
while BB:
  b,ind=heappop(BB)
  b,ai=-b,A[ind]
  if b<ai:
    ans=-1
    break
  d=B[(ind-1)%n]+B[(ind+1)%n]
  dd=(b-ai)//d
  if dd==0:
    if b==ai:
      continue
    ans=-1
    break
  ans+=dd
  B[ind]-=d*dd
  push(b-d*dd,ind)
  
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]
ma=max(A)
import heapq
BB=[]
for i in range(n):
  BB.append([-B[i],i])
heapq.heapify(BB)
b,ind=heapq.heappop(BB)
ans=0
while b<-ma:
  if ind==0:
    d=B[-1]+B[1]
  elif ind==n-1:
    d=B[-2]+B[0]
  else:
    d=B[ind-1]+B[ind+1]
  b_next,ind_next=heapq.heappop(BB)
  ans+=(-b+b_next-1)//d+1
  b+=d*((-b+b_next-1)//d+1)
  heapq.heappush(BB,[b,ind])
  heapq.heappush(BB,[b_next,ind_next])
  B[ind]=-b
  b,ind=heapq.heappop(BB)
  ans+=((-b+b_next-1)//d+1)
print(ans if A==B else -1)


Fixed submission:
n=int(input())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]

from heapq import heappop, heappush
push = lambda x,i: heappush(BB,(-x,i))
BB=[]

for i,b in enumerate(B):
  push(b,i)

ans=0
while BB:
  b,ind=heappop(BB)
  b,ai=-b,A[ind]
  if b<ai:
    ans=-1
    break
  d=B[(ind-1)%n]+B[(ind+1)%n]
  dd=(b-ai)//d
  if dd==0:
    if b==ai:
      continue
    ans=-1
    break
  ans+=dd
  B[ind]-=d*dd
  push(b-d*dd,ind)
  
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]
ma=max(A)
import heapq
BB=[]
for i in range(n):
  BB.append([-B[i],i])
heapq.heapify(BB)
b,ind=heapq.heappop(BB)
ans=0
while b<-ma:
  if ind==0:
    d=B[-1]+B[1]
  elif ind==n-1:
    d=B[-2]+B[0]
  else:
    d=B[ind-1]+B[ind+1]
  b_next,ind_next=heapq.heappop(BB)
  ans+=(-b+b_next-1)//d+1
  b+=d*((-b+b_next-1)//d+1)
  heapq.heappush(BB,[b,ind])
  heapq.heappush(BB,[b_next,ind_next])
  B[ind]=-b
  b,ind=heapq.heappop(BB)
  ans+=((-b+b_next-1)//d+1)
print(ans if A==B else -1)

Fixed submission:
n=int(input())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]

from heapq import heappop, heappush
push = lambda x,i: heappush(BB,(-x,i))
BB=[]

for i,b in enumerate(B):
  push(b,i)

ans=0
while BB:
  b,ind=heappop(BB)
  b,ai=-b,A[ind]
  if b<ai:
    ans=-1
    break
  d=B[(ind-1)%n]+B[(ind+1)%n]
  dd=(b-ai)//d
  if dd==0:
    if b==ai:
      continue
    ans=-1
    break
  ans+=dd
  B[ind]-=d*dd
  push(b-d*dd,ind)
  
print(ans)