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

Solution in C (GCC 5.4.1) (judge verdict: Accepted)

#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>

#define BUFFER_MAX 100

int cmp(const void* x, const void* y) {
	int a = *(const int*)x, b = *(const int*)y;
	if (a < b) return -1;
	return a > b;
}

int N, K;
int a[8192];

int cardNum;
int cards[8192];

uint64_t canMake[2][BUFFER_MAX];

void merge(uint64_t* dest, const uint64_t* src, int offset) {
	int deltaIndex = offset / 64;
	int shift = offset % 64;
	int i;
	for (i = 0; i < BUFFER_MAX; i++) {
		dest[i] = src[i];
	}
	if (shift == 0) {
		for (i = deltaIndex; i < BUFFER_MAX; i++) {
			dest[i] |= src[i - deltaIndex];
		}
	} else {
		dest[deltaIndex] |= src[0] << shift;
		for (i = deltaIndex + 1; i < BUFFER_MAX; i++) {
			dest[i] |= ((src[i - deltaIndex] << shift) | (src[i - deltaIndex - 1] >> (64 - shift)));
		}
	}
}

/* [min, max) */
int exists(const uint64_t* data, int min, int max) {
	int minIndex = min / 64;
	int minShift = min % 64;
	int maxIndex = max / 64;
	int maxShift = max % 64;
	uint64_t minMask = (~UINT64_C(0)) << minShift;
	uint64_t maxMask = (~UINT64_C(0)) << maxShift;
	if (minIndex == maxIndex) {
		uint64_t mask = minMask & ~maxMask;
		return (data[minIndex] & mask) != 0;
	} else {
		int i;
		if ((data[minIndex] & minMask) != 0) return 1;
		for (i = minIndex + 1; i < maxIndex; i++) {
			if (data[i] != 0) return 1;
		}
		if ((data[maxIndex] & ~maxMask) != 0) return 1;
		return 0;
	}
}

int isFuhituyou(const int* cards, int syugoNum, int targetIndex) {
	int i;
	int current = 0;
	for (i = 0; i < BUFFER_MAX; i++) {
		canMake[current][i] = 0;
	}
	canMake[current][0] = 1;
	for (i = 0; i < syugoNum; i++) {
		if (i != targetIndex) {
			merge(canMake[1 - current], canMake[current], cards[i]);
			current = 1 - current;
		}
	}
	return !exists(canMake[current], K - cards[targetIndex], K);
}

int main(void) {
	int i;
	int sum = 0;
	int yes, no;
	if (scanf("%d%d", &N, &K) != 2) return 1;
	cardNum = 0;
	for (i = 0; i < N; i++) {
		if (scanf("%d", &a[i]) != 1) return 1;
		if (a[i] < K) {
			cards[cardNum++] = a[i];
			if (sum < K) sum += a[i];
		}
	}
	if (sum < K) {
		/* K未満のカードをすべて足してもK未満 → 全て不必要である */
		printf("%d\n", cardNum);
		return 0;
	}
	qsort(cards, cardNum, sizeof(*cards), cmp);
	/* 最初が不必要でない → 全て不必要でない */
	if (!isFuhituyou(cards, cardNum, 0)) {
		puts("0");
		return 0;
	}
	yes = 0;
	no = cardNum;
	while (yes + 1 < no) {
		int mid = yes + (no - yes) / 2;
		if (isFuhituyou(cards, cardNum, mid)) yes = mid; else no = mid;
	}
	printf("%d\n", yes + 1);
	return 0;
}

/*

K以上のカード → 単体でよい集合 → 判定に関係ない (不必要なカードではない)
K未満のカード → aのカードを除いた時、総和がK-a以上K未満になる集合を作れるか？

MAX 5000枚 → 平方分割 72 × 72 程度

各ブロックを除いて作れる数をDP 約5000 × 72
各カードについて、所属するブロックと残りでDP 約72 × 5000

↑1回のDPが5000じゃなくて、5000×5000やんけ＼(^o^)／

仮に一番下のカードaを除いたセットで、総和がK-a以上K未満になる集合を作れるとすると
一番下のカードを加えれば総和がK以上になる → その集合の要素は不必要でない
それ以上のカードを加えても総和がK以上になる → それ以上のカードも不必要でない

作れないなら不必要 (予想)

不必要 → それがあるかによってK以上になるかに影響を与えない → 抜かなくてよい

仮にあるカードが「不必要でない」 → そのカード以上は全て不必要でない
(不必要でない根拠の集合を構成 or そのカードの代わりに「よい集合」にできる ため)

仮にあるカードが「不必要」→ そのカード以下は全て不必要
(そのカード以下に不必要でないカードがあると、上に矛盾するため)

「不必要でない」かの判定 → そのカードを除いてDPでK-a以上K未満にできるか
ビット演算による定数倍高速化：5000×5000 → 5000×80くらい (64倍)

不必要 or notの二分探索

*/