prithivMLmods/Coder-Stat huggingface dataset:
Problem p03782 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
using namespace std;
int n, k;
const int N = 5001;
int x[N], tmp[N];
bool f[N][N];
bool check(int d) {
  int i, j, cnt = 0;
  memset(tmp, 0, sizeof(tmp));
  memset(f, false, sizeof(f));
  for (i = 1; i <= n; i++)
    if (i != d)
      tmp[++cnt] = x[i];
  f[1][0] = true;
  if (tmp[1] <= k)
    f[1][tmp[1]] = true;
  for (i = 2; i <= n - 1; i++)
    for (j = 0; j <= k; j++)
      if (j - tmp[i] >= 0)
        f[i][j] = f[i - 1][j - tmp[i]];
      else
        f[i][j] = f[i - 1][j];
  for (i = k - x[d]; i < k; i++)
    if (f[n - 1][i])
      return true;
  return false;
}
int main() {
  int i, j, l, r;
  cin >> n >> k;
  for (i = 1; i <= n; i++)
    scanf("%d", &x[i]);
  sort(x + 1, x + n + 1);
  l = 1, r = n;
  while (l <= r) {
    int mid = (l + r) / 2;
    if (!check(mid))
      l = mid + 1;
    else
      r = mid - 1;
  }
  cout << r;
  /* cout<<check(1);
   //cout<<f[5][16]<<' '<<f[5][19];
   for(i=1;i<=n;i++)
       for(j=0;j<=k;j++)
           if(f[i][j])
               cout<<i<<' '<<j<<endl;*/
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
using namespace std;
int n, k;
const int N = 5001;
int x[N], tmp[N];
bool f[N][N];
bool check(int d) {
  if (x[d] >= k)
    return true;
  int i, j, cnt = 0;
  memset(tmp, 0, sizeof(tmp));
  memset(f, false, sizeof(f));
  for (i = 1; i <= n; i++)
    if (i != d)
      tmp[++cnt] = x[i];
  f[1][0] = true;
  if (tmp[1] <= k)
    f[1][tmp[1]] = true;
  for (i = 2; i <= n - 1; i++)
    for (j = 0; j <= k; j++)
      if (j - tmp[i] >= 0)
        f[i][j] = f[i - 1][j - tmp[i]];
      else
        f[i][j] = f[i - 1][j];
  for (i = k - x[d]; i < k; i++)
    if (f[n - 1][i])
      return true;
  return false;
}
int main() {
  int i, j, l, r;
  cin >> n >> k;
  for (i = 1; i <= n; i++)
    scanf("%d", &x[i]);
  sort(x + 1, x + n + 1);
  l = 1, r = n;
  while (l <= r) {
    int mid = (l + r) / 2;
    if (!check(mid))
      l = mid + 1;
    else
      r = mid - 1;
  }
  cout << r;
  /* cout<<check(1);
   //cout<<f[5][16]<<' '<<f[5][19];
   for(i=1;i<=n;i++)
       for(j=0;j<=k;j++)
           if(f[i][j])
               cout<<i<<' '<<j<<endl;*/
  return 0;
}
Problem p03782 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 5000;
int a[MAXN];
bool poss[MAXN + 1];

bool isNec(int pos, int n, int k) {
  fill(poss, poss + k, false);
  poss[0] = true;
  for (int i = 0; i < n; i++) {
    if (i == pos)
      continue;
    for (int j = k - 1; j >= 0; j--) {
      if (poss[j] && j + a[i] <= k - 1)
        poss[j + a[i]] = true;
    }
  }
  for (int i = k - a[pos]; i < k; i++) {
    if (poss[i])
      return true;
  }
  return false;
}

int main() {
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a, a + n);
  int ans = -1;
  for (int jump = n / 2 + 1; jump > 0; jump /= 2) {
    while (ans + jump < n && !isNec(ans + jump, n, k))
      ans += jump;
  }
  cout << ans + 1 << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 5000;
int a[MAXN];
bool poss[MAXN + 1];

bool isNec(int pos, int n, int k) {
  if (a[pos] >= k)
    return true;
  fill(poss, poss + k, false);
  poss[0] = true;
  for (int i = 0; i < n; i++) {
    if (i == pos)
      continue;
    for (int j = k - 1; j >= 0; j--) {
      if (poss[j] && j + a[i] <= k - 1)
        poss[j + a[i]] = true;
    }
  }
  for (int i = k - a[pos]; i < k; i++) {
    if (poss[i])
      return true;
  }
  return false;
}

int main() {
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a, a + n);
  int ans = -1;
  for (int jump = n / 2 + 1; jump > 0; jump /= 2) {
    while (ans + jump < n && !isNec(ans + jump, n, k))
      ans += jump;
  }
  cout << ans + 1 << endl;
  return 0;
}
Problem p03782 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, s, e) for (int i = s; i < (e); i++)
#define All(v) (v).begin(), (v).end()
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define int long long
using namespace std;
typedef long long llint;
typedef pair<int, int> P;
const int MOD = (int)1e9 + 7;
const int INF = (int)1e18 * 5;

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

bool solve(int idx) {
  // dp: a[idx] を含まずに構成可能なaの和j
  memset(dp, 0, sizeof(dp));
  dp[0][0] = 1;
  REP(i, N) {
    REP(j, K) {
      chmax(dp[i + 1][j], dp[i][j]);
      if (i != idx && j + a[i] < K)
        chmax(dp[i + 1][j + a[i]], dp[i][j]);
    }
  }

  FOR(i, K - a[idx], K) {
    if (dp[N][i])
      return true;
  }

  return false;
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> N >> K;
  REP(i, N) cin >> a[i];

  sort(a, a + N);

  int ok = N;
  int ng = -1;
  while (abs(ok - ng) > 1) {
    int mid = (ok + ng) / 2;
    if (solve(mid)) {
      ok = mid;
    } else {
      ng = mid;
    }
  }
  cout << ok << "\n";

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, s, e) for (int i = s; i < (e); i++)
#define All(v) (v).begin(), (v).end()
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define int long long
using namespace std;
typedef long long llint;
typedef pair<int, int> P;
const int MOD = (int)1e9 + 7;
const int INF = (int)1e18 * 5;

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

