Problem p03782 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#if __cplusplus >= 201103L
#include <array>
#include <forward_list>
#include <initializer_list>
#include <tuple>

#define cauto const auto &
#else

#endif

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

typedef vector<int> vint;
typedef vector<vector<int>> vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long>> vvll, vvLL;

#define VV(T) vector<vector<T>>

template <class T>
void initvv(vector<vector<T>> &v, int a, int b, const T &t = T()) {
  v.assign(a, vector<T>(b, t));
}

template <class F, class T> void convert(const F &f, T &t) {
  stringstream ss;
  ss << f;
  ss >> t;
}

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define reep(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) reep((i), 0, (n))
#define ALL(v) (v).begin(), (v).end()
#define PB push_back
#define F first
#define S second
#define mkp make_pair
#define RALL(v) (v).rbegin(), (v).rend()
#define DEBUG
#ifdef DEBUG
#define dump(x) cout << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cout << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;
#else
#define dump(x)
#define debug(x)
#endif

#define MOD 1000000007LL
#define EPS 1e-8
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
#define maxs(x, y) x = max(x, y)
#define mins(x, y) x = min(x, y)

bool pre[5010][5010];
bool suf[5010][5010];

void mainmain() {
  int n;
  cin >> n;
  int K;
  cin >> K;
  vll v(n);
  rep(i, n) cin >> v[i];
  sort(ALL(v));
  int ans = 0;
  // int m = 100010;
  pre[0][0] = true;
  suf[n - 1][0] = true;
  rep(i, n) {
    if (i)
      rep(j, 5010) { pre[i][j] = pre[i - 1][j]; }
    if (v[i] >= K)
      continue;
    for (int j = K; j >= v[i]; j--) {
      pre[i][j] |= pre[i][j - v[i]];
    }
  }
  for (int i = n - 1; i >= 0; i--) {
    if (i != n - 1) {
      rep(j, 5010) { suf[i][j] = suf[i + 1][j]; }
    }
    if (v[i] >= K)
      continue;
    for (int j = K; j >= v[i]; j--) {
      suf[i][j] |= suf[i][j - v[i]];
    }
  }
  vvint aa(n);
  vvint bb(n);
  rep(i, n) {
    rep(j, 5010) {
      if (pre[i][j])
        aa[i].PB(j);
      if (suf[i][j])
        bb[i].PB(j);
    }
  }
  rep(i, n) {
    bool ok = false;
    if (i == 0) {
      for (cauto x : bb[i + 1]) {
        if (x >= K)
          break;
        if (x + v[i] >= K)
          ok = true;
      }
    } else if (i == n - 1) {
      for (cauto x : aa[i - 1]) {
        if (x >= K)
          break;
        if (x + v[i] >= K)
          ok = true;
      }
    } else {
      for (cauto x : aa[i - 1]) {
        // cout<<i<<" "<<x<<endl;
        if (K - x - v[i] < 0)
          continue;
        auto it = lower_bound(ALL(bb[i + 1]), K - x - v[i]);
        if (it == bb[i + 1].end())
          continue;
        // cout<< *it << endl;
        if (*it + x < K) {
          ok = true;
          // cout<<"hh "<<i<<" "<< *it << x << endl;
        }
      }
    }
    if (v[i] >= K)
      ok = true;
    if (!ok) {
      ans++;
      // cout<<i<<" "<<v[i]<<endl;
    }
  }
  cout << ans << endl;
}

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

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#if __cplusplus >= 201103L
#include <array>
#include <forward_list>
#include <initializer_list>
#include <tuple>

#define cauto const auto &
#else

#endif

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

typedef vector<int> vint;
typedef vector<vector<int>> vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long>> vvll, vvLL;

#define VV(T) vector<vector<T>>

template <class T>
void initvv(vector<vector<T>> &v, int a, int b, const T &t = T()) {
  v.assign(a, vector<T>(b, t));
}

template <class F, class T> void convert(const F &f, T &t) {
  stringstream ss;
  ss << f;
  ss >> t;
}

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define reep(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) reep((i), 0, (n))
#define ALL(v) (v).begin(), (v).end()
#define PB push_back
#define F first
#define S second
#define mkp make_pair
#define RALL(v) (v).rbegin(), (v).rend()
#define DEBUG
#ifdef DEBUG
#define dump(x) cout << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cout << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;
#else
#define dump(x)
#define debug(x)
#endif

#define MOD 1000000007LL
#define EPS 1e-8
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
#define maxs(x, y) x = max(x, y)
#define mins(x, y) x = min(x, y)

bool pre[5010][5010];
bool suf[5010][5010];

void mainmain() {
  int n;
  cin >> n;
  int K;
  cin >> K;
  vll v(n);
  rep(i, n) cin >> v[i];
  sort(ALL(v));
  int ans = 0;
  // int m = 100010;
  pre[0][0] = true;
  suf[n - 1][0] = true;
  rep(i, n) {
    if (i)
      rep(j, 5010) { pre[i][j] = pre[i - 1][j]; }
    if (v[i] >= K)
      continue;
    for (int j = K; j >= v[i]; j--) {
      pre[i][j] |= pre[i][j - v[i]];
    }
  }
  for (int i = n - 1; i >= 0; i--) {
    if (i != n - 1) {
      rep(j, 5010) { suf[i][j] = suf[i + 1][j]; }
    }
    if (v[i] >= K)
      continue;
    for (int j = K; j >= v[i]; j--) {
      suf[i][j] |= suf[i][j - v[i]];
    }
  }
  vvint aa(n);
  vvint bb(n);
  rep(i, n) {
    rep(j, 5010) {
      if (pre[i][j])
        aa[i].PB(j);
      if (suf[i][j])
        bb[i].PB(j);
    }
  }
  if (n == 1) {
    if (v[0] >= K)
      cout << 0 << endl;
    else
      cout << 1 << endl;
    return;
  }
  rep(i, n) {
    bool ok = false;
    if (i == 0) {
      for (cauto x : bb[i + 1]) {
        if (x >= K)
          break;
        if (x + v[i] >= K)
          ok = true;
      }
    } else if (i == n - 1) {
      for (cauto x : aa[i - 1]) {
        if (x >= K)
          break;
        if (x + v[i] >= K)
          ok = true;
      }
    } else {
      for (cauto x : aa[i - 1]) {
        // cout<<i<<" "<<x<<endl;
        if (K - x - v[i] < 0)
          continue;
        auto it = lower_bound(ALL(bb[i + 1]), K - x - v[i]);
        if (it == bb[i + 1].end())
          continue;
        // cout<< *it << endl;
        if (*it + x < K) {
          ok = true;
          // cout<<"hh "<<i<<" "<< *it << x << endl;
        }
      }
    }
    if (v[i] >= K)
      ok = true;
    if (!ok) {
      ans++;
      // cout<<i<<" "<<v[i]<<endl;
    }
  }
  cout << ans << endl;
}

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