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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef pair<ll, ll> pll;
const ll mod = 1e9 + 7;
// const ll mod=998244353;
const ll inf = 1LL << 61;

int main() {
  ll n, k;
  cin >> n >> k;
  vec a(n);
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  sort(a.begin(), a.end());
  ll ok = n, ng = -1;
  while (ng + 1 < ok) {
    ll m = (ok + ng) / 2;
    mat dp(n + 1, vec(k));
    dp[0][0] = 1;
    for (ll i = 0; i < n; i++) {
      for (ll j = 0; j < k; j++) {
        dp[i + 1][j] |= dp[i][j];
        if (i != m && j + a[i] < k) {
          dp[i + 1][j + a[i]] |= dp[i][j];
        }
      }
    }
    ll f = 0;
    for (ll i = k - a[m]; i < k; i++) {
      if (dp[n][i])
        f = 1;
    }
    if (f)
      ok = m;
    else
      ng = m;
  }
  cout << ng + 1 << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef pair<ll, ll> pll;
const ll mod = 1e9 + 7;
// const ll mod=998244353;
const ll inf = 1LL << 61;

int main() {
  ll n, k;
  cin >> n >> k;
  vec a(n);
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  sort(a.begin(), a.end());
  ll ok = n, ng = -1;
  while (ng + 1 < ok) {
    ll m = (ok + ng) / 2;
    mat dp(n + 1, vec(k));
    dp[0][0] = 1;
    for (ll i = 0; i < n; i++) {
      for (ll j = 0; j < k; j++) {
        dp[i + 1][j] |= dp[i][j];
        if (i != m && j + a[i] < k) {
          dp[i + 1][j + a[i]] |= dp[i][j];
        }
      }
    }
    ll f = 0;
    for (ll i = max(k - a[m], 0LL); i < k; i++) {
      if (dp[n][i])
        f = 1;
    }
    if (f)
      ok = m;
    else
      ng = m;
  }
  cout << ng + 1 << endl;
}
Problem p03780 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define int long long
#define REP(i, n) for (int i = 0; i < n; i++)
typedef pair<int, int> P;
int a[5005];
int dp[5005][5005];
void init() {
  REP(i, 5005)
  REP(j, 5005) dp[i][j] = 0;
}
signed main() {
  int n, k;
  cin >> n >> k;
  REP(i, n) cin >> a[i];
  sort(a, a + n);

  int ok = -1, ng = n;
  while (ng - ok > 1) {
    int i = (ok + ng) / 2;
    init();
    dp[0][0] = 1;
    for (int j = 0; j < n; j++) { // card
      for (int l = 0; l < k; l++) {
        dp[j + 1][l] += dp[j][l];
        if (j != i && l + a[j] <= k)
          dp[j + 1][l + a[j]] += dp[j][l];
      }
    }
    int flg = 0;
    for (int j = k - a[i]; j <= k - 1; j++) {
      if (dp[n][j])
        flg++;
    }
    if (flg)
      ng = i;
    else
      ok = i;
  }
  cout << ng << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define int long long
#define REP(i, n) for (int i = 0; i < n; i++)
typedef pair<int, int> P;
int a[5005];
int dp[5005][5005];
void init() {
  REP(i, 5005)
  REP(j, 5005) dp[i][j] = 0;
}
signed main() {
  int n, k;
  cin >> n >> k;
  REP(i, n) cin >> a[i];
  sort(a, a + n);

  int ok = -1, ng = n;
  while (ng - ok > 1) {
    int i = (ok + ng) / 2;
    init();
    dp[0][0] = 1;
    for (int j = 0; j < n; j++) { // card
      for (int l = 0; l < k; l++) {
        dp[j + 1][l] += dp[j][l];
        if (j != i && l + a[j] <= k)
          dp[j + 1][l + a[j]] += dp[j][l];
      }
    }
    int flg = 0;
    for (int j = max(0ll, k - a[i]); j <= k - 1; j++) {
      if (dp[n][j])
        flg++;
    }
    if (flg)
      ng = i;
    else
      ok = i;
  }
  cout << ng << endl;
}
Problem p03781 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <vector>

using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repr(i, n) for (ll i = n - 1; i >= 0; i--)
#define repone(i, n) for (ll i = 1; i <= (ll)(n); i++)
#define each(i, mp) for (auto i : mp)
#define FOR(i, m, n) for (ll i = m; i < n; i++)
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
const ll mod = 1000000007;
const ll inf = ll(1e9);
const ll half_inf = ll(1e5);
const ll ll_inf = ll(1e9) * ll(1e9);

int main() {
  ll x;
  cin >> x;
  ll i = 1;
  while (true) {
    if (2 * x <= i * (i + 1)) {
      cout << i << endl;
      return 0;
    }
  }
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <vector>

using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repr(i, n) for (ll i = n - 1; i >= 0; i--)
#define repone(i, n) for (ll i = 1; i <= (ll)(n); i++)
#define each(i, mp) for (auto i : mp)
#define FOR(i, m, n) for (ll i = m; i < n; i++)
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
const ll mod = 1000000007;
const ll inf = ll(1e9);
const ll half_inf = ll(1e5);
const ll ll_inf = ll(1e9) * ll(1e9);

int main() {
  ll x;
  cin >> x;
  ll i = 1;
  while (true) {
    if (2 * x <= i * (i + 1)) {
      cout << i << endl;
      return 0;
    }
    i++;
  }
}
Problem p03781 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
typedef vector<ll> vel;
typedef vector<int> vei;
typedef vector<char> vec;
typedef vector<bool> veb;
typedef vector<string> ves;
typedef vector<vector<ll>> ve_vel;
typedef vector<vector<int>> ve_vei;
typedef vector<vector<char>> ve_vec;
typedef vector<vector<bool>> ve_veb;
typedef vector<vector<string>> ve_ves;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i < (int)(n); i++)
#define rep2(i, n) for (int i = 2; i < (int)(n); i++)
#define repk(i, k, n) for (int i = k; i < (int)(n); i++)
#define fs first
#define sc second
#define pub push_back
#define pob pop_back
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define maxel(a) *max_element(all(a))
#define minel(a) *min_element(all(a))
#define acc accumulate
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define mod (1000000007)
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

ll X;
vel dp(1000000005, 0);

ll time(ll X) {
  if (X == 0)
    return 0;
  if (dp[X] != 0)
    return dp[X];
  return dp[X] = min(time(X - time(X) - 1) + 1, time(X + time(X) + 1) + 1);
}

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

  ll X;
  cin >> X;
  for (ll ans = 1; ans <= 100000000; ans++) {
    if (ans * (ans + 1) >= 2 * X) {
      cout << ans << endl;
      return 0;
    }
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
typedef vector<ll> vel;
typedef vector<int> vei;
typedef vector<char> vec;
typedef vector<bool> veb;
typedef vector<string> ves;
typedef vector<vector<ll>> ve_vel;
typedef vector<vector<int>> ve_vei;
typedef vector<vector<char>> ve_vec;
typedef vector<vector<bool>> ve_veb;
typedef vector<vector<string>> ve_ves;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i < (int)(n); i++)
#define rep2(i, n) for (int i = 2; i < (int)(n); i++)
#define repk(i, k, n) for (int i = k; i < (int)(n); i++)
#define fs first
#define sc second
#define pub push_back
#define pob pop_back
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define maxel(a) *max_element(all(a))
#define minel(a) *min_element(all(a))
#define acc accumulate
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define mod (1000000007)
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

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

  ll X;
  cin >> X;
  for (ll ans = 1; ans <= 100000000; ans++) {
    if (ans * (ans + 1) >= 2 * X) {
      cout << ans << endl;
      return 0;
    }
  }
  return 0;
}
Problem p03781 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <fstream>
#include <iostream>
#include <math.h>
#include <stack>
#include <vector>
using namespace std;

int main() {
  int N;
  while (scanf("%d", &N) != NULL) {
    int ans = sqrt(1 + 8 * N);
    int res = 0;
    if (ans * ans == 1 + 8 * N) {
      res = (ans - 1) / 2;
    } else
      res = (ans - 1) / 2 + 1;
    cout << res << endl;
  }
  return 0;
}


Fixed submission:
#include <fstream>
#include <iostream>
#include <math.h>
#include <stack>
#include <vector>
using namespace std;

int main() {
  int N;
  cin >> N;
  int ans = 1;
  for (ans = 1; ans * (ans + 1) / 2 < N; ans++)
    ;
  cout << ans << endl;
  return 0;
}
Problem p03781 (Python)

Buggy submission (status: Time Limit Exceeded):
#!/usr/bin/env python3

x = int(input())
S = {0}
i = 0
while x not in S:
    i += 1
    S = S | {s - i for s in S} | {s + i for s in S}
print(i)


Fixed submission:
#!/usr/bin/env python3

import math

print(math.ceil((-1 + (8 * int(input()) + 1) ** 0.5) / 2))
Problem p03782 (C++)

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

// why am I so weak

int n, k;
int a[5055];
bitset<5000> l[5055], r[5055];

int last[5055];

int main() {
  scanf("%d %d", &n, &k);

  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }

  l[0].set(0);
  if (a[0] < k)
    l[0].set(a[0]);

  r[n - 1].set(0);
  if (a[0] < k)
    r[n - 1].set(a[n - 1]);

  for (int i = 1; i < n; i++) {
    l[i] = l[i - 1];
    l[i] |= l[i - 1] << a[i];
  }

  for (int i = n - 2; i >= 0; i--) {
    r[i] = r[i + 1];
    r[i] |= r[i + 1] << a[i];
  }

  int res = 0;

  for (int i = 0; i < n; i++)
    if (a[i] < k) {
      memset(last, -1, sizeof(last));

      bool cool = false;

      if (i) {
        for (int j = 0; j < k; j++) {
          if (l[i - 1][j])
            last[j] = j;
          else
            last[j] = last[j - 1];

          if (l[i - 1][j] && j + a[i] >= k)
            cool = true;
        }
      }

      if (i + 1 < n) {
        for (int j = 0; j < k; j++)
          if (r[i + 1][j]) {
            // x + j < k
            // x < k - j

            if (j + a[i] >= k) {
              cool = true;
              break;
            }

            int bound = k - j - 1;

            if (bound >= 0) {
              int wow = last[bound];

              if (~wow && wow + j + a[i] >= k) {
                cool = true;
                break;
              }
            }
          }
      }

      if (!cool)
        res++;
    }

  printf("%d\n", res);

  return 0;
}

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