bool solve(int idx) {
  // dp: a[idx] を含まずに構成可能なaの和j
  memset(dp, 0, sizeof(dp));
  dp[0][0] = 1;
  REP(i, N) {
    REP(j, K) {
      chmax(dp[i + 1][j], dp[i][j]);
      if (i != idx && j + a[i] < K)
        chmax(dp[i + 1][j + a[i]], dp[i][j]);
    }
  }

  FOR(i, max((int)0, K - a[idx]), K) {
    if (dp[N][i])
      return true;
  }

  return false;
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> N >> K;
  REP(i, N) cin >> a[i];

  sort(a, a + N);

  int ok = N;
  int ng = -1;
  while (abs(ok - ng) > 1) {
    int mid = (ok + ng) / 2;
    if (solve(mid)) {
      ok = mid;
    } else {
      ng = mid;
    }
  }
  cout << ok << "\n";

  return 0;
}
Problem p03782 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;

int N, K;
long long a[5002];

bool dp[5003][5000];

bool is_needed(int x) {
  for (int k = 0; k < K; k++) {
    dp[0][k] = false;
  }
  dp[0][0] = true;
  for (int i = 0; i < N; i++) {
    for (int k = 0; k < K; k++) {
      dp[i + 1][k] = dp[i][k];
    }
    if (i != x) {
      for (int k = 0; k < K; k++) {
        if (a[i] + k < K) {
          dp[i + 1][a[i] + k] = dp[i][k] || dp[i + 1][a[i] + k];
        }
      }
    }
  }
  bool ret = false;
  for (int k = K - a[x]; k < K; k++) {
    ret = ret || dp[N][k];
  }
  return ret;
}

// is_needed(begin) == false, is_needed(end) == true
int find(int begin, int end) {
  if (end - begin == 1) {
    return begin;
  }
  int mid = (begin + end) / 2;
  if (is_needed(mid)) {
    return find(begin, mid);
  } else {
    return find(mid, end);
  }
}

int main() {
  cin >> N >> K;
  for (int i = 0; i < N; i++) {
    cin >> a[i];
  }
  sort(a, a + N);
  if (is_needed(0) == true) {
    cout << 0 << endl;
    return 0;
  }
  if (is_needed(N - 1) == false) {
    cout << N << endl;
    return 0;
  }
  cout << find(0, N - 1) + 1 << endl;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;

int N, K;
long long a[5002];

bool dp[5003][5000];

bool is_needed(int x) {
  for (int k = 0; k < K; k++) {
    dp[0][k] = false;
  }
  dp[0][0] = true;
  for (int i = 0; i < N; i++) {
    for (int k = 0; k < K; k++) {
      dp[i + 1][k] = dp[i][k];
    }
    if (i != x) {
      for (int k = 0; k < K; k++) {
        if (a[i] + k < K) {
          dp[i + 1][a[i] + k] = dp[i][k] || dp[i + 1][a[i] + k];
        }
      }
    }
  }
  bool ret = false;
  for (int k = max(0LL, K - a[x]); k < K; k++) {
    ret = ret || dp[N][k];
  }
  return ret;
}

// is_needed(begin) == false, is_needed(end) == true
int find(int begin, int end) {
  if (end - begin == 1) {
    return begin;
  }
  int mid = (begin + end) / 2;
  if (is_needed(mid)) {
    return find(begin, mid);
  } else {
    return find(mid, end);
  }
}

int main() {
  cin >> N >> K;
  for (int i = 0; i < N; i++) {
    cin >> a[i];
  }
  sort(a, a + N);
  if (is_needed(0) == true) {
    cout << 0 << endl;
    return 0;
  }
  if (is_needed(N - 1) == false) {
    cout << N << endl;
    return 0;
  }
  cout << find(0, N - 1) + 1 << endl;
}
Problem p03782 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
const int N = 5005;
int n, k, sum;
int a[N], dp[N][N];
bool check(int x) {
  memset(dp, 0, sizeof(dp));
  if (x == 1) {
    dp[2][0] = 1;
    dp[2][a[2]] = 1;
    for (int i = 3; i <= n; i++) {
      for (int j = 0; j <= k; j++) {
        dp[i][j] = dp[i - 1][j];
        if (j >= a[i])
          dp[i][j] = dp[i - 1][j] | dp[i - 1][j - a[i]];
      }
    }
  } else {
    dp[1][0] = 1;
    dp[1][a[1]] = 1;
    for (int i = 2; i <= n; i++) {
      if (x == i) {
        for (int j = 0; j <= k; j++)
          dp[i][j] = dp[i - 1][j];
        continue;
      }
      for (int j = 0; j <= k; j++) {
        dp[i][j] = dp[i - 1][j];
        if (j >= a[i])
          dp[i][j] = dp[i - 1][j] || dp[i - 1][j - a[i]];
      }
    }
  }
  /*
  printf("x:%d\n",x);
  for(int i=1;i<=n;i++)
  {
          for(int j=0;j<=k;j++)
                  printf("%d ",dp[i][j]);
          printf("\n");
  }
  */
  for (int i = k - a[x]; i < k; i++)
    if (dp[n][i])
      return true;
  return false;
}
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    a[i] = min(a[i], k);
  }
  if (n == 1) {
    if (a[1] >= k)
      printf("0\n");
    else
      printf("1\n");
    return 0;
  }
  sort(a + 1, a + n + 1);
  for (int i = 1; i <= n; i++)
    if (!check(i))
      sum++;
  printf("%d\n", sum);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 5005;
int n, k, sum;
int a[N], dp[N][N];
bool check(int x) {
  memset(dp, 0, sizeof(dp));
  if (x == 1) {
    dp[2][0] = 1;
    dp[2][a[2]] = 1;
    for (int i = 3; i <= n; i++) {
      for (int j = 0; j <= k; j++) {
        dp[i][j] = dp[i - 1][j];
        if (j >= a[i])
          dp[i][j] = dp[i - 1][j] | dp[i - 1][j - a[i]];
      }
    }
  } else {
    dp[1][0] = 1;
    dp[1][a[1]] = 1;
    for (int i = 2; i <= n; i++) {
      if (x == i) {
        for (int j = 0; j <= k; j++)
          dp[i][j] = dp[i - 1][j];
        continue;
      }
      for (int j = 0; j <= k; j++) {
        dp[i][j] = dp[i - 1][j];
        if (j >= a[i])
          dp[i][j] = dp[i - 1][j] || dp[i - 1][j - a[i]];
      }
    }
  }
  /*
  printf("x:%d\n",x);
  for(int i=1;i<=n;i++)
  {
          for(int j=0;j<=k;j++)
                  printf("%d ",dp[i][j]);
          printf("\n");
  }
  */
  for (int i = k - a[x]; i < k; i++)
    if (dp[n][i])
      return true;
  return false;
}
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    a[i] = min(a[i], k);
  }
  if (n == 1) {
    if (a[1] >= k)
      printf("0\n");
    else
      printf("1\n");
    return 0;
  }
  sort(a + 1, a + n + 1);
  int lb = 0, ub = n + 1;
  while (lb + 1 < ub) {
    int mid = (lb + ub) / 2;
    if (check(mid))
      ub = mid;
    else
      lb = mid;
  }
  printf("%d\n", lb);
  return 0;
}
Problem p03782 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#define _USE_MATH_DEFINES
#include <functional>
#include <math.h>
using namespace std;

