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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <typeinfo>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
#define Ma_PI 3.141592653589793
#define eps 0.00000001
#define LONG_INF 3000000000000000000
#define GOLD 1.61803398874989484820458
#define MAX_MOD 1000000007
#define REP(i, n) for (long long i = 0; i < n; ++i)
#define seg_size 524288
vector<int> ar;
int n, k;
int judge(int border) {
  int gogo[6000] = {};
  gogo[0] = 1;
  for (int i = 0; i < n; ++i) {
    if (i != border) {
      for (int q = k - 1; q >= 0; --q) {
        if (q + ar[i] < k) {
          gogo[q + ar[i]] |= gogo[q];
        }
      }
    }
  }
  for (int i = k - ar[border]; i < k; ++i) {
    if (gogo[i])
      return 0;
  }
  return 1;
}
int main() {
  cin >> n >> k;
  REP(i, n) {
    int tmp;
    cin >> tmp;
    ar.push_back(tmp);
  }
  sort(ar.begin(), ar.end());
  int bot = 0;
  int top = n - 1;
  while (top - bot > 1) {
    int mid = (top + bot) / 2;
    if (judge(mid)) {
      bot = mid;
    } else
      top = mid;
  }
  if (judge(top)) {
    cout << top + 1 << endl;
    return 0;
  } else if (judge(bot)) {
    cout << bot + 1 << endl;
  } else
    cout << 0 << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <typeinfo>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
#define Ma_PI 3.141592653589793
#define eps 0.00000001
#define LONG_INF 3000000000000000000
#define GOLD 1.61803398874989484820458
#define MAX_MOD 1000000007
#define REP(i, n) for (long long i = 0; i < n; ++i)
#define seg_size 524288
vector<int> ar;
int n, k;
int judge(int border) {
  if (ar[border] >= k)
    return 0;
  int gogo[6000] = {};
  gogo[0] = 1;
  for (int i = 0; i < n; ++i) {
    if (i != border) {
      for (int q = k - 1; q >= 0; --q) {
        if (q + ar[i] < k) {
          gogo[q + ar[i]] |= gogo[q];
        }
      }
    }
  }
  for (int i = k - ar[border]; i < k; ++i) {
    if (gogo[i])
      return 0;
  }
  return 1;
}
int main() {
  cin >> n >> k;
  REP(i, n) {
    int tmp;
    cin >> tmp;
    ar.push_back(tmp);
  }
  sort(ar.begin(), ar.end());
  int bot = 0;
  int top = n - 1;
  while (top - bot > 1) {
    int mid = (top + bot) / 2;
    if (judge(mid)) {
      bot = mid;
    } else
      top = mid;
  }
  if (judge(top)) {
    cout << top + 1 << endl;
    return 0;
  } else if (judge(bot)) {
    cout << bot + 1 << endl;
  } else
    cout << 0 << endl;
  return 0;
}
Problem p03780 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstring>
#include <iterator>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
#pragma warning(disable : 4996)

typedef long long ll;
#define MIN(a, b) ((a) > (b) ? (b) : (a))
#define MAX(a, b) ((a) < (b) ? (b) : (a))
#define LINF 9223372036854775807
#define INF 2147483647
#define MOD 1000000007
using namespace std;

int main(int argc, char *argv[]) {
  long n, k;
  scanf("%ld%ld", &n, &k);

  vector<long> a(n);
  long i;
  for (i = 0; i < n; i++) {
    scanf("%ld", &a[i]);
  }
  sort(a.begin(), a.end());

  vector<long> s(n + 1);
  s[0] = 0;
  for (i = 0; i < n; i++) {
    s[i + 1] = s[i] + a[i];
  }

  long ans = 0;
  long ll = -1, rr = n;
  while (rr - ll > 1) {
    long curr = (ll + rr) / 2;

    vector<long> vis(k, 0);

    vis[0] = 1;
    if (0 >= k - a[curr]) {
      continue;
    }

    long j;
    for (i = 0; i < n; i++) {
      if (i == curr)
        continue;
      for (j = k - 1; j >= 0; j--) {
        if (vis[j] == 0 && j >= a[i] && vis[j - a[i]]) {
          vis[j] = 1;
          if (j >= k - a[curr]) {
            break;
          }
        }
      }
      if (j >= 0) {
        break;
      }
    }

    if (i == n) {
      ll = curr; // curr is no need
    } else {
      rr = curr; // curr is needed
    }
  }

  printf("%ld\n", ll + 1);

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstring>
#include <iterator>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
#pragma warning(disable : 4996)

typedef long long ll;
#define MIN(a, b) ((a) > (b) ? (b) : (a))
#define MAX(a, b) ((a) < (b) ? (b) : (a))
#define LINF 9223372036854775807
#define INF 2147483647
#define MOD 1000000007
using namespace std;

int main(int argc, char *argv[]) {
  long n, k;
  scanf("%ld%ld", &n, &k);

  vector<long> a(n);
  long i;
  for (i = 0; i < n; i++) {
    scanf("%ld", &a[i]);
  }
  sort(a.begin(), a.end());

  vector<long> s(n + 1);
  s[0] = 0;
  for (i = 0; i < n; i++) {
    s[i + 1] = s[i] + a[i];
  }

  long ans = 0;
  long ll = -1, rr = n;
  while (rr - ll > 1) {
    long curr = (ll + rr) / 2;

    vector<long> vis(k, 0);

    vis[0] = 1;
    if (0 >= k - a[curr]) {
      rr = curr; // curr is needed
      continue;
    }

    long j;
    for (i = 0; i < n; i++) {
      if (i == curr)
        continue;
      for (j = k - 1; j >= 0; j--) {
        if (vis[j] == 0 && j >= a[i] && vis[j - a[i]]) {
          vis[j] = 1;
          if (j >= k - a[curr]) {
            break;
          }
        }
      }
      if (j >= 0) {
        break;
      }
    }

    if (i == n) {
      ll = curr; // curr is no need
    } else {
      rr = curr; // curr is needed
    }
  }

  printf("%ld\n", ll + 1);

  return 0;
}
Problem p03780 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#define LL long long
#define maxn 5500
#define rep(i, l, r) for (int i = l; i <= r; i++)
#define dow(i, l, r) for (int i = r; i >= l; i--)
using namespace std;

int f[maxn], num[maxn], n, m, f2[maxn];
int main() {
  scanf("%d %d", &n, &m);
  rep(i, 0, n - 1) scanf("%d", num + i);
  sort(num, num + n);
  int r = n - 1;
  while (num[r] >= m)
    --r;
  f[0] = 1;
  //	rep(i,0,r) printf("%d %d\n",i,num[i]);
  int sum = 0, ans = 0;
  memset(f, 0, sizeof(f));
  f[0] = 1;
  rep(i, 0, r) dow(j, num[i], m + num[i] - 1) f[j] += f[j - num[i]];
  //	rep(j,1,m*2) printf("%d %d\n",j,f[j]);
  rep(i, m, m * 2) sum += f[i];
  rep(i, 0, r) {
    rep(j, 0, m * 2) f2[j] = f[j];
    rep(j, num[i], m + num[i] - 1) f2[j] -= f2[j - num[i]];
    int now = 0;
    rep(j, m, m * 2) now += f2[j];
    //	rep(j,1,m*2) printf("%d %d\n",j,f2[j]);
    //	printf("%d %d\n",now,sum);
    if (now == sum)
      ++ans;
  }
  printf("%d\n", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#define LL long long
#define maxn 500500
#define rep(i, l, r) for (int i = l; i <= r; i++)
#define dow(i, l, r) for (int i = r; i >= l; i--)
using namespace std;

int f[maxn], num[maxn], n, m, f2[maxn];
int main() {
  scanf("%d %d", &n, &m);
  rep(i, 0, n - 1) scanf("%d", num + i);
  sort(num, num + n);
  int r = n - 1;
  while (num[r] >= m)
    --r;
  f[0] = 1;
  //	rep(i,0,r) printf("%d %d\n",i,num[i]);
  int sum = 0, ans = 0;
  memset(f, 0, sizeof(f));
  f[0] = 1;
  rep(i, 0, r) dow(j, num[i], m + num[i] - 1) f[j] += f[j - num[i]];
  //	rep(j,1,m*2) printf("%d %d\n",j,f[j]);
  rep(i, m, m * 2) sum += f[i];
  rep(i, 0, r) {
    rep(j, 0, m * 2) f2[j] = f[j];
    rep(j, num[i], m + num[i] - 1) f2[j] -= f2[j - num[i]];
    int now = 0;
    rep(j, m, m * 2) now += f2[j];
    //	rep(j,1,m*2) printf("%d %d\n",j,f2[j]);
    //	printf("%d %d\n",now,sum);
    if (now == sum)
      ++ans;
  }
  printf("%d\n", ans);
  return 0;
}
Problem p03780 (C++)

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

using namespace std;
typedef long long ll;
ll sum;
bool dp[5005][5005];
int N, K;
int a[5005];
int main() {
  cin >> N >> K;
  for (int i = 0; i < N; i++) {
    cin >> a[i];
    sum += a[i];
  }
  if (sum < K) {
    cout << N << endl;
    return 0;
  }
  sort(a, a + N, greater<int>());

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

  while (r - l > 1) {
    for (int i = 0; i <= N; i++)
      fill(dp[i], dp[i] + N + 1, false);
    int mid = (l + r) / 2;
    if (a[mid] > K) {
      l = mid;
      continue;
    }

    int tmp = a[mid];
    a[mid] = 0;
    dp[0][0] = dp[0][a[0]] = true;
    for (int i = 0; i < N; i++) {
      for (int j = K - 1; j >= 0; j--) {
        dp[i + 1][j] = dp[i][j];
        if (dp[i][j] && j + a[i] < 5000) {
          dp[i + 1][j + a[i]] = dp[i][j];
        }
      }
    }
    a[mid] = tmp;
    bool there = false;
    for (int i = K - tmp; i < K; i++) {
      if (dp[N][i]) {
        there = true;
        break;
      }
    }
    if (there)
      l = mid;
    else
      r = mid;
    // cout <<r<<endl;
  }

  cout << N - r << endl;
}


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

using namespace std;
typedef long long ll;
ll sum;
bool dp[5005][5005];
int N, K;
int a[5005];
int main() {
  cin >> N >> K;
  for (int i = 0; i < N; i++) {
    cin >> a[i];
    sum += a[i];
  }
  if (sum < K) {
    cout << N << endl;
    return 0;
  }
  sort(a, a + N, greater<int>());

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

  while (r - l > 1) {
    for (int i = 0; i <= N; i++)
      fill(dp[i], dp[i] + N + 1, false);
    int mid = (l + r) / 2;
    if (a[mid] > K) {
      l = mid;
      continue;
    }

    int tmp = a[mid];
    a[mid] = 0;
    dp[0][0] = true;
    for (int i = 0; i < N; i++) {
      for (int j = K - 1; j >= 0; j--) {
        dp[i + 1][j] = dp[i][j];
        if (dp[i][j] && j + a[i] < 5000) {
          dp[i + 1][j + a[i]] = dp[i][j];
        }
      }
    }
    a[mid] = tmp;
    bool there = false;
    for (int i = K - tmp; i < K; i++) {
      if (dp[N][i]) {
        there = true;
        break;
      }
    }
    if (there)
      l = mid;
    else
      r = mid;
    // cout <<r<<endl;
  }

  cout << N - r << endl;
}
Problem p03780 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <string>
#include <utility>
#include <vector>

#define INF 1000000000000000000
using namespace std;
typedef long long ll;

int n;
ll k;
ll a[5010];
bool dp[5010];

bool need(int m) {
  for (int i = 1; i < k; i++) {
    dp[i] = false;
  }
  dp[0] = true;
  for (int i = 0; i < n; i++) {
    if (i == m)
      continue;
    for (int j = k - 1; j >= 0; j--) {
      if (dp[j] && (j + a[i]) < k) {
        dp[j + a[i]] = true;
      }
    }
  }
  for (int i = k - a[m]; i < k; i++) {
    if (dp[i])
      return true;
  }
  return false;
}

int main() {
  cin >> n >> k;
  int lo, hi;
  lo = 0;
  hi = n - 1;

  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a, a + n);
  if (!need(n - 1)) {
    cout << n << endl;
    return 0;
  }
  while (lo != hi) {
    int mid = (lo + hi) / 2;
    if (need(mid)) {
      hi = mid;
    } else {
      lo = mid + 1;
    }
  }
  cout << lo << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <string>
#include <utility>
#include <vector>

#define INF 1000000000000000000
using namespace std;
typedef long long ll;

int n;
ll k;
ll a[5010];
bool dp[5010];

bool need(int m) {
  for (int i = 1; i < k; i++) {
    dp[i] = false;
  }
  dp[0] = true;
  for (int i = 0; i < n; i++) {
    if (i == m)
      continue;
    for (int j = k - 1; j >= 0; j--) {
      if (dp[j] && (j + a[i]) < k) {
        dp[j + a[i]] = true;
      }
    }
  }
  for (int i = max(0, (int)(k - a[m])); i < k; i++) {
    if (dp[i])
      return true;
  }
  return false;
}

int main() {
  cin >> n >> k;
  int lo, hi;
  lo = 0;
  hi = n - 1;

  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a, a + n);
  if (!need(n - 1)) {
    cout << n << endl;
    return 0;
  }
  while (lo != hi) {
    int mid = (lo + hi) / 2;
    if (need(mid)) {
      hi = mid;
    } else {
      lo = mid + 1;
    }
  }
  cout << lo << endl;

  return 0;
}
Problem p03780 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; i++)
#define FORR(i, n) for (ll i = (ll)n - 1LL; i >= 0LL; i--)
#define rep(i, n) FOR(i, 0, n)
#define ALL(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
template <typename T> using V = vector<T>;

constexpr int Mod = 998244353;
constexpr int mod = 1e9 + 7;
constexpr ll inf = 1LL << 60;

template <typename T> constexpr bool chmax(T &a, const T &b) {
  if (a >= b)
    return false;
  a = b;
  return true;
}
template <typename T> constexpr bool chmin(T &a, const T &b) {
  if (a <= b)
    return false;
  a = b;
  return true;
}

/*-------------------------------------------*/

ll n, k, sum;
V<ll> a;

bool need(const int &mid) {
  V<bool> dp(k);
  dp[0] = true;
  rep(i, n) {
    if (i == mid)
      continue;
    FORR(j, k) {
      if (j - a[i] < 0)
        break;
      if (dp[j - a[i]])
        dp[j] = true;
    }
  }
  FOR(i, k - a[mid], k) if (dp[i]) return true;
  return false;
}

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

  cin >> n >> k;
  a.resize(n);
  rep(i, n) {
    cin >> a[i];
    sum += a[i];
  }
  sort(ALL(a));
  ll ok = -1, ng = n;
  while (ok + 1 < ng)
    (need(ng + ok >> 1) ? ng : ok) = ng + ok >> 1;
  cout << ng << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; i++)
#define FORR(i, n) for (ll i = (ll)n - 1LL; i >= 0LL; i--)
#define rep(i, n) FOR(i, 0, n)
#define ALL(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
template <typename T> using V = vector<T>;

constexpr int Mod = 998244353;
constexpr int mod = 1e9 + 7;
constexpr ll inf = 1LL << 60;

template <typename T> constexpr bool chmax(T &a, const T &b) {
  if (a >= b)
    return false;
  a = b;
  return true;
}
template <typename T> constexpr bool chmin(T &a, const T &b) {
  if (a <= b)
    return false;
  a = b;
  return true;
}

/*-------------------------------------------*/

ll n, k, sum;
V<ll> a;

bool need(const int &mid) {
  bitset<5005> b(1);
  rep(i, n) if (i != mid) b |= b << a[i];
  FOR(i, max(0LL, k - a[mid]), k) if (b[i]) return true;
  return false;
}

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

  cin >> n >> k;
  a.resize(n);
  rep(i, n) {
    cin >> a[i];
    sum += a[i];
  }
  sort(ALL(a));
  ll ok = -1, ng = n;
  while (ok + 1 < ng)
    (need(ng + ok >> 1) ? ng : ok) = ng + ok >> 1;
  cout << ng << endl;

  return 0;
}
Problem p03780 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define zero_pad(num) setfill('0') << std::right << setw(num)
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  sort(a.begin(), a.end());
  int l = -1;
  int r = n;
  while (l + 1 < r) {
    int x = (l + r) / 2;
    vector<int> dp(k + 1, 0);
    dp[0] = 1;
    rep(i, n) {
      if (i == x)
        continue;
      for (int j = k - 1; j >= 0; j--) {
        if (dp[j] == 1) {
          dp[min(j + a[i], k)] = 1;
        }
      }
    }
    bool v = true;
    for (int i = k - a[x]; i < k; i++) {
      if (dp[i] == 1)
        v = false;
    }
    if (v)
      l = x;
    else
      r = x;
  }
  cout << l + 1 << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define zero_pad(num) setfill('0') << std::right << setw(num)
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  sort(a.begin(), a.end());
  int l = -1;
  int r = n;
  while (l + 1 < r) {
    int x = (l + r) / 2;
    vector<int> dp(k + 1, 0);
    dp[0] = 1;
    rep(i, n) {
      if (i == x)
        continue;
      for (int j = k - 1; j >= 0; j--) {
        if (dp[j] == 1) {
          dp[min(j + a[i], k)] = 1;
        }
      }
    }
    bool v = true;
    for (int i = max(k - a[x], 0); i < k; i++) {
      if (dp[i] == 1)
        v = false;
    }
    if (v)
      l = x;
    else
      r = x;
  }
  cout << l + 1 << endl;
}
Problem p03780 (C++)

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

