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()))

# B < A の要素が存在すればFalse
if any([b < a for a, b in zip(A, B)]):
  print(-1)
  exit()

# B > A の index をheapqueに入れる。
que = []
for i, (a, b) in enumerate(zip(A, B)):
  if b > a:
    heappush(que,(-b, i)) 

ans = 0
while que:
  val, idx = heappop(que)
  val *= -1
  l = idx - 1
  r = idx + 1 if idx != n - 1 else 0
  ac = B[l] + B[r]
  if val > ac:
    cnt = min((val ) // ac, (val - A[idx]) // ac)
    heappush(que, (-(val - cnt * ac), idx))
    ans += cnt
    B[idx] -= cnt * ac 

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

Fixed submission:
from heapq import heapify, heappop, heappush

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

q = [(-val, i) for i, val in enumerate(b) if val != a[i]]
heapify(q)

# 3print(q)

cnt = 0

while q:
  bval, i = heappop(q)
  bval = - bval - a[i]
  dv, bval = divmod(bval, b[i - 1] + b[(i + 1) % n])
#   print(dv, bval,i)
  if dv == 0:
    print(-1)
    exit()
  bval += a[i]
  b[i] = bval
  cnt += dv
  if bval != a[i]:
    heappush(q, (- bval, i))
print(cnt)

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()))

# B < A の要素が存在すればFalse
if any([b < a for a, b in zip(A, B)]):
  print(-1)
  exit()

# B > A の index をheapqueに入れる。
que = []
for i, (a, b) in enumerate(zip(A, B)):
  if b > a:
    heappush(que,(-b, i)) 

ans = 0
while que:
  val, idx = heappop(que)
  val *= -1
  l = idx - 1
  r = idx + 1 if idx != n - 1 else 0
  ac = B[l] + B[r]
  if val > ac:
    cnt = min((val ) // ac, (val - A[idx]) // ac)
    heappush(que, (-(val - cnt * ac), idx))
    ans += cnt
    B[idx] -= cnt * ac 

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

  

Fixed submission:
from heapq import heapify, heappop, heappush

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

q = [(-val, i) for i, val in enumerate(b) if val != a[i]]
heapify(q)

# 3print(q)

cnt = 0

while q:
  bval, i = heappop(q)
  bval = - bval - a[i]
  dv, bval = divmod(bval, b[i - 1] + b[(i + 1) % n])
#   print(dv, bval,i)
  if dv == 0:
    print(-1)
    exit()
  bval += a[i]
  b[i] = bval
  cnt += dv
  if bval != a[i]:
    heappush(q, (- bval, i))
print(cnt)

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

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

q = [(-val, i) for i, val in enumerate(b) if val != a[i]]
heapify(q)

# 3print(q)

cnt = 0

while q:
  bval, i = heappop(q)
  bval = - bval - a[i]
  dv, bval = divmod(bval, b[i - 1] + b[(i + 1) % n])
#   print(dv, bval,i)
  if dv == 0:
    print(-1)
    exit()
  bval += a[i]
  b[i] = bval
  cnt += dv
  if bval != a[i]:
    heappush(q, (- bval, i))
print(cnt)

Fixed submission:
from heapq import heapify, heappop, heappush

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

q = [(-val, i) for i, val in enumerate(b) if val != a[i]]
heapify(q)

# 3print(q)

cnt = 0

while q:
  bval, i = heappop(q)
  bval = - bval - a[i]
  dv, bval = divmod(bval, b[i - 1] + b[(i + 1) % n])
#   print(dv, bval,i)
  if dv == 0:
    print(-1)
    exit()
  bval += a[i]
  b[i] = bval
  cnt += dv
  if bval != a[i]:
    heappush(q, (- bval, i))
print(cnt)

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

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

q = [(-val, i) for i, val in enumerate(b) if val != a[i]]
heapify(q)

# 3print(q)

cnt = 0

while q:
  bval, i = heappop(q)
  bval = - bval - a[i]
  dv, bval = divmod(bval, b[i - 1] + b[(i + 1) % n])
#   print(dv, bval,i)
  if dv == 0:
    print(-1)
    exit()
  bval += a[i]
  b[i] = bval
  cnt += dv
  if bval != a[i]:
    heappush(q, (- bval, i))
print(cnt)

Fixed submission:
from heapq import heapify, heappop, heappush

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

q = [(-val, i) for i, val in enumerate(b) if val != a[i]]
heapify(q)

# 3print(q)

cnt = 0

while q:
  bval, i = heappop(q)
  bval = - bval - a[i]
  dv, bval = divmod(bval, b[i - 1] + b[(i + 1) % n])
#   print(dv, bval,i)
  if dv == 0:
    print(-1)
    exit()
  bval += a[i]
  b[i] = bval
  cnt += dv
  if bval != a[i]:
    heappush(q, (- bval, i))
print(cnt)