Buggy python submission (verdict: Time Limit Exceeded):
from sys import exit
n = int(input())
a = [int(e) for e in input().split()]
b = [int(e) for e in input().split()]
result = 0
q = [i for i in range(n) if b[i] != a[i]]
while len(q) != 0:
    nq = []
    c = 0
    for i in q:
        if i == 0 or i == n - 1:
            j = b[(n + i - 1) % n] + b[(n + i + 1) % n]
        else:
            j = b[i - 1] + b[i + 1]
        if j > b[i] - a[i]:
            nq.append(i)
            continue
        c += 1
        k = (b[i] - a[i]) // j
        result += k
        b[i] -= j * k
        if a[i] != b[i]:
            nq.append(i)
    if c == 0 and len(nq) != 0:
        print(-1)
        exit()
    q = nq
print(result)


Fixed submission:
def main():
  import sys
  _int = int
  n = _int(input())
  a = [_int(e) for e in input().split()]
  b = [_int(e) for e in input().split()]
  result = 0
  q = [i for i in range(n) if b[i] != a[i]]
  while len(q) != 0:
    nq = []
    c = 0
    for i in q:
      if i == 0 or i == n - 1:
        j = b[(n + i - 1) % n] + b[(n + i + 1) % n]
      else:
        j = b[i - 1] + b[i + 1]
      if j > b[i] - a[i]:
        nq.append(i)
        continue
      c += 1
      k = (b[i] - a[i]) // j
      result += k
      b[i] -= j * k
      if a[i] != b[i]:
        nq.append(i)
    if c == 0 and len(nq) != 0:
      print(-1)
      sys.exit()
    q = nq
  print(result)
main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
  import sys
  from heapq import heappush, heappop, heapify
  _int = int
  n = _int(input())
  a = [_int(e) for e in input().split()]
  b = [_int(e) for e in input().split()]
  result = 0
  hq = [(-b[i], i) for i in range(n) if b[i] != a[i]]
  heapify(hq)
  while len(hq) != 0:
    _, i = heappop(hq)
    if i == 0 or i == n - 1:
      j = b[(n + i - 1) % n] + b[(n + i + 1) % n]
    else:
      j = b[i - 1] + b[i + 1]
    if j > b[i]:
      print(-1)
      sys.exit() 
      k = (b[i] - a[i]) // j
      if k == 0:
        k = 1
      result += k
      b[i] -= j * k
    if a[i] > b[i]:
      print(-1)
      sys.exit()
    if a[i] != b[i]:
      heappush(hq, (-b[i], i))
  print(result)
main()

Fixed submission:
def main():
  import sys
  _int = int
  n = _int(input())
  a = [_int(e) for e in input().split()]
  b = [_int(e) for e in input().split()]
  result = 0
  q = [i for i in range(n) if b[i] != a[i]]
  while len(q) != 0:
    nq = []
    c = 0
    for i in q:
      if i == 0 or i == n - 1:
        j = b[(n + i - 1) % n] + b[(n + i + 1) % n]
      else:
        j = b[i - 1] + b[i + 1]
      if j > b[i] - a[i]:
        nq.append(i)
        continue
      c += 1
      k = (b[i] - a[i]) // j
      result += k
      b[i] -= j * k
      if a[i] != b[i]:
        nq.append(i)
    if c == 0 and len(nq) != 0:
      print(-1)
      sys.exit()
    q = nq
  print(result)
main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
  import sys
  from heapq import heappush, heappop, heapify
  _int = int
  n = _int(input())
  a = [_int(e) for e in input().split()]
  b = [_int(e) for e in input().split()]
  result = 0
  hq = [(-b[i], i) for i in range(n) if b[i] != a[i]]
  heapify(hq)
  while len(hq) != 0:
    _, i = heappop(hq)
    if i == 0 or i == n - 1:
      j = b[(n + i - 1) % n] + b[(n + i + 1) % n]
    else:
      j = b[i - 1] + b[i + 1]
    if j > b[i]:
      print(-1)
      sys.exit() 
      k = (b[i] - a[i]) // j
      result += k
      b[i] -= j * k
    if a[i] > b[i]:
      print(-1)
      sys.exit()
    if a[i] != b[i]:
      heappush(hq, (-b[i], i))
  print(result)
main()