int main() {
  int a, b;
  cin >> a >> b;

  long long int c[a], d[a];

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

  for (int i = 0; i < a; i++) {
    for (int j = 0; j < a - i - 1; j++) {
      if (c[j] < c[j + 1])
        swap(c[j], c[j + 1]);
    }
  }

  for (int i = 0; i < a; i++) {
    vector<int> k;
    long long int gou = 0;
    for (int j = i; j < a; j++) {
      if (gou + c[j] >= b) {
        k.push_back(j);
        for (int v = 0; v < k.size(); v++) {
          d[k[v]] = 1;
        }
      } else {
        k.push_back(j);
        gou += c[j];
      }
    }
  }

  int ans = 0;

  for (int i = 0; i < a; i++) {
    if (!d[i])
      ans++;
  }

  cout << ans << endl;
}


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

int main() {
  int a, b;
  cin >> a >> b;

  long long int c[a], d[a];

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

  for (int i = 0; i < a; i++) {
    for (int j = 0; j < a - i - 1; j++) {
      if (c[j] < c[j + 1])
        swap(c[j], c[j + 1]);
    }
  }

  for (int i = 0; i < a; i++) {
    vector<int> k;
    long long int gou = 0;
    for (int j = i; j < a; j++) {
      if (gou + c[j] >= b) {
        k.push_back(j);
        for (int v = 0; v < k.size(); v++) {
          d[k[v]] = 1;
        }
        k.resize(0);
      } else {
        k.push_back(j);
        gou += c[j];
      }
    }
  }

  int ans = 0;

  for (int i = 0; i < a; i++) {
    if (!d[i])
      ans++;
  }

  cout << ans << endl;
}
Problem p03780 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
#define all(v) v.begin(), v.end()
int n, k, a[5010];
bool dp1[5010][5010], dp2[5010][5010];
signed main() {
  cin >> n >> k;
  rep(i, n) cin >> a[i];
  dp1[0][0] = true;
  rep(i, n - 1) {
    rep(j, k) {
      if (dp1[i][j])
        dp1[i + 1][j] = true;
      if (dp1[i][j] && j + a[i] <= k)
        dp1[i + 1][j + a[i]] = true;
    }
  }
  dp2[n - 1][0] = true;
  for (int i = n - 1; i > 0; i--) {
    rep(j, k) {
      if (dp2[i][j])
        dp2[i - 1][j] = true;
      if (dp2[i][j] && j + a[i] <= k)
        dp2[i - 1][j + a[i]] = true;
    }
  }
  int ans = 0;
  rep(i, n) {
    vector<int> a1, a2;
    rep(j, k + 1) {
      if (dp1[i][j])
        a1.push_back(j);
      if (dp2[i][j])
        a2.push_back(j);
    }
    bool flag = true;
    for (int x : a1) {
      if (upper_bound(all(a2), k - x - 1) - lower_bound(all(a2), k - x - a[i]))
        flag = false;
    }
    if (flag)
      ans++;
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
// #define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
#define all(v) v.begin(), v.end()
int n, k, a[5010];
bool dp1[5010][5010], dp2[5010][5010];
signed main() {
  cin >> n >> k;
  rep(i, n) cin >> a[i];
  dp1[0][0] = true;
  rep(i, n - 1) {
    rep(j, k) {
      if (dp1[i][j])
        dp1[i + 1][j] = true;
      if (dp1[i][j] && j + a[i] <= k)
        dp1[i + 1][j + a[i]] = true;
    }
  }
  dp2[n - 1][0] = true;
  for (int i = n - 1; i > 0; i--) {
    rep(j, k) {
      if (dp2[i][j])
        dp2[i - 1][j] = true;
      if (dp2[i][j] && j + a[i] <= k)
        dp2[i - 1][j + a[i]] = true;
    }
  }
  int ans = 0;
  rep(i, n) {
    vector<int> a1, a2;
    rep(j, k + 1) {
      if (dp1[i][j])
        a1.push_back(j);
      if (dp2[i][j])
        a2.push_back(j);
    }
    bool flag = true;
    for (int x : a1) {
      if (upper_bound(all(a2), k - x - 1) - lower_bound(all(a2), k - x - a[i]))
        flag = false;
    }
    if (flag)
      ans++;
  }
  cout << ans << endl;
  return 0;
}
Problem p03780 (C++)

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

using lint = long long;

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

  sort(a.begin(), a.end());

  int ans = 0;
  int lb = 0, ub = N;
  while (ub - lb > 0) {
    int mid = (lb + ub) / 2;

    lint Kp_min = K - a[mid];
    lint Kp_max = K;

    vector<lint> ap = a;
    ap.erase(ap.begin() + mid);

    vector<vector<bool>> can(N, vector<bool>(K + 1, false));
    can[0][0] = true;
    for (int j = 1; j <= N - 1; j++) {
      for (int k = 0; k <= K; k++) {
        if (can[j - 1][k] == true) {
          can[j][k] = true;
        }
        if (k - ap[j - 1] >= 0 and can[j - 1][k - ap[j - 1]] == true) {
          can[j][k] = true;
        }
      }
    }

    bool need = false;
    for (int j = Kp_min; j < Kp_max; j++) {
      if (can[N - 1][j] == true) {
        need = true;
        break;
      }
    }

    if (need == true) {
      ub = mid;
    } else {
      lb = mid + 1;
    }
  }

  cout << lb << endl;
  return 0;
}

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

using lint = long long;

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

  sort(a.begin(), a.end());

  int ans = 0;
  int lb = 0, ub = N;
  while (ub - lb > 0) {
    int mid = (lb + ub) / 2;

    lint Kp_min = max(lint(0), K - a[mid]);
    lint Kp_max = K;

    vector<lint> ap = a;
    ap.erase(ap.begin() + mid);

    vector<vector<bool>> can(N, vector<bool>(K + 1, false));
    can[0][0] = true;
    for (int j = 1; j <= N - 1; j++) {
      for (int k = 0; k <= K; k++) {
        if (can[j - 1][k] == true) {
          can[j][k] = true;
        }
        if (k - ap[j - 1] >= 0 and can[j - 1][k - ap[j - 1]] == true) {
          can[j][k] = true;
        }
      }
    }

    bool need = false;
    for (int j = Kp_min; j < Kp_max; j++) {
      if (can[N - 1][j] == true) {
        need = true;
        break;
      }
    }

    if (need == true) {
      ub = mid;
    } else {
      lb = mid + 1;
    }
  }

  cout << lb << endl;
  return 0;
}
Problem p03780 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <ctype.h>
#include <functional>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
const ll MOD = 1000000007;
const ll INF = 1000000000;
int main() {
  int n, k;
  cin >> n >> k;
  ll a[5001];
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  sort(a + 1, a + n + 1);
  ll s;
  int ans = n;
  int ng = 0;
  int ok = n + 1;
  while (ok - ng > 1) {
    int mid = (ok + ng) / 2;
    s = a[mid];
    a[mid] = 0;
    bool dp[500][500] = {false};
    dp[0][0] = true;
    bool f = false;
    for (int i = 1; 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 - a[i]];
        }
        if (i == n && j >= k - s && j < k && dp[i][j]) {
          f = true;
        }
      }
    }
    if (f) {
      ok = mid;
    } else {
      ng = mid;
    }
    a[mid] = s;
  }
  cout << ng << endl;
  getchar();
  getchar();
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <ctype.h>
#include <functional>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
const ll MOD = 1000000007;
const ll INF = 1000000000;
int main() {
  int n, k;
  cin >> n >> k;
  ll a[5001];
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  sort(a + 1, a + n + 1);
  ll s;
  int ans = n;
  int ng = 0;
  int ok = n + 1;
  while (ok - ng > 1) {
    int mid = (ok + ng) / 2;
    s = a[mid];
    a[mid] = 0;
    bool dp[5001][5001] = {false};
    dp[0][0] = true;
    bool f = false;
    for (int i = 1; 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 - a[i]];
        }
        if (i == n && j >= k - s && j < k && dp[i][j]) {
          f = true;
        }
      }
    }
    if (f) {
      ok = mid;
    } else {
      ng = mid;
    }
    a[mid] = s;
  }
  cout << ng << endl;
  getchar();
  getchar();
}
Problem p03780 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(x) (x).begin(), (x).end()
using namespace std;
const int INF = 1145141919, MOD = 1e9 + 7;
const long long LINF = 8931145141919364364, LMOD = 998244353;
// const int dx[]={1,0,-1,0,1,1,-1,-1},dy[]={0,-1,0,1,1,-1,-1,1};

