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

mod = 10**9+7

def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())
def s(x): #圧縮
    a = []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): #listをスペースごとに分ける
    return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
    return max(map(max,x))
def coco():
    n = onem()

    a = l()

    b = l()

    t = []

    for i in range(n):
        if b[i] != a[i]:
            hq.heappush(t,[-b[i],i])
    co = 0
    while t:
        k = hq.heappop(t)
        k = [-k[0],k[1]]
        if k[1] == 0:
            ppp = (k[0]-a[k[1]]) // (b[-1] + b[1])
            if ppp == 0:
                return -1
                
            co += ppp
            k[0] = (k[0]-a[k[1]]) % (b[-1] + b[1]) + a[k[1]]
        elif k[1] == n-1:
            ppp = (k[0]-a[k[1]]) // (b[-2] + b[0])
            if ppp == 0:
                return -1
            co += ppp
            k[0] = (k[0]-a[k[1]]) % (b[-2] + b[0]) + a[k[1]]
        else:
            ppp = (k[0]-a[k[1]]) // (b[k[1]+1] + b[k[1]-1])
            if ppp == 0:
                return -1
            co += ppp
            k[0] = (k[0]-a[k[1]]) % (b[k[1]+1] + b[k[1]-1]) + a[k[1]]
        b[k[1]] = k[0]
        if b[k[1]] != a[k[1]]:
            hq.heappush(t,[-b[k[1]],k[1]])
    return co

print(coco())


Fixed submission:
import heapq as hq
mod = 10**9+7
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())

def onon():
    n = onem()

    a = l()

    b = l()

    t = []

    for i in range(n):
        if b[i] != a[i]:
            hq.heappush(t,(-b[i],i))
    co = 0
    while t:
        k = hq.heappop(t)
        k = (-k[0],k[1])
        q,w = divmod(k[0] - a[k[1]], b[(k[1] - 1) % n] + b[(k[1] + 1) % n])
        co += q
        if q == 0:
            return -1
        w = a[k[1]] + w
        b[k[1]] = w
        if b[k[1]] != a[k[1]]:
            hq.heappush(t,(-w,k[1]))
    return co
print(onon())

Buggy python submission (verdict: Time Limit Exceeded):
import sys,heapq as hq
sys.setrecursionlimit(100000)
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())
def s(x): #圧縮
    a = []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): #listをスペースごとに分ける
    return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
    return max(map(max,x))
def coco():
    n = onem()

    a = l()

    b = l()

    t = []

    for i in range(n):
        if b[i] != a[i]:
            hq.heappush(t,[-b[i],i])
    co = 0
    while t:
        k = hq.heappop(t)
        k = [-k[0],k[1]]
        if k[1] == 0:
            ppp = (k[0]-a[k[1]]) // (b[-1] + b[1])
            if ppp == 0:
                return -1
                
            co += ppp
            k[0] = (k[0]-a[k[1]]) % (b[-1] + b[1]) + a[k[1]]
        elif k[1] == n-1:
            ppp = (k[0]-a[k[1]]) // (b[-2] + b[0])
            if ppp == 0:
                return -1
            co += ppp
            k[0] = (k[0]-a[k[1]]) % (b[-2] + b[0]) + a[k[1]]
        else:
            ppp = (k[0]-a[k[1]]) // (b[k[1]+1] + b[k[1]-1])
            if ppp == 0:
                return -1
            co += ppp
            k[0] = (k[0]-a[k[1]]) % (b[k[1]+1] + b[k[1]-1]) + a[k[1]]
        b[k[1]] = k[0]
        if b[k[1]] != a[k[1]]:
            hq.heappush(t,[-b[k[1]],k[1]])
    return co

print(coco())


Fixed submission:
import heapq as hq
mod = 10**9+7
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())

def onon():
    n = onem()

    a = l()

    b = l()

    t = []

    for i in range(n):
        if b[i] != a[i]:
            hq.heappush(t,(-b[i],i))
    co = 0
    while t:
        k = hq.heappop(t)
        k = (-k[0],k[1])
        q,w = divmod(k[0] - a[k[1]], b[(k[1] - 1) % n] + b[(k[1] + 1) % n])
        co += q
        if q == 0:
            return -1
        w = a[k[1]] + w
        b[k[1]] = w
        if b[k[1]] != a[k[1]]:
            hq.heappush(t,(-w,k[1]))
    return co