// why am I so weak

int n, k;
int a[5055];
bitset<5000> l[5055], r[5055];

int last[5055];

int main() {
  scanf("%d %d", &n, &k);

  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }

  l[0].set(0);
  if (a[0] < k)
    l[0].set(a[0]);

  r[n - 1].set(0);
  if (a[n - 1] < k)
    r[n - 1].set(a[n - 1]);

  for (int i = 1; i < n; i++) {
    l[i] = l[i - 1];
    l[i] |= l[i - 1] << a[i];
  }

  for (int i = n - 2; i >= 0; i--) {
    r[i] = r[i + 1];
    r[i] |= r[i + 1] << a[i];
  }

  int res = 0;

  for (int i = 0; i < n; i++)
    if (a[i] < k) {
      memset(last, -1, sizeof(last));

      bool cool = false;

      if (i) {
        for (int j = 0; j < k; j++) {
          if (l[i - 1][j])
            last[j] = j;
          else
            last[j] = last[j - 1];

          if (l[i - 1][j] && j + a[i] >= k)
            cool = true;
        }
      }

      if (i + 1 < n) {
        for (int j = 0; j < k; j++)
          if (r[i + 1][j]) {
            // x + j < k
            // x < k - j

            if (j + a[i] >= k) {
              cool = true;
              break;
            }

            int bound = k - j - 1;

            if (bound >= 0) {
              int wow = last[bound];

              if (~wow && wow + j + a[i] >= k) {
                cool = true;
                break;
              }
            }
          }
      }

      if (!cool)
        res++;
    }

  printf("%d\n", res);

  return 0;
}
Problem p03782 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ALL(c) (c).begin(), (c).end()
#define EACH(i, c) for (auto i = (c).begin(); i != (c).end(); ++i)
using namespace std;
#define int long long

