Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

def resolve():
    n = int(input())
    aa = list(map(int, input().split()))
    bb = list(map(int, input().split()))

    q = deque()
    diffs = set()

    for i in range(n):
        if bb[i] != aa[i]:
            diffs.add(i)

        bef_i = i-1 if i > 0 else n-1
        aft_i = i+1 if i < n-1 else 0
        if bb[i] > aa[i] and bb[i] > bb[bef_i] + bb[aft_i]:
            q.append(i)

    count = 0

    while len(q) > 0:
        i = q.popleft()

        bef_i = i-1 if i > 0 else n-1
        bef2_i = bef_i-1 if bef_i > 0 else n-1
        aft_i = i+1 if i < n-1 else 0
        aft2_i = aft_i+1 if aft_i < n-1 else 0

        while bb[i] > aa[i] and bb[i] > bb[bef_i] + bb[aft_i]:
            bb[i] = bb[i] - bb[bef_i] - bb[aft_i]
            count += 1

        if bb[i] == aa[i]:
            diffs.remove(i)

        if bb[bef_i] > aa[bef_i] and bb[bef_i] > bb[bef2_i] + bb[i]:
            q.append(bef_i)

        if bb[aft_i] > aa[aft_i] and bb[aft_i] > bb[aft2_i] + bb[i]:
            q.append(aft_i)

    if len(diffs) > 0:
        print(-1)
        return

    print(count)
resolve()

Fixed submission:
from collections import deque

def resolve():
    n = int(input())
    aa = list(map(int, input().split()))
    bb = list(map(int, input().split()))

    q = deque()
    diffs = set()

    for i in range(n):
        if bb[i] != aa[i]:
            diffs.add(i)

        bef_i = i-1 if i > 0 else n-1
        aft_i = i+1 if i < n-1 else 0
        if bb[i] > aa[i] and bb[i] > bb[bef_i] + bb[aft_i]:
            q.append(i)

    count = 0

    while len(q) > 0:
        i = q.popleft()

        bef_i = i-1 if i > 0 else n-1
        bef2_i = bef_i-1 if bef_i > 0 else n-1
        aft_i = i+1 if i < n-1 else 0
        aft2_i = aft_i+1 if aft_i < n-1 else 0

        count += (bb[i] - aa[i]) // (bb[bef_i] + bb[aft_i])
        bb[i] = (bb[i] - aa[i]) % (bb[bef_i] + bb[aft_i]) + aa[i]

        if bb[i] == aa[i]:
            diffs.remove(i)

        if bb[bef_i] > aa[bef_i] and bb[bef_i] > bb[bef2_i] + bb[i]:
            q.append(bef_i)

        if bb[aft_i] > aa[aft_i] and bb[aft_i] > bb[aft2_i] + bb[i]:
            q.append(aft_i)

    if len(diffs) > 0:
        print(-1)
        return

    print(count)
resolve()

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

def resolve():
    n = int(input())
    aa = list(map(int, input().split()))
    bb = list(map(int, input().split()))

    q = deque()
    diffs = set()

    for i in range(n):
        if bb[i] < aa[i]:
            print(-1)
            return

        if bb[i] != aa[i]:
            diffs.add(i)

        bef_i = i-1 if i > 0 else n-1
        aft_i = i+1 if i < n-1 else 0
        if bb[i] > aa[i] and bb[i] > bb[bef_i] + bb[aft_i]:
            q.append(i)

    count = 0

    while len(q) > 0:
        i = q.popleft()

        bef_i = i-1 if i > 0 else n-1
        bef2_i = bef_i-1 if bef_i > 0 else n-1
        aft_i = i+1 if i < n-1 else 0
        aft2_i = aft_i+1 if aft_i < n-1 else 0

        bb[i] = bb[i] - bb[bef_i] - bb[aft_i]
        count += 1

        if bb[i] == aa[i]:
            diffs.remove(i)

        if bb[i] < aa[i]:
            print(-1)
            return

        if bb[i] > aa[i] and bb[i] > bb[bef_i] + bb[aft_i]:
            q.append(i)

        if bb[bef_i] > aa[bef_i] and bb[bef_i] > bb[bef2_i] + bb[i]:
            q.append(bef_i)

        if bb[aft_i] > aa[aft_i] and bb[aft_i] > bb[aft2_i] + bb[i]:
            q.append(aft_i)

    if len(diffs) > 0:
        print(-1)
        return

    print(count)
