Problem p03780 (C++)

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

#define task "D"
#define all(v) (v).begin(), (v).end()
#define rep(i, l, r) for (int i = (l); i <= (r); ++i)
#define Rep(i, r, l) for (int i = (r); i >= (l); --i)
#define DB(X)                                                                  \
  { cerr << #X << " = " << (X) << '\n'; }
#define DB1(A, _)                                                              \
  { cerr << #A << "[" << _ << "] = " << (A[_]) << '\n'; }
#define DB2(A, _, __)                                                          \
  { cerr << #A << "[" << _ << "][" << __ << "] = " << (A[_][__]) << '\n'; }
#define DB3(A, _, __, ___)                                                     \
  {                                                                            \
    cerr << #A << "[" << _ << "][" << __ << "][" << ___                        \
         << "] = " << (A[_][__][___]) << '\n';                                 \
  }
#define PR(A, l, r)                                                            \
  {                                                                            \
    cerr << '\n';                                                              \
    rep(_, l, r) DB1(A, _);                                                    \
    cerr << '\n';                                                              \
  }
#define SZ(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back
#define pf push_front
#define F first
#define S second
#define by(x)                                                                  \
  [](const auto &a, const auto &b) {                                           \
    return a.x < b.x;                                                          \
  } // sort(arr, arr + N, by(a));
#define next ___next
#define prev ___prev
#define y1 ___y1
#define left ___left
#define right ___right
#define y0 ___y0
#define div ___div
#define j0 ___j0
#define jn ___jn

using ll = long long;
using ld = long double;
using ull = unsigned long long;
using namespace std;
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ll> vl;
const int N = 5002;
int n, k, a[N];
bool pre[N][N], suf[N][N];
int main() {
#ifdef HynDuf
  freopen(task ".in", "r", stdin);
  // freopen(task".out", "w", stdout);
#else
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
#endif
  cin >> n >> k;
  rep(i, 1, n) cin >> a[i];
  pre[0][0] = 1;
  rep(i, 1, n) rep(j, 0, k) {
    pre[i][j] = pre[i - 1][j];
    if (j >= a[i])
      pre[i][j] |= pre[i - 1][j - a[i]];
  }
  suf[n + 1][0] = 1;
  Rep(i, n, 1) rep(j, 0, k) {
    suf[i][j] = suf[i + 1][j];
    if (j >= a[i])
      suf[i][j] |= suf[i + 1][j - a[i]];
  }
  int ans = 0;
  rep(i, 1, n) {
    int numr = 0;
    rep(j, k - a[i], k - 1) numr += suf[i + 1][j];
    if (pre[i - 1][0] && numr)
      ans++;
    else {
      int r = k - a[i] - 1;
      rep(l, 1, k - 1) {
        numr -= suf[i + 1][r + a[i]];
        numr += suf[i + 1][r];
        r--;
        if (pre[i - 1][l] && numr) {
          ans++;
          break;
        }
      }
    }
  }
  cout << n - ans;
  return 0;
}


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

#define task "D"
#define all(v) (v).begin(), (v).end()
#define rep(i, l, r) for (int i = (l); i <= (r); ++i)
#define Rep(i, r, l) for (int i = (r); i >= (l); --i)
#define DB(X)                                                                  \
  { cerr << #X << " = " << (X) << '\n'; }
#define DB1(A, _)                                                              \
  { cerr << #A << "[" << _ << "] = " << (A[_]) << '\n'; }
#define DB2(A, _, __)                                                          \
  { cerr << #A << "[" << _ << "][" << __ << "] = " << (A[_][__]) << '\n'; }
#define DB3(A, _, __, ___)                                                     \
  {                                                                            \
    cerr << #A << "[" << _ << "][" << __ << "][" << ___                        \
         << "] = " << (A[_][__][___]) << '\n';                                 \
  }
#define PR(A, l, r)                                                            \
  {                                                                            \
    cerr << '\n';                                                              \
    rep(_, l, r) DB1(A, _);                                                    \
    cerr << '\n';                                                              \
  }
#define SZ(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back
#define pf push_front
#define F first
#define S second
#define by(x)                                                                  \
  [](const auto &a, const auto &b) {                                           \
    return a.x < b.x;                                                          \
  } // sort(arr, arr + N, by(a));
#define next ___next
#define prev ___prev
#define y1 ___y1
#define left ___left
#define right ___right
#define y0 ___y0
#define div ___div
#define j0 ___j0
#define jn ___jn

using ll = long long;
using ld = long double;
using ull = unsigned long long;
using namespace std;
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ll> vl;
const int N = 5002;
int n, k, a[N];
bool pre[N][N], suf[N][N];
int main() {
#ifdef HynDuf
  freopen(task ".in", "r", stdin);
  // freopen(task".out", "w", stdout);
#else
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
#endif
  cin >> n >> k;
  rep(i, 1, n) cin >> a[i];
  pre[0][0] = 1;
  rep(i, 1, n) rep(j, 0, k) {
    pre[i][j] = pre[i - 1][j];
    if (j >= a[i])
      pre[i][j] |= pre[i - 1][j - a[i]];
  }
  suf[n + 1][0] = 1;
  Rep(i, n, 1) rep(j, 0, k) {
    suf[i][j] = suf[i + 1][j];
    if (j >= a[i])
      suf[i][j] |= suf[i + 1][j - a[i]];
  }
  int ans = 0;
  rep(i, 1, n) {
    if (a[i] >= k) {
      ans++;
      continue;
    }
    int numr = 0;
    rep(j, k - a[i], k - 1) numr += suf[i + 1][j];
    if (pre[i - 1][0] && numr)
      ans++;
    else {
      int r = k - a[i] - 1;
      rep(l, 1, k - 1) {
        numr -= suf[i + 1][r + a[i]];
        numr += suf[i + 1][r];
        r--;
        if (pre[i - 1][l] && numr) {
          ans++;
          break;
        }
      }
    }
  }
  cout << n - ans;
  return 0;
}