bool dp[5010];

bool check(int c, int K, const vector<int> &a) {
  memset(dp, 0, sizeof(dp));

  dp[0] = true;
  for (int i = 0; i < a.size(); ++i) {
    if (i == c)
      continue;
    for (int j = K; j >= 0; --j) {
      dp[j] |= dp[j - a[i]];
    }
  }

  // cout << a[c] << ":";
  // for(int i = 0; i <= K; ++i) cout << dp[i];
  // cout << endl;
  for (int i = max(K - a[c], 0LL); i < K; ++i) {
    if (dp[i])
      return true;
  }
  return false;
}

signed main() {
  int N, K;
  cin >> N >> K;
  vector<int> a(N);
  for (int i = 0; i < N; ++i)
    cin >> a[i];
  sort(ALL(a));
  int l = -1, u = N;
  while (u - l > 1) {
    int c = (l + u) / 2;
    if (check(c, K, a)) {
      u = c;
    } else {
      l = c;
    }
  }
  cout << u << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ALL(c) (c).begin(), (c).end()
#define EACH(i, c) for (auto i = (c).begin(); i != (c).end(); ++i)
using namespace std;
#define int long long

bool dp[5010];

bool check(int c, int K, const vector<int> &a) {
  memset(dp, 0, sizeof(dp));

  dp[0] = true;
  for (int i = 0; i < a.size(); ++i) {
    if (i == c)
      continue;
    for (int j = K; j >= a[i]; --j) {
      dp[j] |= dp[j - a[i]];
    }
  }

  // cout << a[c] << ":";
  // for(int i = 0; i <= K; ++i) cout << dp[i];
  // cout << endl;
  for (int i = max(K - a[c], 0LL); i < K; ++i) {
    if (dp[i])
      return true;
  }
  return false;
}

signed main() {
  int N, K;
  cin >> N >> K;
  vector<int> a(N);
  for (int i = 0; i < N; ++i)
    cin >> a[i];
  sort(ALL(a));
  int l = -1, u = N;
  while (u - l > 1) {
    int c = (l + u) / 2;
    if (check(c, K, a)) {
      u = c;
    } else {
      l = c;
    }
  }
  cout << u << endl;
  return 0;
}
Problem p03782 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define FOR(i, begin, end) for (int i = (begin); i < (end); i++)
#define REP(i, n) FOR(i, 0, n)
#define IFOR(i, begin, end) for (int i = (end)-1; i >= (begin); i--)
#define IREP(i, n) IFOR(i, 0, n)
#define SORT(a) sort(a.begin(), a.end())
#define REVERSE(a) reverse(a.begin(), a.end())
// #define int long long
#define INF 1000000000000000
using namespace std;

typedef vector<int> vec;
typedef vector<vec> mat;
typedef pair<int, int> Pii;

template <typename T> void readvec(vector<T> &a);
void readindex(vector<int> &a);

bool is_needed(vec &vals, int i, int K) {
  vector<bool> dp(K, false);
  dp[0] = true;
  REP(j, vals.size()) {
    vector<bool> tmp = dp;
    if (j == i)
      continue;
    REP(k, K - vals[j]) if (dp[k]) tmp[k + vals[j]] = true;
    dp = tmp;
  }
  bool needed = false;
  FOR(k, K - vals[i], K) {
    if (dp[k]) {
      needed = true;
      break;
    }
  }
  return needed;
}

signed main() {

  int N, K;
  cin >> N >> K;
  vec a(N);
  readvec(a);
  vec vals(0);
  REP(i, N) if (a[i] < K) vals.push_back(a[i]);

  SORT(vals);
  if (is_needed(vals, 0, K)) {
    cout << 0;
    return 0;
  }

  int i1 = 0, i2 = vals.size();
  while (i2 - i1 > 1) {
    int i = (i1 + i2) / 2;
    if (is_needed(vals, i, K)) {
      i2 = i;
    } else {
      i1 = i;
    }
  }

  cout << i2;

  return 0;
}

template <typename T> void readvec(vector<T> &a) {
  REP(i, a.size()) { cin >> a[i]; }
}
void readindex(vector<int> &a) {
  REP(i, a.size()) {
    cin >> a[i];
    a[i]--;
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define FOR(i, begin, end) for (int i = (begin); i < (end); i++)
#define REP(i, n) FOR(i, 0, n)
#define IFOR(i, begin, end) for (int i = (end)-1; i >= (begin); i--)
#define IREP(i, n) IFOR(i, 0, n)
#define SORT(a) sort(a.begin(), a.end())
#define REVERSE(a) reverse(a.begin(), a.end())
// #define int long long
#define INF 1000000000000000
using namespace std;

typedef vector<int> vec;
typedef vector<vec> mat;
typedef pair<int, int> Pii;

template <typename T> void readvec(vector<T> &a);
void readindex(vector<int> &a);

bool is_needed(vec &vals, int i, int K) {
  vector<bool> dp(K, false);
  dp[0] = true;
  REP(j, vals.size()) {
    vector<bool> tmp = dp;
    if (j == i)
      continue;
    REP(k, K - vals[j]) if (dp[k]) tmp[k + vals[j]] = true;
    dp = tmp;
  }
  bool needed = false;
  FOR(k, K - vals[i], K) {
    if (dp[k]) {
      needed = true;
      break;
    }
  }
  return needed;
}

signed main() {

  int N, K;
  cin >> N >> K;
  vec a(N);
  readvec(a);
  vec vals(0);
  REP(i, N) if (a[i] < K) vals.push_back(a[i]);

  SORT(vals);
  if (vals.size() == 0) {
    cout << 0;
    return 0;
  }
  if (is_needed(vals, 0, K)) {
    cout << 0;
    return 0;
  }

  int i1 = 0, i2 = vals.size();
  while (i2 - i1 > 1) {
    int i = (i1 + i2) / 2;
    if (is_needed(vals, i, K)) {
      i2 = i;
    } else {
      i1 = i;
    }
  }

  cout << i2;

  return 0;
}

template <typename T> void readvec(vector<T> &a) {
  REP(i, a.size()) { cin >> a[i]; }
}
void readindex(vector<int> &a) {
  REP(i, a.size()) {
    cin >> a[i];
    a[i]--;
  }
}
Problem p03782 (C++)

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

int dp[5010][2];

int n, k;
int a[5001];
bool isnec(int x) {
  for (int i = 0; i <= k; i++)
    dp[i][0] = dp[i][1] = 0;

  int z = 0;

  dp[0][0] = 1;
  for (int i = 0; i < n; i++) {
    if (i == x)
      continue;
    for (int j = 0; j <= k; j++) {
      dp[j][1 - z] = dp[j][z] || (j >= a[i] && dp[j - a[i]][z]);
    }

    z = 1 - z;
  }

  for (int j = max(k - a[x], 0); j < k; j++) {
    if (dp[j][z])
      return true;
  }

  return false;
}

int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int cnt = 0;
  for (int i = 0; i < n; i++)
    if (!isnec(i))
      cnt++;
  cout << cnt << "\n";
  return 0;
}

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

int dp[5010][2];

int n, k;
int a[5001];
bool isnec(int x) {
  for (int i = 0; i <= k; i++)
    dp[i][0] = dp[i][1] = 0;

  int z = 0;

  dp[0][0] = 1;
  for (int i = 0; i < n; i++) {
    if (i == x)
      continue;
    for (int j = 0; j <= k; j++) {
      dp[j][1 - z] = dp[j][z] || (j >= a[i] && dp[j - a[i]][z]);
    }

    z = 1 - z;
  }

  for (int j = max(k - a[x], 0); j < k; j++) {
    if (dp[j][z])
      return true;
  }

  return false;
}

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

  sort(a, a + n);

  int p = 0;
  int q = n;

  while (q - p > 0) {
    int s = (p + q) / 2;
    if (!isnec(s)) {
      p = s + 1;
    } else {
      q = s;
    }
  }

  cout << q << "\n";
  return 0;
}
Problem p03782 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define LL long long
#define mp make_pair
#define pb push_back
#define INF (1 << 30)
using namespace std;
typedef pair<int, int> pii;
int n, K;
vector<int> a;
int tot;
// int a[5010];
int f[5010];
bool check(int k) {
  int t = a[k];
  a.erase(a.begin() + k);
  int m = n - 1;
  memset(f, 0, sizeof(f));
  f[0] = 1;
  for (int i = 1; i <= m; i++) {
    for (int j = K; j >= a[i]; j--) {
      f[j] = max(f[j], f[j - a[i]]);
    }
  }
  a.insert(a.begin() + k, t);
  bool flg = 0;
  for (int i = max(K - t, 0); i < K; i++) {
    if (f[i]) {
      flg = 1;
      break;
    }
  }
  return flg == 0;
  // for(int i=1;i<=K;i++)cout<<f[i]<<' ';
  // puts("");
}
int main() {
  // freopen("my sol.in","r",stdin);
  // freopen("my sol.out","w",stdout);
  cin >> n >> K;
  a.resize(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  sort(a.begin() + 1, a.end());
  int ll = 1, rr = n + 1;
  while (rr - ll > 1) {
    // cout<<ll<<' '<<rr<<endl;
    int mid = (ll + rr) >> 1;
    if (check(mid))
      ll = mid;
    else
      rr = mid - 1;
  }
  // cout<<ll<<' '<<rr<<endl;
  if (check(rr))
    cout << rr << endl;
  else
    cout << ll << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define LL long long
#define mp make_pair
#define pb push_back
#define INF (1 << 30)
using namespace std;
typedef pair<int, int> pii;
int n, K;
vector<int> a;
int tot;
// int a[5010];
int f[5010];
bool check(int k) {
  int t = a[k];
  a.erase(a.begin() + k);
  int m = n - 1;
  memset(f, 0, sizeof(f));
  f[0] = 1;
  for (int i = 1; i <= m; i++) {
    for (int j = K; j >= a[i]; j--) {
      f[j] = max(f[j], f[j - a[i]]);
    }
  }
  a.insert(a.begin() + k, t);
  bool flg = 0;
  for (int i = max(K - t, 0); i < K; i++) {
    if (f[i]) {
      flg = 1;
      break;
    }
  }
  return flg == 0;
  // for(int i=1;i<=K;i++)cout<<f[i]<<' ';
  // puts("");
}
int main() {
  // freopen("my sol.in","r",stdin);
  // freopen("my sol.out","w",stdout);
  cin >> n >> K;
  a.resize(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  sort(a.begin() + 1, a.end());
  int ll = 0, rr = n;
  while (rr - ll > 1) {
    // cout<<ll<<' '<<rr<<endl;
    int mid = (ll + rr) >> 1;
    if (check(mid))
      ll = mid;
    else
      rr = mid - 1;
  }
  // cout<<ll<<' '<<rr<<endl;
  if (check(rr))
    cout << rr << endl;
  else
    cout << ll << endl;
  return 0;
}
Problem p03782 (C++)

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

using namespace std;

int N, K, A[5000];
bool dp1[5001][5001], dp2[5001][5001];

int main() {
  cin >> N >> K;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  dp1[0][0] = true;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j <= K; j++) {
      dp1[i + 1][min(j + A[i], K)] |= dp1[i][j];
      dp1[i + 1][j] |= dp1[i][j];
    }
  }
  dp2[N][0] = true;
  for (int i = N; i > 0; i--) {
    for (int j = 0; j <= K; j++) {
      dp2[i - 1][min(j + A[i - 1], K)] |= dp2[i][j];
      dp2[i - 1][j] |= dp2[i][j];
    }
  }

  int ret = N;

  for (int i = 0; i < N; i++) {
    bool flag = false;
    int sum[5001] = {};
    for (int j = 0; j < K; j++)
      sum[j + 1] = sum[j] + dp2[i + 1][j];
    for (int j = 0; j < K; j++) {
      if (dp1[i][j] && sum[K - j] - sum[K - A[i] - j] > 0)
        flag = true;
    }
    ret -= flag;
  }
  cout << ret << endl;
}

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

using namespace std;

int N, K, A[5000];
bool dp1[5001][5001], dp2[5001][5001];

int main() {
  cin >> N >> K;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  dp1[0][0] = true;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j <= K; j++) {
      dp1[i + 1][min(j + A[i], K)] |= dp1[i][j];
      dp1[i + 1][j] |= dp1[i][j];
    }
  }
  dp2[N][0] = true;
  for (int i = N; i > 0; i--) {
    for (int j = 0; j <= K; j++) {
      dp2[i - 1][min(j + A[i - 1], K)] |= dp2[i][j];
      dp2[i - 1][j] |= dp2[i][j];
    }
  }

  int ret = N;

  for (int i = 0; i < N; i++) {
    bool flag = false;
    int sum[5001] = {};
    for (int j = 0; j < K; j++)
      sum[j + 1] = sum[j] + dp2[i + 1][j];
    for (int j = 0; j < K; j++) {
      if (dp1[i][j] && sum[K - j] - sum[max(0, K - A[i] - j)] > 0)
        flag = true;
    }
    ret -= flag;
  }
  cout << ret << endl;
}
Problem p03782 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
#define pb push_back
#define mp make_pair
#define ll long long
using namespace std;
const int N = 5e3 + 5;

