Buggy python submission (verdict: Time Limit Exceeded):
words = lambda t : list(map(t, input().split()))



n = int(input())
a = words(int)
b = words(int)

def getNext(i):
    if i == n-1:
        return 0
    else:
        return i+1

def getPrev(i):
    if i == 0:
        return n-1
    else:
        return i-1

import queue
q = queue.Queue()

def verify(i):
    if b[i] != a[i] and b[i] - (b[getNext(i)] + b[getPrev(i)]) >= a[i]:
        return True
    else:
        return False

for i in range(len(b)):
    if b[i] >= a[i] and verify(i):
        q.put(i)

ans = 0
succeed = True
while not q.empty():
    i = q.get()
    ni = getNext(i)
    pi = getPrev(i)
    #print(i, b)
    d = b[ni] + b[pi]
    if b[i] % d == a[i] % d:
        ans += b[i] // d - (a[i] // d)
        b[i] = a[i]
    else:
        ans += b[i] // d
        b[i] %= d
    if b[i] < a[i]:
        succeed = False
        break

    if verify(ni):
        q.put(ni)
    if verify(pi):
        q.put(pi)

for i in range(len(b)):
    if a[i] != b[i]:
        succeed = False
        break

if succeed:
    print(ans)
else:
    print(-1)


Fixed submission:
words = lambda t : list(map(t, input().split()))



n = int(input())
a = words(int)
b = words(int)

def getNext(i):
    if i == n-1:
        return 0
    else:
        return i+1

def getPrev(i):
    if i == 0:
        return n-1
    else:
        return i-1

from collections import deque
q = deque()

def verify(i):
    if b[i] != a[i] and b[i] - (b[getNext(i)] + b[getPrev(i)]) >= a[i]:
        return True
    else:
        return False

for i in range(len(b)):
    if b[i] >= a[i] and verify(i):
        q.append(i)

ans = 0
succeed = True
while not len(q) == 0:
    i = q.popleft()
    ni = getNext(i)
    pi = getPrev(i)
    #print(i, b)
    d = b[ni] + b[pi]
    if b[i] % d == a[i] % d:
        ans += b[i] // d - (a[i] // d)
        b[i] = a[i]
    else:
        ans += b[i] // d
        b[i] %= d
    if b[i] < a[i]:
        succeed = False
        break
    if verify(ni):
        q.append(ni)
    if verify(pi):
        q.append(pi)

for i in range(len(b)):
    if a[i] != b[i]:
        succeed = False
        break

if succeed:
    print(ans)
else:
    print(-1)

Buggy python submission (verdict: Time Limit Exceeded):
words = lambda t : list(map(t, input().split()))

def getNext(xs, i):
    if i == len(xs)-1:
        return 0
    else:
        return i+1

def getPrev(xs, i):
    if i == 0:
        return len(xs)-1
    else:
        return i-1

n = int(input())
a = words(int)
b = words(int)

import queue
q = queue.Queue()

def verify(i):
    if b[i] != a[i] and b[i] - (b[getNext(b,i)] + b[getPrev(b,i)]) >= a[i]:
        return True
    else:
        return False

for i in range(len(b)):
    if b[i] >= a[i] and verify(i):
        q.put(i)

ans = 0
succeed = True
while not q.empty():
    i = q.get()
    #print(i, b)
    d = b[getNext(b,i)] + b[getPrev(b,i)]
    if b[i] % d == a[i] % d:
        ans += b[i] // d - (a[i] // d)
        b[i] = a[i]
    else:
        ans += b[i] // d
        b[i] = b[i] % d
    if b[i] < a[i]:
        succeed = False
        break
    if verify(getNext(b,i)):
        q.put(getNext(b,i))
    if verify(getPrev(b,i)):
        q.put(getPrev(b,i))

for i in range(len(b)):
    if a[i] != b[i]:
        succeed = False
        break

if succeed:
    print(ans)
else:
    print(-1)


Fixed submission:
words = lambda t : list(map(t, input().split()))



n = int(input())
a = words(int)
b = words(int)

def getNext(i):
    if i == n-1:
        return 0
    else:
        return i+1

def getPrev(i):
    if i == 0:
        return n-1
    else:
        return i-1

from collections import deque
q = deque()

def verify(i):
    if b[i] != a[i] and b[i] - (b[getNext(i)] + b[getPrev(i)]) >= a[i]:
        return True
    else:
        return False

for i in range(len(b)):
    if b[i] >= a[i] and verify(i):
        q.append(i)

ans = 0
succeed = True
while not len(q) == 0:
    i = q.popleft()
    ni = getNext(i)
    pi = getPrev(i)
    #print(i, b)
    d = b[ni] + b[pi]
    if b[i] % d == a[i] % d:
        ans += b[i] // d - (a[i] // d)
        b[i] = a[i]
    else:
        ans += b[i] // d
        b[i] %= d
    if b[i] < a[i]:
        succeed = False
        break
    if verify(ni):
        q.append(ni)
    if verify(pi):
        q.append(pi)

for i in range(len(b)):
    if a[i] != b[i]:
        succeed = False
        break