#define rep(i, x) for (ll i = 0; i < x; i++)
#define repn(i, x) for (ll i = 1; i <= x; i++)

typedef long long ll;
const ll INF = 1e17;
const ll MOD = 1000000007;
const ll MAX = 1000001;

ll max(ll a, ll b) {
  if (a > b) {
    return a;
  }
  return b;
}

ll min(ll a, ll b) {
  if (a > b) {
    return b;
  }
  return a;
}

ll gcd(ll a, ll b) {
  if (b == 0) {
    return a;
  }
  if (a < b) {
    return gcd(b, a);
  }
  return gcd(b, a % b);
}

ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

struct edge {
  ll ind;
  ll to;
  ll d;
};

///////////////////////////

ll N, K;
vector<ll> a;

bool nn(ll x) {
  vector<bool> dp(K + 1, 0);
  dp[0] = 1;

  repn(i, N) {
    if (i == x) {
      continue;
    }

    vector<ll> v;
    rep(j, K) {
      if (j >= a[i] && dp[j - a[i]] == 1) {
        v.push_back(j);
      }
    }
    rep(k, v.size()) { dp[v[k]] = 1; }
  }

  rep(j, a[x]) {
    if (dp[K - a[x] + j] == 1) {
      return false;
    }
  }

  return true;
}

int main() {

  cin >> N >> K;
  a.resize(N + 1);

  repn(i, N) cin >> a[i];
  sort(++a.begin(), a.end());

  ll l = 0;
  ll r = N + 1;

  while (r - l > 1) {
    ll m = (r + l) / 2;

    if (nn(m)) {
      l = m;
    } else {
      r = m;
    }
  }

  cout << l;
  system("PAUSE");
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#define _USE_MATH_DEFINES
#include <functional>
#include <math.h>
using namespace std;

#define rep(i, x) for (ll i = 0; i < x; i++)
#define repn(i, x) for (ll i = 1; i <= x; i++)

typedef long long ll;
const ll INF = 1e17;
const ll MOD = 1000000007;
const ll MAX = 1000001;

ll max(ll a, ll b) {
  if (a > b) {
    return a;
  }
  return b;
}

ll min(ll a, ll b) {
  if (a > b) {
    return b;
  }
  return a;
}

ll gcd(ll a, ll b) {
  if (b == 0) {
    return a;
  }
  if (a < b) {
    return gcd(b, a);
  }
  return gcd(b, a % b);
}

ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

struct edge {
  ll ind;
  ll to;
  ll d;
};

///////////////////////////

ll N, K;
vector<ll> a;

bool nn(ll x) {

  if (a[x] > K) {
    return false;
  }

  vector<bool> dp(K + 1, 0);
  dp[0] = 1;

  repn(i, N) {
    if (i == x) {
      continue;
    }

    vector<ll> v;
    rep(j, K) {
      if (j >= a[i] && dp[j - a[i]] == 1) {
        v.push_back(j);
      }
    }
    rep(k, v.size()) { dp[v[k]] = 1; }
  }

  rep(j, a[x]) {
    if (dp[K - a[x] + j] == 1) {
      return false;
    }
  }

  return true;
}

int main() {

  cin >> N >> K;
  a.resize(N + 1);

  repn(i, N) cin >> a[i];
  sort(++a.begin(), a.end());

  ll l = 0;
  ll r = N + 1;

  while (r - l > 1) {
    ll m = (r + l) / 2;

    if (nn(m)) {
      l = m;
    } else {
      r = m;
    }
  }

  cout << l;
  system("PAUSE");
}
Problem p03782 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>

using namespace std;

const int mod = 1e9 + 7;

int dp[5001];
int dp2[5001];

int add(int x, int y) {
  x += y;
  if (x >= mod) {
    x -= mod;
  }
  return x;
}

void upd(int &x, int y) {
  x += y;
  if (x >= mod) {
    x -= mod;
  }
}

int main() {
  int n, K;
  cin >> n >> K;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  dp[0] = 1;
  for (int i = 0; i < n; i++) {
    for (int j = K - 1; j >= a[i]; j--) {
      upd(dp[j], dp[j - a[i]]);
    }
  }
  // dp[i+1][j] = dp[i][j-a[i]]+dp[i][j]
  // dp[i][j]=dp[i+1][j]-dp[i][j-a[i]]
  int ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < a[i]; j++) {
      dp2[j] = dp[j];
    }
    for (int j = a[i]; j < K; j++) {
      dp2[j] = add(dp[j], mod - dp2[j - a[i]]);
    }
    bool any = false;
    for (int j = 0; j < K; j++) {
      if (dp2[j] != 0 && j + a[i] >= K) {
        any = true;
      }
    }
    if (!any) {
      ans++;
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>

using namespace std;

const int mod = 1e9 + 7;

int dp[5001];
int dp2[5001];

int add(int x, int y) {
  x += y;
  if (x >= mod) {
    x -= mod;
  }
  return x;
}

void upd(int &x, int y) {
  x += y;
  if (x >= mod) {
    x -= mod;
  }
}

int main() {
  int n, K;
  cin >> n >> K;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  dp[0] = 1;
  for (int i = 0; i < n; i++) {
    for (int j = K - 1; j >= a[i]; j--) {
      upd(dp[j], dp[j - a[i]]);
    }
  }
  // dp[i+1][j] = dp[i][j-a[i]]+dp[i][j]
  // dp[i][j]=dp[i+1][j]-dp[i][j-a[i]]
  int ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < min(K, a[i]); j++) {
      dp2[j] = dp[j];
    }
    for (int j = a[i]; j < K; j++) {
      dp2[j] = add(dp[j], mod - dp2[j - a[i]]);
    }
    bool any = false;
    for (int j = 0; j < K; j++) {
      if (dp2[j] != 0 && j + a[i] >= K) {
        any = true;
      }
    }
    if (!any) {
      ans++;
    }
  }
  cout << ans << endl;
}
Problem p03782 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <stack>
#include <utility>
#include <vector>

