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

n=int(input())
arr1=list(map(int,input().split()))
arr2=list(map(int,input().split()))
q=[]
for i in range(n):
  if arr2[i]!=arr1[i]:
    heapq.heappush(q,(-arr2[i],i))
cnt=0
while q:
  val,pos=heapq.heappop(q)
  val*=-1
  diff=arr2[pos-1]+arr2[(pos+1)%n]
  move=(val-arr1[pos])//diff
  if move==0:
    break
  cnt+=move
  tmp=arr2[pos]-diff*move
  arr2[pos]=tmp
  if tmp!=arr1[pos]:
    heapq.heappush(q,(-tmp,pos))
for i in range(n):
  if arr1[i]!=arr2[i]:
    print(-1)
    break
else:
  print(cnt)

Fixed submission:
import heapq

n=int(input())
arr1=list(map(int,input().split()))
arr2=list(map(int,input().split()))
q=[]
for i in range(n):
  if arr2[i]!=arr1[i]:
    heapq.heappush(q,(-arr2[i],i))
cnt=0
while q:
  val,pos=heapq.heappop(q)
  val*=-1
  diff=arr2[pos-1]+arr2[(pos+1)%n]
  move,tmp=divmod(val-arr1[pos],diff)
  if move==0:
    break
  cnt+=move
  tmp+=arr1[pos]
  arr2[pos]=tmp
  if tmp!=arr1[pos]:
    heapq.heappush(q,(-tmp,pos))
for i in range(n):
  if arr1[i]!=arr2[i]:
    print(-1)
    break
else:
  print(cnt)

Buggy python submission (verdict: Runtime Error):
import heapq

n=int(input())
arr1=list(map(int,input().split()))
arr2=list(map(int,input().split()))
q=[]
dic={}
for i in range(n):
  heapq.heappush(q,(-arr2[i],i))
cnt=0
while q:
  val,pos=heapq.heappop(q)
  val*=-1
  diff=arr2[pos-1]+arr2[(pos+1)%n]
  move=(val-arr1[pos])//diff
  if move==0:
    break
  cnt+=move
  tmp=arr2[pos]-diff*move
  arr2[pos]=tmp
  heapq.heappush(q,-tmp)
for i in range(n):
  if arr1[i]!=arr2[i]:
    print(-1)
    break
else:
  print(cnt)

Fixed submission:
import heapq

n=int(input())
arr1=list(map(int,input().split()))
arr2=list(map(int,input().split()))
q=[]
for i in range(n):
  if arr2[i]!=arr1[i]:
    heapq.heappush(q,(-arr2[i],i))
cnt=0
while q:
  val,pos=heapq.heappop(q)
  val*=-1
  diff=arr2[pos-1]+arr2[(pos+1)%n]
  move,tmp=divmod(val-arr1[pos],diff)
  if move==0:
    break
  cnt+=move
  tmp+=arr1[pos]
  arr2[pos]=tmp
  if tmp!=arr1[pos]:
    heapq.heappush(q,(-tmp,pos))
for i in range(n):
  if arr1[i]!=arr2[i]:
    print(-1)
    break
else:
  print(cnt)

Buggy python submission (verdict: Runtime Error):
import heapq

n=int(input())
arr1=list(map(int,input().split()))
arr2=list(map(int,input().split()))
q=[]
for i in range(n):
  heapq.heappush(q,(-arr2[i],i))
cnt=0
num=0
while 1:
  val,pos=-heapq.heappop(q)
  diff=arr2[pos-1]+arr2[(pos+1)%n]
  move=(val-diff)//diff+1
  if move<=0:
    break
  tmp=arr2[pos]-diff*move
  if tmp<=0:
    break
  arr2[pos]=tmp
  heapq.heappush(q,-tmp)
  if tmp==arr1[pos]:
    num+=1
  cnt+=move
  if num==n:
    break
for i in range(n):
  if arr1[i]!=arr2[i]:
    print(-1)
    break
else:
  print(cnt)

Fixed submission:
import heapq

n=int(input())
arr1=list(map(int,input().split()))
arr2=list(map(int,input().split()))
q=[]
for i in range(n):
  if arr2[i]!=arr1[i]:
    heapq.heappush(q,(-arr2[i],i))
cnt=0
while q:
  val,pos=heapq.heappop(q)
  val*=-1
  diff=arr2[pos-1]+arr2[(pos+1)%n]
  move,tmp=divmod(val-arr1[pos],diff)
  if move==0:
    break
  cnt+=move
  tmp+=arr1[pos]
  arr2[pos]=tmp
  if tmp!=arr1[pos]:
    heapq.heappush(q,(-tmp,pos))
for i in range(n):
  if arr1[i]!=arr2[i]:
    print(-1)
    break
else:
  print(cnt)

Buggy python submission (verdict: Runtime Error):
import heapq

n=int(input())
arr1=list(map(int,input().split()))
arr2=list(map(int,input().split()))
q=[]
dic={}
for i in range(n):
  heapq.heappush(q,-arr2[i])
  dic[arr2[i]]=i
cnt=0
while len(q)!=0:
  val=-heapq.heappop(q)
  pos=dic[val]
  if val==arr1[pos]:
    continue
  diff=arr2[pos-1]+arr2[(pos+1)%n]
  if val-diff<arr1[pos]:
    continue
  move=(val-arr1[pos])//diff
  cnt+=move
  tmp=arr2[pos]-diff*move
  arr2[pos]=tmp
  heapq.heappush(q,-tmp)
  dic[tmp]=pos
for i in range(n):
  if arr1[i]!=arr2[i]:
    print(-1)
    break
else:
  print(cnt)

Fixed submission:
import heapq

n=int(input())
arr1=list(map(int,input().split()))
arr2=list(map(int,input().split()))
q=[]
for i in range(n):
  if arr2[i]!=arr1[i]:
    heapq.heappush(q,(-arr2[i],i))
cnt=0
while q:
  val,pos=heapq.heappop(q)
  val*=-1
  diff=arr2[pos-1]+arr2[(pos+1)%n]
  move,tmp=divmod(val-arr1[pos],diff)
  if move==0:
    break
  cnt+=move
  tmp+=arr1[pos]
  arr2[pos]=tmp
  if tmp!=arr1[pos]:
    heapq.heappush(q,(-tmp,pos))
for i in range(n):
  if arr1[i]!=arr2[i]:
    print(-1)
    break
else:
  print(cnt)