Problem p02635 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define fi first
#define se second
typedef pair<ll, ll> P;
using VP = vector<P>;
using VVP = vector<VP>;
using VI = vector<ll>;
using VVI = vector<VI>;
using VVVI = vector<VVI>;
const int inf = 1e9 + 7;
const ll INF = 1LL << 61;
const ll mod = 998244353;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll i, j;
  string s;
  cin >> s;
  s += '0';
  ll k;
  cin >> k;
  ll cnt = 0;
  VI v;
  ll u = 0;
  for (i = 0; i < s.size(); i++) {
    if (s[i] == '0') {
      v.pb(cnt);
      u += cnt;
      cnt = 0;
    } else
      cnt++;
  }
  k = min(k, u);
  ll n = v.size();
  VVVI dp(n + 1, VVI(u + 1, VI(u + 1, 0ll)));
  for (j = v[0]; j <= u; j++) {
    dp[0][j - v[0]][j - v[0]] = 1;
  }
  for (i = 1; i < n; i++) {
    for (ll j2 = 0; j2 <= u; j2++) {
      for (ll l = 0; l <= u; l++) {
        for (ll j1 = max(0ll, j2 - l); j1 <= u; j1++) {
          if (v[i] + j2 - j1 < 0ll)
            break;
          if (j2 >= j1) {
            dp[i][j2][l] += dp[i - 1][j1][l - (j2 - j1)];
          } else {
            dp[i][j2][l] += dp[i - 1][j1][l];
          }
          dp[i][j2][l] %= mod;
        }
      }
    }
  }
  ll ans = 0;
  for (ll l = 0; l <= k; l++) {
    ans += dp[n - 1][0][l];
    ans %= mod;
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define fi first
#define se second
typedef pair<ll, ll> P;
using VP = vector<P>;
using VVP = vector<VP>;
using VI = vector<ll>;
using VVI = vector<VI>;
using VVVI = vector<VVI>;
const int inf = 1e9 + 7;
const ll INF = 1LL << 61;
const ll mod = 998244353;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll i, j;
  string s;
  cin >> s;
  s += '0';
  ll k;
  cin >> k;
  ll cnt = 0;
  VI v;
  ll u = 0;
  for (i = 0; i < s.size(); i++) {
    if (s[i] == '0') {
      v.pb(cnt);
      u += cnt;
      cnt = 0;
    } else
      cnt++;
  }
  ll n = v.size();
  VVVI dp(n + 1, VVI(u + 1, VI(u + 1, 0ll)));
  for (j = v[0]; j <= u; j++) {
    dp[0][j - v[0]][j - v[0]] = 1;
  }

  for (i = 1; i < n; i++) {
    for (ll j2 = 0; j2 <= u; j2++) {
      for (ll l = 0; l <= u; l++) {
        for (ll j1 = max(0ll, j2 - l); j1 <= u; j1++) {
          if (v[i] + j2 - j1 < 0ll)
            break;
          if (j2 >= j1) {
            dp[i][j2][l] += dp[i - 1][j1][l - (j2 - j1)];
          } else {
            dp[i][j2][l] += dp[i - 1][j1][l];
          }
          dp[i][j2][l] %= mod;
        }
      }
    }
  }
  ll ans = 0;
  for (ll l = 0; l <= k; l++) {
    ans += dp[n - 1][0][l];
    ans %= mod;
  }
  cout << ans << endl;
}