print(onon())

Buggy python submission (verdict: Runtime Error):
import heapq as hq
sys.setrecursionlimit(100000)
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())
def s(x): #圧縮
    a = []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): #listをスペースごとに分ける
    return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
    return max(map(max,x))
def coco():
    n = onem()

    a = l()

    b = l()

    t = []

    for i in range(n):
        if b[i] != a[i]:
            hq.heappush(t,[-b[i],i])
    co = 0
    while t:
        k = hq.heappop(t)
        k = [-k[0],k[1]]
        if k[1] == 0:
            ppp = (k[0]-a[k[1]]) // (b[-1] + b[1])
            if ppp == 0:
                return -1
                
            co += ppp
            k[0] = (k[0]-a[k[1]]) % (b[-1] + b[1]) + a[k[1]]
        elif k[1] == n-1:
            ppp = (k[0]-a[k[1]]) // (b[-2] + b[0])
            if ppp == 0:
                return -1
            co += ppp
            k[0] = (k[0]-a[k[1]]) % (b[-2] + b[0]) + a[k[1]]
        else:
            ppp = (k[0]-a[k[1]]) // (b[k[1]+1] + b[k[1]-1])
            if ppp == 0:
                return -1
            co += ppp
            k[0] = (k[0]-a[k[1]]) % (b[k[1]+1] + b[k[1]-1]) + a[k[1]]
        b[k[1]] = k[0]
        if b[k[1]] != a[k[1]]:
            hq.heappush(t,[-b[k[1]],k[1]])
    return co

print(coco())


Fixed submission:
import heapq as hq
mod = 10**9+7
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())

def onon():
    n = onem()

    a = l()

    b = l()

    t = []

    for i in range(n):
        if b[i] != a[i]:
            hq.heappush(t,(-b[i],i))
    co = 0
    while t:
        k = hq.heappop(t)
        k = (-k[0],k[1])
        q,w = divmod(k[0] - a[k[1]], b[(k[1] - 1) % n] + b[(k[1] + 1) % n])
        co += q
        if q == 0:
            return -1
        w = a[k[1]] + w
        b[k[1]] = w
        if b[k[1]] != a[k[1]]:
            hq.heappush(t,(-w,k[1]))
    return co
print(onon())

Buggy python submission (verdict: Time Limit Exceeded):
import sys,heapq as hq
mod = 10**9+7
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())


n = onem()

a = l()

b = l()

t = []

for i in range(n):
    if b[i] != a[i]:
        hq.heappush(t,(-b[i],i))
co = 0
while t:
    k = hq.heappop(t)
    k = (-k[0],k[1])
    q,w = divmod(k[0] - a[k[1]], b[(k[1] - 1) % n] + b[(k[1] + 1) % n])
    co += q
    if q == 0:
        print(-1)
        exit()
    w = a[k[1]] + w
    b[k[1]] = w
    if b[k[1]] != a[k[1]]:
        hq.heappush(t,(-w,k[1]))

print(co)

Fixed submission:
import heapq as hq
mod = 10**9+7
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())

def onon():
    n = onem()

    a = l()

    b = l()

    t = []

    for i in range(n):
        if b[i] != a[i]:
            hq.heappush(t,(-b[i],i))
    co = 0
    while t:
        k = hq.heappop(t)
        k = (-k[0],k[1])
        q,w = divmod(k[0] - a[k[1]], b[(k[1] - 1) % n] + b[(k[1] + 1) % n])
        co += q
        if q == 0:
            return -1
        w = a[k[1]] + w
        b[k[1]] = w
        if b[k[1]] != a[k[1]]:
            hq.heappush(t,(-w,k[1]))
    return co
print(onon())

Buggy python submission (verdict: Time Limit Exceeded):
import sys,heapq as hq
sys.setrecursionlimit(100000)
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())
def s(x): #圧縮
    a = []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): #listをスペースごとに分ける
    return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
    return max(map(max,x))

n = onem()

a = l()

b = l()

t = []

for i in range(n):
    if b[i] != a[i]:
        hq.heappush(t,(-b[i],i))
