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

#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define vll vector<ll>
#define vvvl vector<vvl>
#define vvl vector<vector<ll>>
#define VV(a, b, c, d) vector<vector<d>>(a, vector<d>(b, c))
#define VVV(a, b, c, d) vector<vvl>(a, vvl(b, vll(c, d)));
#define re(c, b) for (ll c = 0; c < b; c++)
#define all(obj) (obj).begin(), (obj).end()
typedef long long int ll;
typedef long double ld;
using namespace std;

ll P = 998244353;
int main(int argc, char const *argv[]) {
  string s;
  std::cin >> s;
  ll k;
  std::cin >> k;

  vector<ll> box;
  vector<ll> accum;
  ll cnt = 0;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == '0')
      box.push_back(cnt), cnt = 0;
    else
      cnt++;
    if (i == s.size() - 1)
      box.push_back(cnt);
  }
  for (int i = 0; i < box.size(); i++) {
    ll tmp = (i != 0 ? accum[i - 1] : 0) + box[i];
    accum.push_back(tmp);
  }
  ll m = box.size();
  k = min(k, accum[m - 1]);
  vvvl dp = VVV(m + 1, accum[m - 1] + 1, k + 1, 0);

  dp[0][0][0] = 1;
  for (int i = 1; i <= m; i++) { // i箱
    ll min_num = accum[i - 1];
    for (int j = min_num; j <= accum[m - 1]; j++) { // 総和がj
      for (int l = 0; l <= k; l++) {                // 操作回数がk
        // r追加
        // box[i-1]追加までは無料
        // それ以上はr-box[i-1]コスト
        for (int r = 0; r <= j; r++) {
          ll cost = max(0LL, r - box[i - 1]);
          if (cost > l)
            break;
          dp[i][j][l] = (dp[i][j][l] + dp[i - 1][j - r][l - cost]) % P;
        }
      }
    }
  }
  ll ans = 0;
  for (int i = 0; i <= k; i++)
    ans = (ans + dp[m][accum[m - 1]][i]) % P;
  std::cout << ans << '\\n';
  return 0;
}


Error: TLE

Fixed submission:

#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define vll vector<ll>
#define vvvl vector<vvl>
#define vvl vector<vector<ll>>
#define VV(a, b, c, d) vector<vector<d>>(a, vector<d>(b, c))
#define VVV(a, b, c, d) vector<vvl>(a, vvl(b, vll(c, d)));
#define re(c, b) for (ll c = 0; c < b; c++)
#define all(obj) (obj).begin(), (obj).end()
typedef long long int ll;
typedef long double ld;
using namespace std;

ll P = 998244353;
int main(int argc, char const *argv[]) {
  string s;
  std::cin >> s;
  ll k;
  std::cin >> k;
  k = min(k, 300LL);

  vector<ll> box;
  vector<ll> accum;
  ll cnt = 0;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == '0')
      box.push_back(cnt), cnt = 0;
    else
      cnt++;
    if (i == s.size() - 1)
      box.push_back(cnt);
  }
  for (int i = 0; i < box.size(); i++) {
    ll tmp = (i != 0 ? accum[i - 1] : 0) + box[i];
    accum.push_back(tmp);
  }
  ll m = box.size();
  vvvl dp = VVV(m + 1, accum[m - 1] + 1, k + 1, 0);

  dp[0][0][0] = 1;
  for (int i = 1; i <= m; i++) { // i箱
    ll min_num = accum[i - 1];
    for (int j = min_num; j <= accum[m - 1]; j++) { // 総和がj
      for (int l = 0; l <= k; l++) {                // 操作回数がk
        // r追加
        // box[i-1]追加までは無料
        // それ以上はr-box[i-1]コスト
        for (int r = 0; r <= j; r++) {
          ll cost = max(0LL, r - box[i - 1]);
          if (cost > l)
            break;
          dp[i][j][l] = (dp[i][j][l] + dp[i - 1][j - r][l - cost]) % P;
        }
      }
    }
  }
  ll ans = 0;
  for (int i = 0; i <= k; i++)
    ans = (ans + dp[m][accum[m - 1]][i]) % P;
  std::cout << ans << '\\n';
  return 0;
}