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

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

const long long MOD = 998244353;

int A[305];
int cnt[305];

char S[305];
int K, N;

long long memo[305][305][305];

long long dp(int i, int k, int skipped) {
  if (skipped < 0) {
    return 0;
  }
  if (k < 0) {
    return 0;
  }
  if (i == 0) {
    return skipped == 0 && k == 0;
  }

  if (memo[i][k][skipped] != -1) {
    return memo[i][k][skipped];
  }

  long long res = 0;
  for (int j = 0; j <= 300; j++) {
    if (j >= cnt[i]) {
      res += dp(i - 1, k, skipped - (j - cnt[i]));
      if (skipped - (j - cnt[i]) < 0) {
        break;
      }
    } else {
      res += dp(i - 1, k - (cnt[i] - j), skipped + (cnt[i] - j));
    }
  }
  res %= MOD;
  // printf("dp(%d, %d, %d)=%lld\\n", i, k, skipped, res);

  return memo[i][k][skipped] = res;
}

int main() {
  scanf(" %s %d", S, &K);
  N = strlen(S);
  K = min(K, N);

  memset(cnt, 0, sizeof(cnt));
  int cntZero = 0;
  int indx = 0;
  for (int i = 0; i < N; i++) {
    if (S[i] == '0') {
      cntZero++;
    } else {
      A[indx] = cntZero + 1;
      cnt[A[indx]]++;
      indx++;
    }
  }

  memset(memo, -1, sizeof(memo));
  long long ans = 0;
  for (int i = 0; i <= K; i++) {
    ans += dp(cntZero + 1, i, 0);
  }
  ans %= MOD;
  printf("%lld", ans);

  return 0;
}


Error: -11

Fixed submission:

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

const long long MOD = 998244353;

char S[305];

int A[305];

long long memo[105][105][105][105];

int N;
int M = 0;
int P = 0;

int cntZero[305];
int cnt2[305];

long long dp(int i, int j, int K, int skipped) {
  if (K < 0) {
    return 0;
  }

  if (i == N && j == N) {
    return 1;
  }

  if (i == N || j == N) {
    return 1;
  }

  if (memo[i][j][K][skipped] != -1) {
    return memo[i][j][K][skipped];
  }

  long long res = 0;
  if (S[j] == '0') {
    // place a 1
    res += dp(i + 1, j, K - 1, skipped + 1);

    // place a 0
    res += dp(i + 1, j + 1, K, skipped);
  }

  if (S[j] == '1') {
    if (skipped >= 1) {
      res += dp(i, j + 1, K, skipped - 1);
    }

    // place a 1 in the corresponding position
    res += dp(i + 1, j + 1, K, skipped);
  }

  res %= MOD;
  // printf("dp(%d, %d, %d, %d)=%lld\\n", i, j, K, skipped, res);

  return memo[i][j][K][skipped] = res;

  return 0;
}

/*
long long dp(int curNum, int i, int j, int K){
    if(K < 0){
        return 0;
    }else if(i == M || j == M){
        return K == 0;
    }

    if(A[j] < curNum){
        //return 0;
        return dp(curNum, i, j+1, K-1);
    }

    if(memo[curNum][i][j][K] != -1){
        return memo[curNum][i][j][K];
    }

    long long res = 0;
    //res += dp(curNum, i, j+1, K-1);
    res += dp(curNum+1, i, j, K); // not use curNum

    if(A[j] == curNum && (j == 0 || A[j] != A[j-1])){
        // use curNum, decide not to change
        res += dp(curNum, i+1, j+1, K);
    }else{
        // use curNum, decide to change
        res += dp(curNum, i+1, j+1, K-1);
    }

    res %= MOD;
    //printf("dp(%d, %d, %d, %d)=%lld\\n", curNum, i, j, K, res);

    return memo[curNum][i][j][K] = res;
}
*/

int main() {
  scanf(" %s", S);
  N = strlen(S);

  int K;
  scanf("%d", &K);
  K = min(K, N);

  for (int i = 0; i < N; i++) {
    M += (S[i] == '1');
  }

  cntZero[0] = S[0] == '0';
  for (int i = 1; i < N; i++) {
    cntZero[i] = cntZero[i - 1] + (S[i] == '0');
  }

  int cnt = 0;
  for (int i = 0; i < N; i++) {
    if (S[i] == '1') {
      A[cnt++] = cntZero[i] + 1;
    }
  }

  memset(cnt2, 0, sizeof(cnt2));

  for (int i = 0; i < cnt; i++) {
    cnt2[A[i]]++;
  }

  for (int i = 0; i <= 300; i++) {
    assert(cnt2[i] <= 1);
  }

  /*
  for(int i = 0; i < cnt; i ++){
      printf("A[%d]=%d\\n", i, A[i]);
  }
  */

  memset(memo, -1, sizeof(memo));
  printf("%lld", dp(0, 0, K, 0));
  return 0;
}