Buggy python submission (verdict: Time Limit Exceeded):
import math
#import numpy as np
import queue
from collections import deque,defaultdict
import heapq
from sys import stdin,setrecursionlimit
#from scipy.sparse.csgraph import dijkstra
#from scipy.sparse import csr_matrix
ipt = stdin.readline
setrecursionlimit(10**7)

def main():
    n = int(ipt())
    a = [int(i) for i in ipt().split()]
    b = [int(i) for i in ipt().split()]
    hq = []
    ma = max(a)
    ans = 0
    for i,bi in enumerate(b):
        heapq.heappush(hq,(-bi,i))
    while len(hq) > 0:
        nbi,i = heapq.heappop(hq)
        if len(hq) > 0:
            nnbi,ni = heapq.heappop(hq)
            heapq.heappush(hq,(nnbi,ni))
        else:
            nnbi = -ma
        if i < n-1:
            bp = b[i+1]
        else:
            bp = b[0]
        bm = b[i-1]
        if -nbi-bm-bp < a[i]:
            print(-1)
            exit()
        else:
            t = math.ceil((nnbi-nbi)/(bm+bp))
            b[i] -= (bm+bp)*t
            if b[i] > a[i]:
                heapq.heappush(hq,(-b[i],i))
            ans += t
    print(ans)
    return

if __name__ == '__main__':
    main()


Fixed submission:
import math
#import numpy as np
import queue
from collections import deque,defaultdict
import heapq
from sys import stdin,setrecursionlimit
#from scipy.sparse.csgraph import dijkstra
#from scipy.sparse import csr_matrix
ipt = stdin.readline
setrecursionlimit(10**7)

def main():
    n = int(ipt())
    a = [int(i) for i in ipt().split()]
    b = [int(i) for i in ipt().split()]
    hq = []
    ans = 0
    for i,bi in enumerate(b):
        if bi > a[i]:
            heapq.heappush(hq,(-bi,i))
    while len(hq) > 0:
        #print(hq,b)
        nbi,i = heapq.heappop(hq)
        if i == n-1:
            bp = b[0]
        else:
            bp = b[i+1]
        bm = b[i-1]
        t = (-a[i]-nbi)//(bm+bp)
        if t == 0:
            print(-1)
            exit()
        b[i] -= (bm+bp)*t
        if b[i] > a[i]:
            heapq.heappush(hq,(-b[i],i))
        ans += t
    print(ans)
    return

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import math
#import numpy as np
import queue
from collections import deque,defaultdict
import heapq
from sys import stdin,setrecursionlimit
#from scipy.sparse.csgraph import dijkstra
#from scipy.sparse import csr_matrix
ipt = stdin.readline
setrecursionlimit(10**7)

def main():
    n = int(ipt())
    a = [int(i) for i in ipt().split()]
    b = [int(i) for i in ipt().split()]
    hq = []
    ans = 0
    for i,bi in enumerate(b):
        heapq.heappush(hq,(-bi,i))
    while len(hq) > 0:
        nbi,i = heapq.heappop(hq)
        if i < n-1:
            bp = b[i+1]
        else:
            bp = b[0]
        bm = b[i-1]
        if -nbi-bm-bp < a[i]:
            print(-1)
            exit()
        else:
            b[i] -= (bm+bp)
            if b[i] > a[i]:
                heapq.heappush(hq,(-b[i],i))
            ans += 1
    print(ans)
    return

if __name__ == '__main__':
    main()


Fixed submission:
import math
#import numpy as np
import queue
from collections import deque,defaultdict
import heapq
from sys import stdin,setrecursionlimit
#from scipy.sparse.csgraph import dijkstra
#from scipy.sparse import csr_matrix
ipt = stdin.readline
setrecursionlimit(10**7)

def main():
    n = int(ipt())
    a = [int(i) for i in ipt().split()]
    b = [int(i) for i in ipt().split()]
    hq = []
    ans = 0
    for i,bi in enumerate(b):
        if bi > a[i]:
            heapq.heappush(hq,(-bi,i))
    while len(hq) > 0:
        #print(hq,b)
        nbi,i = heapq.heappop(hq)
        if i == n-1:
            bp = b[0]
        else:
            bp = b[i+1]
        bm = b[i-1]
        t = (-a[i]-nbi)//(bm+bp)
        if t == 0:
            print(-1)
            exit()
        b[i] -= (bm+bp)*t
        if b[i] > a[i]:
            heapq.heappush(hq,(-b[i],i))
        ans += t
    print(ans)
    return

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import math
#import numpy as np
import queue
from collections import deque,defaultdict
import heapq
from sys import stdin,setrecursionlimit
#from scipy.sparse.csgraph import dijkstra
#from scipy.sparse import csr_matrix
ipt = stdin.readline
setrecursionlimit(10**7)

def main():
    n = int(ipt())
    a = [int(i) for i in ipt().split()]
    b = [int(i) for i in ipt().split()]
    hq = []
    ans = 0
    for i,bi in enumerate(b):
        heapq.heappush(hq,(-bi,i))
    while len(hq) > 0:
        nbi,i = heapq.heappop(hq)
        if i < n-1:
            bp = b[i+1]
        else:
            bp = b[0]
        bm = b[i-1]
        if -nbi-bm-bp < a[i]:
            print(-1)
            exit()
        else:
            b[i] -= (bm+bp)
            if b[i] > a[i]:
                heapq.heappush(hq,(-b[i],i))
            ans += 1
    print(ans)
    return

if __name__ == '__main__':
    main()


Fixed submission:
import math
#import numpy as np
import queue
from collections import deque,defaultdict
import heapq
from sys import stdin,setrecursionlimit
#from scipy.sparse.csgraph import dijkstra
#from scipy.sparse import csr_matrix
ipt = stdin.readline
setrecursionlimit(10**7)

def main():
    n = int(ipt())
    a = [int(i) for i in ipt().split()]
    b = [int(i) for i in ipt().split()]
    hq = []
    ans = 0
    for i,bi in enumerate(b):
        if bi > a[i]:
            heapq.heappush(hq,(-bi,i))
    while len(hq) > 0:
        #print(hq,b)
        nbi,i = heapq.heappop(hq)
        if i == n-1:
            bp = b[0]
        else:
            bp = b[i+1]
        bm = b[i-1]
        t = (-a[i]-nbi)//(bm+bp)
        if t == 0:
            print(-1)
            exit()
        b[i] -= (bm+bp)*t
        if b[i] > a[i]:
            heapq.heappush(hq,(-b[i],i))
        ans += t
    print(ans)
    return

if __name__ == '__main__':
    main()