Problem p03782 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;
#define F first
#define S second
#define ios                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
ll x;
vector<int> tmp[6005];
vector<int> shel_dplft[6005];
vector<int> shel_dprt[6005];
int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  vector<int> dp(5001, 0);
  dp[0] = 1;
  for (int i = 1; i <= n; i++) {
    vector<int> lol;
    for (int j = 5000 - a[i]; j >= 0; j--)
      dp[j + a[i]] += dp[j];
    for (int j = 0; j <= 5000; j++)
      if (dp[j])
        lol.push_back(j);
    shel_dplft[i] = lol;
  }
  dp.clear();
  dp.assign(5001, 0);
  dp[0] = 1;
  for (int i = n; i >= 1; i--) {
    vector<int> lol;
    for (int j = 5000 - a[i]; j >= 0; j--)
      dp[j + a[i]] += dp[j];
    for (int j = 0; j <= 5000; j++)
      if (dp[j])
        lol.push_back(j);
    shel_dprt[i] = lol;
  }
  shel_dplft[0].push_back(0);
  shel_dprt[n + 1].push_back(0);
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    vector<int> red = shel_dplft[i - 1];
    vector<int> blue = shel_dprt[i + 1];
    for (auto v : red) {
      int lo = 0, hi = blue.size() - 1;
      while (lo < hi) {
        int md = lo + (hi - lo + 1) / 2;
        if (v + blue[md] < k)
          lo = md;
        else
          hi = md - 1;
      }
      if (v + blue[lo] + a[i] >= k && v + blue[lo] < k) {
        cnt++;
        break;
      }
    }
  }
  cout << n - cnt << '\n';
  return 0;
}


Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
#define F first
#define S second
#define ios                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
ll x;
vector<int> tmp[6005];
vector<int> shel_dplft[6005];
vector<int> shel_dprt[6005];
int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  vector<int> dp(5001, 0);
  dp[0] = 1;
  for (int i = 1; i <= n; i++) {
    vector<int> lol;
    for (int j = 5000 - a[i]; j >= 0; j--)
      dp[j + a[i]] += dp[j];
    for (int j = 0; j <= 5000; j++)
      if (dp[j])
        lol.push_back(j);
    shel_dplft[i] = lol;
  }
  dp.clear();
  dp.assign(5001, 0);
  dp[0] = 1;
  for (int i = n; i >= 1; i--) {
    vector<int> lol;
    for (int j = 5000 - a[i]; j >= 0; j--)
      dp[j + a[i]] += dp[j];
    for (int j = 0; j <= 5000; j++)
      if (dp[j])
        lol.push_back(j);
    shel_dprt[i] = lol;
  }
  shel_dplft[0].push_back(0);
  shel_dprt[n + 1].push_back(0);
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    vector<int> red = shel_dplft[i - 1];
    vector<int> blue = shel_dprt[i + 1];
    for (auto v : red) {
      int lo = 0, hi = blue.size() - 1;
      while (lo < hi) {
        int md = lo + (hi - lo + 1) / 2;
        if (v + blue[md] < k)
          lo = md;
        else
          hi = md - 1;
      }
      if (v + blue[lo] + a[i] >= k && v + blue[lo] < k) {
        cnt++;
        break;
      }
    }
  }
  cout << n - cnt << '\n';
  return 0;
}