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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ll long long
#define ld long double
#define pb push_back
#define all(x) (x).begin(), (x).end()
template <typename T>
using ord_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int N = 307, Q = 300, mod = 998244353;
int K, m, p[N], dp[N][N][2 * N], pr[N][N][2 * N], ps[N][N][N];
string s;

void add(int &x, int y) {
  x += y;
  if (x >= mod)
    x -= mod;
}

int get(int i, int c, int l, int r, int f) {
  if (l > r || c + Q < 0)
    return 0;
  if (f)
    return (pr[i][r][c + Q] - ((l > 0) ? pr[i][l - 1][c + Q] : 0) + mod) % mod;
  if (c < 0)
    return 0;
  return (ps[i][r][c] - ((l > 0) ? ps[i][l - 1][c] : 0) + mod) % mod;
}

int main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL);
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
#endif

  cin >> s >> K;
  s += '0';
  int n = s.size(), cur = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '0') {
      p[++m] = cur;
    } else
      cur++;
  }
  K = min(K, p[m]);
  dp[0][0][0] = 1;
  for (int i = 0; i <= m; i++) {
    for (int j = p[i]; j <= cur; j++) {
      if (i > 0) {
        for (int k = 0; k <= K; k++) {
          dp[i][j][k] = get(i - 1, k + (p[i] - p[i - 1]) - j, 0,
                            j - (p[i] - p[i - 1]), 1);
          add(dp[i][j][k], get(i - 1, k, j - (p[i] - p[i - 1]) + 1, j, 0));
        }
      }
      for (int c = -Q; c <= Q; c++) {
        pr[i][j][c + Q] = (j > 0) ? pr[i][j - 1][c + Q] : 0;
        if (j + c >= 0 && j + c <= Q)
          add(pr[i][j][c + Q], dp[i][j][j + c]);
        if (c >= 0) {
          ps[i][j][c] = (j > 0) ? ps[i][j - 1][c] : 0;
          add(ps[i][j][c], dp[i][j][c]);
        }
      }
    }
  }
  int res = 0;
  for (int i = 0; i <= cur; i++)
    for (int j = 0; j <= K; j++)
      add(res, dp[m][i][j]);
  cout << res;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ll long long
#define ld long double
#define pb push_back
#define all(x) (x).begin(), (x).end()
template <typename T>
using ord_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int N = 307, Q = 300, mod = 998244353;
int K, m, p[N], dp[N][N][2 * N], pr[N][N][2 * N], ps[N][N][2 * N];
string s;

void add(int &x, int y) {
  x += y;
  if (x >= mod)
    x -= mod;
}

int get(int i, int c, int l, int r, int f) {
  if (l > r || c + Q < 0)
    return 0;
  if (f)
    return (pr[i][r][c + Q] - ((l > 0) ? pr[i][l - 1][c + Q] : 0) + mod) % mod;
  if (c < 0)
    return 0;
  return (ps[i][r][c] - ((l > 0) ? ps[i][l - 1][c] : 0) + mod) % mod;
}

int main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL);
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
#endif

  cin >> s >> K;
  s += '0';
  int n = s.size(), cur = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '0') {
      p[++m] = cur;
    } else
      cur++;
  }
  dp[0][0][0] = 1;
  for (int i = 0; i <= m; i++) {
    for (int j = p[i]; j <= cur; j++) {
      if (i > 0) {
        for (int k = 0; k <= K; k++) {
          dp[i][j][k] = get(i - 1, k + (p[i] - p[i - 1]) - j, 0,
                            j - (p[i] - p[i - 1]), 1);
          add(dp[i][j][k], get(i - 1, k, j - (p[i] - p[i - 1]) + 1, j, 0));
        }
      }
      for (int c = -Q; c <= Q; c++) {
        pr[i][j][c + Q] = (j > 0) ? pr[i][j - 1][c + Q] : 0;
        if (j + c >= 0 && j + c <= Q)
          add(pr[i][j][c + Q], dp[i][j][j + c]);
        if (c >= 0) {
          ps[i][j][c] = (j > 0) ? ps[i][j - 1][c] : 0;
          add(ps[i][j][c], dp[i][j][c]);
        }
      }
    }
  }
  int res = 0;
  for (int i = 0; i <= cur; i++)
    for (int j = 0; j <= K; j++)
      add(res, dp[m][i][j]);
  cout << res;
}