Fixed submission:
def main():
  import sys
  _int = int
  n = _int(input())
  a = [_int(e) for e in input().split()]
  b = [_int(e) for e in input().split()]
  result = 0
  q = [i for i in range(n) if b[i] != a[i]]
  while len(q) != 0:
    nq = []
    c = 0
    for i in q:
      if i == 0 or i == n - 1:
        j = b[(n + i - 1) % n] + b[(n + i + 1) % n]
      else:
        j = b[i - 1] + b[i + 1]
      if j > b[i] - a[i]:
        nq.append(i)
        continue
      c += 1
      k = (b[i] - a[i]) // j
      result += k
      b[i] -= j * k
      if a[i] != b[i]:
        nq.append(i)
    if c == 0 and len(nq) != 0:
      print(-1)
      sys.exit()
    q = nq
  print(result)
main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
  import sys
  _int = int
  n = _int(input())
  a = [_int(e) for e in input().split()]
  b = [_int(e) for e in input().split()]
  result = 0
  q = [i for i in range(n) if b[i] != a[i]]
  while len(q) != 0:
    nq = []
    c = 0
    for i in q:
      if i == 0 or i == n - 1:
        j = b[(n + i - 1) % n] + b[(n + i + 1) % n]
      else:
        j = b[i - 1] + b[i + 1]
      if j > b[i]:
        nq.append(i)
        continue
      c += 1
      k = (b[i] - a[i]) // j
      result += k
      b[i] -= j * k
      if a[i] != b[i]:
        nq.append(i)
    if c == 0 and len(nq) != 0:
      print(-1)
      sys.exit()
    q = nq
  print(result)
main()

Fixed submission:
def main():
  import sys
  _int = int
  n = _int(input())
  a = [_int(e) for e in input().split()]
  b = [_int(e) for e in input().split()]
  result = 0
  q = [i for i in range(n) if b[i] != a[i]]
  while len(q) != 0:
    nq = []
    c = 0
    for i in q:
      if i == 0 or i == n - 1:
        j = b[(n + i - 1) % n] + b[(n + i + 1) % n]
      else:
        j = b[i - 1] + b[i + 1]
      if j > b[i] - a[i]:
        nq.append(i)
        continue
      c += 1
      k = (b[i] - a[i]) // j
      result += k
      b[i] -= j * k
      if a[i] != b[i]:
        nq.append(i)
    if c == 0 and len(nq) != 0:
      print(-1)
      sys.exit()
    q = nq
  print(result)
main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
  import sys
  _int = int
  n = _int(input())
  a = [_int(e) for e in input().split()]
  b = [_int(e) for e in input().split()]
  result = 0
  q = [i for i in range(n) if b[i] != a[i]]
  while len(q) != 0:
    nq = []
    c = 0
    for i in q:
      if i == 0 or i == n - 1:
        j = b[(n + i - 1) % n] + b[(n + i + 1) % n]
      else:
        j = b[i - 1] + b[i + 1]
      if j > b[i]:
        nq.append(i)
        continue
      c += 1
      k = (b[i] - a[i]) // j
      result += k
      b[i] -= j * k
      if a[i] != b[i]:
        nq.append(i)
    if c == 0 and len(nq) != 0:
      print(-1)
      sys.exit()
    q = nq
  print(result)
main()

Fixed submission:
def main():
  import sys
  _int = int
  n = _int(input())
  a = [_int(e) for e in input().split()]
  b = [_int(e) for e in input().split()]
  result = 0
  q = [i for i in range(n) if b[i] != a[i]]
  while len(q) != 0:
    nq = []
    c = 0
    for i in q:
      if i == 0 or i == n - 1:
        j = b[(n + i - 1) % n] + b[(n + i + 1) % n]
      else:
        j = b[i - 1] + b[i + 1]
      if j > b[i] - a[i]:
        nq.append(i)
        continue
      c += 1
      k = (b[i] - a[i]) // j
      result += k
      b[i] -= j * k
      if a[i] != b[i]:
        nq.append(i)
    if c == 0 and len(nq) != 0:
      print(-1)
      sys.exit()
    q = nq
  print(result)
main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
  import sys
  _int = int
  n = _int(input())
  a = [_int(e) for e in input().split()]
  b = [_int(e) for e in input().split()]
  result = 0
  q = [i for i in range(n) if b[i] != a[i]]
  while len(q) != 0:
    nq = []
    c = 0
    for i in q:
      j = b[(n + i - 1) % n] + b[(n + i + 1) % n]
      if j > b[i]:
        nq.append(i)
        continue
      c += 1
      k = (b[i] - a[i]) // j
      result += k
      b[i] -= j * k
      if a[i] != b[i]:
        nq.append(i)
    if c == 0 and len(nq) != 0:
      print(-1)
      sys.exit()
    q = nq
  print(result)