int n, k;
vector<int> a;
// dp[i][j]:=i番目まで使って和jが実現できる
bool dp[5010][5010];
bool DP(int index) {
  rep(i, 5010) rep(j, 5010) dp[i][j] = 0;
  dp[0][0] = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= k; j++) {
      dp[i][j] = dp[i - 1][j];
      if (i - 1 == index)
        continue;
      if (j - a[i - 1] >= 0)
        dp[i][j] = dp[i - 1][j - a[i - 1]];
    }
  }
  // cout<<a[index]<<endl;
  // rep(j,k+1){
  //     printf("(%d):%d\n",j,dp[n][j]);
  // }
  for (int i = k - a[index]; i < k; i++) {
    if (dp[n][i])
      return true;
  }
  return false;
}
int main() {
  cin >> n >> k;
  rep(i, n) {
    int aa;
    cin >> aa;
    a.push_back(aa);
  }
  sort(all(a));
  int l = -1, r = n, ans = 0;
  while (abs(r - l) > 1) {
    // cout<<l<<" "<<r<<endl;
    if (!DP((r + l) / 2)) {
      l = (r + l) / 2;
      ans = l + 1;
    } else
      r = (r + l) / 2;
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(x) (x).begin(), (x).end()
using namespace std;
const int INF = 1145141919, MOD = 1e9 + 7;
const long long LINF = 8931145141919364364, LMOD = 998244353;
// const int dx[]={1,0,-1,0,1,1,-1,-1},dy[]={0,-1,0,1,1,-1,-1,1};

int n, k;
vector<int> a;
// dp[i][j]:=i番目まで使って和jが実現できる
bool dp[5010][5010];
bool DP(int index) {
  rep(i, 5010) rep(j, 5010) dp[i][j] = 0;
  dp[0][0] = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= k; j++) {
      dp[i][j] = dp[i - 1][j];
      if (i - 1 == index)
        continue;
      if (j - a[i - 1] >= 0)
        dp[i][j] = dp[i - 1][j - a[i - 1]];
    }
  }
  // cout<<a[index]<<endl;
  // rep(j,k+1){
  //     printf("(%d):%d\n",j,dp[n][j]);
  // }
  for (int i = max(k - a[index], 0); i < k; i++) {
    if (dp[n][i])
      return true;
  }
  return false;
}
int main() {
  cin >> n >> k;
  rep(i, n) {
    int aa;
    cin >> aa;
    a.push_back(aa);
  }
  sort(all(a));
  int l = -1, r = n, ans = 0;
  while (abs(r - l) > 1) {
    // cout<<l<<" "<<r<<endl;
    if (!DP((r + l) / 2)) {
      l = (r + l) / 2;
      ans = l + 1;
    } else
      r = (r + l) / 2;
  }
  cout << ans << endl;
  return 0;
}
Problem p03780 (C++)

