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

#include <bits/stdc++.h>
using namespace std;
using lint = long long;
constexpr lint inf = 1LL << 60;
constexpr lint mod = 998244353;

template <int mod> struct modint {
  lint v;
  modint() : v(0) {}
  modint(signed v) : v(v) {}
  modint(lint t) {
    v = t % mod;
    if (v < 0)
      v += mod;
  }

  modint pow(lint k) {
    modint res(1), tmp(v);
    while (k) {
      if (k & 1)
        res *= tmp;
      tmp *= tmp;
      k >>= 1;
    }
    return res;
  }
  static modint add_identity() { return modint(0); }
  static modint mul_identity() { return modint(1); }
  modint inv() { return pow(mod - 2); }

  modint &operator+=(modint a) {
    v += a.v;
    if (v >= mod)
      v -= mod;
    return *this;
  }
  modint &operator-=(modint a) {
    v += mod - a.v;
    if (v >= mod)
      v -= mod;
    return *this;
  }
  modint &operator*=(modint a) {
    v = v * a.v % mod;
    return *this;
  }
  modint &operator/=(modint a) { return (*this) *= a.inv(); }

  modint operator+(modint a) const { return modint(v) += a; };
  modint operator-(modint a) const { return modint(v) -= a; };
  modint operator*(modint a) const { return modint(v) *= a; };
  modint operator/(modint a) const { return modint(v) /= a; };

  modint operator-() const { return v ? modint(mod - v) : modint(v); }

  bool operator==(const modint a) const { return v == a.v; }
  bool operator!=(const modint a) const { return v != a.v; }
  bool operator<(const modint a) const { return v < a.v; }
};
using mint = modint<mod>;
ostream &operator<<(ostream &os, mint m) { return os << m.v; }

lint dp[500][500][500];

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  string s;
  cin >> s;
  int ns = s.size();
  lint k;
  cin >> k;
  vector<int> ones;
  int one = 0;
  for (int i = 0; i < ns; ++i) {
    if (s[i] == '0') {
      ones.push_back(one);
      one = 0;
    } else {
      one++;
    }
  }
  ones.push_back(one);
  int m = ones.size();
  // vector<vector<vector<mint>>> dp(m + 1, vector<vector<mint>>(n + 1,
  // vector<mint>(n + 1, 0))); vector<vector<vector<lint>>> dp(m + 1,
  // vector<vector<lint>>(n + 1, vector<lint>(n + 1, 0)));
  for (int i = 0; i <= m; ++i) {
    for (int j = 0; j <= ns; ++j) {
      for (int l = 0; l <= ns; ++l) {
        dp[i][j][l] = 0;
      }
    }
  }
  dp[0][0][0] = 1;
  for (int i = 1; i <= m - 1; ++i) {
    vector<vector<lint>> temp(400, vector<lint>(400, 0));
    for (int j = 0; j <= ns; ++j) {
      for (int l = 0; l <= ns; ++l) {
        temp[j][l] = dp[i - 1][j][l];
        dp[i][j][l] = dp[i - 1][j][l];
      }
    }
    for (int j = 0; j <= ns; ++j) {
      for (int l = 0; l <= ns; ++l) {
        for (int x = 1; x <= ones[i - 1]; ++x) { // total  < 300
          if (k - x < 0 || l - x < 0)
            break;
          temp[j][l - x] += temp[j][l];
          temp[j][l - x] %= mod;
        }
      }
    }
    for (int j = 0; j <= ns; ++j) {
      for (int l = 0; l <= ns; ++l) {
        temp[j][l] -= dp[i - 1][j][l];
        if (temp[j][l] < 0)
          temp[j][l] += mod;
      }
    }

    // for (int j = 0; j <= ns; ++j) {
    //     for (int l = 0; l <= ns; ++l) {
    //         for (int x = 0; x <= ones[i - 1]; ++x) { // total  < 300
    //             if (k - x < 0)
    //                 break;
    //             dp[i][j][l - x] += dp[i - 1][j][l];
    //             dp[i][j][l - 1] %= mod;
    //         }
    //     }
    // }

    // if (i == m)
    //     break;

    for (int j = 1; j <= ns; ++j) {
      for (int l = 1; l <= ns; ++l) {
        if (j + l >= ns)
          break;
        dp[i][j + l - 1][l] += dp[i][j + l - 2][l - 1];
        dp[i][j + l - 1][l] %= mod;
      }
    }
    for (int j = 2; j <= ns; ++j) {
      for (int l = 1; l <= ns; ++l) {
        if (j + l >= ns)
          break;
        dp[i][l][j + l - 1] += dp[i][l - 1][j + l - 2];
        dp[i][l][j + l - 1] %= mod;
      }
    }

    for (int j = 0; j <= ns; ++j) {
      for (int l = 0; l <= ns; ++l) {
        dp[i][j][l] += temp[j][l];
        dp[i][j][l] %= mod;
      }
    }
  }
  int id = min(k, lint(ns));
  lint ret = 0;
  for (int i = 0; i <= id; ++i) {
    for (int j = 0; j <= ones[m - 1]; ++j) {
      ret += dp[m - 1][i][j];
      ret %= mod;
    }
  }
  cout << ret << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using lint = long long;