int n, k;
int a[N];

bool have[N];
bool check(int id) {
  if (id == 0)
    return true;

  vector<int> sums = {0};
  memset(have, 0, sizeof(have));
  have[0] = true;
  for (int i = 1; i <= n; i++) {
    if (i == id)
      continue;
    int sz = (int)sums.size();
    for (int j = 0; j < sz; j++) {
      if (!have[sums[j] + a[i]]) {
        sums.pb(sums[j] + a[i]);
        have[sums[j] + a[i]] = true;
      }
    }
  }
  for (int x : sums)
    if (x < k && x + a[id] >= k)
      return false;
  return true;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

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

  sort(a + 1, a + n + 1);
  int low = 0, high = n;
  while (low <= high) {
    int mid = (low + high) / 2;
    if (check(mid))
      low = mid + 1;
    else
      high = mid - 1;
  }

  cout << low - 1 << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
#define pb push_back
#define mp make_pair
#define ll long long
using namespace std;
const int N = 5e3 + 5;

int n, k;
int a[N];

bool have[N];
bool check(int id) {
  if (id == 0)
    return true;

  vector<int> sums = {0};
  memset(have, 0, sizeof(have));
  have[0] = true;
  for (int i = 1; i <= n; i++) {
    if (i == id)
      continue;
    int sz = (int)sums.size();
    for (int j = 0; j < sz; j++) {
      if (sums[j] + a[i] < k && !have[sums[j] + a[i]]) {
        sums.pb(sums[j] + a[i]);
        have[sums[j] + a[i]] = true;
      }
    }
  }
  for (int x : sums)
    if (x < k && x + a[id] >= k)
      return false;
  return true;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

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

  sort(a + 1, a + n + 1);
  int low = 0, high = n;
  while (low <= high) {
    int mid = (low + high) / 2;
    if (check(mid))
      low = mid + 1;
    else
      high = mid - 1;
  }

  cout << low - 1 << endl;

  return 0;
}
Problem p03782 (C++)

Buggy submission (status: Runtime Error):
/// Zhupengfei 2018
/// Hereby declare: this program is in public domain.

/// Solution for problem: arc070d Sol2

#include <bits/stdc++.h>

#ifdef LOCAL
#pragma GCC poison printf // To avoid fastio problems
#pragma GCC poison scanf
#endif

using namespace std;

int n, K;
int a[5001] = {};
bool dp[5001] = {};

bool check(int x) {
  if (x >= K)
    return true;
  memset(dp, 0, sizeof(dp));
  dp[0] = true;
  int skip = -1;
  for (int i = 0; i < n; i++) {
    if (a[i] == x) {
      skip = i;
      break;
    }
  }
  for (int i = 0; i < n; i++) {
    if (i == skip)
      continue;
    for (int j = K - 1; j >= 0; j--) {
      if (dp[j] && j + a[i] < K) {
        dp[j + a[i]] = true;
      }
    }
  }
  for (int i = 0; i < K; i++) {
    if (dp[i] && i + x >= K)
      return true;
  }
  return false;
}

void solve() {
  int l = -1, r = n;
  while (r - l > 1) {
    int m = l + (r - 1) / 2;
    if (check(a[m])) {
      r = m;
    } else {
      l = m;
    }
  }
  cout << r << endl;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> K;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  sort(a, a + n);
  solve();
  return 0;
}


Fixed submission:
/// Zhupengfei 2018
/// Hereby declare: this program is in public domain.

/// Solution for problem: arc070d Sol2

#include <bits/stdc++.h>

#ifdef LOCAL
#pragma GCC poison printf // To avoid fastio problems
#pragma GCC poison scanf
#endif

using namespace std;

int n, K;
int a[5001] = {};
bool dp[5001] = {};

bool check(int x) {
  if (x >= K)
    return true;
  memset(dp, 0, sizeof(dp));
  dp[0] = true;
  int skip = -1;
  for (int i = 0; i < n; i++) {
    if (a[i] == x) {
      skip = i;
      break;
    }
  }
  for (int i = 0; i < n; i++) {
    if (i == skip)
      continue;
    for (int j = K - 1; j >= 0; j--) {
      if (dp[j] && j + a[i] < K) {
        dp[j + a[i]] = true;
      }
    }
  }
  for (int i = 0; i < K; i++) {
    if (dp[i] && i + x >= K)
      return true;
  }
  return false;
}

void solve() {
  int l = -1, r = n;
  while (r - l > 1) {
    int m = l + (r - l) / 2;
    if (check(a[m])) {
      r = m;
    } else {
      l = m;
    }
  }
  cout << r << endl;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> K;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  sort(a, a + n);
  solve();
  return 0;
}
Problem p03782 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define show(x) cout << #x << " = " << x << endl;
typedef long long ll;
typedef pair<int, int> pii;
int n, k;
int a[5005];
bool check(int c) {
  vector<bool> dp(k + 1, false);
  vector<bool> dp2(k + 1, false);
  dp[0] = true;
  rep(i, n) {
    rep(j, k + 1) dp2[j] = dp[j];
    if (i == c)
      continue;
    rep(j, k + 1) {
      if (dp2[j] && j + a[i] < k + 1) {
        dp[j + a[i]] = true;
      }
    }
  }
  for (int i = max(k - a[c], 0); i < k; i++) {
    if (i < 0)
      continue;
    if (dp[i]) {
      return true;
    }
  }
  return false;
}
int main() {
  cin >> n >> k;
  // if(n>400||k>400)return 0;
  rep(i, n) { cin >> a[i]; }
  sort(a, a + n);
  int l = 0, r = n;
  int count = 0;
  while (count < 100) {
    int k = (l + r) / 2;
    if (check(k) == true) {
      r = k;
    } else {
      l = k;
    }
    count++;
  }
  cout << r << endl;
  /*rep(i,n){
      if(check(i)){show(i)}
  }*/
  // cout << n-count << endl;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define show(x) cout << #x << " = " << x << endl;
typedef long long ll;
typedef pair<int, int> pii;
int n, k;
int a[5005];
bool check(int c) {
  vector<bool> dp(k + 1, false);
  vector<bool> dp2(k + 1, false);
  dp[0] = true;
  rep(i, n) {
    rep(j, k + 1) dp2[j] = dp[j];
    if (i == c)
      continue;
    rep(j, k + 1) {
      if (dp2[j] && j + a[i] < k + 1) {
        dp[j + a[i]] = true;
      }
    }
  }
  for (int i = max(k - a[c], 0); i < k; i++) {
    if (i < 0)
      continue;
    if (dp[i]) {
      return true;
    }
  }
  return false;
}
int main() {
  cin >> n >> k;
  // if(n>400||k>400)return 0;
  rep(i, n) { cin >> a[i]; }
  sort(a, a + n);
  int l = 0, r = n;
  int count = 0;
  while (count < 16) {
    int k = (l + r) / 2;
    if (check(k) == true) {
      r = k;
    } else {
      l = k;
    }
    count++;
  }
  cout << r << endl;
  /*rep(i,n){
      if(check(i)){show(i)}
  }*/
  // cout << n-count << endl;
}
Problem p03782 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
const int N = 5001;
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;
  for (;;)
    ;
  while (lb + 1 < ub) {
    int mid = (lb + ub) / 2;
    if (check(mid))
      ub = mid;
    else
      lb = mid;
  }
  printf("%d\n", lb);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 5001;
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: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

int n, k;
int a[5005];

int C(int x) {
  bitset<5005> dp;
  if (a[x] >= k)
    return 1;
  dp.reset();
  dp[0] = 1;
  for (int i = 0; i < n; ++i) {
    if (i == x)
      continue;
    dp |= (dp << a[i]);
  }
  for (int i = k - 1; i >= k - a[x]; --i)
    if (dp[i])
      return 1;
  return 0;
}

int main() {
  while (~scanf("%d %d", &n, &k)) {
    for (int i = 0; i < n; ++i)
      scanf("%d", &a[i]);
    sort(a, a + n);
    int l = -1, r = n;
    while (l < r) {
      int mid = (l + r) / 2;
      if (C(mid))
        r = mid;
      else
        l = mid + 1;
    }
    //        int lo = -1, hi = n;
    //    while(hi-lo>1){
    //        int m = (lo+hi)/2;
    //        if(C(m)) hi = m;
    //        else lo = m;
    //    }
    // return hi;
    // if(!C(l)) l--;
    printf("%d\n", l);
  }
}


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

int n, k;
int a[5005];

int C(int x) {
  bitset<5005> dp;
  if (a[x] >= k)
    return 1;
  dp.reset();
  dp[0] = 1;
  for (int i = 0; i < n; ++i) {
    if (i == x)
      continue;
    dp |= (dp << a[i]);
  }
  for (int i = k - 1; i >= k - a[x]; --i)
    if (dp[i])
      return 1;
  return 0;
}

int main() {
  while (~scanf("%d %d", &n, &k)) {
    for (int i = 0; i < n; ++i)
      scanf("%d", &a[i]);
    sort(a, a + n);
    int l = 0, r = n;
    while (l < r) {
      int mid = (l + r) / 2;
      if (C(mid))
        r = mid;
      else
        l = mid + 1;
    }
    //        int lo = -1, hi = n;
    //    while(hi-lo>1){
    //        int m = (lo+hi)/2;
    //        if(C(m)) hi = m;
    //        else lo = m;
    //    }
    // return hi;
    // if(!C(l)) l--;
    printf("%d\n", l);
  }
}
Problem p03782 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>

using namespace std;

int main(void) {
  int N, K;
  cin >> N >> K;
  vector<int> a(N + 2);
  for (int i = 1; i <= N; i++) {
    cin >> a[i];
  }

  vector<vector<int64_t>> l(N + 2, vector<int64_t>(K)),
      r(N + 2, vector<int64_t>(K));
  l[0][0] = 1;
  r[N + 1][0] = 1;
  for (int i = 1; i <= N; i++) {
    for (int j = 0; j < K; j++) {
      l[i][j] += l[i - 1][j];
      if (j + a[i] >= K)
        continue;
      l[i][j + a[i]] += l[i - 1][j];
    }
  }
  for (int i = N; i; i--) {
    for (int j = 0; j < K; j++) {
      r[i][j] += r[i + 1][j];
      if (j + a[i] >= K)
        continue;
      r[i][j + a[i]] += r[i + 1][j];
    }
  }

  int need = 0;
  for (int i = 1; i <= N; i++) {
    partial_sum(r[i + 1].begin(), r[i + 1].end(), r[i + 1].begin());
    for (int j = 0; j < K; j++) {
      if (l[i - 1][j] == 0)
        continue;
      if (K - a[i] <= j && j < K) {
        ++need;
        break;
      }
      if (r[i + 1][K - 1 - j] - r[i + 1][max(K - a[i] - 1 - j, 0)]) {
        need++;
        break;
      }
    }
  }

  cout << N - need << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>

using namespace std;

int main(void) {
  int N, K;
  cin >> N >> K;
  vector<int> a(N + 2);
  for (int i = 1; i <= N; i++) {
    cin >> a[i];
  }

  vector<vector<int>> l(N + 2, vector<int>(K)), r(N + 2, vector<int>(K));
  l[0][0] = 1;
  r[N + 1][0] = 1;
  for (int i = 1; i <= N; i++) {
    for (int j = 0; j < K; j++) {
      l[i][j] += l[i - 1][j];
      if (j + a[i] >= K)
        continue;
      l[i][j + a[i]] += l[i - 1][j];
    }
  }
  for (int i = N; i; i--) {
    for (int j = 0; j < K; j++) {
      r[i][j] += r[i + 1][j];
      if (j + a[i] >= K)
        continue;
      r[i][j + a[i]] += r[i + 1][j];
    }
  }

  int need = 0;
  for (int i = 1; i <= N; i++) {
    partial_sum(r[i + 1].begin(), r[i + 1].end(), r[i + 1].begin());
    for (int j = 0; j < K; j++) {
      if (l[i - 1][j] == 0)
        continue;
      if (K - a[i] <= j && j < K) {
        ++need;
        break;
      }
      if (r[i + 1][K - 1 - j] - r[i + 1][max(K - a[i] - 1 - j, 0)]) {
        need++;
        break;
      }
    }
  }

  cout << N - need << endl;
  return 0;
}
Problem p03782 (C++)

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

const int MAXN = 5123;

int n, k;
int v[MAXN];
bool memo[MAXN];

bool solve(int x) {
  memset(memo, 0, sizeof memo);
  memo[0] = true;

  for (int i = 0; i < n; ++i) {
    if (i == x)
      continue;
    for (int j = k; j >= v[i]; --j)
      memo[j] |= memo[j - v[i]];
  }

  for (int i = k - v[x]; i < k; ++i)
    if (memo[i])
      return false;

  return true;
}

int main() {

  cin >> n >> k;

  int ans = 0;
  for (int i = 0; i < n; ++i)
    scanf("%d", v + i);

  sort(v, v + n);

  if (!solve(0)) {
    cout << ans << endl;
    return 0;
  }

  int l = 0, r = n;
  while (r - l > 1) {
    int mid = (l + r) >> 1;
    if (solve(mid))
      l = mid;
    else
      r = mid;
  }

  cout << ans + l + 1 << endl;

  return 0;
}

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

const int MAXN = 5123;

int n, k;
int v[MAXN];
bool memo[MAXN];

bool solve(int x) {
  memset(memo, 0, sizeof memo);
  memo[0] = true;

  for (int i = 0; i < n; ++i) {
    if (i == x)
      continue;
    for (int j = k; j >= v[i]; --j)
      memo[j] |= memo[j - v[i]];
  }

  for (int i = max(k - v[x], 0); i < k; ++i)
    if (memo[i])
      return false;

  return true;
}

int main() {

  cin >> n >> k;

  int ans = 0;
  for (int i = 0; i < n; ++i)
    scanf("%d", v + i);

  sort(v, v + n);

  if (!solve(0)) {
    cout << ans << endl;
    return 0;
  }

  int l = 0, r = n;
  while (r - l > 1) {
    int mid = (l + r) >> 1;
    if (solve(mid))
      l = mid;
    else
      r = mid;
  }

  cout << ans + l + 1 << endl;

  return 0;
}
Problem p03782 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
using namespace std;
#define int long long
int mod = 1e9 + 7;

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 l = 0;
  //  int r=n;
  int r = n;
  for (int i = 0; i < n; i++) {
    if (a[i] >= k) {
      r = i;
      break;
    }
  }

  int mid;
  for (int x = 0; x < 15; x++) {
    mid = (l + r) / 2;
    // cout<<l<<" "<<mid<<" "<<r<<endl;
    vector<vector<int>> dp(n + 1, vector<int>(k + 2, 0));
    dp[0][0] = 1;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < k + 2; j++) {
        if (dp[i][j] == 1) {
          dp[i + 1][j] = 1;
          if (i != mid && j + a[i] < k + 2)
            dp[i + 1][j + a[i]] = 1;
        }
      }
    }
    bool f = false;
    for (int i = max(0LL, k - a[mid]); i < k; i++)
      if (dp[n][i])
        f = true;
    if (!f) {
      l = mid;
    } else {
      r = mid;
    }
  }
  cout << r << endl;
  /*
  if(r>n)cout<<0<<endl;
  else cout<<r<<endl;*/
}