#define int long long
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
typedef pair<int, int> pii;
const int INF = 1l << 60;
#define u_b upper_bound
#define l_b lower_bound

int N, K;
int A[5050];
bool dp[5050][5050];

bool isNecessary(int x) {
  memset(dp, 0, sizeof(dp));
  dp[0][0] = true;
  for (int i = 0; i < N; ++i) {
    rep(k, K + 1) {
      dp[i + 1][k] = dp[i][k];
      if (i != x && k - A[i] >= 0) {
        dp[i + 1][k] |= dp[i][k - A[i]];
      }
    }
  }
  for (int r = K - A[x]; r < K; ++r) {
    if (dp[N][r])
      return true;
  }
  return false;
}

signed main() {
  cin >> N >> K;
  rep(i, N) { cin >> A[i]; }
  sort(A, A + N);
  int notNecessary = -1;
  int neccessary = N;
  while (neccessary - notNecessary > 1) {
    int m = (notNecessary + neccessary) / 2;
    if (isNecessary(m))
      neccessary = m;
    else
      notNecessary = m;
  }
  cout << notNecessary + 1 << endl;
}


Fixed submission:
#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <stack>
#include <utility>
#include <vector>

#define int long long
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
typedef pair<int, int> pii;
const int INF = 1l << 60;
#define u_b upper_bound
#define l_b lower_bound

int N, K;
int A[5050];
bool dp[5050][5050];

bool isNecessary(int x) {
  memset(dp, 0, sizeof(dp));
  dp[0][0] = true;
  for (int i = 0; i < N; ++i) {
    rep(k, K + 1) {
      dp[i + 1][k] = dp[i][k];
      if (i != x && k - A[i] >= 0) {
        dp[i + 1][k] |= dp[i][k - A[i]];
      }
    }
  }
  for (int r = max(0ll, K - A[x]); r < K; ++r) {
    if (dp[N][r])
      return true;
  }
  return false;
}

signed main() {
  cin >> N >> K;
  rep(i, N) { cin >> A[i]; }
  sort(A, A + N);
  int notNecessary = -1;
  int neccessary = N;
  while (neccessary - notNecessary > 1) {
    int m = (notNecessary + neccessary) / 2;
    if (isNecessary(m))
      neccessary = m;
    else
      notNecessary = m;
  }
  cout << notNecessary + 1 << endl;
}
Problem p03782 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <vector>
using namespace std;
int a[5000];
int bubuwa(int t); // tを省いた中で、部分和がk-a[t]以上k未満になるかどうか
int n, k;
bool dp[5001][5001];
int main() {

  // B

  cin >> n >> k;
  int i;
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }

  sort(a, a + n);

  int left, right, mid;
  left = -1;
  right = n;
  mid = (left + right) / 2;

  while (right - left > 1) {

    if (bubuwa(mid) == 1) {

      right = mid;

    } else {

      left = mid;
    }
    mid = (right + left) / 2;
  }

  cout << right << endl;
  return 0;

  // ある値が必要であれば、それ以上もすべて必要である。従って二分探索を
  // 用いて、最初に必要となる数を求める。必要かどうかの判定は部分和問題
  // である。その数を省いた数列から部分和を取って、k未満かつその数があ
  // ればk以上となるような値となるかを調べる

  // A

  /*int x;
  cin >> x;
  int i;
  for (i = 0; i <= x; i++) {

          if (((i * (i - 1)) / 2 < x) && (x <= (i * (i + 1)) / 2)) {
                  cout << i << endl;
                  return 0;
          }

  }
  return 0;
  */
}