co = 0
while t:
    k = hq.heappop(t)
    k = (-k[0],k[1])
    q,w = divmod(k[0] - a[k[1]], b[(k[1] - 1) % n] + b[(k[1] + 1) % n])
    co += q
    if q == 0:
        print(-1)
        exit()
    w = a[k[1]] + w
    b[k[1]] = w
    if b[k[1]] != a[k[1]]:
        hq.heappush(t,(-w,k[1]))

print(co)


Fixed submission:
import heapq as hq
mod = 10**9+7
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())

def onon():
    n = onem()

    a = l()

    b = l()

    t = []

    for i in range(n):
        if b[i] != a[i]:
            hq.heappush(t,(-b[i],i))
    co = 0
    while t:
        k = hq.heappop(t)
        k = (-k[0],k[1])
        q,w = divmod(k[0] - a[k[1]], b[(k[1] - 1) % n] + b[(k[1] + 1) % n])
        co += q
        if q == 0:
            return -1
        w = a[k[1]] + w
        b[k[1]] = w
        if b[k[1]] != a[k[1]]:
            hq.heappush(t,(-w,k[1]))
    return co
print(onon())

Buggy python submission (verdict: Time Limit Exceeded):
import sys,heapq as hq
sys.setrecursionlimit(100000)
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())
def s(x): #圧縮
    a = []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): #listをスペースごとに分ける
    return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
    return max(map(max,x))

n = onem()

a = l()

b = l()

t = []

for i in range(n):
    if b[i] != a[i]:
        hq.heappush(t,(-b[i],i))
co = 0
while t:
    k = hq.heappop(t)
    k = (-k[0],k[1])
    q,w = divmod(k[0] - a[k[1]], b[(k[1] - 1) % n] + b[(k[1] + 1) % n])
    co += q
    if q == 0:
        print(-1)
        exit()
    w = a[k[1]] + w
    b[k[1]] = w
    if b[k[1]] != a[k[1]]:
        hq.heappush(t,(-w,k[1]))

print(co)

Fixed submission:
import heapq as hq
mod = 10**9+7
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())

def onon():
    n = onem()

    a = l()

    b = l()

    t = []

    for i in range(n):
        if b[i] != a[i]:
            hq.heappush(t,(-b[i],i))
    co = 0
    while t:
        k = hq.heappop(t)
        k = (-k[0],k[1])
        q,w = divmod(k[0] - a[k[1]], b[(k[1] - 1) % n] + b[(k[1] + 1) % n])
        co += q
        if q == 0:
            return -1
        w = a[k[1]] + w
        b[k[1]] = w
        if b[k[1]] != a[k[1]]:
            hq.heappush(t,(-w,k[1]))
    return co
print(onon())

Buggy python submission (verdict: Runtime Error):
import sys,heapq as hq
sys.setrecursionlimit(100000)
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())
def s(x): #圧縮
    a = []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): #listをスペースごとに分ける
    return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
    return max(map(max,x))

n = onem()

a = l()

b = l()

t = []

for i in range(n):
    if b[i] != a[i]:
        hq.heappush(t,(-b[i],i))
co = 0
while t:
    k = hq.heappop(t)
    k = (-k[0],k[1])
    if k[1] == 0:
        ppp = (k[0]-a[k[1]]) // (b[-1] + b[1])
        if ppp == 0:
            print(-1)
            exit()
        co += ppp
        k[0] = (k[0]-a[k[1]]) % (b[-1] + b[1]) + a[k[1]]
    elif k[1] == n-1:
        ppp = (k[0]-a[k[1]]) // (b[-2] + b[0])
        if ppp == 0:
            print(-1)
            exit()
        co += ppp
        k[0] = (k[0]-a[k[1]]) % (b[-2] + b[0]) + a[k[1]]
    else:
        ppp = (k[0]-a[k[1]]) // (b[k[1]+1] + b[k[1]-1])
        if ppp == 0:
            print(-1)
            exit()
        co += ppp
        k[0] = (k[0]-a[k[1]]) % (b[k[1]+1] + b[k[1]-1]) + a[k[1]]
    b[k[1]] = k[0]
    if b[k[1]] != a[k[1]]:
        hq.heappush(t,(-b[k[1]],k[1]))

print(co)

Fixed submission:
import heapq as hq
mod = 10**9+7
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())