Fixed submission:
#include "bits/stdc++.h"
using namespace std;
#define int long long
int mod = 1e9 + 7;

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 l = 0;
  //  int r=n;
  int r = n;
  for (int i = 0; i < n; i++) {
    if (a[i] >= k) {
      r = i;
      break;
    }
  }

  int mid;
  for (int x = 0; x < 13; x++) {
    mid = (l + r) / 2;
    // cout<<l<<" "<<mid<<" "<<r<<endl;
    vector<vector<int>> dp(n + 1, vector<int>(k + 2, 0));
    dp[0][0] = 1;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < k + 2; j++) {
        if (dp[i][j] == 1) {
          dp[i + 1][j] = 1;
          if (i != mid && j + a[i] < k + 2)
            dp[i + 1][j + a[i]] = 1;
        }
      }
    }
    bool f = false;
    for (int i = max(0LL, k - a[mid]); i < k; i++)
      if (dp[n][i])
        f = true;
    if (!f) {
      l = mid;
    } else {
      r = mid;
    }
  }
  cout << r << endl;
  /*
  if(r>n)cout<<0<<endl;
  else cout<<r<<endl;*/
}
Problem p03782 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
bool DP(int n, int k, vector<int> &a, int ex) {
  vector<vector<bool>> dp(n + 1, vector<bool>(k + 1, false));
  dp[0][0] = true;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= k; j++) {
      dp[i + 1][j] = dp[i + 1][j] || dp[i][j];
      if (i != ex)
        dp[i + 1][min(k, j + a[i])] = dp[i + 1][min(k, j + a[i])] || dp[i][j];
    }
  }
  for (int j = max(0, k - a[ex]); j < k; j++) {
    if (dp[n][j])
      return true;
  }
  return false;
}
int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int ans = 0;
  for (int i = 0; i < n; i++)
    if (!DP(n, k, a, i))
      ans++;
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
bool DP(int n, int k, vector<int> &a, int ex) {
  vector<vector<bool>> dp(n + 1, vector<bool>(k + 1, false));
  dp[0][0] = true;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= k; j++) {
      dp[i + 1][j] = dp[i + 1][j] || dp[i][j];
      if (i != ex)
        dp[i + 1][min(k, j + a[i])] = dp[i + 1][min(k, j + a[i])] || dp[i][j];
    }
  }
  for (int j = max(0, k - a[ex]); j < k; j++) {
    if (dp[n][j])
      return true;
  }
  return false;
}
int 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 noneed = -1, need = n;
  while (noneed + 1 < need) {
    int mid = (noneed + need) / 2;
    auto itr = lower_bound(a.begin(), a.end(), a[mid]);
    if (DP(n, k, a, itr - a.begin()))
      need = itr - a.begin();
    else
      noneed = upper_bound(a.begin(), a.end(), a[mid]) - a.begin() - 1;
  }
  cout << need << endl;
}
Problem p03782 (C++)

