Problem p03151 (python)

Fixed submission:
# https://atcoder.jp/contests/keyence2019/tasks/keyence2019_c

import sys
# sys.setrecursionlimit(100000)
import heapq


def input():
    return sys.stdin.readline().strip()


def input_int():
    return int(input())


def input_int_list():
    return [int(i) for i in input().split()]


def main():
    n = input_int()
    A = input_int_list()
    B = input_int_list()
    plus = []
    minus = 0
    cnt = 0

    # a < b はすべて調整の必要あり
    # a < b の補填は それぞれの b > a のうち b-a が大きいところから貪欲に補填する
    for i in range(n):
        d = B[i] - A[i]
        if d > 0:
            minus += d
            cnt += 1
        else:
            heapq.heappush(plus, d)

    while minus > 0 and plus:
        minus -= -heapq.heappop(plus)
        cnt += 1

    if minus <= 0:
        print(cnt)
    else:
        print(-1)
    return


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
# https://atcoder.jp/contests/keyence2019/tasks/keyence2019_c

import sys
# sys.setrecursionlimit(100000)
import heapq


def input():
    return sys.stdin.readline().strip()


def input_int():
    return int(input())


def input_int_list():
    return [int(i) for i in input().split()]


def main():
    n = input_int()
    A = input_int_list()
    B = input_int_list()

    if sum(A) < sum(B):
        print(-1)
        return
    plus = []
    minus = []

    # a < b はすべて調整の必要あり
    # a < b の補填は それぞれの b > a のうち b-a が大きいところから貪欲に補填する
    for i in range(n):
        d = B[i] - A[i]
        if d > 0:
            minus.append(d)
        else:
            heapq.heappush(plus, d)

    ans = 0
    charge = 0
    for d in minus:
        while d > 0:
            if charge == 0:
                if heapq:
                    charge = -heapq.heappop(plus)
                    ans += 1
                else:
                    print(-1)
                    return
            if d <= charge:
                charge -= d
                d = 0
            else:
                charge = 0
                d -= charge

    print(len(minus) + ans)
    return


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
# https://atcoder.jp/contests/keyence2019/tasks/keyence2019_c

import sys
# sys.setrecursionlimit(100000)
import heapq


def input():
    return sys.stdin.readline().strip()


def input_int():
    return int(input())


def input_int_list():
    return [int(i) for i in input().split()]


def main():
    n = input_int()
    A = input_int_list()
    B = input_int_list()

    if sum(A) < sum(B):
        print(-1)
        return
    plus = []
    minus = []

    # a < b はすべて調整の必要あり
    # a < b の補填は それぞれの b > a のうち b-a が大きいところから貪欲に補填する
    for i in range(n):
        d = B[i] - A[i]
        if d > 0:
            minus.append(d)
        else:
            heapq.heappush(plus, d)

    ans = 0
    charge = 0
    for d in minus:
        while d > 0:
            if charge == 0:
                charge = -heapq.heappop(plus)
                ans += 1
            if d <= charge:
                charge -= d
                d = 0
            else:
                charge = 0
                d -= charge

    print(len(minus) + ans)
    return


if __name__ == "__main__":
    main()