Problem:


Score : 600 points

Problem Statement

AtCoDeer the deer has N cards with positive integers written on them. The number on the i-th card (1≤i≤N) is a_i.
Because he loves big numbers, he calls a subset of the cards good when the sum of the numbers written on the cards in the subset, is K or greater.

Then, for each card i, he judges whether it is unnecessary or not, as follows:

If, for any good subset of the cards containing card i, the set that can be obtained by eliminating card i from the subset is also good, card i is unnecessary.

Otherwise, card i is NOT unnecessary.

Find the number of the unnecessary cards. Here, he judges each card independently, and he does not throw away cards that turn out to be unnecessary.

Constraints

All input values are integers.

1≤N≤5000

1≤K≤5000

1≤a_i≤10^9 (1≤i≤N)

Partial Score

300 points will be awarded for passing the test set satisfying N,K≤400.

Input

The input is given from Standard Input in the following format:

N K
a_1 a_2 ... a_N

Output

Print the number of the unnecessary cards.

Sample Input 1

3 6
1 4 3

Sample Output 1

1

There are two good sets: {2,3} and {1,2,3}.

Card 1 is only contained in {1,2,3}, and this set without card 1, {2,3}, is also good. Thus, card 1 is unnecessary.

For card 2, a good set {2,3} without card 2, {3}, is not good. Thus, card 2 is NOT unnecessary.

Neither is card 3 for a similar reason, hence the answer is 1.

Sample Input 2

5 400
3 1 4 1 5

Sample Output 2

5

In this case, there is no good set. Therefore, all the cards are unnecessary.

Sample Input 3

6 20
10 4 3 10 25 2

Sample Output 3

3



Sample input:
3 6
1 4 3


Sample output:
1

Submission status: Runtime Error

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def II(): return int(input())
def LS(): return input().split()
def S(): return input()


def main():
    n,k = LI()
    a = sorted(LI(), key=lambda x: -x)
    ii = [2**i for i in range(k)]
    s = [0 for _ in range(k)]
    rs = 0
    for i in range(n):
        ai = a[i]
        iii = ii[i]
        if ai >= k:
            rs |= iii
            continue
        for j in range(k-1,0,-1):
            sj = s[j]
            if sj:
                if j+ai < k:
                    s[j+ai] |= sj
                    s[j+ai] |= iii
                else:
                    rs |= sj
                    rs |= iii
        s[ai] |= iii

    r = n
    for iii in ii:
        if (rs & iii) > 0:
            r -= 1

    return r


print(main())

Submission status: Runtime Error

Solution code:
from sys import stdin
rr = lambda: stdin.readline().strip()
rrI = lambda: int(rr())
rrM = lambda: map(int, rr().split())
N,K = rrM()
A = rrM()
#N,K,A = 3,6,[1,4,3]
#N,K,A = 5,20,[3,1,4,1,5]
#N,K,A = 6,20,[10,4,3,10,25,2]
nogood = sum(A) < K
A = [x for x in A if x < K]
N = len(A)

#For each x, among set S = A-x,
#necessary if there's no subset of S summing in [K-x, K).
dp = [ None for _ in xrange(K) ]
#dp[k][i] = a subset summing to k that doesnt use i

def search(index, sum_, indices):
    if index == N: return
    newsum = sum_ + A[index]
    if newsum >= K: return
    if dp[newsum] is None:
        dp[newsum] = set(indices)
    else:
        dp[newsum] &= set(indices)
        
    search(index+1, sum_, indices)
    search(index+1, newsum, indices|{index} )
search(0,0,set())
#i in dp[k] means summing to k must use i
#dp[k] is None means cant sum to k
#print dp


ans = 0
for i, x in enumerate(A):
    for k in xrange(K-x, K):
        if i in (dp[k] or ()):
            ans += 1
            break
print ans if not nogood else N

Submission status: Runtime Error

Solution code:
def solve(N, K, A):
    assert N <= 400 and K <= 400
    count = 0
    for i, a in enumerate(A):
        flags = [False] * K
        flags[0] = True
        for j, aa in enumerate(A):
            if i == j or aa >= K:
                continue
            for k in range(K-1, aa-1, -1):
                flags[k] |= flags[k-aa]
        if any(flags[-a:]):
            count += 1
    return N - count

if __name__ == "__main__":
    N, K = [int(i) for i in input().split()]
    A = [int(i) for i in input().split()]
    print(solve(N, K, A))

Submission status: Runtime Error

Solution code:
import sys 

N, K = map(int, sys.stdin.readline().split())
cards = sorted(map(int, sys.stdin.readline().split()) , reverse=True)
i = 0 
s = 0 
while i < N:
    s += cards[i]
    if s >= K:
        s = 0 
        j = i                                                                                                                                                                                                                         
    i += 1
if s > K:
    print N
else:
    print N-j-1