constexpr lint inf = 1LL << 60;
constexpr lint mod = 998244353;

template <int mod> struct modint {
  lint v;
  modint() : v(0) {}
  modint(signed v) : v(v) {}
  modint(lint t) {
    v = t % mod;
    if (v < 0)
      v += mod;
  }

  modint pow(lint k) {
    modint res(1), tmp(v);
    while (k) {
      if (k & 1)
        res *= tmp;
      tmp *= tmp;
      k >>= 1;
    }
    return res;
  }
  static modint add_identity() { return modint(0); }
  static modint mul_identity() { return modint(1); }
  modint inv() { return pow(mod - 2); }

  modint &operator+=(modint a) {
    v += a.v;
    if (v >= mod)
      v -= mod;
    return *this;
  }
  modint &operator-=(modint a) {
    v += mod - a.v;
    if (v >= mod)
      v -= mod;
    return *this;
  }
  modint &operator*=(modint a) {
    v = v * a.v % mod;
    return *this;
  }
  modint &operator/=(modint a) { return (*this) *= a.inv(); }

  modint operator+(modint a) const { return modint(v) += a; };
  modint operator-(modint a) const { return modint(v) -= a; };
  modint operator*(modint a) const { return modint(v) *= a; };
  modint operator/(modint a) const { return modint(v) /= a; };

  modint operator-() const { return v ? modint(mod - v) : modint(v); }

  bool operator==(const modint a) const { return v == a.v; }
  bool operator!=(const modint a) const { return v != a.v; }
  bool operator<(const modint a) const { return v < a.v; }
};
using mint = modint<mod>;
ostream &operator<<(ostream &os, mint m) { return os << m.v; }

lint dp[400][400][400];

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  string s;
  cin >> s;
  int ns = s.size();
  lint k;
  cin >> k;
  vector<int> ones;
  int one = 0;
  for (int i = 0; i < ns; ++i) {
    if (s[i] == '0') {
      ones.push_back(one);
      one = 0;
    } else {
      one++;
    }
  }
  ones.push_back(one);
  int m = ones.size();
  // vector<vector<vector<mint>>> dp(m + 1, vector<vector<mint>>(n + 1,
  // vector<mint>(n + 1, 0))); vector<vector<vector<lint>>> dp(m + 1,
  // vector<vector<lint>>(n + 1, vector<lint>(n + 1, 0)));
  for (int i = 0; i <= m; ++i) {
    for (int j = 0; j <= ns; ++j) {
      for (int l = 0; l <= ns; ++l) {
        dp[i][j][k] = 0;
      }
    }
  }
  dp[0][0][0] = 1;
  for (int i = 1; i <= m - 1; ++i) {
    vector<vector<lint>> temp(400, vector<lint>(400, 0));
    for (int j = 0; j <= ns; ++j) {
      for (int l = 0; l <= ns; ++l) {
        temp[j][l] = dp[i - 1][j][l];
        dp[i][j][l] = dp[i - 1][j][l];
      }
    }
    for (int j = 0; j <= ns; ++j) {
      for (int l = 0; l <= ns; ++l) {
        for (int x = 1; x <= ones[i - 1]; ++x) { // total  < 300
          if (k - x < 0 || l - x < 0)
            break;
          temp[j][l - x] += temp[j][l];
          temp[j][l - x] %= mod;
        }
      }
    }
    for (int j = 0; j <= ns; ++j) {
      for (int l = 0; l <= ns; ++l) {
        temp[j][l] -= dp[i - 1][j][l];
        if (temp[j][l] < 0)
          temp[j][l] += mod;
      }
    }

    // for (int j = 0; j <= ns; ++j) {
    //     for (int l = 0; l <= ns; ++l) {
    //         for (int x = 0; x <= ones[i - 1]; ++x) { // total  < 300
    //             if (k - x < 0)
    //                 break;
    //             dp[i][j][l - x] += dp[i - 1][j][l];
    //             dp[i][j][l - 1] %= mod;
    //         }
    //     }
    // }

    // if (i == m)
    //     break;

    for (int j = 1; j <= ns; ++j) {
      for (int l = 1; l <= ns; ++l) {
        if (j + l >= ns)
          break;
        dp[i][j + l - 1][l] += dp[i][j + l - 2][l - 1];
        dp[i][j + l - 1][l] %= mod;
      }
    }
    for (int j = 2; j <= ns; ++j) {
      for (int l = 1; l <= ns; ++l) {
        if (j + l >= ns)
          break;
        dp[i][l][j + l - 1] += dp[i][l - 1][j + l - 2];
        dp[i][l][j + l - 1] %= mod;
      }
    }

    for (int j = 0; j <= ns; ++j) {
      for (int l = 0; l <= ns; ++l) {
        dp[i][j][l] += temp[j][l];
        dp[i][j][l] %= mod;
      }
    }
  }
  int id = min(k, lint(ns));
  lint ret = 0;
  for (int i = 0; i <= id; ++i) {
    for (int j = 0; j <= ones[m - 1]; ++j) {
      ret += dp[m - 1][i][j];
      ret %= mod;
    }
  }
  cout << ret << endl;
  return 0;
}