Buggy submission (status: Runtime Error):
// dp[x+1][y] := x枚まで使った時に総和をyにできるか?
// dp[0][0] = 0 (0 <= i <= N)
// もしdp[i][j] = trueなら、dp[i+1][j] = true; dp[i+1][j + a[i]] = true;
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define REP(i, n) for (int i = 0; i < (int)n; ++i)

const int MAX_N = 5010;
const int MAX_K = 5010;
ll a[MAX_N];
int N;
ll K;
bool dp[MAX_N][MAX_K];

bool need(int p) { // p枚目(0-indexed)のカードは必要かどうか
  REP(i, N + 1) REP(j, K) dp[i][j] = false;
  dp[0][0] = true;
  REP(i, N) {
    if (i == p) {
      REP(j, K) dp[i + 1][j] = dp[i][j];
      continue;
    } else {
      for (ll j = 0; j < K; ++j)
        if (dp[i][j]) {
          dp[i + 1][j] = true;
          if (j + a[i] < K)
            dp[i + 1][j + a[i]] = true;
        }
    }
  }

  for (int i = K - a[p]; i < K; ++i)
    if (dp[N][i])
      return true;
  return false;
}

int main() {
  scanf("%d %lld", &N, &K);
  REP(i, N) scanf("%lld", &a[i]);
  sort(a, a + N);
  int low = 0, high = N - 1;
  while (high - low > 1) {
    int mid = (high + low) / 2;
    if (need(mid))
      high = mid;
    else
      low = mid;
  }
  if (!need(high))
    printf("%d\n", high + 1);
  else if (need(low))
    printf("0\n");
  else
    printf("%d\n", low + 1);
  return 0;
}