def onon():
    n = onem()

    a = l()

    b = l()

    t = []

    for i in range(n):
        if b[i] != a[i]:
            hq.heappush(t,(-b[i],i))
    co = 0
    while t:
        k = hq.heappop(t)
        k = (-k[0],k[1])
        q,w = divmod(k[0] - a[k[1]], b[(k[1] - 1) % n] + b[(k[1] + 1) % n])
        co += q
        if q == 0:
            return -1
        w = a[k[1]] + w
        b[k[1]] = w
        if b[k[1]] != a[k[1]]:
            hq.heappush(t,(-w,k[1]))
    return co
print(onon())

Buggy python submission (verdict: Time Limit Exceeded):
import sys,heapq as hq
sys.setrecursionlimit(100000)
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())
def s(x): #圧縮
    a = []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): #listをスペースごとに分ける
    return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
    return max(map(max,x))

n = onem()

a = l()

b = l()

t = []

for i in range(n):
    if b[i] != a[i]:
        hq.heappush(t,[-b[i],i])
co = 0
while t:
    k = hq.heappop(t)
    k = [-k[0],k[1]]
    if k[1] == 0:
        ppp = (k[0]-a[k[1]]) // (b[-1] + b[1])
        if ppp == 0:
            print(-1)
            exit()
        co += ppp
        k[0] = (k[0]-a[k[1]]) % (b[-1] + b[1]) + a[k[1]]
    elif k[1] == n-1:
        ppp = (k[0]-a[k[1]]) // (b[-2] + b[0])
        if ppp == 0:
            print(-1)
            exit()
        co += ppp
        k[0] = (k[0]-a[k[1]]) % (b[-2] + b[0]) + a[k[1]]
    else:
        ppp = (k[0]-a[k[1]]) // (b[k[1]+1] + b[k[1]-1])
        if ppp == 0:
            print(-1)
            exit()
        co += ppp
        k[0] = (k[0]-a[k[1]]) % (b[k[1]+1] + b[k[1]-1]) + a[k[1]]
    b[k[1]] = k[0]
    if b[k[1]] != a[k[1]]:
        hq.heappush(t,[-b[k[1]],k[1]])

print(co)

Fixed submission:
import heapq as hq
mod = 10**9+7
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())

def onon():
    n = onem()

    a = l()

    b = l()

    t = []

    for i in range(n):
        if b[i] != a[i]:
            hq.heappush(t,(-b[i],i))
    co = 0
    while t:
        k = hq.heappop(t)
        k = (-k[0],k[1])
        q,w = divmod(k[0] - a[k[1]], b[(k[1] - 1) % n] + b[(k[1] + 1) % n])
        co += q
        if q == 0:
            return -1
        w = a[k[1]] + w
        b[k[1]] = w
        if b[k[1]] != a[k[1]]:
            hq.heappush(t,(-w,k[1]))
    return co
print(onon())

Buggy python submission (verdict: Time Limit Exceeded):
import sys,heapq as hq
sys.setrecursionlimit(100000)
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())
def s(x): #圧縮
    a = []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): #listをスペースごとに分ける
    return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
    return max(map(max,x))

n = onem()

a = l()

b = l()

t = []

for i in range(n):
    if b[i] != a[i]:
        hq.heappush(t,[-b[i],i])
hq.heapify(t)
co = 0
while t:
    k = hq.heappop(t)
    k = [-k[0],k[1]]
    if k[1] == 0:
        ppp = (k[0]-a[k[1]]) // (b[-1] + b[1])
        if ppp == 0:
            print(-1)
            exit()
        co += ppp
        k[0] = (k[0]-a[k[1]]) % (b[-1] + b[1]) + a[k[1]]
    elif k[1] == n-1:
        ppp = (k[0]-a[k[1]]) // (b[-2] + b[0])
        if ppp == 0:
            print(-1)
            exit()
        co += ppp
        k[0] = (k[0]-a[k[1]]) % (b[-2] + b[0]) + a[k[1]]
    else:
        ppp = (k[0]-a[k[1]]) // (b[k[1]+1] + b[k[1]-1])
        if ppp == 0:
            print(-1)
            exit()
        co += ppp
        k[0] = (k[0]-a[k[1]]) % (b[k[1]+1] + b[k[1]-1]) + a[k[1]]
    b[k[1]] = k[0]
    if b[k[1]] != a[k[1]] and len(t) != 0:
        hq.heappush(t,[-b[k[1]],k[1]])

print(co)

Fixed submission:
import heapq as hq
mod = 10**9+7
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())

