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

#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define mp make_pair
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef complex<double> point;
typedef double DOUBLE;
#define xx real()
#define yy imag()

#define REP(i, a, b) for (int i = (a); i < (int)(b); i++)
#define REPN(i, a, b) for (int i = (a); i <= (int)(b); i++)
#define FA(it, x)                                                              \\
  for (__typeof((x).begin()) it = (x).begin(); it != (x).end(); it++)
#define SZ(x) (int)(x).size()
#define BE(x) (x).begin(), (x).end()
#define SORT(x) sort(BE(x))
#define _1 first
#define _2 second

#define x1 gray_cat_x1
#define y1 gray_cat_y1
#define j0 gray_cat_j0

template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

#define file "I1"

const double EPS = 1e-9;
const double PI = acos(-1.);
const ll LL_INF = 1e17 + 16;
const int INF = 1e9 + 10;
const ll MOD = 998244353;

const int MAXN = 3e2 + 5;

int cnt[MAXN];

ll dp[MAXN][MAXN][MAXN];
ll dp_s[MAXN][MAXN][MAXN];
ll dp_sd[MAXN][MAXN][MAXN];

char s[MAXN];

void solve() {
  int m;
  scanf("%s%d", &s[0], &m);
  int cur = 0, n = strlen(s), top = 0;
  m = min(m, n);
  REP(i, 0, n) {
    if (s[i] == '0') {
      cnt[top++] = cur;
      cur = 0;
    } else {
      cur++;
    }
  }
  if (cur) {
    cnt[top++] = cur;
  }
  n = top;
  dp[n][0][0] = 1;
  REPN(j, 0, m) { dp_s[n][j][0] = 1; }
  // dp_sd[n][0][0] = 1;
  for (int i = n - 1; i >= 0; i--) {
    REPN(j, 0, m) {
      REPN(k, 0, m) {
        dp[i][j][k] = (dp[i][j][k] + dp_s[i + 1][j][k]) % MOD;
        if (j > cnt[i]) {
          dp[i][j][k] =
              (dp[i][j][k] + MOD - dp_s[i + 1][j - cnt[i] - 1][k]) % MOD;
        }
        dp[i][j][k] = (dp[i][j][k] + dp_sd[i + 1][j][k]) % MOD;

        dp_s[i][j][k] = dp[i][j][k];
        if (j) {
          dp_s[i][j][k] = (dp_s[i][j][k] + dp_s[i][j - 1][k]) % MOD;
        }
      }
    }
    for (int j = m; j >= 0; j--) {
      REPN(k, 1, m) {
        dp_sd[i][j][k] = dp[i][j + 1][k - 1] + dp_sd[i][j + 1][k - 1];
      }
    }
    /*for(int j = m; j >= 0; j--) {
            REPN(k, 0, m) {
                    dp_sd[i][j][j + k] = (dp_sd[i][j][j + k] + dp_sd[i][j + 1][j
    + k]) % MOD;
            }
    }*/
  }

  /*for(int i = n; i >= 0; i--) {
          REPN(j, 0, m) {
                  REPN(k, 0, m) {
                          printf("%d %d %d: %lld %lld %lld\\n", i, j, k,
  dp[i][j][k], dp_s[i][j][k], dp_sd[i][j][k]);
                  }
          }
  }*/

  ll ans = 0ll;
  REPN(k, 0, m) { ans = (ans + dp[0][0][k]) % MOD; }
  printf("%lld\\n", ans);
}

int main() {

  // freopen(file".in", "r", stdin); freopen(file".out", "w", stdout);
  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define mp make_pair
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef complex<double> point;
typedef double DOUBLE;
#define xx real()
#define yy imag()

#define REP(i, a, b) for (int i = (a); i < (int)(b); i++)
#define REPN(i, a, b) for (int i = (a); i <= (int)(b); i++)
#define FA(it, x)                                                              \\
  for (__typeof((x).begin()) it = (x).begin(); it != (x).end(); it++)
#define SZ(x) (int)(x).size()
#define BE(x) (x).begin(), (x).end()
#define SORT(x) sort(BE(x))
#define _1 first
#define _2 second

#define x1 gray_cat_x1
#define y1 gray_cat_y1
#define j0 gray_cat_j0

template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

#define file "I1"

const double EPS = 1e-9;
const double PI = acos(-1.);
const ll LL_INF = 1e17 + 16;
const int INF = 1e9 + 10;
const ll MOD = 998244353;

const int MAXN = 3e2 + 5;

int cnt[MAXN];

ll dp[MAXN][MAXN][MAXN];
ll dp_s[MAXN][MAXN][MAXN];
ll dp_sd[MAXN][MAXN][MAXN];

char s[MAXN];

void solve() {
  int m;
  scanf("%s%d", &s[0], &m);
  int cur = 0, n = strlen(s), top = 0;
  REP(i, 0, n) {
    if (s[i] == '0') {
      cnt[top++] = cur;
      cur = 0;
    } else {
      cur++;
    }
  }
  if (cur) {
    cnt[top++] = cur;
  }
  n = top;
  dp[n][0][0] = 1;
  REPN(j, 0, m) { dp_s[n][j][0] = 1; }
  // dp_sd[n][0][0] = 1;
  for (int i = n - 1; i >= 0; i--) {
    REPN(j, 0, m) {
      REPN(k, 0, m) {
        dp[i][j][k] = (dp[i][j][k] + dp_s[i + 1][j][k]) % MOD;
        if (j > cnt[i]) {
          dp[i][j][k] =
              (dp[i][j][k] + MOD - dp_s[i + 1][j - cnt[i] - 1][k]) % MOD;
        }
        dp[i][j][k] = (dp[i][j][k] + dp_sd[i + 1][j][k]) % MOD;

        dp_s[i][j][k] = dp[i][j][k];
        if (j) {
          dp_s[i][j][k] = (dp_s[i][j][k] + dp_s[i][j - 1][k]) % MOD;
        }
      }
    }
    for (int j = m; j >= 0; j--) {
      REPN(k, 1, m) {
        dp_sd[i][j][k] = dp[i][j + 1][k - 1] + dp_sd[i][j + 1][k - 1];
      }
    }
    /*for(int j = m; j >= 0; j--) {
            REPN(k, 0, m) {
                    dp_sd[i][j][j + k] = (dp_sd[i][j][j + k] + dp_sd[i][j + 1][j
    + k]) % MOD;
            }
    }*/
  }

  /*for(int i = n; i >= 0; i--) {
          REPN(j, 0, m) {
                  REPN(k, 0, m) {
                          printf("%d %d %d: %lld %lld %lld\\n", i, j, k,
  dp[i][j][k], dp_s[i][j][k], dp_sd[i][j][k]);
                  }
          }
  }*/

  ll ans = 0ll;
  REPN(k, 0, m) { ans = (ans + dp[0][0][k]) % MOD; }
  printf("%lld\\n", ans);
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
  // freopen(file".in", "r", stdin); freopen(file".out", "w", stdout);
  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
}