if succeed:
    print(ans)
else:
    print(-1)

Buggy python submission (verdict: Time Limit Exceeded):
words = lambda t : list(map(t, input().split()))

def getNext(xs, i):
    if i == len(xs)-1:
        return 0
    else:
        return i+1

def getPrev(xs, i):
    if i == 0:
        return len(xs)-1
    else:
        return i-1

n = int(input())
a = words(int)
b = words(int)

import queue
q = queue.Queue()

def verify(i):
    if b[i] != a[i] and b[i] - (b[getNext(b,i)] + b[getPrev(b,i)]) >= a[i]:
        return True
    else:
        return False

for i in range(len(b)):
    if verify(i):
        q.put(i)

ans = 0
succeed = True
while not q.empty():
    i = q.get()
    #print(i, b)
    d = b[getNext(b,i)] + b[getPrev(b,i)]
    if b[i] % d == a[i] % d:
        ans += b[i] // d - (a[i] // d)
        b[i] = a[i]
    else:
        ans += b[i] // d
        b[i] = b[i] % d
    if b[i] < a[i]:
        succeed = False
        break
    if verify(getNext(b,i)):
        q.put(getNext(b,i))
    if verify(getPrev(b,i)):
        q.put(getPrev(b,i))

for i in range(len(b)):
    if a[i] != b[i]:
        succeed = False
        break

if succeed:
    print(ans)
else:
    print(-1)


Fixed submission:
words = lambda t : list(map(t, input().split()))



n = int(input())
a = words(int)
b = words(int)

def getNext(i):
    if i == n-1:
        return 0
    else:
        return i+1

def getPrev(i):
    if i == 0:
        return n-1
    else:
        return i-1

from collections import deque
q = deque()

def verify(i):
    if b[i] != a[i] and b[i] - (b[getNext(i)] + b[getPrev(i)]) >= a[i]:
        return True
    else:
        return False

for i in range(len(b)):
    if b[i] >= a[i] and verify(i):
        q.append(i)

ans = 0
succeed = True
while not len(q) == 0:
    i = q.popleft()
    ni = getNext(i)
    pi = getPrev(i)
    #print(i, b)
    d = b[ni] + b[pi]
    if b[i] % d == a[i] % d:
        ans += b[i] // d - (a[i] // d)
        b[i] = a[i]
    else:
        ans += b[i] // d
        b[i] %= d
    if b[i] < a[i]:
        succeed = False
        break
    if verify(ni):
        q.append(ni)
    if verify(pi):
        q.append(pi)

for i in range(len(b)):
    if a[i] != b[i]:
        succeed = False
        break

if succeed:
    print(ans)
else:
    print(-1)

Buggy python submission (verdict: Runtime Error):
words = lambda t : list(map(t, input().split()))

def getNext(xs, i):
    if i == len(xs)-1:
        return 0
    else:
        return i+1

def getPrev(xs, i):
    if i == 0:
        return len(xs)-1
    else:
        return i-1

n = int(input())
a = words(int)
b = words(int)

import queue
q = queue.Queue()

def verify(i):
    if b[i] - (b[getNext(b,i)] + b[getPrev(b,i)]) >= a[i]:
        return True
    else:
        return False

for i in range(len(b)):
    if verify(i):
        q.put(i)

ans = 0
while not q.empty():
    i = q.get()
    ans += b[i] // (b[getNext(b,i)] + b[getPrev(b,i)])
    b[i] = b[i] % (b[getNext(b,i)] + b[getPrev(b,i)])
    if verify(getNext(b,i)):
        q.put(getNext(b,i))
    if verify(getPrev(b,i)):
        q.put(getPrev(b,i))

succeed = True
for i in range(len(b)):
    if a[i] != b[i]:
        succeed = False
        break

if succeed:
    print(ans)
else:
    print(-1)


Fixed submission:
words = lambda t : list(map(t, input().split()))



n = int(input())
a = words(int)
b = words(int)

def getNext(i):
    if i == n-1:
        return 0
    else:
        return i+1

def getPrev(i):
    if i == 0:
        return n-1
    else:
        return i-1

from collections import deque
q = deque()

def verify(i):
    if b[i] != a[i] and b[i] - (b[getNext(i)] + b[getPrev(i)]) >= a[i]:
        return True
    else:
        return False

for i in range(len(b)):
    if b[i] >= a[i] and verify(i):
        q.append(i)

ans = 0
succeed = True
while not len(q) == 0:
    i = q.popleft()
    ni = getNext(i)
    pi = getPrev(i)
    #print(i, b)
    d = b[ni] + b[pi]
    if b[i] % d == a[i] % d:
        ans += b[i] // d - (a[i] // d)
        b[i] = a[i]
    else:
        ans += b[i] // d
        b[i] %= d
    if b[i] < a[i]:
        succeed = False
        break
    if verify(ni):
        q.append(ni)
    if verify(pi):
        q.append(pi)

for i in range(len(b)):
    if a[i] != b[i]:
        succeed = False
        break

if succeed:
    print(ans)
else:
    print(-1)