main()

Fixed submission:
def main():
  import sys
  _int = int
  n = _int(input())
  a = [_int(e) for e in input().split()]
  b = [_int(e) for e in input().split()]
  result = 0
  q = [i for i in range(n) if b[i] != a[i]]
  while len(q) != 0:
    nq = []
    c = 0
    for i in q:
      if i == 0 or i == n - 1:
        j = b[(n + i - 1) % n] + b[(n + i + 1) % n]
      else:
        j = b[i - 1] + b[i + 1]
      if j > b[i] - a[i]:
        nq.append(i)
        continue
      c += 1
      k = (b[i] - a[i]) // j
      result += k
      b[i] -= j * k
      if a[i] != b[i]:
        nq.append(i)
    if c == 0 and len(nq) != 0:
      print(-1)
      sys.exit()
    q = nq
  print(result)
main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
  import sys
  from heapq import heappush, heappop, heapify
  _int = int
  _min = min
  n = _int(input())
  a = [_int(e) for e in input().split()]
  b = [_int(e) for e in input().split()]
  result = 0
  finished = 0
  hq = [(-b[i], i) for i in range(n)]
  heapify(hq)
  while True:
    _, i = heappop(hq)
    if a[i] == b[i]:
      finished += 1
      continue
    if i == 0:
      j = b[n - 1] + b[1]
    elif i == n - 1:
      j = b[n - 2] + b[0]
    else:
      j = b[i - 1] + b[i + 1]
    k = b[i] - a[i]
    if len(hq) != 0:
      l = b[i] + hq[0][0]
      if l < k:
        k = l
    k //= j
    if k <= 0:
      k = 1
    b[i] -= j * k
    if a[i] > b[i]:
      print(-1)
      sys.exit()
    result += k
    if a[i] == b[i]:
      finished += 1
      if finished == n:
        print(result)
        sys.exit()
    else:
      heappush(hq, (-b[i], i))
main()

Fixed submission:
def main():
  import sys
  _int = int
  n = _int(input())
  a = [_int(e) for e in input().split()]
  b = [_int(e) for e in input().split()]
  result = 0
  q = [i for i in range(n) if b[i] != a[i]]
  while len(q) != 0:
    nq = []
    c = 0
    for i in q:
      if i == 0 or i == n - 1:
        j = b[(n + i - 1) % n] + b[(n + i + 1) % n]
      else:
        j = b[i - 1] + b[i + 1]
      if j > b[i] - a[i]:
        nq.append(i)
        continue
      c += 1
      k = (b[i] - a[i]) // j
      result += k
      b[i] -= j * k
      if a[i] != b[i]:
        nq.append(i)
    if c == 0 and len(nq) != 0:
      print(-1)
      sys.exit()
    q = nq
  print(result)
main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
  import sys
  from heapq import heappush, heappop, heapify
  _int = int
  _min = min
  n = _int(input())
  a = [_int(e) for e in input().split()]
  b = [_int(e) for e in input().split()]
  result = 0
  finished = 0
  hq = [(-b[i], i) for i in range(n)]
  heapify(hq)
  while True:
    _, i = heappop(hq)
    if a[i] == b[i]:
      finished += 1
      continue
    if i == 0:
      j = b[n - 1] + b[1]
    elif i == n - 1:
      j = b[n - 2] + b[0]
    else:
      j = b[i - 1] + b[i + 1]
    k = b[i] - a[i]
    if len(hq) != 0:
      l = b[i] + hq[0][0]
      if l < k:
        k = l
    k //= j
    if k <= 0:
      k = 1
    b[i] -= j * k
    if a[i] > b[i]:
      print(-1)
      sys.exit()
    result += k
    if a[i] == b[i]:
      finished += 1
      if finished == n:
        print(result)
        sys.exit()
    else:
      heappush(hq, (-b[i], i))
main()

Fixed submission:
def main():
  import sys
  _int = int
  n = _int(input())
  a = [_int(e) for e in input().split()]
  b = [_int(e) for e in input().split()]
  result = 0
  q = [i for i in range(n) if b[i] != a[i]]
  while len(q) != 0:
    nq = []
    c = 0
    for i in q:
      if i == 0 or i == n - 1:
        j = b[(n + i - 1) % n] + b[(n + i + 1) % n]
      else:
        j = b[i - 1] + b[i + 1]
      if j > b[i] - a[i]:
        nq.append(i)
        continue
      c += 1
      k = (b[i] - a[i]) // j
      result += k
      b[i] -= j * k
      if a[i] != b[i]:
        nq.append(i)
    if c == 0 and len(nq) != 0:
      print(-1)
      sys.exit()
    q = nq
  print(result)
