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

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cmath>
#include <cstdarg>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
using namespace std;

namespace Namespace_Random_Generator {
class Random_Generator {
public:
  inline unsigned int get_random_value() const {
    return (((rand()) & 65535UL) << 16UL) + ((rand()) & 65535UL);
  }
};
}; // namespace Namespace_Random_Generator
using namespace Namespace_Random_Generator;
Random_Generator QQ;

int const MAX_N = 315;

int dp[2][MAX_N][MAX_N / 2 * MAX_N / 2];
int groups[MAX_N], groupsBackCount[MAX_N];
int const MODULE = 998244353;

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

int main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  string st;
  int k;
  cin >> st >> k;
  int n = (int)st.length();
  int cnt_ones = 0, cnt_moves = 0;
  int zeroC = 0;
  int maxGroup = 0;
  for (int i = 0; i < n; i++)
    if (st[i] == '1') {
      groups[zeroC]++;
      cnt_ones++;
      cnt_moves += zeroC;
      maxGroup = zeroC;
    } else {
      zeroC++;
    }
  groupsBackCount[maxGroup] = groups[maxGroup];
  for (int i = maxGroup - 1; i >= 0; i--)
    groupsBackCount[i] = groupsBackCount[i + 1] + groups[i];

  int from = 0;
  dp[from][0][0] = 1;
  for (int i = 0; i <= maxGroup; i++) {
    int to = 1 - from;
    for (int used = 0; used <= cnt_ones; used++)
      for (int moves = 0; moves <= min(k, cnt_moves); moves++)
        dp[to][used][moves] = 0;

    for (int used = 0; used <= cnt_ones; used++)
      for (int moves = 0; moves <= min(k, cnt_moves); moves++)
        if (dp[from][used][moves])
          for (int newTaken = max(0, cnt_ones - used - groupsBackCount[i + 1]);
               newTaken <= cnt_ones - used; newTaken++)
            add(dp[to][used + newTaken][moves + max(0, newTaken - groups[i])],
                dp[from][used][moves]);

    swap(from, to);
  }

  int ans = 0;
  for (int moves = 0; moves <= min(cnt_moves, k); moves++)
    add(ans, dp[from][cnt_ones][moves]);

  cout << ans;
  return 0;
}


Error: -11

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cmath>
#include <cstdarg>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
using namespace std;

namespace Namespace_Random_Generator {
class Random_Generator {
public:
  inline unsigned int get_random_value() const {
    return (((rand()) & 65535UL) << 16UL) + ((rand()) & 65535UL);
  }
};
}; // namespace Namespace_Random_Generator
using namespace Namespace_Random_Generator;
Random_Generator QQ;

int const MAX_N = 315;
double const EPS = 1e-8;
double const PI = 3.1415926535897932384626433832795;

int dp[2][MAX_N][(MAX_N) * (MAX_N) / 2];
int groups[MAX_N], groupsBackCount[MAX_N];
int const MODULE = 998244353;

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

int main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  string st;
  int k;
  cin >> st >> k;
  int n = (int)st.length();
  int cnt_ones = 0, cnt_moves = 0;
  int zeroC = 0;
  int maxGroup = 0;
  for (int i = 0; i < n; i++)
    if (st[i] == '1') {
      groups[zeroC]++;
      cnt_ones++;
      cnt_moves += zeroC;
      maxGroup = zeroC;
    } else {
      zeroC++;
    }
  groupsBackCount[maxGroup] = groups[maxGroup];
  for (int i = maxGroup - 1; i >= 0; i--)
    groupsBackCount[i] = groupsBackCount[i + 1] + groups[i];

  int from = 0;
  dp[from][0][0] = 1;
  for (int i = 0; i <= maxGroup; i++) {
    int to = 1 - from;
    for (int used = 0; used <= cnt_ones; used++)
      for (int moves = 0; moves <= min(k, cnt_moves); moves++)
        dp[to][used][moves] = 0;

    for (int used = 0; used <= cnt_ones; used++)
      for (int moves = 0; moves <= min(k, cnt_moves); moves++)
        if (dp[from][used][moves])
          for (int newTaken = max(0, cnt_ones - used - groupsBackCount[i + 1]);
               newTaken <= cnt_ones - used; newTaken++)
            add(dp[to][used + newTaken][moves + max(0, newTaken - groups[i])],
                dp[from][used][moves]);

    swap(from, to);
  }

  int ans = 0;
  for (int moves = 0; moves <= k; moves++)
    add(ans, dp[from][cnt_ones][moves]);

  cout << ans;
  return 0;
}