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

#include <bits/stdc++.h>
using namespace std;
//{
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORD(i, a, b) for (int i = (a); i >= (b); --i)
#define VAR(v, i) __typeof(i) v = (i)
#define all(v) (v).begin(), (v).end()

#define PII pair<int, int>
#define st first
#define nd second
#define pb push_back
#define lint long long int
#define VI vector<int>

#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 {
  ~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 << "]";
}
}
;
//{
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

#define make(x)                                                                \\
  int(x);                                                                      \\
  scanf("%d", &(x));
#define make2(x, y)                                                            \\
  int(x), (y);                                                                 \\
  scanf("%d%d", &(x), &(y));
#define make3(x, y, z)                                                         \\
  int(x), (y), (z);                                                            \\
  scanf("%d%d%d", &(x), &(y), &(z));
#define make4(x, y, z, t)                                                      \\
  int(x), (y), (z), (t);                                                       \\
  scanf("%d%d%d%d", &(x), &(y), &(z), &(t));
#define makev(v, n)                                                            \\
  VI(v);                                                                       \\
  FOR(i, 0, (n)) {                                                             \\
    make(a);                                                                   \\
    (v).pb(a);                                                                 \\
  }
#define IOS ios_base::sync_with_stdio(0)
#define HEAP priority_queue

#define read(x) scanf("%d", &(x));
#define read2(x, y) scanf("%d%d", &(x), &(y));
#define read3(x, y, z) scanf("%d%d%d", &(x), &(y), &(z));
#define read4(x, y, z, t) scanf("%d%d%d%d", &(x), &(y), &(z), &(t));
#define readv(v, n)                                                            \\
  FOR(i, 0, (n)) {                                                             \\
    make(a);                                                                   \\
    (v).pb(a);                                                                 \\
  }
#define jeb() fflush(stdout);
//}

int k;
char s[305];
int mod = 998244353;
VI w;

int memo[305][305][605];

int rob(int pos, int dod, int kara) {
  if (kara < 0)
    return 0;
  if (pos == 0) {
    if (kara < dod)
      return 0;
    return 1;
  }
  if (memo[pos][dod][kara] != -1) {
    return memo[pos][dod][kara];
  }
  int ans = 0;
  FOR(i, 0, w[pos] + dod + 1) {
    ans = ans + rob(pos - 1, dod + w[pos] - i, kara - abs(w[pos] - i));
    if (ans >= mod)
      ans -= mod;
  }
  memo[pos][dod][kara] = ans;
  return ans;
}

int main() {
  scanf("%s", s);
  read(k);
  int n = strlen(s);
  s[n] = '0';
  n++;
  FOR(i, 0, n) {
    if (s[i] == '0') {
      int j = i - 1;
      while (j >= 0 && s[j] == '1')
        j--;
      if (j >= 0)
        w.pb(i - j - 1);
      else
        w.pb(0);
    }
  }
  // debug() << imie(w);
  int kara = 2 * k;
  int sum = 0;
  FOR(j, 0, w.size()) sum += w[j];
  int N = w.size();
  kara = min(kara, 2 * sum);
  FOR(i, 0, N) FOR(j, 0, sum + 1) FOR(u, 0, kara + 1) memo[i][j][u] = -1;
  printf("%d\\n", rob(w.size() - 1, 0, kara));
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
//{
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORD(i, a, b) for (int i = (a); i >= (b); --i)
#define VAR(v, i) __typeof(i) v = (i)
#define all(v) (v).begin(), (v).end()

#define PII pair<int, int>
#define st first
#define nd second
#define pb push_back
#define lint long long int
#define VI vector<int>

#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 {
  ~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 << "]";
}
}
;
//{
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

#define make(x)                                                                \\
  int(x);                                                                      \\
  scanf("%d", &(x));
#define make2(x, y)                                                            \\
  int(x), (y);                                                                 \\
  scanf("%d%d", &(x), &(y));
#define make3(x, y, z)                                                         \\
  int(x), (y), (z);                                                            \\
  scanf("%d%d%d", &(x), &(y), &(z));
#define make4(x, y, z, t)                                                      \\
  int(x), (y), (z), (t);                                                       \\
  scanf("%d%d%d%d", &(x), &(y), &(z), &(t));
#define makev(v, n)                                                            \\
  VI(v);                                                                       \\
  FOR(i, 0, (n)) {                                                             \\
    make(a);                                                                   \\
    (v).pb(a);                                                                 \\
  }
#define IOS ios_base::sync_with_stdio(0)
#define HEAP priority_queue

#define read(x) scanf("%d", &(x));
#define read2(x, y) scanf("%d%d", &(x), &(y));
#define read3(x, y, z) scanf("%d%d%d", &(x), &(y), &(z));
#define read4(x, y, z, t) scanf("%d%d%d%d", &(x), &(y), &(z), &(t));
#define readv(v, n)                                                            \\
  FOR(i, 0, (n)) {                                                             \\
    make(a);                                                                   \\
    (v).pb(a);                                                                 \\
  }
#define jeb() fflush(stdout);
//}

int k;
char s[305];
int mod = 998244353;
VI w;

map<pair<int, PII>, int> memo;

int rob(int pos, int dod, int kara) {
  if (kara < 0)
    return 0;
  if (pos == 0) {
    if (kara < dod)
      return 0;
    return 1;
  }
  if (memo.find({pos, {dod, kara}}) != memo.end()) {
    return memo[{pos, {dod, kara}}];
  }
  int ans = 0;
  FOR(i, 0, w[pos] + dod + 1) {
    ans = ans + rob(pos - 1, dod + w[pos] - i, kara - abs(w[pos] - i));
    if (ans >= mod)
      ans -= mod;
  }
  memo[{pos, {dod, kara}}] = ans;
  return ans;
}

int main() {
  scanf("%s", s);
  read(k);
  int n = strlen(s);
  s[n] = '0';
  n++;
  FOR(i, 0, n) {
    if (s[i] == '0') {
      int j = i - 1;
      while (j >= 0 && s[j] == '1')
        j--;
      if (j >= 0)
        w.pb(i - j - 1);
      else
        w.pb(0);
    }
  }
  // debug() << imie(w);
  int kara = 2 * k;
  int sum = 0;
  FOR(j, 0, w.size()) sum += w[j];
  kara = min(kara, 2 * sum);
  printf("%d\\n", rob(w.size() - 1, 0, kara));
}