Buggy submission (status: Runtime Error):


#include <algorithm>
#include <bitset>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
#define i_7 1000000007
#define i_5 1000000005

ll mod(ll a) {
  ll c = a % i_7;
  if (c >= 0)
    return c;
  else
    return c + i_7;
}
typedef pair<int, int> i_i;
typedef pair<ll, ll> l_l;
#define inf 100000000 /*10^8*/

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

int main() {
  int n, k;
  cin >> n >> k;
  int a[n];
  for (int i = 0; i <= n - 1; i++)
    cin >> a[i];
  sort(a, a + n);
  int l = 0, r = n;
  while (l < r) {
    int m = (l + r) / 2;
    bool c[k];
    memset(c, false, sizeof(c));
    c[0] = true;
    for (int i = 0; i <= n - 1; i++) {
      if (i != m) {
        for (int j = k - 1; j >= a[i]; j--) {
          c[j] |= c[j - a[i]];
        }
      }
    }
    bool flag = false;
    int last = min(0, k - a[m]);
    for (int j = k - 1; j >= last; j--)
      flag |= c[j];
    if (flag)
      r = m;
    else
      l = m + 1;
  }
  cout << l << endl;
  return 0;
}


Fixed submission:


#include <algorithm>
#include <bitset>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
#define i_7 1000000007
#define i_5 1000000005

