Buggy python submission (verdict: Time Limit Exceeded):
def p_c():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    l = [-1] * N
    idx = set(range(N))

    for i in range(N):
        l[i] = B[i - 1] + B[(i + 1) % N]

    ans = 0

    while 1:
        f = False
        for i in list(idx):
            if A[i] <= B[i] - l[i]:
                f = True
                ans += 1
                B[i] -= l[i]
                l[i - 1] -= l[i]
                l[(i + 1) % N] -= l[i]
                idx.add((N + i - 1) % N)
                idx.add((i + 1) % N)
            else:
                idx.remove(i)
        if not f:
            break

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


if __name__ == '__main__':
    p_c()


Fixed submission:
def p_c():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    l = [-1] * N

    for i in range(N):
        l[i] = B[i - 1] + B[(i + 1) % N]

    ans = 0

    while 1:
        f = False
        for i in range(N):
            if A[i] <= B[i] - l[i]:
                f = True
                n = (B[i] - A[i]) // l[i]
                ans += n
                B[i] -= n * l[i]
                l[i - 1] -= n * l[i]
                l[(i + 1) % N] -= n * l[i]
        if not f:
            break

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


if __name__ == '__main__':
    p_c()

Buggy python submission (verdict: Time Limit Exceeded):
def p_c():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    l = [-1] * N

    for i in range(N):
        l[i] = B[i - 1] + B[(i + 1) % N]

    ans = 0

    while 1:
        f = False
        for i in range(N):
            if A[i] <= B[i] - l[i]:
                f = True
                ans += 1
                B[i] -= l[i]
                l[i - 1] -= l[i]
                l[(i + 1) % N] -= l[i]
        if not f:
            break

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


if __name__ == '__main__':
    p_c()


Fixed submission:
def p_c():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    l = [-1] * N

    for i in range(N):
        l[i] = B[i - 1] + B[(i + 1) % N]

    ans = 0

    while 1:
        f = False
        for i in range(N):
            if A[i] <= B[i] - l[i]:
                f = True
                n = (B[i] - A[i]) // l[i]
                ans += n
                B[i] -= n * l[i]
                l[i - 1] -= n * l[i]
                l[(i + 1) % N] -= n * l[i]
        if not f:
            break

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


if __name__ == '__main__':
    p_c()

Buggy python submission (verdict: Time Limit Exceeded):
def p_c():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))

    ans = 0

    while 1:
        f = False
        for i in range(N):
            adv = B[i-1] + B[(i+1) % N]
            if A[i] <= B[i] - adv:
                f = True
                ans += 1
                B[i] -= adv
        if not f:
            break

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


if __name__ == '__main__':
    p_c()


Fixed submission:
def p_c():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    l = [-1] * N

    for i in range(N):
        l[i] = B[i - 1] + B[(i + 1) % N]

    ans = 0

    while 1:
        f = False
        for i in range(N):
            if A[i] <= B[i] - l[i]:
                f = True
                n = (B[i] - A[i]) // l[i]
                ans += n
                B[i] -= n * l[i]
                l[i - 1] -= n * l[i]
                l[(i + 1) % N] -= n * l[i]
        if not f:
            break

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


if __name__ == '__main__':
    p_c()