Problem p02692 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define ll long long
#define X first
#define Y second
#define all(x) x.begin(), x.end()
#define ordered_set                                                            \\
  tree<int, null_type, less<int>, 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__) << "] "

const int mod = (int)1e9 + 7;
const int MX = (int)1e5 + 10;

void add_self(int &x, int y) {
  x += y;
  if (x >= mod)
    x -= mod;
}
void sub_self(int &x, int y) {
  x -= y;
  if (x < 0)
    x += mod;
}
void mul(int &x, int y) { x = x * 1LL * y % mod; }

int n;
vector<string> s;

int dp[MX][5][5][5];

int solve(int idx, int a, int b, int c) {
  if (a < 0 || b < 0 || c < 0 || a > 4 || b > 4 || c > 4)
    return 0;
  if (idx == n) {
    return a >= 0 && b >= 0 && c >= 0;
  }

  auto &ret = dp[idx][a][b][c];
  if (ret != -1)
    return ret;

  ret = 0;

  if (s[idx] == "AB") {
    ret |= solve(idx + 1, a - 1, b + 1, c);
    ret |= solve(idx + 1, a + 1, b - 1, c);
  } else if (s[idx] == "AC") {
    ret |= solve(idx + 1, a - 1, b, c + 1);
    ret |= solve(idx + 1, a + 1, b, c - 1);
  } else {
    ret |= solve(idx + 1, a, b - 1, c + 1);
    ret |= solve(idx + 1, a, b + 1, c - 1);
  }

  return ret;
}

void build(int idx, int a, int b, int c) {

  if (idx == n)
    return;

  if (s[idx] == "AB") {
    if (solve(idx + 1, a - 1, b + 1, c)) {
      cout << "B\\n", build(idx + 1, a - 1, b + 1, c);
    } else {
      cout << "A\\n", build(idx + 1, a + 1, b - 1, c);
    }
  } else if (s[idx] == "AC") {
    if (solve(idx + 1, a - 1, b, c + 1)) {
      cout << "C\\n", build(idx + 1, a - 1, b, c + 1);
    } else {
      cout << "A\\n", build(idx + 1, a + 1, b, c - 1);
    }
  } else {
    if (solve(idx + 1, a, b + 1, c - 1)) {
      cout << "B\\n", build(idx + 1, a, b + 1, c - 1);
    } else {
      cout << "C\\n", build(idx + 1, a, b - 1, c + 1);
    }
  }
}

int main() {

  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);

  int a, b, c;
  cin >> n >> a >> b >> c;

  s.resize(n);
  for (auto &x : s)
    cin >> x;

  memset(dp, 0xff, sizeof dp);

  a = min(a, 3);
  b = min(b, 3);
  c = min(c, 3);

  if (solve(0, a, b, c) == 1) {
    cout << "Yes\\n";
    build(0, a, b, c);
  } else {
    cout << "No\\n";
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define ll long long
#define X first
#define Y second
#define all(x) x.begin(), x.end()
#define ordered_set                                                            \\
  tree<int, null_type, less<int>, 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__) << "] "

const int mod = (int)1e9 + 7;
const int MX = (int)1e5 + 10;

void add_self(int &x, int y) {
  x += y;
  if (x >= mod)
    x -= mod;
}
void sub_self(int &x, int y) {
  x -= y;
  if (x < 0)
    x += mod;
}
void mul(int &x, int y) { x = x * 1LL * y % mod; }

int n, a, b, c;
vector<string> s;

int dp[MX][4][4][4][4][4][4];

int solve(int idx, int incA, int incB, int incC, int decA, int decB, int decC) {
  if (idx == n) {
    return incA - decA + a >= 0 && incB - decB + b >= 0 && incC - decC + c >= 0;
  }

  auto &ret = dp[idx][incA][incB][incC][decA][decB][decC];
  if (ret != -1)
    return ret;

  ret = 0;

  if (s[idx] == "AB") {
    if (decA <= 2 && a + incA - decA >= 0)
      ret |= solve(idx + 1, incA, incB + 1, incC, decA + 1, decB, decC);
    if (decB <= 2 && b + incB - decB >= 0)
      ret |= solve(idx + 1, incA + 1, incB, incC, decA, decB + 1, decC);
  } else if (s[idx] == "AC") {
    if (decA <= 2 && a + incA - decA >= 0)
      ret |= solve(idx + 1, incA, incB, incC + 1, decA + 1, decB, decC);
    if (decC <= 2 && c + incC - decC >= 0)
      ret |= solve(idx + 1, incA + 1, incB, incC, decA, decB, decC + 1);
  } else {
    if (decB <= 2 && b + incB - decB >= 0)
      ret |= solve(idx + 1, incA, incB, incC + 1, decA, decB + 1, decC);
    if (decC <= 2 && c + incC - decC >= 0)
      ret |= solve(idx + 1, incA, incB + 1, incC, decA, decB, decC + 1);
  }

  return ret;
}

void build(int idx, int incA, int incB, int incC, int decA, int decB,
           int decC) {

  if (idx == n)
    return;

  if (s[idx] == "AB") {
    if (solve(idx + 1, incA, incB + 1, incC, decA + 1, decB, decC)) {
      cout << "B\\n";
      build(idx + 1, incA, incB + 1, incC, decA + 1, decB, decC);
    } else {
      cout << "A\\n";
      build(idx + 1, incA + 1, incB, incC, decA, decB + 1, decC);
    }
  } else if (s[idx] == "AC") {
    if (solve(idx + 1, incA, incB, incC + 1, decA + 1, decB, decC)) {
      cout << "C\\n";
      build(idx + 1, incA, incB, incC + 1, decA + 1, decB, decC);
    } else {
      cout << "A\\n";
      build(idx + 1, incA + 1, incB, incC, decA, decB, decC + 1);
    }
  } else {
    if (solve(idx + 1, incA, incB, incC + 1, decA, decB + 1, decC)) {
      cout << "C\\n";
      build(idx + 1, incA, incB, incC + 1, decA, decB + 1, decC);
    } else {
      cout << "B\\n";
      build(idx + 1, incA, incB + 1, incC, decA, decB, decC + 1);
    }
  }
}

int main() {

  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);

  cin >> n >> a >> b >> c;

  s.resize(n);
  for (auto &x : s)
    cin >> x;

  memset(dp, 0xff, sizeof dp);

  if (solve(0, 0, 0, 0, 0, 0, 0) == 1) {
    cout << "Yes\\n";
    build(0, 0, 0, 0, 0, 0, 0);
  } else {
    cout << "No\\n";
  }

  return 0;
}