Language: C++

#include <iostream>
#include <queue>
#include <set>
#include <algorithm>
#include <string>
#include <list>
#include <math.h>
#include <map>
using namespace std;
#define REP(i, k, n) for(int i = k;i < n; i++)
#define rep(i, n) REP(i,0,n)
int dx[4] = {0,-1,1,0};
int dy[4] = {-1,0,0,1};
bool dp[5001][5001];
int tar = 0;
int N, K;
int a[5001];
void solve(int depth, int cost){
	if(depth == N + 1)return;
	if(dp[depth][cost])return;

	dp[depth][cost] = true;
	solve(depth+1, cost);
	if((cost+a[depth] < K) && (tar != depth)){
		solve(depth+1, cost+a[depth]);
	}
}

int main(void){
	cin >> N >> K;
	for(int i = 0; i < N; i++){
		cin >> a[i];
	}
	//dp[j枚目まで][総和をxにできるか]
	sort(a, a+N);
	int r = N;
	int l = -1;
	while(r - l > 1){
		tar = (r+l) / 2;
		if(a[tar]>K){
			l=tar+1;continue;
		}
		fill_n(*dp, 5001*5001, false);
		// solve(0, 0);
		bool flag = false;
		dp[0][0] = true;
		rep(i,N)rep(j,K){
			if(tar == i){
				dp[i+1][j] = dp[i][j];
			}else{
				dp[i+1][j] = dp[i][j];
				if(j-a[i] >= 0){
					dp[i+1][j] |= dp[i][j-a[i]];
				}
			}
		}
		// cout << tar << ", " << a[tar] << endl;
		// rep(i,K){
		// 	cout << dp[N][i] << " ";
		// }cout << endl;
		REP(i,K-a[tar], K){
			flag |= dp[N][i];
		}//cout << (flag?"YES":"NO")<<endl;
		if(!flag){
			l = tar;
		}else{
			r = tar;
		}
	}
	cout << l+1 << endl;
}
/*
20
19 10 9 1
*/
#include <iostream>
#include <queue>
#include <set>
#include <algorithm>
#include <string>
#include <list>
#include <math.h>
#include <map>
using namespace std;
#define REP(i, k, n) for(int i = k;i < n; i++)
#define rep(i, n) REP(i,0,n)
int dx[4] = {0,-1,1,0};
int dy[4] = {-1,0,0,1};
bool dp[5001][5001];
int tar = 0;
int N, K;
int a[5001];
void solve(int depth, int cost){
	if(depth == N + 1)return;
	if(dp[depth][cost])return;

	dp[depth][cost] = true;
	solve(depth+1, cost);
	if((cost+a[depth] < K) && (tar != depth)){
		solve(depth+1, cost+a[depth]);
	}
}

int main(void){
	cin >> N >> K;
	for(int i = 0; i < N; i++){
		cin >> a[i];
	}
	//dp[j枚目まで][総和をxにできるか]
	sort(a, a+N);
	int r = N;
	int l = -1;
	while(r - l > 1){
		tar = (r+l) / 2;
		if(a[tar]>K){
			l=tar+1;continue;
		}
		fill_n(*dp, 5001*5001, false);
		// solve(0, 0);
		bool flag = false;
		dp[0][0] = true;
		rep(i,N)rep(j,K+1){
			if(tar == i){
				dp[i+1][j] = dp[i][j];
			}else{
				dp[i+1][j] = dp[i][j];
				if(j-a[i] >= 0){
					dp[i+1][j] |= dp[i][j-a[i]];
				}
			}
		}
		// cout << tar << ", " << a[tar] << endl;
		// rep(i,K){
		// 	cout << dp[N][i] << " ";
		// }cout << endl;
		REP(i,K-a[tar], K){
			flag |= dp[N][i];
		}//cout << (flag?"YES":"NO")<<endl;
		if(!flag){
			l = tar;
		}else{
			r = tar;
		}
	}
	cout << r << endl;
}
/*
20
19 10 9 1
*/


Compiler output:
a.cc:87:5: error: redefinition of 'int dx [4]'
   87 | int dx[4] = {0,-1,1,0};
      |     ^~
a.cc:12:5: note: 'int dx [4]' previously defined here
   12 | int dx[4] = {0,-1,1,0};
      |     ^~
a.cc:88:5: error: redefinition of 'int dy [4]'
   88 | int dy[4] = {-1,0,0,1};
      |     ^~
a.cc:13:5: note: 'int dy [4]' previously defined here
   13 | int dy[4] = {-1,0,0,1};
      |     ^~
a.cc:89:6: error: redefinition of 'bool dp [5001][5001]'
   89 | bool dp[5001][5001];
      |      ^~
a.cc:14:6: note: 'bool dp [5001][5001]' previously declared here
   14 | bool dp[5001][5001];
      |      ^~
a.cc:90:5: error: redefinition of 'int tar'
   90 | int tar = 0;
      |     ^~~
a.cc:15:5: note: 'int tar' previously defined here
   15 | int tar = 0;
      |     ^~~
a.cc:91:5: error: redefinition of 'int N'
   91 | int N, K;
      |     ^
a.cc:16:5: note: 'int N' previously declared here
   16 | int N, K;
      |     ^
a.cc:91:8: error: redefinition of 'int K'
   91 | int N, K;
      |        ^
a.cc:16:8: note: 'int K' previously declared here
   16 | int N, K;
      |        ^
a.cc:92:5: error: redefinition of 'int a [5001]'
   92 | int a[5001];
      |     ^
a.cc:17:5: note: 'int a [5001]' previously declared here
   17 | int a[5001];
      |     ^
a.cc:93:6: error: redefinition of 'void solve(int, int)'
   93 | void solve(int depth, int cost){
      |      ^~~~~
a.cc:18:6: note: 'void solve(int, int)' previously defined here
   18 | void solve(int depth, int cost){
      |      ^~~~~
a.cc:104:5: error: redefinition of 'int main()'
  104 | int main(void){
      |     ^~~~
a.cc:29:5: note: 'int main()' previously defined here
   29 | int main(void){
      |     ^~~~