Problem p03780 (python)

Fixed submission:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from pprint import pprint
from collections import Counter, defaultdict, deque
import queue
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(10000)


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, K, A):
    A.sort()

    s = 0
    ans = N
    for i in range(N-1, -1, -1):
        if s + A[i] < K:
            s += A[i]
        else:
            ans = min(ans, i)
    return ans


def main():
    N, K = read_int_n()
    A = read_int_n()
    print(slv(N, K, A))


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from pprint import pprint
from collections import Counter, defaultdict, deque
import queue
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(10000)


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


def Prim(g):
    V = list(g.keys())
    for v in g.values():
        V.extend(list(v.keys()))
    V = list(set(V))

    used = set([])
    q = []
    heapq.heappush(q, (0, V[0]))

    ret = 0
    while q:
        c, v = heapq.heappop(q)

        if v in used:
            continue
        used.add(v)
        ret += c
        for u in g[v]:
            heapq.heappush(q, (g[v][u], u))
    return ret


@mt
def slv2(N, K, A):
    @lru_cache(maxsize=None)
    def f(w):
        if K - A[w] < 0:
            return True
        dp = [0] * (K)

        for i, a in enumerate(A):
            if i == w:
                continue
            tdp = dp[:]
            for j in range(K):
                if dp[j] and j + a < K:
                    tdp[j+a] = 1
            if a < K:
                tdp[a] = 1
            dp = tdp

        if sum(dp[K-A[w]:]) > 0:
            return True

        return False

    ans = 0
    for i in range(N):
        if not f(i):
            ans += 1

    return ans


@mt
def slv(N, K, A):
    @lru_cache(maxsize=None)
    def f(w):
        if K - A[w] <= 0:
            return True
        dp = [0] * (K)
        tdp = [0] * (K)

        for i, a in enumerate(A):
            if i == w:
                continue
            tdp[i] = dp[i]
            for j in range(K):
                if dp[j] and j + a < K:
                    tdp[j+a] = 1
            if a < K:
                tdp[a] = 1
            dp, tdp = tdp, dp

        if sum(dp[K-A[w]:]) > 0:
            return True

        return False

    A.sort()
    l = 0
    r = N-1
    x = 0
    while r-l > 1:
        # error_print(x)
        # x += 1
        c = (l+r)//2
        if f(c):
            r = c
        else:
            l = c

    if f(l):
        return l
    return r + 1 if not f(r) else l + 1


def main():
    N, K = read_int_n()
    A = read_int_n()
    print(slv(N, K, A))

    # for i in range(10000):
    #     N = random.randint(1, 50)
    #     K = random.randint(1, 50)
    #     A = [random.randint(1, 10) for _ in range(N)]
    #     x = slv(N, K, A[:])
    #     y = slv2(N, K, A[:])
    #     print(x, y)
    #     if x != y:
    #         print(N, K)
    #         print(' '.join(map(str, A)))
    #         return


if __name__ == '__main__':
    main()