Fixed submission:
// dp[x+1][y] := x枚まで使った時に総和をyにできるか?
// dp[0][0] = 0 (0 <= i <= N)
// もしdp[i][j] = trueなら、dp[i+1][j] = true; dp[i+1][j + a[i]] = true;
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define REP(i, n) for (int i = 0; i < (int)n; ++i)

const int MAX_N = 5010;
const int MAX_K = 5010;
ll a[MAX_N];
int N;
ll K;
bool dp[MAX_N][MAX_K];

bool need(int p) { // p枚目(0-indexed)のカードは必要かどうか
  REP(i, N + 1) REP(j, K) dp[i][j] = false;
  dp[0][0] = true;
  REP(i, N) {
    if (i == p) {
      REP(j, K) dp[i + 1][j] = dp[i][j];
      continue;
    } else {
      for (ll j = 0; j < K; ++j)
        if (dp[i][j]) {
          dp[i + 1][j] = true;
          if (j + a[i] < K)
            dp[i + 1][j + a[i]] = true;
        }
    }
  }

  for (int i = max(0LL, K - a[p]); i < K; ++i)
    if (dp[N][i])
      return true;
  return false;
}

int main() {
  scanf("%d %lld", &N, &K);
  REP(i, N) scanf("%lld", &a[i]);
  sort(a, a + N);
  int low = 0, high = N - 1;
  while (high - low > 1) {
    int mid = (high + low) / 2;
    if (need(mid))
      high = mid;
    else
      low = mid;
  }
  if (!need(high))
    printf("%d\n", high + 1);
  else if (need(low))
    printf("0\n");
  else
    printf("%d\n", low + 1);
  return 0;
}
Problem p03780 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
int N, K;
bool DP[5000][5000];
std::vector<int> A;
bool solve(int x) {
  for (int i = 0; i <= N; i++) {
    for (int j = 0; j < K; j++) {
      DP[i][j] = false;
    }
  }
  DP[0][0] = true;
  /*for (int j = 0; j <= N; j++) {
          std::cout << "    " << j << std::endl;

          for (int i = K - A[x]; i < K; i++) {
                  if (DP[j][i]) {
                          std::cout << "   " << i << std::endl;
                  }
          }
  }*/
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < K; j++) {
      if (DP[i][j])
        DP[i + 1][j] = true;
      if (i == x)
        continue;
      if (j + A[i] < K && DP[i][j])
        DP[i + 1][j + A[i]] = true;
    }
  }
  for (int i = K - A[x]; i < K; i++) {
    if (DP[N][i])
      return true;
  }
  return false;
}