int bubuwa(int t) {

  int i, j;
  memset(dp, 0, sizeof(dp));
  dp[0][0] = true;
  for (i = 0; i < n; i++) {
    // if (i != t) {
    for (j = 0; j <= k; j++) {

      dp[i + 1][j] |= dp[i][j];
      if (j >= a[i] && i != t)
        dp[i + 1][j] |= dp[i][j - a[i]];
    }
    //}
  }

  for (i = k - a[t]; i < k; i++) {
    if (dp[n][i] == true) {
      return 1;
    }
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <vector>
using namespace std;
int a[5000];
int bubuwa(int t); // tを省いた中で、部分和がk-a[t]以上k未満になるかどうか
int n, k;
bool dp[5001][5001];
int main() {

  // B

  cin >> n >> k;
  int i;
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }

  sort(a, a + n);

  int left, right, mid;
  left = -1;
  right = n;
  mid = (left + right) / 2;

  while (right - left > 1) {

    if (bubuwa(mid) == 1) {

      right = mid;

    } else {

      left = mid;
    }
    mid = (right + left) / 2;
  }

  cout << right << endl;
  return 0;

  // ある値が必要であれば、それ以上もすべて必要である。従って二分探索を
  // 用いて、最初に必要となる数を求める。必要かどうかの判定は部分和問題
  // である。その数を省いた数列から部分和を取って、k未満かつその数があ
  // ればk以上となるような値となるかを調べる

  // A

  /*int x;
  cin >> x;
  int i;
  for (i = 0; i <= x; i++) {

          if (((i * (i - 1)) / 2 < x) && (x <= (i * (i + 1)) / 2)) {
                  cout << i << endl;
                  return 0;
          }

  }
  return 0;
  */
}

int bubuwa(int t) {

  int i, j;
  memset(dp, 0, sizeof(dp));
  dp[0][0] = true;
  for (i = 0; i < n; i++) {
    // if (i != t) {
    for (j = 0; j <= k; j++) {

      dp[i + 1][j] |= dp[i][j];
      if (j >= a[i] && i != t)
        dp[i + 1][j] |= dp[i][j - a[i]];
    }
    //}
  }

  for (i = max(k - a[t], 0); i < k; i++) {
    if (dp[n][i] == true) {
      return 1;
    }
  }
  return 0;
}
Problem p03782 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
int a[6001], f[6001][6001], g[6001][6001], s[6001][6001], i, j, k, n, m, ans;
int main() {
  scanf("%d%d", &n, &m);
  memset(f, 0, sizeof(f));
  memset(g, 0, sizeof(g));
  for (i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  for (i = 1, f[0][0] = 1; i <= n; i++)
    for (j = m; j >= 0; j--) {
      f[i][j] = f[i - 1][j];
      if (j >= a[i])
        f[i][j] |= f[i - 1][j - a[i]];
    }
  for (i = n, g[n + 1][0] = 1; i; i--) {
    for (j = m; j >= 0; j--) {
      g[i][j] = g[i + 1][j];
      if (j >= a[i])
        g[i][j] |= g[i + 1][j - a[i]];
    }
    for (j = 0, s[i][0] = 0; j <= m; j++)
      s[i][j + 1] = s[i][j] + g[i][j];
  }
  for (i = 1, s[n + 1][0] = 0; i <= m + 1; i++)
    s[n + 1][i] = 1;
  for (i = 1, ans = 0; i <= n; i++) {
    for (k = 0, j = 0; j <= m; j++)
      if (f[i - 1][j] && (s[i + 1][m - j] - s[i + 1][max(m - j - a[i], 0)])) {
        k = 1;
        break;
      }
    if (!k)
      ans++;
  }
  printf("%d\n", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
bool f[6001][6001], g[6001][6001];
int a[6001], s[6001][6001], i, j, k, n, m, ans;
int main() {
  scanf("%d%d", &n, &m);
  memset(f, 0, sizeof(f));
  memset(g, 0, sizeof(g));
  for (i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  for (i = 1, f[0][0] = 1; i <= n; i++)
    for (j = m; j >= 0; j--) {
      f[i][j] = f[i - 1][j];
      if (j >= a[i])
        f[i][j] |= f[i - 1][j - a[i]];
    }
  for (i = n, g[n + 1][0] = 1; i; i--) {
    for (j = m; j >= 0; j--) {
      g[i][j] = g[i + 1][j];
      if (j >= a[i])
        g[i][j] |= g[i + 1][j - a[i]];
    }
    for (j = 0, s[i][0] = 0; j <= m; j++)
      s[i][j + 1] = s[i][j] + g[i][j];
  }
  for (i = 1, s[n + 1][0] = 0; i <= m + 1; i++)
    s[n + 1][i] = 1;
  for (i = 1, ans = 0; i <= n; i++) {
    for (k = 0, j = 0; j <= m; j++)
      if (f[i - 1][j] && (s[i + 1][m - j] - s[i + 1][max(m - j - a[i], 0)])) {
        k = 1;
        break;
      }
    if (!k)
      ans++;
  }
  printf("%d\n", ans);
  return 0;
}
Problem p03782 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
#define uint unsigned int
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repr(i, a, b) for (int i = a; i >= b; i--)
#define all(a) (a).begin(), (a).end()
#define sz(a) (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__)
using namespace std;
using pii = pair<int, int>;
constexpr int MOD = 1000000007;
constexpr int INF = 1LL << 30;
constexpr 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, 0, N) if (i != x) repr(j, K, 0) {
    if (j + a[i] <= K)
      dp[j + a[i]] |= dp[j];
  }
  bool ok = 0;
  rep(i, K - a[x], K) ok |= dp[i];
  return !ok;
}

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

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

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long
#define uint unsigned int
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repr(i, a, b) for (int i = a; i >= b; i--)
#define all(a) (a).begin(), (a).end()
#define sz(a) (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__)
using namespace std;
using pii = pair<int, int>;
constexpr int MOD = 1000000007;
constexpr int INF = 1LL << 30;
constexpr 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, 0, N) if (i != x) repr(j, K, 0) {
    if (j + a[i] <= K)
      dp[j + a[i]] |= dp[j];
  }
  bool ok = 0;
  rep(i, max(0LL, K - a[x]), K) ok |= dp[i];
  return !ok;
}

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

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

  return 0;
}
Problem p03782 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
vector<ll> a;
int n, K;
bool judge(int x) {
  vector<vector<bool>> dp(5010, vector<bool>(5010));
  dp[0][0] = true;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= K; j++) {
      if (i == x) {
        dp[i][j] = dp[i][j] || dp[i - 1][j];
      } else {
        dp[i][j] = dp[i][j] || dp[i - 1][j];
        if (dp[i - 1][j] && j + a[i] <= K) {
          dp[i][j + a[i]] = true;
        }
      }
    }
  }
  bool flag = false;
  for (int i = K - a[x]; i <= K - 1; i++) {
    if (dp[n][i]) {
      flag = true;
    }
  }
  return flag;
}
int main() {
  cin >> n >> K;
  a.push_back(0);
  for (int i = 0; i < n; i++) {
    ll b;
    cin >> b;
    a.push_back(b);
  }
  sort(a.begin(), a.end());
  int lb = 0;
  int ub = n + 1;
  while (ub - lb > 1) {
    int mid = (lb + ub) / 2;
    if (judge(mid)) {
      ub = mid;
    } else {
      lb = mid;
    }
  }
  cout << lb << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
vector<ll> a;
int n, K;
bool judge(int x) {
  if (K - a[x] <= 0)
    return true;
  vector<vector<bool>> dp(5010, vector<bool>(5010));
  dp[0][0] = true;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= K; j++) {
      if (i == x) {
        dp[i][j] = dp[i][j] || dp[i - 1][j];
      } else {
        dp[i][j] = dp[i][j] || dp[i - 1][j];
        if (dp[i - 1][j] && j + a[i] <= K) {
          dp[i][j + a[i]] = true;
        }
      }
    }
  }
  bool flag = false;
  for (int i = K - a[x]; i <= K - 1; i++) {
    if (dp[n][i]) {
      flag = true;
    }
  }
  return flag;
}
int main() {
  cin >> n >> K;
  a.push_back(0);
  for (int i = 0; i < n; i++) {
    ll b;
    cin >> b;
    a.push_back(b);
  }
  sort(a.begin(), a.end());
  int lb = 0;
  int ub = n + 1;
  while (ub - lb > 1) {
    int mid = (lb + ub) / 2;
    if (judge(mid)) {
      ub = mid;
    } else {
      lb = mid;
    }
  }
  cout << lb << endl;
}
Problem p03782 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
#define int long long
using pii = std::pair<int, int>;
using namespace std;

const int maxn = 5005;

