Buggy python submission (verdict: Time Limit Exceeded):
N,K = map(int, input().split())
A = [int(a) for a in input().split()]

A.sort()

def f(n): #aiが必要か不必要か判定
    if A[n] >= K:
        return True
    if n == 0:
        L = A[n+1:]
    elif n == N-1:
        L = A[:n]
    else:
        L = A[:n] + A[n+1:]
    
    dp0 = [0]*K
    dp0[0] = 1
    dp1 = [0]*K
    for i in range(N-1):
        for j in range(K):
            if i%2 == 0:
                if j < L[i]:
                    dp1[j] = dp0[j]
                    continue
                dp1[j] = dp0[j] | dp0[j-L[i]]
            else:
                if j < L[i]:
                    dp0[j] = dp1[j]
                    continue
                dp0[j] = dp1[j] | dp1[j-L[i]]
        
    res = False
    if N%2 == 0:
        for j in range(K-A[n], K):
            if dp1[j]:
                res = True
                break
    else:
        for j in range(K-A[n], K):
            if dp0[j]:
                res = True
                break
            
    return res

l = -1
r = N
while r-l > 1:
    m = (l+r)//2
    if f(m):
        r = m
    else:
        l = m
        
print(r)

Fixed submission:
N,K = map(int, input().split())
A = [int(a) for a in input().split()]

A.sort()
for i in range(N):
    if A[i] >= K:
        A = A[:i]
        break

N = len(A)

def f(n): #aiが必要か不必要か判定
    dp = [0]*K
    dp[0] = 1
    for i in range(N):
        if i == n:
            continue
        for j in range(K-1, A[i]-1, -1):
            dp[j] |= dp[j-A[i]]
            
    res = False
    for j in range(K-A[n], K):
        if dp[j]:
            res = True
            break
    return res

l = -1
r = N
while r-l > 1:
    m = (l+r)//2
    if f(m):
        r = m
    else:
        l = m
        
print(r)

Buggy python submission (verdict: Time Limit Exceeded):
N,K = map(int, input().split())
A = [int(a) for a in input().split()]

A.sort()

def f(n): #aiが必要か不必要か判定
    if A[n] >= K:
        return True
    if n == 0:
        L = A[n+1:]
    elif n == N-1:
        L = A[:n]
    else:
        L = A[:n] + A[n+1:]
    
    dp = [[False]*K for _ in range(2)]
    dp[0][0] = True
    for i in range(N-1):
        for j in range(K):
            if j < L[i]:
                dp[(i+1)%2][j] = dp[i%2][j]
                continue
            dp[(i+1)%2][j] = dp[i%2][j] | dp[i%2][j-L[i]]
        
    res = False
    for j in range(K-A[n], K):
        if dp[(N+1)%2][j]:
            res = True
            break
            
    return res

l = -1
r = N
while r-l > 1:
    m = (l+r)//2
    if f(m):
        r = m
    else:
        l = m
        
print(r)

Fixed submission:
N,K = map(int, input().split())
A = [int(a) for a in input().split()]

A.sort()
for i in range(N):
    if A[i] >= K:
        A = A[:i]
        break

N = len(A)

def f(n): #aiが必要か不必要か判定
    dp = [0]*K
    dp[0] = 1
    for i in range(N):
        if i == n:
            continue
        for j in range(K-1, A[i]-1, -1):
            dp[j] |= dp[j-A[i]]
            
    res = False
    for j in range(K-A[n], K):
        if dp[j]:
            res = True
            break
    return res

l = -1
r = N
while r-l > 1:
    m = (l+r)//2
    if f(m):
        r = m
    else:
        l = m
        
print(r)

Buggy python submission (verdict: Time Limit Exceeded):
N,K = map(int, input().split())
A = [int(a) for a in input().split()]

A.sort()

def f(n): #aiが必要か不必要か判定
    if A[n] >= K:
        return True
    if n == 0:
        L = A[n+1:]
    elif n == N-1:
        L = A[:n]
    else:
        L = A[:n] + A[n+1:]
    
    dp = [[False]*K for _ in range(N)]
    for i in range(N):
        dp[i][0] = True
    for i in range(N-1):
        for j in range(K):
            if j < L[i]:
                dp[i+1][j] = dp[i][j]
            else:
                dp[i+1][j] = dp[i][j] | dp[i][j-L[i]]
                
    res = False
    for j in range(K-A[n], K):
        if dp[-1][j]:
            res = True
            break
            
    return res

l = -1
r = N
while r-l > 1:
    m = (l+r)//2
    if f(m):
        r = m
    else:
        l = m
        
print(r)

Fixed submission:
N,K = map(int, input().split())
A = [int(a) for a in input().split()]

A.sort()
for i in range(N):
    if A[i] >= K:
        A = A[:i]
        break

N = len(A)

def f(n): #aiが必要か不必要か判定
    dp = [0]*K
    dp[0] = 1
    for i in range(N):
        if i == n:
            continue
        for j in range(K-1, A[i]-1, -1):
            dp[j] |= dp[j-A[i]]
            
    res = False
    for j in range(K-A[n], K):
        if dp[j]:
            res = True
            break
    return res

l = -1
r = N
while r-l > 1:
    m = (l+r)//2
    if f(m):
        r = m
    else:
        l = m
        
print(r)