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

#include <bits/stdc++.h>

using namespace std;

#ifdef NeverBeRed
#include "debug.h"
#else
#define debug(...) 9715
#endif
typedef long long ll;
typedef long double ld;
typedef complex<ld> point;
#define F first
#define S second

const int mod = 998244353;
int dp[305][305][305];
int dp2[305][305][305];
int dp3[305][305][305];

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

int main() {
#ifdef TurnRed
  // freopen("a.in", "r", stdin);
  // freopen("a.out", "w", stdout);
#endif

  ios_base::sync_with_stdio(0), cin.tie(0);

  string s;
  int K;
  cin >> s >> K;

  vector<int> a = {0};
  for (auto i : s) {
    if (i == '1')
      ++a.back();
    else
      a.push_back(0);
  }

  int n = a.size();
  int l = s.length();
  dp[0][0][0] = 1;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j <= l; ++j)
      for (int k = 0; k <= l; ++k) {
        //				for (int x = 0; x <= min(a[i], j); ++x)
        //					add(dp[i+1][j-x][k],
        //dp[i][j][k]); 				for (int x = 1; x + j <= l; ++x) 					add(dp[i+1][j+x][k+x],
        //dp[i][j][k]);
        add(dp2[i + 1][j][k], dp[i][j][k]);
        if (j - min(a[i], j) > 0)
          add(dp2[i + 1][j - min(a[i], j) - 1][k], mod - dp[i][j][k]);

        add(dp3[i + 1][j + 1][k + 1], dp[i][j][k]);
      }

    for (int j = 0; j <= l; ++j)
      for (int k = 0; k <= l; ++k)
        add(dp3[i + 1][j + 1][k + 1], dp3[i + 1][j][k]);

    for (int j = l; j >= 0; --j)
      for (int k = 0; k <= l; ++k)
        add(dp2[i + 1][j][k], dp2[i + 1][j + 1][k]);

    for (int j = 0; j <= l; ++j)
      for (int k = 0; k <= l; ++k) {
        dp[i + 1][j][k] = dp2[i + 1][j][k];
        add(dp[i + 1][j][k], dp3[i + 1][j][k]);
      }
  }
  int ans = 0;
  for (int k = 0; k <= min(K, l); ++k)
    add(ans, dp[n][0][k]);
  cout << ans << "\\n";

  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#ifdef NeverBeRed
#include "debug.h"
#else
#define debug(...) 9715
#endif
typedef long long ll;
typedef long double ld;
typedef complex<ld> point;
#define F first
#define S second

const int mod = 998244353;
int dp[305][305][305];
int dp2[305][305][305];
int dp3[305][305][305];

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

int main() {
#ifdef TurnRed
  // freopen("a.in", "r", stdin);
  // freopen("a.out", "w", stdout);
#endif

  ios_base::sync_with_stdio(0), cin.tie(0);

  string s;
  int K;
  cin >> s >> K;

  vector<int> a = {0};
  for (auto i : s) {
    if (i == '1')
      ++a.back();
    else
      a.push_back(0);
  }

  int n = a.size();
  int l = s.length();
  dp[0][0][0] = 1;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j <= l; ++j)
      for (int k = 0; k <= l; ++k) {
        //				for (int x = 0; x <= min(a[i], j); ++x)
        //					add(dp[i+1][j-x][k],
        //dp[i][j][k]); 				for (int x = 1; x + j <= l; ++x) 					add(dp[i+1][j+x][k+x],
        //dp[i][j][k]);
        add(dp2[i + 1][j][k], dp[i][j][k]);
        if (j - min(a[i], j) > 0)
          add(dp2[i + 1][j - min(a[i], j) - 1][k], mod - dp[i][j][k]);

        add(dp3[i + 1][j + 1][k + 1], dp[i][j][k]);
      }

    for (int j = 0; j <= l; ++j)
      for (int k = 0; k <= l; ++k)
        add(dp3[i + 1][j + 1][k + 1], dp3[i + 1][j][k]);

    for (int j = l; j >= 0; --j)
      for (int k = 0; k <= l; ++k)
        add(dp2[i + 1][j][k], dp2[i + 1][j + 1][k]);

    for (int j = 0; j <= l; ++j)
      for (int k = 0; k <= l; ++k) {
        dp[i + 1][j][k] = dp2[i + 1][j][k];
        add(dp[i + 1][j][k], dp3[i + 1][j][k]);
      }
  }
  int ans = 0;
  for (int k = 0; k <= K; ++k)
    add(ans, dp[n][0][k]);
  cout << ans << "\\n";

  return 0;
}