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

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

typedef long long ll;
const ll INF = 1010000000000000017LL;
const ll MOD = 998244353;

#define REP(i, n) for (ll i = 0; i < n; i++)

// #define DEBUG(fmt, ...)
#define DEBUG(fmt, ...) fprintf(stderr, fmt "\\n", ##__VA_ARGS__)

ll totalOnes = 0;
ll table[301][301][301];

int main() {
  std::cout << std::fixed << std::setprecision(10);

  string S;
  ll K;

  cin >> S >> K;

  vector<int> boxes;

  ll numOne = 0;
  for (int i = 0; i < S.size(); i++) {
    if (S[i] == '0') {
      boxes.push_back(numOne);
      numOne = 0;
    } else {
      numOne++;
      totalOnes++;
    }
  }
  boxes.push_back(numOne);

  REP(i, 301) {
    REP(j, 301) {
      REP(k, 301) {
        if (i == 0) {
          table[i][j][k] = 1;
        } else {
          table[i][j][k] = 0;
        }
      }
    }
  }

  ll currentOnes = boxes[0];
  for (int box = 1; box <= boxes.size() - 1; box++) {
    currentOnes += boxes[box];
    for (int ones = 0; ones <= (totalOnes - currentOnes); ones++) {
      for (int ops = 0; ops <= min(K, totalOnes); ops++) {
        for (int b = 0; b <= boxes[box] + ones; b++) {
          ll new_ops = ops - max(boxes[box] - b, 0);
          if (new_ops >= 0) {
            table[box][ones][ops] +=
                (table[box - 1][boxes[box] + ones - b][new_ops] % MOD);
            table[box][ones][ops] %= MOD;
          }
        }
      }
    }
  }

  cout << table[boxes.size() - 1][0][min(K, totalOnes)] << endl;
}


Error: -11

Fixed submission:

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

typedef long long ll;
const ll INF = 1010000000000000017LL;
const ll MOD = 998244353;

#define REP(i, n) for (ll i = 0; i < n; i++)

// #define DEBUG(fmt, ...)
#define DEBUG(fmt, ...) fprintf(stderr, fmt "\\n", ##__VA_ARGS__)

ll totalOnes = 0;
ll table[301][301][301];
/*
ll DFS(vector<int>& boxes, int box, int ones, ll ops){
   DEBUG("box=%d, ones=%d ops=%lld", box, ones, ops);
   if( box == 0 ){
        return 1;
   }

   // TODO: Use MOD
   if( table[box][ones][ops] != -1 ){
        return table[box][ones][max(ops, (ll)boxes[box])];
   }

   ll count = 0;
   for( int b = boxes[box]+ones; b >= 0; b-- ){
      //DEBUG("b = %d", b);
      ll new_ops= ops - max(boxes[box]-b,0);
      if( new_ops >= 0 ){
         count += DFS(boxes, box-1, (boxes[box]+ones)-b, new_ops);
      }
   }

   //DEBUG("box=%d, ones=%d return %lld", box, ones, count);
   table[box][ones][max(ops, (ll)boxes[box])] = count;
   return count;
}
*/

int main() {
  std::cout << std::fixed << std::setprecision(10);

  string S;
  ll K;

  cin >> S >> K;

  vector<int> boxes;

  ll numOne = 0;
  for (int i = 0; i < S.size(); i++) {
    if (S[i] == '0') {
      boxes.push_back(numOne);
      numOne = 0;
    } else {
      numOne++;
      totalOnes++;
    }
  }
  boxes.push_back(numOne);

  REP(i, 301) {
    REP(j, 301) {
      REP(k, 301) {
        if (i == 0) {
          table[i][j][k] = 1;
        } else {
          table[i][j][k] = 0;
        }
      }
    }
  }

  for (int box = 1; box <= 300; box++) {
    for (int ones = 0; ones <= totalOnes; ones++) {
      for (int ops = 0; ops <= max(K, totalOnes); ops++) {
        for (int b = 0; b <= boxes[box] + ones; b++) {
          ll new_ops = ops - max(boxes[box] - b, 0);
          if (new_ops >= 0) {
            table[box][ones][ops] +=
                (table[box - 1][boxes[box] + ones - b][new_ops] % MOD);
            table[box][ones][ops] %= MOD;
          }
        }
      }
    }
  }

  cout << table[boxes.size() - 1][0][min(K, totalOnes)] << endl;
}