Buggy python submission (verdict: Time Limit Exceeded):
from heapq import heappop,heappush
def main(a,b):
    for i in range(n):
        if b[i]<=a[i]:
            pass
        else:
            print(-1)
            exit()
    pq=[]
    [heappush(pq,[-x,i]) for i,x in enumerate(a)]
    ans=0
    while pq:
        v,i=heappop(pq)
        v=-v
        vv=v
        v-=b[i]
        tmp=a[(i-1)%n]+a[(i+1)%n]
        ans+=v//tmp
        v%=tmp
        v+=b[i]
        a[i]=v
        if v==b[i]:continue
        elif vv<=v or v<b[i]:
            print(-1)
            exit()
        heappush(pq,[-v,i])
    print(ans)
if __name__=='__main__':
    n=int(input())
    b=list(map(int,input().split()))
    a=list(map(int,input().split()))
    main(a,b)


Fixed submission:
def main(n,a,b):
    ans=0
    while True:
        cnt=0
        for i in range(n):
            m=a[(i-1)%n]+a[(i+1)%n]
            if m<a[i] and b[i]<a[i]:
                a[i]-=b[i]
                cnt+=a[i]//m
                a[i]%=m
                a[i]+=b[i]
        if cnt==0:break
        ans+=cnt
    if a==b:
        print(ans)
    else:
        print(-1)
if __name__=='__main__':
    n=int(input())
    b=list(map(int,input().split()))
    a=list(map(int,input().split()))
    main(n,a,b)

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

def main(a,b):
    for i in range(n):
        if b[i]<=a[i]:
            pass
        else:
            print(-1)
            exit()
    pq=[]
    [heappush(pq,[-x,i]) for i,x in enumerate(a)]
    ans=0
    while pq:
        v,i=heappop(pq)
        v=-v
        vv=v
        v-=b[i]
        tmp=a[(i-1)%n]+a[(i+1)%n]
        ans+=v//tmp
        v%=tmp
        v+=b[i]
        a[i]=v
        if v==b[i]:continue
        elif vv<=v or v<b[i]:
            print(-1)
            exit()
        heappush(pq,[-v,i])
    print(ans)
if __name__=='__main__':
    n=int(input())
    b=list(map(int,input().split()))
    a=list(map(int,input().split()))
    main(a,b)


Fixed submission:
def main(n,a,b):
    ans=0
    while True:
        cnt=0
        for i in range(n):
            m=a[(i-1)%n]+a[(i+1)%n]
            if m<a[i] and b[i]<a[i]:
                a[i]-=b[i]
                cnt+=a[i]//m
                a[i]%=m
                a[i]+=b[i]
        if cnt==0:break
        ans+=cnt
    if a==b:
        print(ans)
    else:
        print(-1)
if __name__=='__main__':
    n=int(input())
    b=list(map(int,input().split()))
    a=list(map(int,input().split()))
    main(n,a,b)

Buggy python submission (verdict: Time Limit Exceeded):
import heapq
class pqheap:
    def __init__(self):
        self.p = list()
        self.q = list()
    def insert(self,x):
        heapq.heappush(self.p, x)
        return
    def erase(self,x):
        heapq.heappush(self.q, x)
        return
    def minimum(self):
        while self.q and self.p[0] == self.q[0]:
            heapq.heappop(self.p)
            heapq.heappop(self.q)
        return self.p[0]
#print(a)
def main(a,b):
    for i in range(n):
        if b[i]<=a[i]:
            pass
        else:
            print(-1)
            exit()
    pq=pqheap()
    [pq.insert([-x,i]) for i,x in enumerate(a)]
    ans=0
    while len(pq.p)>len(pq.q):
        m=pq.minimum()
        pq.erase(m)
        v,i=m
        v=-v
        vv=v
        v-=b[i]
        tmp=a[(i-1)%n]+a[(i+1)%n]
        ans+=v//tmp
        v%=tmp
        v+=b[i]
        a[i]=v
        if v==b[i]:continue
        elif vv<=v or v<b[i]:
            print(-1)
            exit()
        pq.insert([-v,i])
    print(ans)
if __name__=='__main__':
    n=int(input())
    b=list(map(int,input().split()))
    a=list(map(int,input().split()))
    main(a,b)


Fixed submission:
def main(n,a,b):
    ans=0
    while True:
        cnt=0
        for i in range(n):
            m=a[(i-1)%n]+a[(i+1)%n]
            if m<a[i] and b[i]<a[i]:
                a[i]-=b[i]
                cnt+=a[i]//m
                a[i]%=m
                a[i]+=b[i]
        if cnt==0:break
        ans+=cnt
    if a==b:
        print(ans)
    else:
        print(-1)