main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
  import sys
  from heapq import heappush, heappop, heapify
  _int = int
  _min = min
  n = _int(input())
  a = [_int(e) for e in input().split()]
  b = [_int(e) for e in input().split()]
  result = 0
  finished = 0
  hq = [(-b[i], i) for i in range(n)]
  heapify(hq)
  while True:
    _, i = heappop(hq)
    if a[i] == b[i]:
      finished += 1
      continue
    if i == 0:
      j = b[n - 1] + b[1]
    elif i == n - 1:
      j = b[n - 2] + b[0]
    else:
      j = b[i - 1] + b[i + 1]
    if len(hq) != 0:
      k = _min(b[i] + hq[0][0], b[i] - a[i]) // j
    else:
      k = (b[i] - a[i]) // j
    if k <= 0:
      k = 1
    b[i] -= j * k
    if a[i] > b[i]:
      print(-1)
      sys.exit()
    result += k
    if a[i] == b[i]:
      finished += 1
      if finished == n:
        print(result)
        sys.exit()
    else:
      heappush(hq, (-b[i], i))
main()

Fixed submission:
def main():
  import sys
  _int = int
  n = _int(input())
  a = [_int(e) for e in input().split()]
  b = [_int(e) for e in input().split()]
  result = 0
  q = [i for i in range(n) if b[i] != a[i]]
  while len(q) != 0:
    nq = []
    c = 0
    for i in q:
      if i == 0 or i == n - 1:
        j = b[(n + i - 1) % n] + b[(n + i + 1) % n]
      else:
        j = b[i - 1] + b[i + 1]
      if j > b[i] - a[i]:
        nq.append(i)
        continue
      c += 1
      k = (b[i] - a[i]) // j
      result += k
      b[i] -= j * k
      if a[i] != b[i]:
        nq.append(i)
    if c == 0 and len(nq) != 0:
      print(-1)
      sys.exit()
    q = nq
  print(result)