int n, k, a[maxn];
int32_t dp[2][maxn][maxn], pref[2][maxn][maxn];
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  dp[0][0][0] = dp[1][n + 1][0] = 1;
  pref[0][0][0] = pref[1][n + 1][0] = 1;
  for (int i = 0; i < n; i++)
    for (int j = 0; j <= k; j++)
      if (dp[0][i][j]) {
        dp[0][i + 1][min(j + a[i + 1], k)] = 1;
        dp[0][i + 1][j] = 1;
      }
  for (int i = n + 1; i > 1; i--)
    for (int j = 0; j <= k; j++)
      if (dp[1][i][j]) {
        dp[1][i - 1][min(j + a[i - 1], k)] = 1;
        dp[1][i - 1][j] = 1;
      }
  for (int l = 0; l < 2; l++)
    for (int i = 1; i <= n; i++) {
      pref[l][i][k] = dp[l][i][k];
      for (int j = k - 1; j >= 0; j--)
        pref[l][i][j] = pref[l][i][j + 1] + dp[l][i][j];
    }
  int ans = n;
  for (int i = 1; i <= n; i++) {
    int useful = 0;
    for (int lsum = 0; lsum < k; lsum++)
      if (dp[0][i - 1][lsum]) {
        int sub = k - lsum;
        int add = max(sub - a[i], 0ll);
        assert(add < sub);
        if (pref[1][i + 1][add] - pref[1][i + 1][sub] > 0)
          useful = 1;
      }
    ans -= useful;
  }
  cout << ans << "\n";
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long
using pii = std::pair<int, int>;
using namespace std;

const int maxn = 5005;

int n, k, a[maxn];
int16_t dp[2][maxn][maxn], pref[2][maxn][maxn];
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  dp[0][0][0] = dp[1][n + 1][0] = 1;
  pref[0][0][0] = pref[1][n + 1][0] = 1;
  for (int i = 0; i < n; i++)
    for (int j = 0; j <= k; j++)
      if (dp[0][i][j]) {
        dp[0][i + 1][min(j + a[i + 1], k)] = 1;
        dp[0][i + 1][j] = 1;
      }
  for (int i = n + 1; i > 1; i--)
    for (int j = 0; j <= k; j++)
      if (dp[1][i][j]) {
        dp[1][i - 1][min(j + a[i - 1], k)] = 1;
        dp[1][i - 1][j] = 1;
      }
  for (int l = 0; l < 2; l++)
    for (int i = 1; i <= n; i++) {
      pref[l][i][k] = dp[l][i][k];
      for (int j = k - 1; j >= 0; j--)
        pref[l][i][j] = pref[l][i][j + 1] + dp[l][i][j];
    }
  int ans = n;
  for (int i = 1; i <= n; i++) {
    int useful = 0;
    for (int lsum = 0; lsum < k; lsum++)
      if (dp[0][i - 1][lsum]) {
        int sub = k - lsum;
        int add = max(sub - a[i], 0ll);
        assert(add < sub);
        if (pref[1][i + 1][add] - pref[1][i + 1][sub] > 0)
          useful = 1;
      }
    ans -= useful;
  }
  cout << ans << "\n";
  return 0;
}
Problem p03782 (C++)

Buggy submission (status: Time Limit Exceeded):
// Copyright 2017 wu_qing
#include <algorithm>
#include <bitset>
#include <cstdio>
const int N = 5e3 + 10, M = N * N;
std::bitset<M> b;
int n, k, t, l, r, cnt, a[N];
inline bool check(int x) {
  b.reset();
  b[0] = 1;
  for (int i = 1; i <= cnt; i++) {
    if (i == x)
      continue;
    b |= b << a[i];
  }
  for (int i = k - a[x]; i < k; i++)
    if (b[i])
      return true;
  return false;
}
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &t);
    if (t <= k)
      a[++cnt] = t;
  }
  std::sort(a + 1, a + cnt + 1);
  l = 0;
  r = cnt;
  while (l < r) {
    int m = (l + r) / 2;
    if (check(m + 1))
      r = m;
    else
      l = m + 1;
  }
  printf("%d\n", r);
  return 0;
}


Fixed submission:
// Copyright 2017 wu_qing
#include <algorithm>
#include <bitset>
#include <cstdio>
const int N = 5e3 + 10;
std::bitset<N> b;
int n, k, t, l, r, cnt, a[N];
inline bool check(int x) {
  b.reset();
  b[0] = 1;
  for (int i = 1; i <= cnt; i++) {
    if (i == x)
      continue;
    b |= b << a[i];
  }
  for (int i = k - a[x]; i < k; i++)
    if (b[i])
      return true;
  return false;
}
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &t);
    if (t <= k)
      a[++cnt] = t;
  }
  std::sort(a + 1, a + cnt + 1);
  l = 0;
  r = cnt;
  while (l < r) {
    int m = (l + r) / 2;
    if (check(m + 1))
      r = m;
    else
      l = m + 1;
  }
  printf("%d\n", r);
  return 0;
}
Problem p03782 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;

// template
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (a); i > (b); i--)
#define ALL(v) (v).begin(), (v).end()
typedef long long int ll;
typedef pair<ll, ll> P;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  fill((T *)array, (T *)(array + N), val);
}
const int inf = INT_MAX / 2;
const ll INF = LLONG_MAX / 2;
// template end

int n, k;
vector<int> a, b;
bool f(int x) {
  b = a;
  b.erase(b.begin() + x);
  bool dp[5010][5010] = {};
  dp[0][0] = 1;
  rep(i, 0, n - 1) {
    rep(j, 0, k + 1) if (dp[i][j]) {
      dp[i + 1][j] = 1;
      dp[i + 1][min(k, j + b[i])] = 1;
    }
  }
  bool res = 0;
  rep(i, max(0, k - a[x]), k) if (dp[n - 1][i]) res = 1;
  return res;
}

int main() {
  scanf("%d%d", &n, &k);
  a.resize(n);
  rep(i, 0, n) scanf("%d", &a[i]);
  sort(ALL(a));
  int cnt = 0;
  rep(i, 0, n) if (f(i)) cnt++;
  printf("%d\n", n - cnt);
  return 0;
}

Fixed submission:
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;

// template
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (a); i > (b); i--)
#define ALL(v) (v).begin(), (v).end()
typedef long long int ll;
typedef pair<ll, ll> P;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  fill((T *)array, (T *)(array + N), val);
}
const int inf = INT_MAX / 2;
const ll INF = LLONG_MAX / 2;
// template end

int n, k;
vector<int> a, b;
bool f(int x) {
  b = a;
  b.erase(b.begin() + x);
  bool dp[5010][5010] = {};
  dp[0][0] = 1;
  rep(i, 0, n - 1) {
    rep(j, 0, k + 1) if (dp[i][j]) {
      dp[i + 1][j] = 1;
      dp[i + 1][min(k, j + b[i])] = 1;
    }
  }
  bool res = 0;
  rep(i, max(0, k - a[x]), k) if (dp[n - 1][i]) res = 1;
  return res;
}