resolve()

Fixed submission:
from collections import deque

def resolve():
    n = int(input())
    aa = list(map(int, input().split()))
    bb = list(map(int, input().split()))

    q = deque()
    diffs = set()

    for i in range(n):
        if bb[i] != aa[i]:
            diffs.add(i)

        bef_i = i-1 if i > 0 else n-1
        aft_i = i+1 if i < n-1 else 0
        if bb[i] > aa[i] and bb[i] > bb[bef_i] + bb[aft_i]:
            q.append(i)

    count = 0

    while len(q) > 0:
        i = q.popleft()

        bef_i = i-1 if i > 0 else n-1
        bef2_i = bef_i-1 if bef_i > 0 else n-1
        aft_i = i+1 if i < n-1 else 0
        aft2_i = aft_i+1 if aft_i < n-1 else 0

        count += (bb[i] - aa[i]) // (bb[bef_i] + bb[aft_i])
        bb[i] = (bb[i] - aa[i]) % (bb[bef_i] + bb[aft_i]) + aa[i]

        if bb[i] == aa[i]:
            diffs.remove(i)

        if bb[bef_i] > aa[bef_i] and bb[bef_i] > bb[bef2_i] + bb[i]:
            q.append(bef_i)

        if bb[aft_i] > aa[aft_i] and bb[aft_i] > bb[aft2_i] + bb[i]:
            q.append(aft_i)

    if len(diffs) > 0:
        print(-1)
        return

    print(count)
resolve()

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

def resolve():
    n = int(input())
    aa = list(map(int, input().split()))
    bb = list(map(int, input().split()))

    q = deque()

    for i in range(n):
        bef_i = i-1 if i > 0 else n-1
        aft_i = i+1 if i < n-1 else 0
        if bb[i] > aa[i] and bb[i] > bb[bef_i] + bb[aft_i]:
            q.append(i)

    count = 0

    while len(q) > 0:
        i = q.popleft()

        bef_i = i-1 if i > 0 else n-1
        bef2_i = bef_i-1 if bef_i > 0 else n-1
        aft_i = i+1 if i < n-1 else 0
        aft2_i = aft_i+1 if aft_i < n-1 else 0

        bb[i] = bb[i] - bb[bef_i] - bb[aft_i]
        count += 1

        if bb[i] > aa[i] and bb[i] > bb[bef_i] + bb[aft_i]:
            q.append(i)

        if bb[bef_i] > aa[bef_i] and bb[bef_i] > bb[bef2_i] + bb[i]:
            q.append(bef_i)

        if bb[aft_i] > aa[aft_i] and bb[aft_i] > bb[aft2_i] + bb[i]:
            q.append(aft_i)

    for i in range(n):
        if aa[i] != bb[i]:
            print(-1)
            return

    print(count)
resolve()

Fixed submission:
from collections import deque

def resolve():
    n = int(input())
    aa = list(map(int, input().split()))
    bb = list(map(int, input().split()))

    q = deque()
    diffs = set()

    for i in range(n):
        if bb[i] != aa[i]:
            diffs.add(i)

        bef_i = i-1 if i > 0 else n-1
        aft_i = i+1 if i < n-1 else 0
        if bb[i] > aa[i] and bb[i] > bb[bef_i] + bb[aft_i]:
            q.append(i)

    count = 0

    while len(q) > 0:
        i = q.popleft()

        bef_i = i-1 if i > 0 else n-1
        bef2_i = bef_i-1 if bef_i > 0 else n-1
        aft_i = i+1 if i < n-1 else 0
        aft2_i = aft_i+1 if aft_i < n-1 else 0

        count += (bb[i] - aa[i]) // (bb[bef_i] + bb[aft_i])
        bb[i] = (bb[i] - aa[i]) % (bb[bef_i] + bb[aft_i]) + aa[i]

        if bb[i] == aa[i]:
            diffs.remove(i)

        if bb[bef_i] > aa[bef_i] and bb[bef_i] > bb[bef2_i] + bb[i]:
            q.append(bef_i)

        if bb[aft_i] > aa[aft_i] and bb[aft_i] > bb[aft2_i] + bb[i]:
            q.append(aft_i)

    if len(diffs) > 0:
        print(-1)
        return

    print(count)
resolve()