Problem p03782 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define ld long double
#define pll pair<ll, ll>

#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define mp make_pair

#define X first
#define Y second

#define LB(x) (x & (-x))
#define BIT(a, b) ((ll)(a & (1 << b)) == 0 ? false : true)

const ll MAXN = 5e3 + 10;

vector<ll> r[MAXN];
vector<ll> l[MAXN];

ll a[MAXN];

bool dpl[MAXN][MAXN];
bool dpr[MAXN][MAXN];

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

  ll n, k, res = 0;
  cin >> n >> k;

  for (ll i = 1; i <= n; i++) {
    cin >> a[i];
  }

  dpl[0][0] = true;
  l[0].pb(0);

  for (ll i = 1; i <= n; i++) {
    for (ll j = 0; j < k; j++) {
      if (a[i] > j) {
        dpl[i][j] = dpl[i - 1][j];
      } else {
        dpl[i][j] = dpl[i - 1][j] | dpl[i - 1][j - a[i]];
      }

      if (dpl[i][j]) {
        l[i].pb(j);
      }
    }
  }

  dpr[n + 1][0] = true;
  r[n + 1].pb(0);

  for (ll i = n; i > 0; i--) {
    for (ll j = 0; j < k; j++) {
      if (a[i] > j) {
        dpr[i][j] = dpr[i + 1][j];
      } else {
        dpr[i][j] = dpr[i + 1][j] | dpr[i + 1][j - a[i]];
      }

      if (dpr[i][j]) {
        r[i].pb(j);
      }
    }
  }

  for (ll i = 1; i <= n; i++) {
    for (ll j = 0; j < l[i - 1].size(); j++) {
      ll u = lower_bound(r[i + 1].begin(), r[i + 1].end(), k - l[i - 1][j]) -
             r[i + 1].begin() - 1;

      if (u == -1) {
        continue;
      } else {
        if ((r[i + 1][u] + l[i - 1][j]) >= (k - a[i])) {
          res++;
          goto E;
        }
      }
    }

  E:;
  }

  cout << n - res;

  return 0;
}


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

using namespace std;

#define ll long long
#define ld long double
#define pll pair<ll, ll>

#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define mp make_pair

#define X first
#define Y second

#define LB(x) (x & (-x))
#define BIT(a, b) ((ll)(a & (1 << b)) == 0 ? false : true)

const ll MAXN = 5e3 + 10;

vector<short> r[MAXN];
vector<short> l[MAXN];

ll a[MAXN];

bool dpl[MAXN][MAXN];
bool dpr[MAXN][MAXN];

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

  ll n, k, res = 0;
  cin >> n >> k;

  for (ll i = 1; i <= n; i++) {
    cin >> a[i];
  }

  dpl[0][0] = true;
  l[0].pb(0);

  for (ll i = 1; i <= n; i++) {
    for (ll j = 0; j < k; j++) {
      if (a[i] > j) {
        dpl[i][j] = dpl[i - 1][j];
      } else {
        dpl[i][j] = dpl[i - 1][j] | dpl[i - 1][j - a[i]];
      }

      if (dpl[i][j]) {
        l[i].pb(j);
      }
    }
  }

  dpr[n + 1][0] = true;
  r[n + 1].pb(0);

  for (ll i = n; i > 0; i--) {
    for (ll j = 0; j < k; j++) {
      if (a[i] > j) {
        dpr[i][j] = dpr[i + 1][j];
      } else {
        dpr[i][j] = dpr[i + 1][j] | dpr[i + 1][j - a[i]];
      }

      if (dpr[i][j]) {
        r[i].pb(j);
      }
    }
  }

  for (ll i = 1; i <= n; i++) {
    for (ll j = 0; j < l[i - 1].size(); j++) {
      ll u = lower_bound(r[i + 1].begin(), r[i + 1].end(), k - l[i - 1][j]) -
             r[i + 1].begin() - 1;

      if (u == -1) {
        continue;
      } else {
        if ((r[i + 1][u] + l[i - 1][j]) >= (k - a[i])) {
          res++;
          goto E;
        }
      }
    }

  E:;
  }

  cout << n - res;

  return 0;
}