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

//~ while (clock()<=69*CLOCKS_PER_SEC)
//~ #pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O3")
//~ #pragma GCC target ("avx2")
//~ #pragma GCC optimize("Ofast")
//~ #pragma GCC
//target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") ~ #pragma
//GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x)                                                                 \\
  sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<(    \\
            c i) {
sim > struct rge {
  c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
  ~debug() { cerr << endl; }
  eni(!=) cerr << boolalpha << i;
  ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
  sim dor(const c &) { ris; }
#endif
}
;
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

#define shandom_ruffle random_shuffle

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vll = vector<ll>;
const int nax = 307;
const ll mod = 998244353;

int n, k;

char wcz[nax];

int m;
int tab[nax];

int dp[nax][nax][2 * nax];

inline void dod(int &a, int b) {
  if ((a += b) >= mod)
    a -= mod;
}

void sdod(ll &a, ll b) { a = (a + b) % mod; }

int main() {
  scanf("%s", wcz + 1);
  scanf("%d", &k);
  n = strlen(wcz + 1);
  wcz[n + 1] = '0';
  n++;
  int x = 0;
  for (int i = 1; i <= n; i++) {
    if (wcz[i] == '1') {
      x++;
    } else {
      m++;
      tab[m] = x;
      x = 0;
    }
  }
  //~ debug() << range(tab+1, tab+1+m);
  dp[0][0][0] = 1;
  int s = 0;
  int cs = 0;
  for (int i = 1; i <= m; i++)
    cs += tab[i];
  for (int i = 1; i <= m; i++) {
    s += tab[i];
    for (int j = s; j <= cs; j++) {
      for (int l = 0; l <= cs; l++) {
        for (int p = 0; p <= j; p++) {
          if (!dp[i - 1][j - p][l])
            continue;
          dod(dp[i][j][l + max(0, tab[i] - p)], dp[i - 1][j - p][l]);
        }
      }
    }
  }
  int wyn = 0;
  for (int i = 0; i < 2 * nax; i++)
    if (i <= k)
      dod(wyn, dp[m][s][i]);
  printf("%d\\n", wyn);
  return 0;
}


Error: TLE

Fixed submission:

//~ while (clock()<=69*CLOCKS_PER_SEC)
//~ #pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O3")
//~ #pragma GCC target ("avx2")
//~ #pragma GCC optimize("Ofast")
//~ #pragma GCC
//target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") ~ #pragma
//GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x)                                                                 \\
  sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<(    \\
            c i) {
sim > struct rge {
  c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
  ~debug() { cerr << endl; }
  eni(!=) cerr << boolalpha << i;
  ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
  sim dor(const c &) { ris; }
#endif
}
;
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

#define shandom_ruffle random_shuffle

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vll = vector<ll>;
const int nax = 307;
const ll mod = 998244353;

int n, k;

char wcz[nax];

int m;
int tab[nax];

ll dp[nax][nax][2 * nax];

void dod(ll &a, ll b) {
  if ((a += b) >= mod)
    a -= mod;
}

void sdod(ll &a, ll b) { a = (a + b) % mod; }

int main() {
  scanf("%s", wcz + 1);
  scanf("%d", &k);
  n = strlen(wcz + 1);
  wcz[n + 1] = '0';
  n++;
  int x = 0;
  for (int i = 1; i <= n; i++) {
    if (wcz[i] == '1') {
      x++;
    } else {
      m++;
      tab[m] = x;
      x = 0;
    }
  }
  //~ debug() << range(tab+1, tab+1+m);
  dp[0][0][0] = 1;
  int s = 0;
  for (int i = 1; i <= m; i++) {
    s += tab[i];
    for (int j = s; j <= n; j++) {
      for (int l = 0; l <= n; l++) {
        for (int p = 0; p <= j; p++) {
          if (!dp[i - 1][j - p][l])
            continue;
          dod(dp[i][j][l + max(0, tab[i] - p)], dp[i - 1][j - p][l]);
        }
      }
    }
  }
  ll wyn = 0;
  for (int i = 0; i < 2 * nax; i++)
    if (i <= k)
      dod(wyn, dp[m][s][i]);
  printf("%lld\\n", wyn);
  return 0;
}