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

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n) for (int i = 1; i < n; i++)
#define rev(i, n) for (int i = n - 1; i >= 0; i--)
#define all(v) v.begin(), v.end()
#define P pair<int, int>
#define len(s) (int)s.size()
#define pb push_back

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
constexpr int mod = 998244353;
constexpr int inf = 3e18;

string S;
int K;
int dp[305][305][305];
int sum[305];
int N;
int memo[305][305], memo2[305][305];
signed main() {
  cin >> S >> K;
  chmin(K, 300ll);
  int cnt = 0;
  vector<int> v;
  int all = 0;
  rep(i, len(S)) {
    if (S[i] == '0') {
      v.pb(cnt);
      cnt = 0;
    } else {
      cnt++;
      all++;
    }
  }
  if (S.back() != '0')
    v.pb(cnt);
  N = len(v);
  rep(i, N) {
    sum[i] += v[i];
    sum[i + 1] = sum[i];
  }
  dp[0][0][0] = 1;
  rep(i, N) {
    memset(memo, 0, sizeof(memo));
    memset(memo2, 0, sizeof(memo2));
    rep(j, all + 1) {
      rep(k, K + 1) {
        if (dp[i][j][k] == 0)
          continue;
        (memo[j][k] += dp[i][j][k]) %= mod;
        if (j + v[i] + 1 <= all) {
          (memo[j + v[i] + 1][k] -= dp[i][j][k] - mod) %= mod;
        }
        if (j + v[i] + 1 <= all) {
          (memo2[j + v[i] + 1][k + 1] += dp[i][j][k]) %= mod;
        }
      }
    }
    rep(j, all + 1) {
      rep(k, K + 1) {
        (memo[j + 1][k] += memo[j][k]) %= mod;
        (memo2[j + 1][k + 1] += memo2[j][k]) %= mod;
      }
    }
    rep(j, all + 1) {
      if (j < sum[i])
        continue;
      rep(k, K + 1) { dp[i + 1][j][k] = (memo[j][k] + memo2[j][k]) % mod; }
    }
  }
  int ans = 0;
  rep(i, K + 1)(ans += dp[N][all][i]) %= mod;
  cout << ans << endl;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n) for (int i = 1; i < n; i++)
#define rev(i, n) for (int i = n - 1; i >= 0; i--)
#define all(v) v.begin(), v.end()
#define P pair<int, int>
#define len(s) (int)s.size()
#define pb push_back

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
constexpr int mod = 998244353;
constexpr int inf = 3e18;

string S;
int K;
int dp[305][305][305];
int sum[305];
int N;
signed main() {
  cin >> S >> K;
  int cnt = 0;
  vector<int> v;
  int all = 0;
  rep(i, len(S)) {
    if (S[i] == '0') {
      v.pb(cnt);
      cnt = 0;
    } else {
      cnt++;
      all++;
    }
  }
  if (S.back() != '0')
    v.pb(cnt);
  N = len(v);
  rep(i, N) {
    sum[i] += v[i];
    sum[i + 1] = sum[i];
  }
  dp[0][0][0] = 1;
  rep(i, N) {
    int memo[305][305], memo2[305][305];
    memset(memo, 0, sizeof(memo));
    memset(memo2, 0, sizeof(memo2));
    rep(j, all + 1) {
      rep(k, K + 1) {
        if (dp[i][j][k] == 0)
          continue;
        (memo[j][k] += dp[i][j][k]) %= mod;
        if (j + v[i] + 1 <= all) {
          (memo[j + v[i] + 1][k] -= dp[i][j][k] - mod) %= mod;
        }
        if (j + v[i] + 1 <= all) {
          (memo2[j + v[i] + 1][k + 1] += dp[i][j][k]) %= mod;
        }
      }
    }
    rep(j, all + 1) {
      rep(k, K + 1) {
        (memo[j + 1][k] += memo[j][k]) %= mod;
        (memo2[j + 1][k + 1] += memo2[j][k]) %= mod;
      }
    }
    rep(j, all + 1) {
      if (j < sum[i])
        continue;
      rep(k, K + 1) { dp[i + 1][j][k] = (memo[j][k] + memo2[j][k]) % mod; }
    }
  }
  int ans = 0;
  rep(i, K + 1)(ans += dp[N][all][i]) %= mod;
  cout << ans << endl;
}