main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
  import sys
  from heapq import heappush, heappop, heapify
  _int = int
  n = _int(input())
  a = [_int(e) for e in input().split()]
  b = [_int(e) for e in input().split()]
  result = 0
  finished = 0
  hq = [(-b[i], i) for i in range(n)]
  heapify(hq)
  while True:
    _, i = heappop(hq)
    if a[i] == b[i]:
      finished += 1
      continue
    if i == 0:
      j = b[n - 1] + b[1]
    elif i == n - 1:
      j = b[n - 2] + b[0]
    else:
      j = b[i - 1] + b[i + 1]
    if len(hq) != 0:
      k = min((b[i] + hq[0][0]) // j, (b[i] - a[i]) // j)
    else:
      k = (b[i] - a[i]) // j
    if k <= 0:
      k = 1
    b[i] -= j * k
    if a[i] > b[i]:
      print(-1)
      sys.exit()
    result += k
    if a[i] == b[i]:
      finished += 1
      if finished == n:
        print(result)
        sys.exit()
    else:
      heappush(hq, (-b[i], i))
main()

Fixed submission:
def main():
  import sys
  _int = int
  n = _int(input())
  a = [_int(e) for e in input().split()]
  b = [_int(e) for e in input().split()]
  result = 0
  q = [i for i in range(n) if b[i] != a[i]]
  while len(q) != 0:
    nq = []
    c = 0
    for i in q:
      if i == 0 or i == n - 1:
        j = b[(n + i - 1) % n] + b[(n + i + 1) % n]
      else:
        j = b[i - 1] + b[i + 1]
      if j > b[i] - a[i]:
        nq.append(i)
        continue
      c += 1
      k = (b[i] - a[i]) // j
      result += k
      b[i] -= j * k
      if a[i] != b[i]:
        nq.append(i)
    if c == 0 and len(nq) != 0:
      print(-1)
      sys.exit()
    q = nq
  print(result)
main()

Buggy python submission (verdict: Runtime Error):
def main():
  import sys
  from heapq import heappush, heappop, heapify
  _int = int
  n = _int(input())
  a = [_int(e) for e in input().split()]
  b = [_int(e) for e in input().split()]
  result = 0
  finished = 0
  hq = [(-b[i], i) for i in range(n)]
  heapify(hq)
  while True:
    _, i = heappop(hq)
    if a[i] == b[i]:
      finished += 1
      continue
    if i == 0:
      j = b[n - 1] + b[1]
    elif i == n - 1:
      j = b[n - 2] + b[0]
    else:
      j = b[i - 1] + b[i + 1]
    k = max((hq[0] - b[i]) // j, 1)
    b[i] -= j * k
    if a[i] > b[i]:
      print(-1)
      sys.exit()
    result += k
    if a[i] == b[i]:
      finished += 1
      if finished == n:
        print(result)
        sys.exit()
    else:
      heappush(hq, (-b[i], i))
main()

Fixed submission:
def main():
  import sys
  _int = int
  n = _int(input())
  a = [_int(e) for e in input().split()]
  b = [_int(e) for e in input().split()]
  result = 0
  q = [i for i in range(n) if b[i] != a[i]]
  while len(q) != 0:
    nq = []
    c = 0
    for i in q:
      if i == 0 or i == n - 1:
        j = b[(n + i - 1) % n] + b[(n + i + 1) % n]
      else:
        j = b[i - 1] + b[i + 1]
      if j > b[i] - a[i]:
        nq.append(i)
        continue
      c += 1
      k = (b[i] - a[i]) // j
      result += k
      b[i] -= j * k
      if a[i] != b[i]:
        nq.append(i)
    if c == 0 and len(nq) != 0:
      print(-1)
      sys.exit()
    q = nq
  print(result)
main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
  import sys
  from heapq import heappush, heappop, heapify
  _int = int
  n = _int(input())
  a = [_int(e) for e in input().split()]
  b = [_int(e) for e in input().split()]
  result = 0
  finished = 0
  hq = [(-b[i], i) for i in range(n)]
  heapify(hq)
  while True:
    _, i = heappop(hq)
    if a[i] == b[i]:
      finished += 1
      continue
    if i == 0:
      b[i] -= b[n - 1] + b[1]
    elif i == n - 1:
      b[i] -= b[n - 2] + b[0]
    else:
      b[i] -= b[i - 1] + b[i + 1]
    if a[i] > b[i]:
      print(-1)
      sys.exit()
    result += 1
    if a[i] == b[i]:
      finished += 1
      if finished == n:
        print(result)
        sys.exit()
    else:
      heappush(hq, (-b[i], i))
main()

Fixed submission:
def main():
  import sys
  _int = int
  n = _int(input())
  a = [_int(e) for e in input().split()]
  b = [_int(e) for e in input().split()]
  result = 0
  q = [i for i in range(n) if b[i] != a[i]]
  while len(q) != 0:
    nq = []
    c = 0
    for i in q:
      if i == 0 or i == n - 1:
        j = b[(n + i - 1) % n] + b[(n + i + 1) % n]
      else:
        j = b[i - 1] + b[i + 1]
      if j > b[i] - a[i]:
        nq.append(i)
        continue
      c += 1
      k = (b[i] - a[i]) // j
      result += k
      b[i] -= j * k
      if a[i] != b[i]:
        nq.append(i)
    if c == 0 and len(nq) != 0:
      print(-1)
      sys.exit()
    q = nq
  print(result)
main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
  import sys
  from heapq import heappush, heappop, heapify
  _int = int
  n = _int(input())
  a = [_int(e) for e in input().split()]
  b = [_int(e) for e in input().split()]
  result = 0
  finished = 0
  hq = [(-b[i], i) for i in range(n)]
  heapify(hq)
  while True:
    _, i = heappop(hq)
    if a[i] == b[i]:
      finished += 1
      continue
    if i == 0:
      b[i] -= b[n - 1] + b[1]
    elif i == n - 1:
      b[i] -= b[n - 2] + b[0]
    else:
      b[i] -= b[i - 1] + b[i + 1]
    if a[i] > b[i]:
      print(-1)
      sys.exit()
    result += 1
    if a[i] == b[i]:
      finished += 1
      if finished == n:
        print(result)
        sys.exit()
    else:
      heappush(hq, (-b[i], i))
main()

Fixed submission:
def main():
  import sys
  _int = int
  n = _int(input())
  a = [_int(e) for e in input().split()]
  b = [_int(e) for e in input().split()]
  result = 0
  q = [i for i in range(n) if b[i] != a[i]]
  while len(q) != 0:
    nq = []
    c = 0
    for i in q:
      if i == 0 or i == n - 1:
        j = b[(n + i - 1) % n] + b[(n + i + 1) % n]
      else:
        j = b[i - 1] + b[i + 1]
      if j > b[i] - a[i]:
        nq.append(i)
        continue
      c += 1
      k = (b[i] - a[i]) // j
      result += k
      b[i] -= j * k
      if a[i] != b[i]:
        nq.append(i)
    if c == 0 and len(nq) != 0:
      print(-1)
      sys.exit()
    q = nq
  print(result)
main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
  import sys
  from heapq import heappush, heappop, heapify
  n = int(input())
  a = [int(e) for e in input().split()]
  b = [int(e) for e in input().split()]
  result = 0
  finished = 0
  hq = [(-b[i], i) for i in range(n)]
  heapify(hq)
  while True:
    _, i = heappop(hq)
    if a[i] == b[i]:
      finished += 1
      continue
    if i == 0:
      b[i] -= b[n - 1] + b[1]
    elif i == n - 1:
      b[i] -= b[n - 2] + b[0]
    else:
      b[i] -= b[i - 1] + b[i + 1]
    if a[i] > b[i]:
      print(-1)
      sys.exit()
    result += 1
    if a[i] == b[i]:
      finished += 1
      if finished == n:
        print(result)
        sys.exit()
    else:
      heappush(hq, (-b[i], i))
main()

Fixed submission:
def main():
  import sys
  _int = int
  n = _int(input())
  a = [_int(e) for e in input().split()]
  b = [_int(e) for e in input().split()]
  result = 0
  q = [i for i in range(n) if b[i] != a[i]]
  while len(q) != 0:
    nq = []
    c = 0
    for i in q:
      if i == 0 or i == n - 1:
        j = b[(n + i - 1) % n] + b[(n + i + 1) % n]
      else:
        j = b[i - 1] + b[i + 1]
      if j > b[i] - a[i]:
        nq.append(i)
        continue
      c += 1
      k = (b[i] - a[i]) // j
      result += k
      b[i] -= j * k
      if a[i] != b[i]:
        nq.append(i)
    if c == 0 and len(nq) != 0:
      print(-1)
      sys.exit()
    q = nq
  print(result)
main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
  import sys
  from heapq import heappush, heappop, heapify
  n = int(input())
  a = [int(e) for e in input().split()]
  b = [int(e) for e in input().split()]
  result = 0
  finished = 0
  hq = [(-b[i], i) for i in range(n)]
  heapify(hq)
  while True:
    _, i = heappop(hq)
    if a[i] == b[i]:
      finished += 1
      continue
    if i == 0:
      b[i] -= b[n - 1] + b[1]
    elif i == n - 1:
      b[i] -= b[n - 2] + b[0]
    else:
      b[i] -= b[i - 1] + b[i + 1]
    if a[i] > b[i]:
      print(-1)
      sys.exit()
    result += 1
    if a[i] == b[i]:
      finished += 1
      if finished == n:
        print(result)
        sys.exit()
    else:
      heappush(hq, (-b[i], i))
main()

Fixed submission:
def main():
  import sys
  _int = int
  n = _int(input())
  a = [_int(e) for e in input().split()]
  b = [_int(e) for e in input().split()]
  result = 0
  q = [i for i in range(n) if b[i] != a[i]]
  while len(q) != 0:
    nq = []
    c = 0
    for i in q:
      if i == 0 or i == n - 1:
        j = b[(n + i - 1) % n] + b[(n + i + 1) % n]
      else:
        j = b[i - 1] + b[i + 1]
      if j > b[i] - a[i]:
        nq.append(i)
        continue
      c += 1
      k = (b[i] - a[i]) // j
      result += k
      b[i] -= j * k
      if a[i] != b[i]:
        nq.append(i)
    if c == 0 and len(nq) != 0:
      print(-1)
      sys.exit()
    q = nq
  print(result)
main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from heapq import heappush, heappop, heapify
n = int(input())
a = [int(e) for e in input().split()]
b = [int(e) for e in input().split()]
result = 0
finished = 0
hq = [(-b[i], i) for i in range(n)]
heapify(hq)
while True:
  _, i = heappop(hq)
  if a[i] == b[i]:
    finished += 1
    continue
  if i == 0:
    b[i] -= b[n - 1] + b[1]
  elif i == n -1:
    b[i] -= b[n - 2] + b[0]
  else:
    b[i] -= b[i - 1] + b[i + 1]
  if a[i] > b[i]:
    print(-1)
    sys.exit()
  result += 1
  if a[i] == b[i]:
    finished += 1
    if finished == n:
      print(result)
      sys.exit()
  else:
    heappush(hq, (-b[i], i))


Fixed submission:
def main():
  import sys
  _int = int
  n = _int(input())
  a = [_int(e) for e in input().split()]
  b = [_int(e) for e in input().split()]
  result = 0
  q = [i for i in range(n) if b[i] != a[i]]
  while len(q) != 0:
    nq = []
    c = 0
    for i in q:
      if i == 0 or i == n - 1:
        j = b[(n + i - 1) % n] + b[(n + i + 1) % n]
      else:
        j = b[i - 1] + b[i + 1]
      if j > b[i] - a[i]:
        nq.append(i)
        continue
      c += 1
      k = (b[i] - a[i]) // j
      result += k
      b[i] -= j * k
      if a[i] != b[i]:
        nq.append(i)
    if c == 0 and len(nq) != 0:
      print(-1)
      sys.exit()
    q = nq
  print(result)
main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from heapq import heappush, heappop, heapify
n = int(input())
a = [int(e) for e in input().split()]
b = [int(e) for e in input().split()]
result = 0
finished = 0
hq = [(-b[i], i) for i in range(n)]
heapify(hq)
while True:
  _, i = heappop(hq)
  if a[i] == b[i]:
    finished += 1
    continue
  if i == 0:
    b[i] -= b[n - 1] + b[1]
  elif i == n -1:
    b[i] -= b[n - 2] + b[0]
  else:
    b[i] -= b[i - 1] + b[i + 1]
  if a[i] > b[i]:
    print(-1)
    sys.exit()
  result += 1
  if a[i] == b[i]:
    finished += 1
    if finished == n:
      print(result)
      sys.exit()
  else:
    heappush(hq, (-b[i], i))


Fixed submission:
def main():
  import sys
  _int = int
  n = _int(input())
  a = [_int(e) for e in input().split()]
  b = [_int(e) for e in input().split()]
  result = 0
  q = [i for i in range(n) if b[i] != a[i]]
  while len(q) != 0:
    nq = []
    c = 0
    for i in q:
      if i == 0 or i == n - 1:
        j = b[(n + i - 1) % n] + b[(n + i + 1) % n]
      else:
        j = b[i - 1] + b[i + 1]
      if j > b[i] - a[i]:
        nq.append(i)
        continue
      c += 1
      k = (b[i] - a[i]) // j
      result += k
      b[i] -= j * k
      if a[i] != b[i]:
        nq.append(i)
    if c == 0 and len(nq) != 0:
      print(-1)
      sys.exit()
    q = nq
  print(result)
main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from heapq import heappush, heappop, heapify
n = int(input())
a = [int(e) for e in input().split()]
b = [int(e) for e in input().split()]
result = 0
finished = 0
for i in range(n):
  if a[i] == b[i]:
    finished += 1
hq = [(-b[i], i) for i in range(n) if a[i] != b[i]]
heapify(hq)
while True:
  _, i = heappop(hq)
  if i == 0:
    b[i] -= b[n - 1] + b[1]
  elif i == n -1:
    b[i] -= b[n - 2] + b[0]
  else:
    b[i] -= b[i - 1] + b[i + 1]
  if a[i] > b[i]:
    print(-1)
    sys.exit()
  result += 1
  if a[i] == b[i]:
    finished += 1
    if finished == n:
      print(result)
      sys.exit()
  else:
    heappush(hq, (-b[i], i))


Fixed submission:
def main():
  import sys
  _int = int
  n = _int(input())
  a = [_int(e) for e in input().split()]
  b = [_int(e) for e in input().split()]
  result = 0
  q = [i for i in range(n) if b[i] != a[i]]
  while len(q) != 0:
    nq = []
    c = 0
    for i in q:
      if i == 0 or i == n - 1:
        j = b[(n + i - 1) % n] + b[(n + i + 1) % n]
      else:
        j = b[i - 1] + b[i + 1]
      if j > b[i] - a[i]:
        nq.append(i)
        continue
      c += 1
      k = (b[i] - a[i]) // j
      result += k
      b[i] -= j * k
      if a[i] != b[i]:
        nq.append(i)
    if c == 0 and len(nq) != 0:
      print(-1)
      sys.exit()
    q = nq
  print(result)
main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from heapq import heappush, heappop
n = int(input())
a = [int(e) for e in input().split()]
b = [int(e) for e in input().split()]
result = 0
finished = 0
for i in range(n):
  if a[i] == b[i]:
    finished += 1
hq = []
for i in range(n):
  if a[i] == b[i]:
    continue
  heappush(hq, (-b[i], i))
while True:
  _, i = heappop(hq)
  if i == 0:
    b[i] -= b[n - 1] + b[1]
  elif i == n -1:
    b[i] -= b[n - 2] + b[0]
  else:
    b[i] -= b[i - 1] + b[i + 1]
  if a[i] > b[i]:
    print(-1)
    sys.exit()
  result += 1
  if a[i] == b[i]:
    finished += 1
    if finished == n:
      print(result)
      sys.exit()
  else:
    heappush(hq, (-b[i], i))


Fixed submission:
def main():
  import sys
  _int = int
  n = _int(input())
  a = [_int(e) for e in input().split()]
  b = [_int(e) for e in input().split()]
  result = 0
  q = [i for i in range(n) if b[i] != a[i]]
  while len(q) != 0:
    nq = []
    c = 0
    for i in q:
      if i == 0 or i == n - 1:
        j = b[(n + i - 1) % n] + b[(n + i + 1) % n]
      else:
        j = b[i - 1] + b[i + 1]
      if j > b[i] - a[i]:
        nq.append(i)
        continue
      c += 1
      k = (b[i] - a[i]) // j
      result += k
      b[i] -= j * k
      if a[i] != b[i]:
        nq.append(i)
    if c == 0 and len(nq) != 0:
      print(-1)
      sys.exit()
    q = nq
  print(result)
main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
n = int(input())
a = [int(e) for e in input().split()]
b = [int(e) for e in input().split()]
result = 0
finished = 0
for i in range(n):
  if a[i] == b[i]:
    finished += 1
while True:
  maxn = -1
  maxi = -1
  for i in range(n):
    if a[i] == b[i]:
      continue
    if maxn < b[i]:
      maxn = b[i]
      maxi = i
  if maxi == 0:
    b[maxi] -= b[n - 1] + b[1]
  elif maxi == n - 1:
    b[maxi] -= b[n - 2] + b[0]
  else:
    b[maxi] -= b[maxi - 1] + b[maxi + 1]
  result += 1
  if a[maxi] == b[maxi]:
    finished += 1
  if finished == n:
    print(result)
    sys.exit()
  if a[maxi] > b[maxi]:
    print(-1)
    sys.exit()


Fixed submission:
def main():
  import sys
  _int = int
  n = _int(input())
  a = [_int(e) for e in input().split()]
  b = [_int(e) for e in input().split()]
  result = 0
  q = [i for i in range(n) if b[i] != a[i]]
  while len(q) != 0:
    nq = []
    c = 0
    for i in q:
      if i == 0 or i == n - 1:
        j = b[(n + i - 1) % n] + b[(n + i + 1) % n]
      else:
        j = b[i - 1] + b[i + 1]
      if j > b[i] - a[i]:
        nq.append(i)
        continue
      c += 1
      k = (b[i] - a[i]) // j
      result += k
      b[i] -= j * k
      if a[i] != b[i]:
        nq.append(i)
    if c == 0 and len(nq) != 0:
      print(-1)
      sys.exit()
    q = nq
  print(result)
main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
n = int(input())
a = [int(e) for e in input().split()]
b = [int(e) for e in input().split()]
result = 0
finished = 0
for i in range(n):
  if a[i] == b[i]:
    finished += 1
while True:
  maxn = -1
  maxi = -1
  for i in range(n):
    if a[i] == b[i]:
      continue
    if maxn < b[i]:
      maxn = b[i]
      maxi = i
  if maxi == 0:
    b[maxi] -= b[n - 1] + b[1]
  elif maxi == n - 1:
    b[maxi] -= b[n - 2] + b[0]
  else:
    b[maxi] -= b[maxi - 1] + b[maxi + 1]
  result += 1
  if a[maxi] == b[maxi]:
    finished += 1
  if finished == n:
    print(result)
    sys.exit()
  if a[maxi] > b[maxi]:
    print(-1)
    sys.exit()


Fixed submission:
def main():
  import sys
  _int = int
  n = _int(input())
  a = [_int(e) for e in input().split()]
  b = [_int(e) for e in input().split()]
  result = 0
  q = [i for i in range(n) if b[i] != a[i]]
  while len(q) != 0:
    nq = []
    c = 0
    for i in q:
      if i == 0 or i == n - 1:
        j = b[(n + i - 1) % n] + b[(n + i + 1) % n]
      else:
        j = b[i - 1] + b[i + 1]
      if j > b[i] - a[i]:
        nq.append(i)
        continue
      c += 1
      k = (b[i] - a[i]) // j
      result += k
      b[i] -= j * k
      if a[i] != b[i]:
        nq.append(i)
    if c == 0 and len(nq) != 0:
      print(-1)
      sys.exit()
    q = nq
  print(result)
main()