def onon():
    n = onem()

    a = l()

    b = l()

    t = []

    for i in range(n):
        if b[i] != a[i]:
            hq.heappush(t,(-b[i],i))
    co = 0
    while t:
        k = hq.heappop(t)
        k = (-k[0],k[1])
        q,w = divmod(k[0] - a[k[1]], b[(k[1] - 1) % n] + b[(k[1] + 1) % n])
        co += q
        if q == 0:
            return -1
        w = a[k[1]] + w
        b[k[1]] = w
        if b[k[1]] != a[k[1]]:
            hq.heappush(t,(-w,k[1]))
    return co
print(onon())

Buggy python submission (verdict: Time Limit Exceeded):
import sys,collections as cl,bisect as bs,heapq as hq
sys.setrecursionlimit(100000)
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())
def s(x): #圧縮
    a = []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): #listをスペースごとに分ける
    return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
    return max(map(max,x))

n = onem()

a = l()

b = l()

t = []

for i in range(n):
    if b[i] != a[i]:
        hq.heappush(t,[-b[i],i])
hq.heapify(t)
co = 0
while t:
    k = hq.heappop(t)
    k = [-k[0],k[1]]
    if k[1] == 0:
        ppp = (k[0]-a[k[1]]) // (b[-1] + b[1])
        if ppp == 0:
            print(-1)
            exit()
        co += ppp
        k[0] = (k[0]-a[k[1]]) % (b[-1] + b[1]) + a[k[1]]
    elif k[1] == n-1:
        ppp = (k[0]-a[k[1]]) // (b[-2] + b[0])
        if ppp == 0:
            print(-1)
            exit()
        co += ppp
        k[0] = (k[0]-a[k[1]]) % (b[-2] + b[0]) + a[k[1]]
    else:
        ppp = (k[0]-a[k[1]]) // (b[k[1]+1] + b[k[1]-1])
        if ppp == 0:
            print(-1)
            exit()
        co += ppp
        k[0] = (k[0]-a[k[1]]) % (b[k[1]+1] + b[k[1]-1]) + a[k[1]]
    b[k[1]] = k[0]
    if b[k[1]] != a[k[1]] and len(t) != 0:
        hq.heappush(t,[-b[k[1]],k[1]])

print(co)

Fixed submission:
import heapq as hq
mod = 10**9+7
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())

def onon():
    n = onem()

    a = l()

    b = l()

    t = []

    for i in range(n):
        if b[i] != a[i]:
            hq.heappush(t,(-b[i],i))
    co = 0
    while t:
        k = hq.heappop(t)
        k = (-k[0],k[1])
        q,w = divmod(k[0] - a[k[1]], b[(k[1] - 1) % n] + b[(k[1] + 1) % n])
        co += q
        if q == 0:
            return -1
        w = a[k[1]] + w
        b[k[1]] = w
        if b[k[1]] != a[k[1]]:
            hq.heappush(t,(-w,k[1]))
    return co
print(onon())

Buggy python submission (verdict: Runtime Error):
import sys,collections as cl,bisect as bs,heapq as hq
sys.setrecursionlimit(100000)
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())
def s(x): #圧縮
    a = []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): #listをスペースごとに分ける
    return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
    return max(map(max,x))

n = onem()

a = l()

b = l()

t = []

for i in range(n):
    hq.heappush(t,[-b[i],i])
heapify(t)
co = 0
while t:
    k = hq.heappop(t)
    k = [-k[0],k[1]]
    if k[1] == 0:
        ppp = (k[0]-a[k[1]]) // (b[-1] + b[1])
        if ppp == 0:
            print(-1)
            exit()
        co += ppp
        k[0] = (k[0]-a[k[1]]) % (b[-1] + b[1]) + a[k[1]]
    elif k[1] == n-1:
        ppp = (k[0]-a[k[1]]) // (b[-2] + b[0])
        if ppp == 0:
            print(-1)
            exit()
        co += ppp
        k[0] = (k[0]-a[k[1]]) % (b[-2] + b[0]) + a[k[1]]
    else:
        ppp = (k[0]-a[k[1]]) // (b[k[1]+1] + b[k[1]-1])
        if ppp == 0:
            print(-1)
            exit()
        co += ppp
        k[0] = (k[0]-a[k[1]]) % (b[k[1]+1] + b[k[1]-1]) + a[k[1]]
    b[k[1]] = k[0]
    if b[k[1]] != a[k[1]] and len(t) != 0:
        hq.heappush(t,[-b[k[1]],k[1]])