ll mod(ll a) {
  ll c = a % i_7;
  if (c >= 0)
    return c;
  else
    return c + i_7;
}
typedef pair<int, int> i_i;
typedef pair<ll, ll> l_l;
#define inf 100000000 /*10^8*/

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

int main() {
  int n, k;
  cin >> n >> k;
  int a[n];
  for (int i = 0; i <= n - 1; i++)
    cin >> a[i];
  sort(a, a + n);
  int l = 0, r = n;
  while (l < r) {
    int m = (l + r) / 2;
    bool c[k];
    memset(c, false, sizeof(c));
    c[0] = true;
    for (int i = 0; i <= n - 1; i++) {
      if (i != m) {
        for (int j = k - 1; j >= a[i]; j--) {
          c[j] |= c[j - a[i]];
        }
      }
    }
    bool flag = false;
    int last = max(0, k - a[m]);
    for (int j = k - 1; j >= last; j--)
      flag |= c[j];
    if (flag)
      r = m;
    else
      l = m + 1;
  }
  cout << l << endl;
  return 0;
}
Problem p03782 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 100000 + 10;
const int M = 1000000007;
const double PI = atan(1) * 4;
const int oo = 1000000000;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
#define pb push_back
#define all(c) (c).begin(), (c).end()
int v[5010], n, k;
ll dp[5010];
bool is(int t) {
  for (int i = 0; i <= k; ++i)
    dp[i] = 0;
  dp[0] = 1;
  for (int i = 0; i < n; ++i) {
    if (i == t)
      continue;
    for (int j = k - v[i]; j >= 0; --j)
      dp[j + v[i]] += dp[j];
  }
  bool f = v[t] >= k;
  for (int i = max(k - v[t], 0); !f && i < k; ++i)
    f |= dp[i] != 0;
  return !f;
}
int main() {
  cin >> n >> k;
  for (int i = 0; i < n; ++i)
    cin >> v[i];
  sort(v, v + n);
  int ans = 0, mn = -1, lo = 0, hi = n - 1, md;
  for (int t = 0; t < n; ++t) {
    md = (lo + hi) / 2;
    if (is(md)) {
      mn = max(mn, md);
      lo = md + 1;
    } else
      hi = md - 1;
  }
  cout << mn + 1 << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 100000 + 10;
const int M = 1000000007;
const double PI = atan(1) * 4;
const int oo = 1000000000;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
#define pb push_back
#define all(c) (c).begin(), (c).end()
int v[5010], n, k;
ll dp[5010];
bool is(int t) {
  for (int i = 0; i <= k; ++i)
    dp[i] = 0;
  dp[0] = 1;
  for (int i = 0; i < n; ++i) {
    if (i == t)
      continue;
    for (int j = k - v[i]; j >= 0; --j)
      dp[j + v[i]] += dp[j];
  }
  bool f = v[t] >= k;
  for (int i = max(k - v[t], 0); !f && i < k; ++i)
    f |= dp[i] != 0;
  return !f;
}
int main() {
  cin >> n >> k;
  for (int i = 0; i < n; ++i)
    cin >> v[i];
  sort(v, v + n);
  int ans = 0, mn = -1, lo = 0, hi = n - 1, md;
  while (lo <= hi) {
    md = (lo + hi) / 2;
    if (is(md)) {
      mn = max(mn, md);
      lo = md + 1;
    } else
      hi = md - 1;
  }
  cout << mn + 1 << endl;
}