Problem p03782 (C++)

Buggy submission (status: Time Limit Exceeded):

#include <bits/stdc++.h>
#define int long long
#define uint unsigned int
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORR(i, a, b) for (int i = (a); i >= (b); --i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define SZ(a) ((int)(a).size())
#define PB(a) push_back(a)
#define EB(...) emplace_back(__VA_ARGS__)
#define MP(a, b) make_pair(a, b)
#define MT(...) make_tuple(__VA_ARGS__)
#define Bit(n) (1LL << (n))
using namespace std;
using pii = pair<int, int>;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
const int MOD = 1000000007;
const int INF = 1LL << 30;
const double EPS = 1e-10;

int N, K;
int a[5010];
int dp[5010];

bool check(int x) {
  fill(dp, dp + 5010, 0);
  dp[0] = 1;
  REP(i, N) {
    if (i == x)
      continue;
    REPR(j, K + 1) {
      if (j - a[i] >= 0)
        dp[j] |= dp[j - a[i]];
    }
  }
  FOR(i, K - a[x], K) if (i >= 0 && dp[i]) return true;
  return false;
}

signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(10);

  cin >> N >> K;
  REP(i, N) cin >> a[i];
  sort(a, a + N);
  int ng = -1, ok = N;
  while (ok - ng > 1) {
    int m = (ng + ok) / 2;
    if (check(m))
      ok = m;
    else
      ng = m;
  }
  if (ok < N && check(ok))
    cout << ok << endl;
  else
    cout << N << endl;

  return 0;
}

Fixed submission:

#include <bits/stdc++.h>
#define int long long
#define uint unsigned int
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORR(i, a, b) for (int i = (a); i >= (b); --i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define SZ(a) ((int)(a).size())
#define PB(a) push_back(a)
#define EB(...) emplace_back(__VA_ARGS__)
#define MP(a, b) make_pair(a, b)
#define MT(...) make_tuple(__VA_ARGS__)
#define Bit(n) (1LL << (n))
using namespace std;
using pii = pair<int, int>;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
const int MOD = 1000000007;
const int INF = 1LL << 30;
const double EPS = 1e-10;

int N, K;
int a[5010];
int dp[5010];

bool check(int x) {
  fill(dp, dp + 5010, 0);
  dp[0] = 1;
  REP(i, N) {
    if (i == x)
      continue;
    REPR(j, K + 1) {
      if (j - a[i] >= 0)
        dp[j] |= dp[j - a[i]];
    }
  }
  FOR(i, max(0LL, K - a[x]), K) if (dp[i]) return true;
  return false;
}

signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(10);

  cin >> N >> K;
  REP(i, N) cin >> a[i];
  sort(a, a + N);
  int ng = -1, ok = N;
  while (ok - ng > 1) {
    int m = (ng + ok) / 2;
    if (check(m))
      ok = m;
    else
      ng = m;
  }
  if (ok < N && check(ok))
    cout << ok << endl;
  else
    cout << N << endl;

  return 0;
}