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

#include <algorithm>
#include <cmath>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <vector>

#define FOR(i, n) for (int i = 0; i < n; i++)
#define FORE(i, a, b) for (int i = a; i <= b; i++)
#define ll long long
// #define int long long
#define ld long double
#define vi vector<ll>
#define pb push_back
#define ff first
#define ss second
#define ii pair<int, int>
#define iii pair<int, ii>
#define il pair<ll, ll>
#define vv vector
#define endl '\\n'

using namespace std;

const ll MOD = 998244353;
ll fxp(ll a, ll b) {
  if (b == 0)
    return 1;
  if (b % 2 == 0) {
    ll c = fxp(a, b / 2);
    return (c * c) % MOD;
  } else {
    return (a * fxp(a, b - 1)) % MOD;
  }
}
ll f[301];

ll nCr(int n, int r) {
  return (((f[n] * fxp(f[r], MOD - 2)) % MOD) * fxp(f[n - r], MOD - 2)) % MOD;
}

ll dp2[301][301];
void pre() {
  FOR(i, 300) { // i places
    if (i == 0) {
      FOR(j, 300) dp2[i][j] = 0;
      dp2[0][0] = 1;
    } else
      FOR(j, 300) { // j balls
        if (j == 0)
          dp2[i][j] = 1;
        else
          FORE(k, 1, i) { dp2[i][j] += dp2[k][j - 1]; }
      }
  }
}

ll dp[301][301][301];
ll pref[301][301];
void solve() {
  f[0] = 1;
  FOR(i, 300) f[i] = (f[i - 1] * i) % MOD;
  pre();
  string s;
  int k;
  cin >> s >> k;
  k = min(k, 300);
  vi all;
  int ctr = 0;
  FOR(i, s.size()) {
    if (s[i] == '0') {
      all.pb(ctr);
      ctr = 0;
    } else {
      ctr++;
    }
  }
  if (ctr != 0)
    all.pb(ctr);

  int xx = all.size();

  // reverse(all.begin(), all.end());
  FOR(i, xx) {
    // ith item
    if (i == 0) {
      FOR(overhead, 301) {
        FOR(j, k + 1) {
          if (j == 0)
            dp[i][overhead][j] = 1;
          else
            dp[i][overhead][j] = 0;
        }
      }
    } else
      FOR(overhead, 301) {
        FOR(j, k + 1) {
          // option 1, end here;
          if (overhead > 0 and i > 0)
            dp[i][overhead][j] +=
                pref[overhead - 1][j]; // dp[i-1][overhead-1][j];
          dp[i][overhead][j] %= MOD;
          FOR(c, all[i] + 1) {
            if (c + overhead > 300 or j - c < 0)
              break;
            dp[i][overhead][j] += dp[i - 1][overhead + c][j - c];
            dp[i][overhead][j] %= MOD;
          }
        }
      }
    FOR(x, 301) {
      FOR(y, k + 1) {
        if (x == 0) {
          pref[x][y] = dp[i][x][y];
        } else {
          pref[x][y] = (dp[i][x][y] + pref[x - 1][y]) % MOD;
        }
      }
    }
  }

  /*
  FOR(i,all.size()){
      cout << all[i] << " ";
      if(i == 0){
          dp[0][0] = 1;
      }else
      FOR(j,k+1){
          FORE(c,0,all[i]){
              if(c > j)break; //  we will try to move c stuff
              dp[i][j] += dp2[i][c]*dp[i-1][j-c];
          }
      }
  }*/
  ll sum = 0;
  FORE(i, 0, k) {

    sum += dp[xx - 1][0][i];
    sum %= MOD;
  }
  // FORE(i,0,k)sum += dp[xx-1][i];

  cout << sum << endl;
}

int main() {

  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int t = 1;
  // cin >> t;
  while (t--)
    solve();
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cmath>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <vector>

#define FOR(i, n) for (int i = 0; i < n; i++)
#define FORE(i, a, b) for (int i = a; i <= b; i++)
#define ll long long
// #define int long long
#define ld long double
#define vi vector<ll>
#define pb push_back
#define ff first
#define ss second
#define ii pair<int, int>
#define iii pair<int, ii>
#define il pair<ll, ll>
#define vv vector
#define endl '\\n'

using namespace std;

const ll MOD = 998244353;
ll fxp(ll a, ll b) {
  if (b == 0)
    return 1;
  if (b % 2 == 0) {
    ll c = fxp(a, b / 2);
    return (c * c) % MOD;
  } else {
    return (a * fxp(a, b - 1)) % MOD;
  }
}
ll f[301];

ll nCr(int n, int r) {
  return (((f[n] * fxp(f[r], MOD - 2)) % MOD) * fxp(f[n - r], MOD - 2)) % MOD;
}

ll dp2[301][301];
void pre() {
  FOR(i, 300) { // i places
    if (i == 0) {
      FOR(j, 300) dp2[i][j] = 0;
      dp2[0][0] = 1;
    } else
      FOR(j, 300) { // j balls
        if (j == 0)
          dp2[i][j] = 1;
        else
          FORE(k, 1, i) { dp2[i][j] += dp2[k][j - 1]; }
      }
  }
}

ll dp[301][301][301];
void solve() {
  f[0] = 1;
  FOR(i, 300) f[i] = (f[i - 1] * i) % MOD;
  pre();
  string s;
  int k;
  cin >> s >> k;
  k = min(k, 300);
  vi all;
  int ctr = 0;
  FOR(i, s.size()) {
    if (s[i] == '0') {
      all.pb(ctr);
      ctr = 0;
    } else {
      ctr++;
    }
  }
  if (ctr != 0)
    all.pb(ctr);

  int xx = all.size();
  // reverse(all.begin(), all.end());
  FOR(i, xx) {
    // ith item
    FOR(overhead, 301) {
      FOR(j, k + 1) {
        // option 1, end here;
        if (overhead > 0)
          dp[i][overhead][j] += dp[i - 1][overhead - 1][j];
        FOR(c, all[i]) { dp[i][overhead][j] += 2; }
      }
    }
  }

  /*
  FOR(i,all.size()){
      cout << all[i] << " ";
      if(i == 0){
          dp[0][0] = 1;
      }else
      FOR(j,k+1){
          FORE(c,0,all[i]){
              if(c > j)break; //  we will try to move c stuff
              dp[i][j] += dp2[i][c]*dp[i-1][j-c];
          }
      }
  }*/
  ll sum = 0;
  // FORE(i,0,k)sum += dp[xx-1][i];

  cout << sum << endl;
}

int main() {

  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int t = 1;
  // cin >> t;
  while (t--)
    solve();
  return 0;
}