int main() {
  scanf("%d%d", &n, &k);
  a.resize(n);
  rep(i, 0, n) scanf("%d", &a[i]);
  sort(ALL(a));
  reverse(ALL(a));
  int l = -1, r = n, mid;
  while (r - l > 1) {
    mid = (l + r) / 2;
    if (f(mid))
      l = mid;
    else
      r = mid;
  }
  printf("%d\n", n - r);
  return 0;
}
Problem p03782 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

int fp[5005][5005], fr[5005][5005], sr[5005][5005];
int n, k, a[5005], an;

int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; ++i)
    scanf("%d", &a[i]), a[i] = min(a[i], k);
  fp[0][0] = 1;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j <= k; ++j) {
      fp[i + 1][j] |= fp[i][j];
      fp[i + 1][min(j + a[i + 1], k)] |= fp[i][j];
    }
  }
  fr[n + 1][0] = 1;
  for (int i = n + 1; i > 1; --i) {
    for (int j = k; j >= 0; --j) {
      fr[i - 1][j] |= fr[i][j];
      fr[i - 1][min(j + a[i - 1], k)] |= fr[i][j];
    }
  }
  for (int i = 1; i <= n + 1; ++i) {
    sr[i][0] = 1;
    for (int j = 1; j <= k; ++j)
      sr[i][j] = sr[i][j - 1] + fr[i][j];
  }
  an = n;
  for (int i = 1; i <= n; ++i) {
    bool ok = 0;
    for (int j = 0; j < k; ++j) {
      int l = k - a[i] - j, r = k - 1 - j;
      l = max(l, 0);
      ok |= (fp[i - 1][j] & (sr[i + 1][r] - (l ? sr[i + 1][l - 1] : 0) > 0));
    }
    an -= ok;
  }
  printf("%d", an);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

bool fp[5005][5005], fr[5005][5005];
int sr[5005][5005];
int n, k, a[5005], an;

int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; ++i)
    scanf("%d", &a[i]), a[i] = min(a[i], k);
  fp[0][0] = 1;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j <= k; ++j) {
      fp[i + 1][j] |= fp[i][j];
      fp[i + 1][min(j + a[i + 1], k)] |= fp[i][j];
    }
  }
  fr[n + 1][0] = 1;
  for (int i = n + 1; i > 1; --i) {
    for (int j = k; j >= 0; --j) {
      fr[i - 1][j] |= fr[i][j];
      fr[i - 1][min(j + a[i - 1], k)] |= fr[i][j];
    }
  }
  for (int i = 1; i <= n + 1; ++i) {
    sr[i][0] = 1;
    for (int j = 1; j <= k; ++j)
      sr[i][j] = sr[i][j - 1] + fr[i][j];
  }
  an = n;
  for (int i = 1; i <= n; ++i) {
    bool ok = 0;
    for (int j = 0; j < k; ++j) {
      int l = k - a[i] - j, r = k - 1 - j;
      l = max(l, 0);
      ok |= (fp[i - 1][j] & (sr[i + 1][r] - (l ? sr[i + 1][l - 1] : 0) > 0));
    }
    an -= ok;
  }
  printf("%d", an);
  return 0;
}
Problem p03782 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define up(i, j, n) for (int i = j; i <= n; i++)
#define down(i, j, n) for (int i = j; i >= n; i--)
#define cmax(a, b) a = max(a, b)
#define cmin(a, b) a = min(a, b)

const int MAXN = 5005;
const int oo = 0x3f3f3f3f;

int N, K, a[MAXN], num[MAXN], top = 0, ans = 0;
bitset<MAXN> F;

bool vaild(int o) {
  bool ok = 1;
  F.reset();
  F[0] = 1;
  up(i, 1, N) {
    if (a[i] == num[o] && ok) {
      ok = 0;
      continue;
    }
    F |= F << a[i];
  }
  up(i, K - num[o], K - 1) if (F[i]) return 1;
  return 0;
}