int main() {
  std::cin >> N >> K;
  for (int i = 0; i < N; i++) {
    int tmp;
    std::cin >> tmp;
    A.push_back(tmp);
  }
  std::sort(A.begin(), A.end());
  int lb = -1;
  int ub = N;
  while (ub - lb > 1) {
    int mid = (ub + lb) / 2;
    if (A[mid] >= K) {
      ub = mid;
      continue;
    }
    if (solve(mid)) {
      ub = mid;
    } else {
      lb = mid;
    }
  }
  std::cout << lb + 1 << std::endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
int N, K;
bool DP[5010][5010];
std::vector<int> A;
bool solve(int x) {
  for (int i = 0; i <= N; i++) {
    for (int j = 0; j < K; j++) {
      DP[i][j] = false;
    }
  }
  DP[0][0] = true;
  /*for (int j = 0; j <= N; j++) {
          std::cout << "    " << j << std::endl;

          for (int i = K - A[x]; i < K; i++) {
                  if (DP[j][i]) {
                          std::cout << "   " << i << std::endl;
                  }
          }
  }*/
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < K; j++) {
      if (DP[i][j])
        DP[i + 1][j] = true;
      if (i == x)
        continue;
      if (j + A[i] < K && DP[i][j])
        DP[i + 1][j + A[i]] = true;
    }
  }
  for (int i = K - A[x]; i < K; i++) {
    if (DP[N][i])
      return true;
  }
  return false;
}

int main() {
  std::cin >> N >> K;
  for (int i = 0; i < N; i++) {
    int tmp;
    std::cin >> tmp;
    A.push_back(tmp);
  }
  std::sort(A.begin(), A.end());
  int lb = -1;
  int ub = N;
  while (ub - lb > 1) {
    int mid = (ub + lb) / 2;
    if (A[mid] >= K) {
      ub = mid;
      continue;
    }
    if (solve(mid)) {
      ub = mid;
    } else {
      lb = mid;
    }
  }
  std::cout << lb + 1 << std::endl;
  return 0;
}
Problem p03780 (C++)

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <array>
#include <cmath>
#include <cstdio>

using namespace std;

array<int, 5000> need;

int K = 0;
int tmp = 0;
int mid = 0;

bool BisectionMethod() {
  if (tmp + need[mid] < K)
    return true;
  else
    return false;
}