print(co)

Fixed submission:
import heapq as hq
mod = 10**9+7
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())

def onon():
    n = onem()

    a = l()

    b = l()

    t = []

    for i in range(n):
        if b[i] != a[i]:
            hq.heappush(t,(-b[i],i))
    co = 0
    while t:
        k = hq.heappop(t)
        k = (-k[0],k[1])
        q,w = divmod(k[0] - a[k[1]], b[(k[1] - 1) % n] + b[(k[1] + 1) % n])
        co += q
        if q == 0:
            return -1
        w = a[k[1]] + w
        b[k[1]] = w
        if b[k[1]] != a[k[1]]:
            hq.heappush(t,(-w,k[1]))
    return co
print(onon())

Buggy python submission (verdict: Time Limit Exceeded):
import sys,collections as cl,bisect as bs,heapq as hq
sys.setrecursionlimit(100000)
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())
def s(x): #圧縮
    a = []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): #listをスペースごとに分ける
    return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
    return max(map(max,x))

n = onem()

a = l()

b = l()

t = []

for i in range(n):
    hq.heappush(t,[-b[i],i])

co = 0
while True:
    if len(t) == 0:
        break
    k = hq.heappop(t)
    co = co + 1
    k = [-k[0],k[1]]
    if k[1] == 0:
        k[0] -= b[-1] + b[1]
    elif k[1] == n-1:
        k[0] -= b[0] + b[-2]
    else:
        k[0] -= b[k[1]+1] + b[k[1]-1]
    b[k[1]] = k[0]
    if b[k[1]] < a[k[1]]:
        print(-1)
        exit()
    if b[k[1]] != a[k[1]]:
        hq.heappush(t,[-b[k[1]],k[1]])

print(co)

Fixed submission:
import heapq as hq
mod = 10**9+7
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())

def onon():
    n = onem()

    a = l()

    b = l()

    t = []

    for i in range(n):
        if b[i] != a[i]:
            hq.heappush(t,(-b[i],i))
    co = 0
    while t:
        k = hq.heappop(t)
        k = (-k[0],k[1])
        q,w = divmod(k[0] - a[k[1]], b[(k[1] - 1) % n] + b[(k[1] + 1) % n])
        co += q
        if q == 0:
            return -1
        w = a[k[1]] + w
        b[k[1]] = w
        if b[k[1]] != a[k[1]]:
            hq.heappush(t,(-w,k[1]))
    return co
print(onon())

Buggy python submission (verdict: Time Limit Exceeded):
import sys,collections as cl,bisect as bs,heapq as hq
sys.setrecursionlimit(100000)
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())
def s(x): #圧縮
    a = []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): #listをスペースごとに分ける
    return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
    return max(map(max,x))

n = onem()

a = l()

b = l()

t = []

for i in range(n):
    hq.heappush(t,[-b[i],i])

co = 0
while True:
    if len(t) == 0:
        break
    k = hq.heappop(t)
    co = co + 1
    k = [-k[0],k[1]]
    if k[1] == 0:
        k[0] -= b[-1] + b[1]
    elif k[1] == n-1:
        k[0] -= b[0] + b[-2]
    else:
        k[0] -= b[k[1]+1] + b[k[1]-1]
    b[k[1]] = k[0]
    if b[k[1]] < a[k[1]]:
        print(-1)
        exit()
    if b[k[1]] != a[k[1]]:
        hq.heappush(t,[-b[k[1]],k[1]])
print(co)

Fixed submission:
import heapq as hq
mod = 10**9+7
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())

def onon():
    n = onem()

    a = l()

    b = l()

    t = []

    for i in range(n):
        if b[i] != a[i]:
            hq.heappush(t,(-b[i],i))
    co = 0
    while t:
        k = hq.heappop(t)
        k = (-k[0],k[1])
        q,w = divmod(k[0] - a[k[1]], b[(k[1] - 1) % n] + b[(k[1] + 1) % n])
        co += q
        if q == 0:
            return -1
        w = a[k[1]] + w
        b[k[1]] = w
        if b[k[1]] != a[k[1]]:
            hq.heappush(t,(-w,k[1]))
    return co
print(onon())