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

#include <algorithm>
#include <chrono>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

typedef long long ll;
typedef long double ld;
#define fastInp                                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);                                                                 \\
  ios_base::sync_with_stdio(0);

const ll MOD = 998244353, INF = 1e9 * 1e9 + 10, eps = 1e-7;

ll dp[405][405][405];

vector<pair<ll, ll>> vec;

int main() {
  fastInp;

  string s;
  ll k;
  cin >> s >> k;

  ll n = s.size();
  ll pr = -1, cnt = 0;
  s.push_back('a');
  k = min(k, ll(400));
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == '0') {
      vec.push_back({cnt, 1});
      cnt = 0;
    } else if (s[i] == 'a') {
      vec.push_back({cnt, 1});
    } else if (s[i] == '1') {
      cnt++;
    }

    pr = s[i] - '0';
  }

  dp[0][0][0] = 1;
  ll ind = 1;

  ll sm = 0;
  for (int i = 0; i < vec.size(); i++) {
    if (vec[i].second == 0)
      continue;
    vector<vector<ll>> pref(k + 1);
    for (int j = 0; j <= k; j++) {
      pref[j].push_back(0);
      for (int t = 0; t <= k; t++)
        pref[j].push_back(pref[j].back() + dp[ind - 1][j][t]);
    }
    for (int t = 0; t <= k; t++) {

      ll cnt = 0;
      for (int j = 0; j <= k; j++) { // j + t -
        if (j < t)
          continue;

        ll vl = 0;
        if (t + 1 < pref[j].size())
          vl = pref[j][t + 1] - pref[j][max(0ll, t - vec[i].first)];
        dp[ind][j][t] += cnt + vl;
        cnt += dp[ind - 1][j][t];
        cnt %= MOD;
        dp[ind][j][t] %= MOD;
        if (i == vec.size() - 1 && j == t && j <= k)
          sm += dp[ind][j][t];
        sm %= MOD;
        // cout << dp[ind][j][t] << " ";
      }
      // cout << "\\n";
    }

    ind++;
    // cout << "-\\n";
  }

  cout << sm;
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <chrono>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

typedef long long ll;
typedef long double ld;
#define fastInp                                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);                                                                 \\
  ios_base::sync_with_stdio(0);

const ll MOD = 998244353, INF = 1e9 * 1e9 + 10, eps = 1e-7;

ll dp[400][400][400];

vector<pair<ll, ll>> vec;

int main() {
  fastInp;

  string s;
  ll k;
  cin >> s >> k;

  ll n = s.size();
  ll pr = -1, cnt = 0;
  s.push_back('a');
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == '0') {
      vec.push_back({cnt, 1});
      cnt = 0;
    } else if (s[i] == 'a') {
      vec.push_back({cnt, 1});
    } else if (s[i] == '1') {
      cnt++;
    }

    pr = s[i] - '0';
  }

  dp[0][0][0] = 1;
  ll ind = 1;

  ll sm = 0;
  for (int i = 0; i < vec.size(); i++) {
    if (vec[i].second == 0)
      continue;
    vector<vector<ll>> pref(k + 1);
    for (int j = 0; j <= k; j++) {
      pref[j].push_back(0);
      for (int t = 0; t <= k; t++)
        pref[j].push_back(pref[j].back() + dp[ind - 1][j][t]);
    }
    for (int t = 0; t <= k; t++) {

      ll cnt = 0;
      for (int j = 0; j <= k; j++) { // j + t -
        if (j < t)
          continue;

        ll vl = 0;
        if (t + 1 < pref[j].size())
          vl = pref[j][t + 1] - pref[j][max(0ll, t - vec[i].first)];
        dp[ind][j][t] += cnt + vl;
        cnt += dp[ind - 1][j][t];
        cnt %= MOD;
        dp[ind][j][t] %= MOD;
        if (i == vec.size() - 1 && j == t && j <= k)
          sm += dp[ind][j][t];
        sm %= MOD;
        // cout << dp[ind][j][t] << " ";
      }
      // cout << "\\n";
    }

    ind++;
    // cout << "-\\n";
  }

  cout << sm;
  return 0;
}