Problem p03780 (C++)

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

#define INF 1000010000
#define nl '\n'
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define fi first
#define se second
#define pii pair<int, int>
#define pdd pair<double, double>
#define all(c) (c).begin(), (c).end()
#define SORT(c) sort(all(c))
#define sz(c) (c).size()
#define rep(i, n) for (int i = 0; i < n; ++i)
#define repi(i, n) for (int i = 1; i <= n; ++i)
#define repn(i, n) for (int i = n - 1; i >= 0; --i)
#define repf(j, i, n) for (int j = i; j < n; ++j)
#define die(s)                                                                 \
  { std::cout << s << nl; }
#define dier(s)                                                                \
  {                                                                            \
    std::cout << s;                                                            \
    return 0;                                                                  \
  }
#define vi vector<int>
typedef long long ll;

using namespace std;

bool f(vi &v, int &n, int &k, int t) {
  vi dp(555555, 0);
  vi mn(555555, -1);

  dp[0] = 1;
  rep(i, n) {
    if (i != t) {
      rep(j, k) {
        if (dp[j] && mn[j] != i) {
          if (j + v[i] < k) {
            dp[j + v[i]] = 1;
            if (mn[j + v[i]] == -1) {
              mn[j + v[i]] = i;
            }
          }
        }
      }
    }
  }

  repf(i, k - v[t], k) {
    if (dp[i]) {
      return 1;
    }
  }

  return 0;
}

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

  int n, k;
  cin >> n >> k;

  vi v(n);
  rep(i, n) { cin >> v[i]; }

  SORT(v);

  int l = 0, r = n - 1;

  if (f(v, n, k, 0))
    dier(0);
  if (!f(v, n, k, n - 1))
    dier(n);

  while (r - l > 1) {
    int mid = (r + l) / 2;
    if (f(v, n, k, mid)) {
      r = mid;
    } else {
      l = mid;
    }
  }

  dier(l + 1);

  return 0;
}

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

#define INF 1000010000
#define nl '\n'
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define fi first
#define se second
#define pii pair<int, int>
#define pdd pair<double, double>
#define all(c) (c).begin(), (c).end()
#define SORT(c) sort(all(c))
#define sz(c) (c).size()
#define rep(i, n) for (int i = 0; i < n; ++i)
#define repi(i, n) for (int i = 1; i <= n; ++i)
#define repn(i, n) for (int i = n - 1; i >= 0; --i)
#define repf(j, i, n) for (int j = i; j < n; ++j)
#define die(s)                                                                 \
  { std::cout << s << nl; }
#define dier(s)                                                                \
  {                                                                            \
    std::cout << s;                                                            \
    return 0;                                                                  \
  }
#define vi vector<int>
typedef long long ll;

using namespace std;

bool f(vi &v, int &n, int &k, int t) {
  vi dp(555555, 0);
  vi mn(555555, -1);

  dp[0] = 1;
  rep(i, n) {
    if (i != t) {
      rep(j, k) {
        if (dp[j] && mn[j] != i) {
          if (j + v[i] < k) {
            dp[j + v[i]] = 1;
            if (mn[j + v[i]] == -1) {
              mn[j + v[i]] = i;
            }
          }
        }
      }
    }
  }

  repf(i, k - v[t], k) {
    if (i >= 0 && dp[i]) {
      return 1;
    }
  }

  return 0;
}

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

  int n, k;
  cin >> n >> k;

  vi v(n);
  rep(i, n) { cin >> v[i]; }

  SORT(v);

  int l = 0, r = n - 1;

  if (f(v, n, k, 0))
    dier(0);
  if (!f(v, n, k, n - 1))
    dier(n);

  while (r - l > 1) {
    int mid = (r + l) / 2;
    if (f(v, n, k, mid)) {
      r = mid;
    } else {
      l = mid;
    }
  }

  dier(l + 1);

  return 0;
}