Problem p03782 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <complex>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cwchar>
#include <cwctype>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <new>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define ll long long
#define INF 1e9
#define rep(i, n) for (int(i) = 0; (i) < n; i++)
#define REP(i, n) for (int(i) = 1; (i) <= n; i++)
#define mk(a, b) make_pair(a, b)
#define fi first
#define se second
#define pii pair<int, int>
#define sz(s) s.size()
#define all(s) (s.begin(), s.end())

const int maxn = 5005;
int n, k;
int a[maxn];
bool dp[maxn];

bool check(int id) {
  rep(i, maxn) dp[i] = false;
  dp[0] = true;
  REP(i, n) {
    if (i == id)
      continue;
    for (int j = k; j >= 0; j--) {
      if (!dp[j])
        continue;
      if (j + a[i] <= k)
        dp[j + a[i]] = true;
    }
  }
  for (int i = k - a[id]; i < k; i++)
    if (dp[i])
      return false;
  return true;
}

int main() {
  ios::sync_with_stdio(false);
  cin >> n >> k;
  REP(i, n) cin >> a[i];
  sort(a + 1, a + 1 + n);
  int l = 0, r = n + 1;
  while (r - l > 1) {
    int mid = (l + r) >> 1;
    if (check(mid))
      l = mid;
    else
      r = mid;
  }
  cout << l;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <complex>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cwchar>
#include <cwctype>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <new>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define ll long long
#define INF 1e9
#define rep(i, n) for (int(i) = 0; (i) < n; i++)
#define REP(i, n) for (int(i) = 1; (i) <= n; i++)
#define mk(a, b) make_pair(a, b)
#define fi first
#define se second
#define pii pair<int, int>
#define sz(s) s.size()
#define all(s) (s.begin(), s.end())

const int maxn = 5005;
int n, k;
int a[maxn];
bool dp[maxn];

bool check(int id) {
  rep(i, maxn) dp[i] = false;
  dp[0] = true;
  REP(i, n) {
    if (i == id)
      continue;
    for (int j = k; j >= 0; j--) {
      if (!dp[j])
        continue;
      if (j + a[i] <= k)
        dp[j + a[i]] = true;
    }
  }
  for (int i = max(k - a[id], 0); i < k; i++)
    if (dp[i])
      return false;
  return true;
}

int main() {
  ios::sync_with_stdio(false);
  cin >> n >> k;
  REP(i, n) cin >> a[i];
  sort(a + 1, a + 1 + n);
  int l = 0, r = n + 1;
  while (r - l > 1) {
    int mid = (l + r) >> 1;
    if (check(mid))
      l = mid;
    else
      r = mid;
  }
  cout << l;
  return 0;
}