int main() {
  int N;
  if (scanf("%d%d", &N, &K) < 2)
    return 0;

  int num = N;
  int ans = 0;

  for (int i = 0; i < N; i++) {
    if (scanf("%d", &tmp) < 1)
      return 0;

    // greater than or equal to K
    // no need
    if (tmp >= K)
      num--;
    else {
      // candidate
      need[ans] = tmp;
      ans++;
    }
  }

  // ascending
  sort(need.begin(), need.begin() + ans - 1);

  tmp = 0;
  while (num >= 2) {
    // add in from the bigger one
    tmp += need[num - 1];

    if (need[num - 2] + tmp < K) {
      num--;
      continue;
    }

    // need all
    if (need[0] + tmp >= K) {
      ans = 0;
      break;
    }

    // find unnecessary numbers
    // bisection method
    int under = 0;
    int upper = num - 2;

    while (under + 1 < upper) {
      // Bisection Method
      // [under, upper]
      // to narrow the range
      mid = (under + upper) / 2;

      bool flg = BisectionMethod();

      if (flg)
        under = mid;
      else
        upper = mid;
    }

    ans = ans - (num - upper);
    num = upper;
  }
  printf("%d\n", ans);
  return 0;
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <array>
#include <cmath>
#include <cstdio>

using namespace std;

array<int, 5000> need;

int K = 0;
int tmp = 0;
int mid = 0;

bool BisectionMethod() {
  if (tmp + need[mid] < K)
    return true;
  else
    return false;
}

int main() {
  int N;
  if (scanf("%d%d", &N, &K) < 2)
    return 0;

  int num = N;
  int ans = 0;

  for (int i = 0; i < N; i++) {
    if (scanf("%d", &tmp) < 1)
      return 0;

    // greater than or equal to K
    // no need
    if (tmp >= K)
      num--;
    else {
      // candidate
      need[ans] = tmp;
      ans++;
    }
  }

  // ascending
  sort(need.begin(), need.begin() + ans);

  tmp = 0;
  while (num >= 2) {
    // add in from the bigger one
    tmp += need[num - 1];

    if (need[num - 2] + tmp < K) {
      num--;
      continue;
    }

    // need all
    if (need[0] + tmp >= K) {
      ans = 0;
      break;
    }

    // find unnecessary numbers
    // bisection method
    int under = 0;
    int upper = num - 2;

    while (under + 1 < upper) {
      // Bisection Method
      // [under, upper]
      // to narrow the range
      mid = (under + upper) / 2;

      bool flg = BisectionMethod();

      if (flg)
        under = mid;
      else
        upper = mid;
    }

    ans = ans - (num - upper);
    num = upper;
  }
  printf("%d\n", ans);
  return 0;
}
Problem p03780 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long int ll;
static const int MAX_N = 5000;
static const int MAX_K = 5000;

int N, K;
int a[MAX_N];

bool C(int num) {
  vector<int> a2;
  a2.push_back(0);
  for (int i = 0; i < N; i++) {
    if (i == num)
      continue;
    a2.push_back(a[i]);
  }
  bool dp[MAX_N][MAX_K + 1];
  fill(dp[0], dp[N], false);
  dp[0][0] = true;
  for (int i = 1; i < N; i++) {
    for (int j = 0; j <= K; j++) {
      if (dp[i - 1][j] || dp[i - 1][j - a2[i]])
        dp[i][j] = true;
    }
  }
  for (int i = max(K - a[num], 0); i < K; i++) {
    if (dp[N - 1][i])
      return true;
  }
  return false;
}

int main() {
  cin >> N >> K;
  for (int i = 0; i < N; i++)
    cin >> a[i];
  sort(a, a + N);
  int lb = -1, ub = N;
  for (; ub - lb > 1;) {
    int mid = (ub + lb) / 2;
    if (!C(mid))
      lb = mid;
    else
      ub = mid;
  }
  cout << ub << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long int ll;
static const int MAX_N = 5000;
static const int MAX_K = 5000;

int N, K;
int a[MAX_N];

bool C(int num) {
  vector<int> a2;
  a2.push_back(0);
  for (int i = 0; i < N; i++) {
    if (i == num)
      continue;
    a2.push_back(a[i]);
  }
  bool dp[MAX_N][MAX_K + 1];
  fill(dp[0], dp[N], false);
  dp[0][0] = true;
  for (int i = 1; i < N; i++) {
    for (int j = 0; j <= K; j++) {
      if (dp[i - 1][j])
        dp[i][j] = true;
      if (j - a2[i] < 0)
        continue;
      if (dp[i - 1][j - a2[i]])
        dp[i][j] = true;
    }
  }
  for (int i = max(K - a[num], 0); i < K; i++) {
    if (dp[N - 1][i])
      return true;
  }
  return false;
}

int main() {
  cin >> N >> K;
  for (int i = 0; i < N; i++)
    cin >> a[i];
  sort(a, a + N);
  int lb = -1, ub = N;
  for (; ub - lb > 1;) {
    int mid = (ub + lb) / 2;
    if (!C(mid))
      lb = mid;
    else
      ub = mid;
  }
  cout << ub << endl;
  return 0;
}
Problem p03780 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <stdio.h>
using namespace std;
const int maxn = 4000 + 10;
int a[maxn];
bool cmp(int x, int y) { return x > y; }
int main() {
  int n, k;
  while (scanf("%d%d", &n, &k) != EOF) {
    int sum = 0, j = 0;
    for (int i = 0; i < n; i++) {
      int x;
      scanf("%d", &x);
      if (x < k) {
        sum += x;
        a[j++] = x;
      }
    }
    if (sum < k) {
      printf("%d\n", j);
      continue;
    }
    sort(a, a + j, cmp);
    int minx = k - a[0];
    int sumx, ii = 0;
    for (;;) {
      sumx = 0;
      int ij = 0;
      if (minx == a[j])
        break;
      if (ii > j)
        break;
      // printf("%d\n",minx);
      while (sumx < k && ij + ii < j) {
        if (sumx + a[ii + ij] < k) {
          sumx += a[ij + ii];
        } else if (sumx + a[ii + ij] == k) {
          if (minx > a[ii + ij])
            minx = a[ii + ij];
        }
        ij++;
      }
      ii++;
    }
    int xx = 0;
    for (int i = 0; i < j; i++) {
      if (a[i] < minx)
        xx++;
    }
    printf("%d\n", xx);
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <stdio.h>
using namespace std;
const int maxn = 5000 + 10;
int a[maxn];
bool cmp(int x, int y) { return x > y; }
int main() {
  int n, k;
  while (scanf("%d%d", &n, &k) != EOF) {
    int sum = 0, j = 0;
    for (int i = 0; i < n; i++) {
      int x;
      scanf("%d", &x);
      if (x < k) {
        sum += x;
        a[j++] = x;
      }
    }
    if (sum < k) {
      printf("%d\n", j);
      continue;
    }
    sort(a, a + j, cmp);
    int minx = k - a[0];
    int sumx, ii = 0;
    for (;;) {
      sumx = 0;
      int ij = 0;
      if (minx == a[j])
        break;
      if (ii > j)
        break;
      // printf("%d\n",minx);
      while (sumx < k && ij + ii < j) {
        if (sumx + a[ii + ij] < k) {
          sumx += a[ij + ii];
        } else if (sumx + a[ii + ij] == k) {
          if (minx > a[ii + ij])
            minx = a[ii + ij];
        }
        ij++;
      }
      ii++;
    }
    int xx = 0;
    for (int i = 0; i < j; i++) {
      if (a[i] < minx)
        xx++;
    }
    printf("%d\n", xx);
  }
  return 0;
}
Problem p03780 (C++)

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

int N, K, a[5000];

bool C(int idx) {
  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++) {
      if (dp[i][j]) {
        dp[i + 1][j] = true;
        if (i != idx && j + a[i] <= K)
          dp[i + 1][j + a[i]] = true;
      }
    }
  }
  for (int j = K - a[idx]; j < K; j++) {
    if (dp[N][j])
      return true;
  }
  return false;
}

int main() {
  cin >> N >> K;
  for (int i = 0; i < N; i++)
    cin >> a[i];
  sort(a, a + N, greater<int>());
  int l = 0, h = N + 1;
  while (h - l > 1) {
    int m = (l + h) / 2;
    if (C(m - 1))
      l = m;
    else
      h = m;
  }
  cout << N - l << endl;
  return 0;
}


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

int N, K, a[5000];

bool C(int idx) {
  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++) {
      if (dp[i][j]) {
        dp[i + 1][j] = true;
        if (i != idx && j + a[i] <= K)
          dp[i + 1][j + a[i]] = true;
      }
    }
  }
  int j = K - a[idx];
  if (j < 0)
    j = 0;
  for (; j < K; j++) {
    if (dp[N][j])
      return true;
  }
  return false;
}

