Problem p02635 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
// #pragma GCC optimize "Ofast,omit-frame-pointer,inline"
using namespace std;
typedef long long ll;

#define REP(i, n) for (ll i = 0; i < ll(n); i++)
#define FOR(i, m, n) for (int i = ll(m); i < ll(n); i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define VI vector<int>
#define VP vector<pair<int, int>>
#define VPP vector<pair<int, pair<int, int>>>
#define VLL vector<long long>
#define VVI vector<vector<int>>
#define VVLL vector<vector<long long>>
#define VC vector<char>
#define VS vector<string>
#define VVC vector<vector<char>>
#define VB vector<bool>
#define VVB vector<vector<bool>>
#define fore(i, a) for (auto &i : a)
typedef pair<int, int> P;
template <typename T>
using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const int INF = (1 << 30) - 1;
const ll INFL = 1LL << 60;
const ll mod = 998244353;

ll dp[305][305][305];

int main() {

  string s;
  cin >> s;
  ll m;
  cin >> m;

  VLL v;
  int cnt = 0;
  REP(i, s.size()) {
    if (s[i] == '0') {
      v.push_back(cnt);
      cnt = 0;
    } else
      cnt++;
  }
  if (cnt != 0) {
    v.push_back(cnt);
  }

  while (v.size() != 0 && v.back() == 0)
    v.pop_back();

  int n = v.size();
  if (v.size() <= 1) {
    cout << 1 << endl;
    return 0;
  }

  reverse(ALL(v));

  // vector<map<pair<int, int>, ll>> dp(305);

  REP(i, min(m + 1, v[0] + 1)) { dp[0][i][i] = 1; }

  int max_m = 0;
  REP(i, n) max_m += v[i];

  FOR(i, 1, n - 1) {
    // cout << i << endl;
    REP(j, max_m + 1) REP(k, max_m + 1) {
      ll cnt = dp[i - 1][j][k];
      ll mi = max(0LL, v[i] - max(0LL, m - k));
      FOR(l, mi, v[i] + j + 1) {
        int o = j + v[i] - l;
        int kk = k + max(0LL, v[i] - l);
        dp[i][o][kk] += cnt;
        dp[i][o][kk] %= mod;
      }
    }
  }
  ll sum = 0;

  REP(i, 305) REP(j, 305) {
    sum += dp[n - 2][i][j];
    sum %= mod;
  }
  cout << sum << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
// #pragma GCC optimize "Ofast,omit-frame-pointer,inline"
using namespace std;
typedef long long ll;

#define REP(i, n) for (ll i = 0; i < ll(n); i++)
#define FOR(i, m, n) for (int i = ll(m); i < ll(n); i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define VI vector<int>
#define VP vector<pair<int, int>>
#define VPP vector<pair<int, pair<int, int>>>
#define VLL vector<long long>
#define VVI vector<vector<int>>
#define VVLL vector<vector<long long>>
#define VC vector<char>
#define VS vector<string>
#define VVC vector<vector<char>>
#define VB vector<bool>
#define VVB vector<vector<bool>>
#define fore(i, a) for (auto &i : a)
typedef pair<int, int> P;
template <typename T>
using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const int INF = (1 << 30) - 1;
const ll INFL = 1LL << 60;
const ll mod = 998244353;

int main() {

  string s;
  cin >> s;
  ll m;
  cin >> m;

  VLL v;
  int cnt = 0;
  REP(i, s.size()) {
    if (s[i] == '0') {
      v.push_back(cnt);
      cnt = 0;
    } else
      cnt++;
  }
  if (cnt != 0) {
    v.push_back(cnt);
  }

  while (v.size() != 0 && v.back() == 0)
    v.pop_back();

  int n = v.size();
  if (v.size() <= 1) {
    cout << 1 << endl;
    return 0;
  }

  reverse(ALL(v));

  vector<map<pair<int, int>, ll>> dp(305);

  REP(i, min(m + 1, v[0] + 1)) { dp[0][{i, i}] = 1; }

  FOR(i, 1, n - 1) {
    for (auto _ : dp[i - 1]) {
      int j = _.first.first;
      int k = _.first.second;
      ll cnt = _.second;
      ll mi = max(0LL, v[i] - max(0LL, m - k));
      FOR(l, mi, v[i] + j + 1) {
        int o = j + v[i] - l;
        int kk = k + max(0LL, v[i] - l);
        // if (kk > m)continue;
        // if (o < 0)continue;
        dp[i][{o, kk}] += cnt;
        dp[i][{o, kk}] %= mod;
      }
    }
  }
  ll sum = 0;

  for (auto i : dp[n - 2]) {
    sum += i.second;
    sum %= mod;
  }

  cout << sum << endl;
}