if __name__=='__main__':
    n=int(input())
    b=list(map(int,input().split()))
    a=list(map(int,input().split()))
    main(n,a,b)

Buggy python submission (verdict: Time Limit Exceeded):
import heapq
class pqheap:
    def __init__(self):
        self.p = list()
        self.q = list()
    def insert(self,x):
        heapq.heappush(self.p, x)
        return
    def erase(self,x):
        heapq.heappush(self.q, x)
        return
    def minimum(self):
        while self.q and self.p[0] == self.q[0]:
            heapq.heappop(self.p)
            heapq.heappop(self.q)
        return self.p[0]
n=int(input())
b=list(map(int,input().split()))
a=list(map(int,input().split()))
#print(a)
for i in range(n):
    if b[i]<=a[i]:
        pass
    else:
        print(-1)
        exit()
pq=pqheap()
[pq.insert([-x,i]) for i,x in enumerate(a)]
ans=0
while len(pq.p)>len(pq.q):
    m=pq.minimum()
    pq.erase(m)
    v,i=m
    v=-v
    vv=v
    v-=b[i]
    tmp=a[(i-1)%n]+a[(i+1)%n]
    ans+=v//tmp
    v%=tmp
    v+=b[i]
    a[i]=v
    if v==b[i]:continue
    elif vv<=v or v<b[i]:
        print(-1)
        exit()
    pq.insert([-v,i])
print(ans)

"""
3
1 1 1
1000 10000 100000
"""

Fixed submission:
def main(n,a,b):
    ans=0
    while True:
        cnt=0
        for i in range(n):
            m=a[(i-1)%n]+a[(i+1)%n]
            if m<a[i] and b[i]<a[i]:
                a[i]-=b[i]
                cnt+=a[i]//m
                a[i]%=m
                a[i]+=b[i]
        if cnt==0:break
        ans+=cnt
    if a==b:
        print(ans)
    else:
        print(-1)
if __name__=='__main__':
    n=int(input())
    b=list(map(int,input().split()))
    a=list(map(int,input().split()))
    main(n,a,b)

Buggy python submission (verdict: Time Limit Exceeded):
import heapq
class pqheap:
    def __init__(self):
        self.p = list()
        self.q = list()

    def insert(self,x):
        heapq.heappush(self.p, x)
        return

    def erase(self,x):
        heapq.heappush(self.q, x)
        return

    def minimum(self):
        while self.q and self.p[0] == self.q[0]:
            heapq.heappop(self.p)
            heapq.heappop(self.q)
        return self.p[0]
n=int(input())
b=list(map(int,input().split()))
a=list(map(int,input().split()))
pq=pqheap()
[pq.insert([-x,i]) for i,x in enumerate(a)]
ans=0
while len(pq.p)>len(pq.q):
    m=pq.minimum()
    pq.erase(m)
    v,i=m
    v=-v
    if v==b[i]:continue
    v-=b[i]    
    tmp=a[(i-1)%n]+a[(i+1)%n]
    if v//tmp==0:
        print(-1)
        exit()
    ans+=v//tmp
    v%=tmp
    v+=b[i]
    a[i]=v
    if v<b[i]:
        print(-1)
        exit()
    pq.insert([-v,i])
print(ans)

"""
3
1 1 1
1000 10000 100000
"""

Fixed submission:
def main(n,a,b):
    ans=0
    while True:
        cnt=0
        for i in range(n):
            m=a[(i-1)%n]+a[(i+1)%n]
            if m<a[i] and b[i]<a[i]:
                a[i]-=b[i]
                cnt+=a[i]//m
                a[i]%=m
                a[i]+=b[i]
        if cnt==0:break
        ans+=cnt
    if a==b:
        print(ans)
    else:
        print(-1)
if __name__=='__main__':
    n=int(input())
    b=list(map(int,input().split()))
    a=list(map(int,input().split()))
    main(n,a,b)

Buggy python submission (verdict: Time Limit Exceeded):
import heapq
class pqheap:
    def __init__(self):
        self.p = list()
        self.q = list()

    def insert(self,x):
        heapq.heappush(self.p, x)
        return

    def erase(self,x):
        heapq.heappush(self.q, x)
        return

    def minimum(self):
        while self.q and self.p[0] == self.q[0]:
            heapq.heappop(self.p)
            heapq.heappop(self.q)
        return self.p[0]
