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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long LL;
mt19937 gene(233);
typedef complex<double> Complex;
#define fi first
#define se second
#define ins insert
#define pb push_back
inline char GET_CHAR() {
  const int maxn = 131072;
  static char buf[maxn], *p1 = buf, *p2 = buf;
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, maxn, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
inline int getInt() {
  int res(0);
  char c = getchar();
  while (c < '0')
    c = getchar();
  while (c >= '0') {
    res = res * 10 + (c - '0');
    c = getchar();
  }
  return res;
}

inline LL fastpo(LL x, LL n, LL mod) {
  LL res(1);
  while (n) {
    if (n & 1) {
      res = res * (LL)x % mod;
    }
    x = x * (LL)x % mod;
    n /= 2;
  }
  return res;
}

inline string itoa(int x, int width = 0) {
  string res;
  if (x == 0)
    res.push_back('0');
  while (x) {
    res.push_back('0' + x % 10);
    x /= 10;
  }
  while ((int)res.size() < width)
    res.push_back('0');
  reverse(res.begin(), res.end());
  return res;
}
const int _B = 131072;
char buf[_B];
int _bl = 0;
inline void flush() {
  fwrite(buf, 1, _bl, stdout);
  _bl = 0;
}
__inline void _putchar(char c) {
  if (_bl == _B)
    flush();
  buf[_bl++] = c;
}
inline void print(LL x, char c) {
  static char tmp[20];
  int l = 0;
  if (!x)
    tmp[l++] = '0';
  else {
    while (x) {
      tmp[l++] = x % 10 + '0';
      x /= 10;
    }
  }
  for (int i = l - 1; i >= 0; i--)
    _putchar(tmp[i]);
  _putchar(c);
}
const int N = 303;
const int LOG = 20;
const int mod = 998244353;
const int inf = 1e9 + 7;
int n, m;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int dp[N][N][N];
int cnt[N], sufsum[N];
__inline void add(int &a, int b) {
  if ((a += b) >= mod)
    a -= mod;
}
int main() {
  string s;
  int k;
  cin >> s >> k;
  k = min(k, 300);
  // s.clear();
  // for(int i = 0; i < 300; i++) s.pb(gene() % 2 + '0');
  n = 1;
  for (char c : s) {
    if (c == '0') {
      n++;
    } else {
      cnt[n]++;
    }
  }
  for (int i = n; i >= 1; i--) {
    sufsum[i] = sufsum[i + 1] + cnt[i];
  }
  dp[0][0][0] = 1;
  for (int i = 1; i <= n + 1; i++) {
    for (int j = 0; j <= sufsum[i]; j++) {
      for (int l = 0; l <= 300; l++) {
        if (dp[i - 1][j][l] == 0)
          continue;
        for (int k = max(0, cnt[i] - j);
             k <= 300 && l + k - cnt[i] <= 300 && j + k - cnt[i] <= 300; k++) {
          add(dp[i][j + k - cnt[i]][l + max(0, k - cnt[i])], dp[i - 1][j][l]);
        }
        //	printf("dp[%d][%d][%d] = %d\\n", i - 1, j, l, dp[i - 1][j][l]);
      }
    }
  }
  int ans = 0;
  for (int l = 0; l <= k; l++) {
    ans = (ans + dp[n][0][l]) % mod;
  }
  cout << ans << endl;
}


Error: -11

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long LL;
mt19937 gene(233);
typedef complex<double> Complex;
#define fi first
#define se second
#define ins insert
#define pb push_back
inline char GET_CHAR() {
  const int maxn = 131072;
  static char buf[maxn], *p1 = buf, *p2 = buf;
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, maxn, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
inline int getInt() {
  int res(0);
  char c = getchar();
  while (c < '0')
    c = getchar();
  while (c >= '0') {
    res = res * 10 + (c - '0');
    c = getchar();
  }
  return res;
}

inline LL fastpo(LL x, LL n, LL mod) {
  LL res(1);
  while (n) {
    if (n & 1) {
      res = res * (LL)x % mod;
    }
    x = x * (LL)x % mod;
    n /= 2;
  }
  return res;
}

inline string itoa(int x, int width = 0) {
  string res;
  if (x == 0)
    res.push_back('0');
  while (x) {
    res.push_back('0' + x % 10);
    x /= 10;
  }
  while ((int)res.size() < width)
    res.push_back('0');
  reverse(res.begin(), res.end());
  return res;
}
const int _B = 131072;
char buf[_B];
int _bl = 0;
inline void flush() {
  fwrite(buf, 1, _bl, stdout);
  _bl = 0;
}
__inline void _putchar(char c) {
  if (_bl == _B)
    flush();
  buf[_bl++] = c;
}
inline void print(LL x, char c) {
  static char tmp[20];
  int l = 0;
  if (!x)
    tmp[l++] = '0';
  else {
    while (x) {
      tmp[l++] = x % 10 + '0';
      x /= 10;
    }
  }
  for (int i = l - 1; i >= 0; i--)
    _putchar(tmp[i]);
  _putchar(c);
}
const int N = 303;
const int LOG = 20;
const int mod = 998244353;
const int inf = 1e9 + 7;
int n, m;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int dp[N][N][N];
int cnt[N], sufsum[N];
__inline void add(int &a, int b) {
  if ((a += b) >= mod)
    a -= mod;
}
int main() {
  string s;
  int k;
  cin >> s >> k;
  // s.clear();
  // for(int i = 0; i < 300; i++) s.pb(gene() % 2 + '0');
  n = 1;
  for (char c : s) {
    if (c == '0') {
      n++;
    } else {
      cnt[n]++;
    }
  }
  for (int i = n; i >= 1; i--) {
    sufsum[i] = sufsum[i + 1] + cnt[i];
  }
  dp[0][0][0] = 1;
  for (int i = 1; i <= n + 1; i++) {
    for (int j = 0; j <= sufsum[i]; j++) {
      for (int l = 0; l <= 300; l++) {
        if (dp[i - 1][j][l] == 0)
          continue;
        for (int k = max(0, cnt[i] - j); k <= min(300, 300 + cnt[i] - j - l);
             k++) {
          add(dp[i][j + k - cnt[i]][l + max(0, k - cnt[i])], dp[i - 1][j][l]);
        }
        //	printf("dp[%d][%d][%d] = %d\\n", i - 1, j, l, dp[i - 1][j][l]);
      }
    }
  }
  int ans = 0;
  for (int l = 0; l <= k; l++) {
    ans = (ans + dp[n][0][l]) % mod;
  }
  cout << ans << endl;
}