Problem p03780 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef long long ll;
typedef pair<ll, ll> P;
const int INF = INT_MAX / 3;
const ll LINF = LLONG_MAX / 3LL;
#define CONST 1000000007
#define EPS (1e-8)
#define PB push_back
#define MP make_pair
#define sz(a) ((int)(a).size())
#define reps(i, n, m) for (int i = (n); i < int(m); i++)
#define rep(i, n) reps(i, 0, n)
#define SORT(a) sort((a).begin(), (a).end())
ll mod(ll a, ll m) { return (a % m + m) % m; }
int dx[9] = {0, 1, 0, -1, 1, 1, -1, -1, 0},
    dy[9] = {1, 0, -1, 0, 1, -1, 1, -1, 0};
ll n, m, k;
ll a[10000];
bool arr[10000];
bool solve(int x) {
  if (n - 1 < x)
    return true;
  if (x < 0)
    return false;
  rep(i, 10000) { arr[i] = false; }
  arr[0] = true;
  bool flg = false;
  ll mx = 0;
  rep(i, n) {
    if (i == x)
      continue;
    for (int j = k; j >= 0; j--) {
      if (arr[j]) {
        mx = max(mx, j + a[i]);
        if (j + a[i] < k) {
          arr[j + a[i]] = true;
        }
      }
    }
  }
  if (mx + a[x] < k)
    return false;
  reps(i, k - a[x], k) {
    if (arr[i])
      return true;
  }
  return false;
}

int main() {

  cin >> n >> k;
  rep(i, n) { cin >> a[i]; }
  sort(a, a + n);

  ll r = 1LL << 31;
  ll ans = r - 1;
  while (r) {
    if (solve(ans - r)) {
      ans -= r;
    }
    r /= 2;
    // cerr<<ans<<endl;
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef long long ll;
typedef pair<ll, ll> P;
const int INF = INT_MAX / 3;
const ll LINF = LLONG_MAX / 3LL;
#define CONST 1000000007
#define EPS (1e-8)
#define PB push_back
#define MP make_pair
#define sz(a) ((int)(a).size())
#define reps(i, n, m) for (int i = (n); i < int(m); i++)
#define rep(i, n) reps(i, 0, n)
#define SORT(a) sort((a).begin(), (a).end())
ll mod(ll a, ll m) { return (a % m + m) % m; }
int dx[9] = {0, 1, 0, -1, 1, 1, -1, -1, 0},
    dy[9] = {1, 0, -1, 0, 1, -1, 1, -1, 0};
ll n, m, k;
ll a[10000];
bool arr[10000];
bool solve(int x) {
  if (n - 1 < x)
    return true;
  if (x < 0)
    return false;
  rep(i, 10000) { arr[i] = false; }
  arr[0] = true;
  bool flg = false;
  ll mx = 0;
  rep(i, n) {
    if (i == x)
      continue;
    for (int j = k; j >= 0; j--) {
      if (arr[j]) {
        mx = max(mx, j + a[i]);
        if (j + a[i] < k) {
          arr[j + a[i]] = true;
        }
      }
    }
  }
  if (mx + a[x] < k)
    return false;
  ll s = k > a[x] ? k - a[x] : 0;
  reps(i, s, k) {
    if (arr[i])
      return true;
  }
  return false;
}

int main() {

  cin >> n >> k;
  rep(i, n) { cin >> a[i]; }
  sort(a, a + n);

  ll r = 1LL << 31;
  ll ans = r - 1;
  while (r) {
    if (solve(ans - r)) {
      ans -= r;
    }
    r /= 2;
    // cerr<<ans<<endl;
  }
  cout << ans << endl;

  return 0;
}