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

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

def solve() :
    h = []
    for i in range(N) :
        if A[i] == B[i] :
            continue
        heappush(h, (-B[i], i))

    ret = 0
    while len(h) > 1 :
        _, i = heappop(h)
        
        if B[i] > A[i] :
            m, M = B[(i-1)%N], B[(i+1)%N]
            if m > M :
                m, M = M, m
            
            x = (B[i] - B[h[0][1]] + m + M - 1) // (m + M)
            B[i] -= x * (m + M)
            ret += x
        
        if A[i] > B[i] :
            return -1
        elif A[i] < B[i] :
            heappush(h, (-B[i], i))
    
    _, i = heappop(h)
    if (B[i] - A[i]) % (B[(i-1)%N] + B[(i+1)%N]) == 0 :
        ret += (B[i] - A[i]) // (B[(i-1)%N] + B[(i+1)%N])
    else :
        return -1
    
    return ret
    
print(solve())

Fixed submission:
from heapq import heappush, heappop

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

def solve() :
    h = []
    for i in range(N) :
        if A[i] == B[i] :
            continue
        heappush(h, (-B[i], i))

    ret = 0
    while h :
        _, i = heappop(h)
       
        m, M = B[(i-1)%N], B[(i+1)%N]
        if m > M :
            m, M = M, m
        
        if M >= A[i] :
            x = (B[i] + m - 1) // (m + M)
            B[i] -= x * (m + M)
            ret += x
        else :
            if (B[i] - A[i]) % (m + M) != 0 :
                return -1
            ret += (B[i] - A[i]) // (m + M)
            B[i] = A[i]
        
        if A[i] > B[i] :
            return -1
            
        elif A[i] < B[i] :
            heappush(h, (-B[i], i))
    
    return ret
    
print(solve())

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

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

def solve() :
    h = []
    for i in range(N) :
        if A[i] == B[i] :
            continue
        heappush(h, (-B[i], i))

    ret = 0
    while h :
        _, i = heappop(h)
        while B[i] > B[(i-1)%N] and B[i] > B[(i+1)%N] and B[i] > A[i] :
            B[i] -= B[(i-1)%N] + B[(i+1)%N]
            ret += 1
            
        if B[i] == A[i] :
            continue
        elif B[i] < A[i] :
            return -1
        else :
            heappush(h, (-B[i], i))
        
    return ret
    
print(solve())

Fixed submission:
from heapq import heappush, heappop

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

def solve() :
    h = []
    for i in range(N) :
        if A[i] == B[i] :
            continue
        heappush(h, (-B[i], i))

    ret = 0
    while h :
        _, i = heappop(h)
       
        m, M = B[(i-1)%N], B[(i+1)%N]
        if m > M :
            m, M = M, m
        
        if M >= A[i] :
            x = (B[i] + m - 1) // (m + M)
            B[i] -= x * (m + M)
            ret += x
        else :
            if (B[i] - A[i]) % (m + M) != 0 :
                return -1
            ret += (B[i] - A[i]) // (m + M)
            B[i] = A[i]
        
        if A[i] > B[i] :
            return -1
            
        elif A[i] < B[i] :
            heappush(h, (-B[i], i))
    
    return ret
    
print(solve())

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

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

def solve() :
    h = []
    for i in range(N) :
        if A[i] == B[i] :
            continue
        heappush(h, (-B[i], i))

    ret = 0
    while h :
        ret += 1
        _, i = heappop(h)
        B[i] -= B[(i-1)%N] + B[(i+1)%N]
        if B[i] == A[i] :
            continue
        elif B[i] < A[i] :
            return -1
        else :
            heappush(h, (-B[i], i))
        
    return ret
    
print(solve())

Fixed submission:
from heapq import heappush, heappop

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

def solve() :
    h = []
    for i in range(N) :
        if A[i] == B[i] :
            continue
        heappush(h, (-B[i], i))

    ret = 0
    while h :
        _, i = heappop(h)
       
        m, M = B[(i-1)%N], B[(i+1)%N]
        if m > M :
            m, M = M, m
        
        if M >= A[i] :
            x = (B[i] + m - 1) // (m + M)
            B[i] -= x * (m + M)
            ret += x
        else :
            if (B[i] - A[i]) % (m + M) != 0 :
                return -1
            ret += (B[i] - A[i]) // (m + M)
            B[i] = A[i]
        
        if A[i] > B[i] :
            return -1
            
        elif A[i] < B[i] :
            heappush(h, (-B[i], i))
    
    return ret
    
print(solve())