n=int(input())
b=list(map(int,input().split()))
a=list(map(int,input().split()))
pq=pqheap()
[pq.insert([-x,i]) for i,x in enumerate(a)]
ans=0
while len(pq.p)>len(pq.q):
    m=pq.minimum()
    pq.erase(m)
    v,i=m
    v=-v
    if v==b[i]:continue
    v-=b[i]    
    tmp=a[(i-1)%n]+a[(i+1)%n]
    ans+=v//tmp
    v%=tmp
    v+=b[i]
    a[i]=v
    if v<b[i]:
        print(-1)
        exit()
    pq.insert([-v,i])
print(ans)


Fixed submission:
def main(n,a,b):
    ans=0
    while True:
        cnt=0
        for i in range(n):
            m=a[(i-1)%n]+a[(i+1)%n]
            if m<a[i] and b[i]<a[i]:
                a[i]-=b[i]
                cnt+=a[i]//m
                a[i]%=m
                a[i]+=b[i]
        if cnt==0:break
        ans+=cnt
    if a==b:
        print(ans)
    else:
        print(-1)
if __name__=='__main__':
    n=int(input())
    b=list(map(int,input().split()))
    a=list(map(int,input().split()))
    main(n,a,b)

Buggy python submission (verdict: Time Limit Exceeded):
import heapq
class pqheap:
    def __init__(self):
        self.p = list()
        self.q = list()

    def insert(self,x):
        heapq.heappush(self.p, x)
        return

    def erase(self,x):
        heapq.heappush(self.q, x)
        return

    def minimum(self):
        while self.q and self.p[0] == self.q[0]:
            heapq.heappop(self.p)
            heapq.heappop(self.q)
        return self.p[0]
n=int(input())
b=list(map(int,input().split()))
a=list(map(int,input().split()))
pq=pqheap()
[pq.insert([-x,i]) for i,x in enumerate(a)]
ans=0
while len(pq.p)>len(pq.q):
    m=pq.minimum()
    pq.erase(m)
    v,i=m
    v=-v
    if v==b[i]:continue
    v-=a[(i-1)%n]
    v-=a[(i+1)%n]
    if v<b[i]:
        print(-1)
        exit()
    pq.insert([-v,i])
    a[i]=v
    ans+=1
print(ans)





Fixed submission:
def main(n,a,b):
    ans=0
    while True:
        cnt=0
        for i in range(n):
            m=a[(i-1)%n]+a[(i+1)%n]
            if m<a[i] and b[i]<a[i]:
                a[i]-=b[i]
                cnt+=a[i]//m
                a[i]%=m
                a[i]+=b[i]
        if cnt==0:break
        ans+=cnt
    if a==b:
        print(ans)
    else:
        print(-1)
if __name__=='__main__':
    n=int(input())
    b=list(map(int,input().split()))
    a=list(map(int,input().split()))
    main(n,a,b)

Buggy python submission (verdict: Time Limit Exceeded):
import heapq
class pqheap:
    def __init__(self):
        self.p = list()
        self.q = list()

    def insert(self,x):
        heapq.heappush(self.p, x)
        return

    def erase(self,x):
        heapq.heappush(self.q, x)
        return

    def minimum(self):
        while self.q and self.p[0] == self.q[0]:
            heapq.heappop(self.p)
            heapq.heappop(self.q)
        return self.p[0]
n=int(input())
b=list(map(int,input().split()))
a=list(map(int,input().split()))
pq=pqheap()
[pq.insert([-x,i]) for i,x in enumerate(a)]
ans=0
while len(pq.p)>len(pq.q):
    m=pq.minimum()
    pq.erase(m)
    v,i=m
    v=-v
    if v==b[i]:continue
    v-=a[(i-1)%n]
    v-=a[(i+1)%n]
    if v<=0:
        print(-1)
        exit()
    pq.insert([-v,i])
    a[i]=v
    ans+=1
print(ans)





Fixed submission:
def main(n,a,b):
    ans=0
    while True:
        cnt=0
        for i in range(n):
            m=a[(i-1)%n]+a[(i+1)%n]
            if m<a[i] and b[i]<a[i]:
                a[i]-=b[i]
                cnt+=a[i]//m
                a[i]%=m
                a[i]+=b[i]
        if cnt==0:break
        ans+=cnt
    if a==b:
        print(ans)
    else:
        print(-1)
if __name__=='__main__':
    n=int(input())
    b=list(map(int,input().split()))
    a=list(map(int,input().split()))
    main(n,a,b)