int main() {
  cin >> N >> K;
  for (int i = 0; i < N; i++)
    cin >> a[i];
  sort(a, a + N, greater<int>());
  int l = 0, h = N + 1;
  while (h - l > 1) {
    int m = (l + h) / 2;
    if (C(m - 1))
      l = m;
    else
      h = m;
  }
  cout << N - l << endl;
  return 0;
}
Problem p03780 (C++)

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

using namespace std;

using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<ll>>;

vector<bitset<5001>> dp(5050);

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  vector<ll> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  sort(a.begin(), a.end());
  a.resize(lower_bound(a.begin(), a.end(), k) - a.begin());

  n = a.size();

  auto f = [&](int i) {
    dp[0][0] = true;
    for (int j = 0; j < n; j++) {
      if (i == j) {
        dp[j + 1] = dp[j];
      } else {
        dp[j + 1] = dp[j] | (dp[j] << a[j]);
      }
    }

    bool ok = false;
    for (int j = k - a[i]; j < k; j++)
      if (dp[n][j])
        ok = true;
    return ok;
  };
  ll l = 0, r = n;
  if (f(l)) {
    cout << 0 << endl;
    return 0;
  }
  while (r - l > 1) {
    ll m = (r + l) / 2;
    if (f(m)) {
      r = m;
    } else {
      l = m;
    }
  }
  cout << r << endl;

  return 0;
}


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

using namespace std;

using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<ll>>;

vector<bitset<5001>> dp(5050);

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  vector<ll> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  sort(a.begin(), a.end());
  a.resize(lower_bound(a.begin(), a.end(), k) - a.begin());

  n = a.size();
  if (n == 0) {
    cout << 0 << endl;
    return 0;
  }

  auto f = [&](int i) {
    dp[0][0] = true;
    for (int j = 0; j < n; j++) {
      if (i == j) {
        dp[j + 1] = dp[j];
      } else {
        dp[j + 1] = dp[j] | (dp[j] << a[j]);
      }
    }

    bool ok = false;
    for (int j = k - a[i]; j < k; j++)
      if (dp[n][j])
        ok = true;
    return ok;
  };
  ll l = 0, r = n;
  if (f(l)) {
    cout << 0 << endl;
    return 0;
  }
  while (r - l > 1) {
    ll m = (r + l) / 2;
    if (f(m)) {
      r = m;
    } else {
      l = m;
    }
  }
  cout << r << endl;

  return 0;
}
Problem p03780 (C++)

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

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)

#define ALL(obj) (obj).begin(), (obj).end()

#define V vector<int>

#define pb(a) push_back(a)
#define mp make_pair

int main() {

  int n, k;
  scanf("%d%d", &n, &k);
  V a(n);
  REP(i, n) scanf("%d", &(a[i]));
  sort(ALL(a), greater<int>());
  vector<bitset<5001>> table(k);
  REP(i, k) table[i].set(i);
  vector<bitset<5001>> dp(k, ~bitset<5001>());
  dp[0] = ~dp[0];
  REP(i, n)
  for (int j = k - 1; j >= a[i]; j--)
    if (!dp[j - a[i]][5000])
      dp[j] &= (dp[j - a[i]] | table[i]);
  int cou = n;
  REP(i, n) FOR(j, max(0, k - a[i]), k) if (!dp[j][i]) {
    cou--;
    break;
  }
  printf("%d", cou);

  return 0;
}


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

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)

#define ALL(obj) (obj).begin(), (obj).end()

#define V vector<int>

#define pb(a) push_back(a)
#define mp make_pair

int main() {

  int n, k;
  scanf("%d%d", &n, &k);
  V a(n);
  REP(i, n) scanf("%d", &(a[i]));
  sort(ALL(a), greater<int>());
  vector<bitset<5001>> table(n);
  REP(i, n) table[i].set(i);
  vector<bitset<5001>> dp(k, ~bitset<5001>());
  dp[0] = ~dp[0];
  REP(i, n)
  for (int j = k - 1; j >= a[i]; j--)
    if (!dp[j - a[i]][5000])
      dp[j] &= (dp[j - a[i]] | table[i]);
  int cou = n;
  REP(i, n) FOR(j, max(0, k - a[i]), k) if (!dp[j][i]) {
    cou--;
    break;
  }
  printf("%d", cou);

  return 0;
}
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 l = -1, r = n;
  while (l + 1 < r) {
    ll tmp = (l + r) >> 1;
    mat dp(n + 1, vec(k, 0));
    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 != tmp && j + a[i] < k) {
          dp[i + 1][j + a[i]] |= dp[i][j];
        }
      }
    }
    ll f = 0;
    for (ll i = k - a[tmp]; i < k; i++) {
      f |= dp[n][i];
    }
    if (f) {
      r = tmp;
    } else {
      l = tmp;
    }
  }
  cout << l + 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 l = -1, r = n;
  while (l + 1 < r) {
    ll tmp = (l + r) >> 1;
    mat dp(n + 1, vec(k, 0));
    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 != tmp && j + a[i] < k) {
          dp[i + 1][j + a[i]] |= dp[i][j];
        }
      }
    }
    ll f = 0;
    for (ll i = max(k - a[tmp], 0LL); i < k; i++) {
      f |= dp[n][i];
    }
    if (f) {
      r = tmp;
    } else {
      l = tmp;
    }
  }
  cout << l + 1 << endl;
}