Problem p03782 (C++)

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
#define INF 0x3f3f3f3f
// #define INF 1100000000000000000LL
#define MOD 1000000007
#define mp make_pair
#define mt make_tuple
#define pb push_back
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pint;
typedef tuple<int, int, int> tint;
typedef vector<int> vint;
typedef vector<ll> vll;
int dx[8] = {0, 0, -1, 1, 1, 1, -1, -1};
int dy[8] = {-1, 1, 0, 0, 1, -1, 1, -1};
const int SIZE = 550;
// ここまでテンプレ
int main() {
  int N, K;
  cin >> N >> K;
  vint a;
  for (int i = 0; i < N; i++) {
    int c;
    cin >> c;
    a.pb(c);
  }
  sort(a.begin(), a.end());
  int noNeed = -1, Need = N;
  while (Need - noNeed > 1) {
    int Now = (Need + noNeed) / 2;
    bool DP[SIZE][SIZE] = {};
    DP[0][0] = 1;
    for (int i = 0; i < N; i++) {
      for (int j = 0; j <= K; j++) {
        if (DP[i][j]) {
          DP[i + 1][j] = 1;
          if (j + a[i] <= K && Now != i)
            DP[i + 1][j + a[i]] = 1;
        }
      }
      /*
      cout<<Now<<" "<<i+1<<endl;
      for(int j=0;j<=K;j++)
              cout<<DP[i+1][j]<<" ";
      cout<<endl;
      */
    }
    bool flag = 0;
    for (int i = max(K - a[Now], 0); i < K; i++) {
      if (DP[N][i])
        flag = 1;
    }
    if (flag)
      Need = Now;
    else
      noNeed = Now;
    // cout<<noNeed<<" "<<Need<<endl;
  }
  cout << noNeed + 1 << endl;
  return 0;
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
#define INF 0x3f3f3f3f
// #define INF 1100000000000000000LL
#define MOD 1000000007
#define mp make_pair
#define mt make_tuple
#define pb push_back
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pint;
typedef tuple<int, int, int> tint;
typedef vector<int> vint;
typedef vector<ll> vll;
int dx[8] = {0, 0, -1, 1, 1, 1, -1, -1};
int dy[8] = {-1, 1, 0, 0, 1, -1, 1, -1};
const int SIZE = 5050;
// ここまでテンプレ
int main() {
  int N, K;
  cin >> N >> K;
  vint a;
  for (int i = 0; i < N; i++) {
    int c;
    cin >> c;
    a.pb(c);
  }
  sort(a.begin(), a.end());
  int noNeed = -1, Need = N;
  while (Need - noNeed > 1) {
    int Now = (Need + noNeed) / 2;
    bool DP[SIZE][SIZE] = {};
    DP[0][0] = 1;
    for (int i = 0; i < N; i++) {
      for (int j = 0; j <= K; j++) {
        if (DP[i][j]) {
          DP[i + 1][j] = 1;
          if (j + a[i] <= K && Now != i)
            DP[i + 1][j + a[i]] = 1;
        }
      }
      /*
      cout<<Now<<" "<<i+1<<endl;
      for(int j=0;j<=K;j++)
              cout<<DP[i+1][j]<<" ";
      cout<<endl;
      */
    }
    bool flag = 0;
    for (int i = max(K - a[Now], 0); i < K; i++) {
      if (DP[N][i])
        flag = 1;
    }
    if (flag)
      Need = Now;
    else
      noNeed = Now;
    // cout<<noNeed<<" "<<Need<<endl;
  }
  cout << noNeed + 1 << endl;
  return 0;
}