int main() {
  scanf("%d%d", &N, &K);
  up(i, 1, N) scanf("%d", &a[i]);
  up(i, 1, N) if (a[i] < K) num[++top] = a[i];
  N = top;
  up(i, 1, N) a[i] = num[i];
  sort(num + 1, num + top + 1);
  top = unique(num + 1, num + top + 1) - (num + 1);
  num[top + 1] = oo;
  int le = 1, ri = top, mi;
  while (le + 1 < ri) {
    mi = (le + ri) >> 1;
    if (vaild(mi))
      ri = mi;
    else
      le = mi;
  }
  int o = top + 1;
  if (vaild(le))
    o = le;
  else if (vaild(ri))
    o = ri;
  up(i, 1, N) if (a[i] < num[o]) ans++;
  printf("%d\n", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define up(i, j, n) for (int i = j; i <= n; i++)
#define down(i, j, n) for (int i = j; i >= n; i--)
#define cmax(a, b) a = max(a, b)
#define cmin(a, b) a = min(a, b)

const int MAXN = 5005;
const int oo = 0x3f3f3f3f;

int N, K, a[MAXN], num[MAXN], top = 0, ans = 0;
bitset<MAXN> F;

bool vaild(int o) {
  bool ok = 1;
  F.reset();
  F[0] = 1;
  up(i, 1, N) {
    if (a[i] == num[o] && ok) {
      ok = 0;
      continue;
    }
    F |= F << a[i];
  }
  up(i, K - num[o], K - 1) if (F[i]) return 1;
  return 0;
}

int main() {
  scanf("%d%d", &N, &K);
  up(i, 1, N) scanf("%d", &a[i]);
  up(i, 1, N) if (a[i] < K) num[++top] = a[i];
  if (!top) {
    puts("0");
    return 0;
  }
  N = top;
  up(i, 1, N) a[i] = num[i];
  sort(num + 1, num + top + 1);
  top = unique(num + 1, num + top + 1) - (num + 1);
  num[top + 1] = oo;
  int le = 1, ri = top, mi;
  while (le + 1 < ri) {
    mi = (le + ri) >> 1;
    if (vaild(mi))
      ri = mi;
    else
      le = mi;
  }
  int o = top + 1;
  if (vaild(le))
    o = le;
  else if (vaild(ri))
    o = ri;
  up(i, 1, N) if (a[i] < num[o]) ans++;
  printf("%d\n", ans);
  return 0;
}
Problem p03782 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstring>
#include <iostream>

constexpr int MAX = 5010;

int n, k;

int a[MAX];
bool dp[MAX][MAX];

bool C(int x) {

  for (int i = std::max(k - x, 0); i < k; ++i) {
    if (dp[n - 1][i])
      return true;
  }
  return false;
}

int main() {

  std::cin >> n >> k;

  for (int i = 0; i < n; ++i)
    std::cin >> a[i];

  std::sort(a, a + n);

  int lb = -1, ub = n;

  while (ub - lb > 1) {

    int mid = (lb + ub) / 2;

    std::memset(dp, false, sizeof(dp));

    dp[0][0] = true;

    if (mid != 0)
      dp[0][a[0]] = true;

    for (int i = 1; i < n; ++i) {
      for (int j = 0; j < k; ++j) {

        if (!(dp[i][j] = dp[i - 1][j]) && i != mid && j >= a[i])
          dp[i][j] = dp[i - 1][j - a[i]];
      }
    }

    if (C(a[mid]))
      ub = mid;
    else
      lb = mid;
  }

  std::cout << ub << std::endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstring>
#include <iostream>

constexpr int MAX = 5010;

int n, k;

int a[MAX];
bool dp[MAX][MAX];

bool C(int x) {

  for (int i = std::max(k - x, 0); i < k; ++i) {
    if (dp[n - 1][i])
      return true;
  }
  return false;
}

int main() {

  std::cin >> n >> k;

  for (int i = 0; i < n; ++i)
    std::cin >> a[i];

  std::sort(a, a + n);

  int lb = -1, ub = n;

  while (ub - lb > 1) {

    int mid = (lb + ub) / 2;

    std::memset(dp, false, sizeof(dp));

    dp[0][0] = true;

    if (mid != 0 && k > a[0])
      dp[0][a[0]] = true;

    for (int i = 1; i < n; ++i) {
      for (int j = 0; j < k; ++j) {

        if (!(dp[i][j] = dp[i - 1][j]) && i != mid && j >= a[i])
          dp[i][j] = dp[i - 1][j - a[i]];
      }
    }

    if (C(a[mid]))
      ub = mid;
    else
      lb = mid;
  }

  std::cout << ub << std::endl;

  return 0;
}
Problem p03782 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL_DEBUG
#include "LOCAL_DEBUG.hpp"
#endif
#define int long long

signed main() {

  int n, K;
  cin >> n >> K;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  sort(a.begin(), a.end());
  int left = -1, right = n;
  while (right - left > 1) {
    int mid = (left + right) / 2;
    vector<int> dp(K + 1, 0);
    dp[0] = 1;
    for (int j = 0; j < n; j++) {
      if (j == mid)
        continue;
      for (int k = K - 1; k >= 0; k--) {
        dp[min(K, k + a[j])] |= dp[k];
      }
    }
    int cnt = 0;
    for (int i = K - a[mid]; i < K; i++)
      cnt += dp[i];
    (cnt <= 0 ? left : right) = mid;
  }
  cout << right << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL_DEBUG
#include "LOCAL_DEBUG.hpp"
#endif
#define int long long

signed main() {

  int n, K;
  cin >> n >> K;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  sort(a.begin(), a.end());
  int left = -1, right = n;
  while (right - left > 1) {
    int mid = (left + right) / 2;
    vector<int> dp(K + 1, 0);
    dp[0] = 1;
    for (int j = 0; j < n; j++) {
      if (j == mid)
        continue;
      for (int k = K - 1; k >= 0; k--) {
        dp[min(K, k + a[j])] |= dp[k];
      }
    }
    int cnt = 0;
    for (int i = max(0LL, K - a[mid]); i < K; i++)
      cnt += dp[i];
    (cnt <= 0 ? left : right) = mid;
  }
  cout << right << endl;

  return 0;
}
Problem p03782 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 5010;

int n, k;
vector<int> a;
int dp1[MAX_N][MAX_N], dp2[MAX_N][MAX_N];
int pref[MAX_N][MAX_N];

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    int t;
    cin >> t;
    if (t < k) {
      a.push_back(t);
    }
  }
  n = a.size();
  dp1[0][0] = 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= k; j++) {
      if (dp1[i][j]) {
        dp1[i + 1][j] = 1;
        if (j + a[i] <= k) {
          dp1[i + 1][j + a[i]] = 1;
        }
      }
    }
  }
  dp2[0][0] = 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= k; j++) {
      if (dp2[i][j]) {
        dp2[i + 1][j] = 1;
        if (j + a[n - 1 - i] <= k) {
          dp2[i + 1][j + a[n - 1 - i]] = 1;
        }
      }
    }
  }
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= k; j++) {
      pref[i][j + 1] = pref[i][j] + dp2[i][j];
    }
  }
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= k; j++) {
      if (dp1[i][j]) {
        int x = pref[n - 1 - i][k - j];
        if (k >= a[i] + j) {
          x -= pref[n - 1 - i][k - a[i] - j];
        }
        if (x > 0) {
          break;
        }
      }
      if (j == k) {
        cnt++;
      }
    }
  }
  cout << cnt << "\n";
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 5010;

int n, k;
vector<int> a;
bool dp1[MAX_N][MAX_N], dp2[MAX_N][MAX_N];
short pref[MAX_N][MAX_N];

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    int t;
    cin >> t;
    if (t < k) {
      a.push_back(t);
    }
  }
  n = a.size();
  dp1[0][0] = 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= k; j++) {
      if (dp1[i][j]) {
        dp1[i + 1][j] = 1;
        if (j + a[i] <= k) {
          dp1[i + 1][j + a[i]] = 1;
        }
      }
    }
  }
  dp2[0][0] = 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= k; j++) {
      if (dp2[i][j]) {
        dp2[i + 1][j] = 1;
        if (j + a[n - 1 - i] <= k) {
          dp2[i + 1][j + a[n - 1 - i]] = 1;
        }
      }
    }
  }
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= k; j++) {
      pref[i][j + 1] = pref[i][j] + dp2[i][j];
    }
  }
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= k; j++) {
      if (dp1[i][j]) {
        int x = pref[n - 1 - i][k - j];
        if (k >= a[i] + j) {
          x -= pref[n - 1 - i][k - a[i] - j];
        }
        if (x > 0) {
          break;
        }
      }
      if (j == k) {
        cnt++;
      }
    }
  }
  cout << cnt << "\n";
}