Problem p02941 (python)

Fixed submission:
#!/usr/bin/env python3
import sys
# import heapq


def solve(N: int, A: "List[int]", B: "List[int]"):

    count = 0
    flag = True
    while flag:
        flag = False
        for i in range(N):
            a, b, c = B[i-1], B[i], B[(i+1) % N]
            if b > A[i] and b > a+c:
                d, m = divmod(b-A[i], a+c)
                if m == 0:
                    B[i] = A[i]
                    count += d
                else:
                    d, m = divmod(b, a+c)
                    count += d
                    B[i] = m
                flag = True
    # print(*A)
    # print(*B)
    if A != B:
        count = -1

    print(count)
    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    B = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, A, B)


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
import sys
import heapq
INF = float("inf")


def solve(N: int, A: "List[int]", B: "List[int]"):

    lh = []
    for i in range(N):
        heapq.heappush(lh, [-B[i], i])

    count = 0
    while len(lh) > 0:
        b, i = heapq.heappop(lh)
        if B[i]-B[(i+1) % N]-B[(i-1) % N] > 0:
            d, m = divmod(B[i], (B[(i+1) % N]+B[(i-1) % N]))
            count += d
            B[i] = m
            if A[i] < B[i]:
                heapq.heappush(lh, [-B[i], i])
        else:
            continue
    for i in range(N):
        if A[i] != B[i]:
            print(-1)
            return

    print(count)
    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    B = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, A, B)


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
import sys
import heapq
INF = float("inf")


class MyHeap(object):
    def __init__(self, x):
        self.heap = [self._encode(e) for e in x]
        heapq.heapify(self.heap)

    def push(self, value):
        heapq.heappush(self.heap, self._encode(value))

    def pop(self):
        return self._decode(heapq.heappop(self.heap))

    def _encode(self, x):
        a, b, i = x
        return [-b, a, i]

    def _decode(self, x):
        b, a, i = x
        return [a, -b, i]

    def __len__(self):
        return len(self.heap)


def solve(N: int, A: "List[int]", B: "List[int]"):

    lh = MyHeap([])
    for i in range(N):
        lh.push((A[i], B[i], i))

    count = 0
    while len(lh) > 0:
        a, b, i = lh.pop()
        if B[i]-B[(i+1) % N]-B[(i-1) % N] > 0:
            count += B[i]//(B[(i+1) % N]+B[(i-1) % N])
            B[i] %= (B[(i+1) % N]+B[(i-1) % N])
            if A[i] < B[i]:
                lh.push([A[i], B[i], i])
        else:
            continue
    for i in range(N):
        if A[i] != B[i]:
            print(-1)
            return

    print(count)
    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    B = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, A, B)


if __name__ == '__main__':
    main()