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

#pragma GCC optimize("Ofast")
#define _USE_MATH_DEFINES
#include "bits/stdc++.h"

using namespace std;

using u8 = uint8_t;
using u16 = uint16_t;
using u32 = uint32_t;
using u64 = uint64_t;
using i8 = int8_t;
using i32 = int32_t;
using i64 = int64_t;
constexpr char newl = '\\n';

template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, b) FOR(i, 0, b)
#define RFO(i, a, b) for (int i = ((b)-1); i >= (a); i--)
#define RF0(i, b) RFO(i, 0, b)
// #define F first
// #define S second
#define debug(x) cout << #x << ": " << x << '\\n';

#if 1

enum class Match : u8 {
  AB,
  AC,
  BC,
};

string calc(int A, int B, int C, const vector<Match> &ms, int start = 0) {
  string result;

  FOR(i, start, ms.size()) {
    switch (ms[i]) {
    case Match::AB:
      if (A > B || A > 1) {
        A--;
        B++;
        result += 'B';
      } else if (A < B) {
        A++;
        B--;
        result += 'A';
      } else if (A == 0) {
        return "";
      } else {
        if (i == ms.size() - 1) {
          result += 'A';
        } else {
          auto r = calc(A + 1, B - 1, C, ms, i + 1);
          if (r.size() > 0) {
            return result + 'A' + r;
          }

          r = calc(A - 1, B + 1, C, ms, i + 1);
          if (r.size() > 0) {
            return result + 'B' + r;
          }

          return "";
        }
      }
      break;
    case Match::AC:
      if (A > C || A > 1) {
        A--;
        C++;
        result += 'C';
      } else if (A < C) {
        A++;
        C--;
        result += 'A';
      } else if (A == 0) {
        return "";
      } else {
        if (i == ms.size() - 1) {
          result += 'A';
        } else {
          auto r = calc(A + 1, B, C - 1, ms, i + 1);
          if (r.size() > 0) {
            return result + 'A' + r;
          }

          r = calc(A - 1, B, C + 1, ms, i + 1);
          if (r.size() > 0) {
            return result + 'C' + r;
          }

          return "";
        }
      }
      break;
    case Match::BC:
      if (B > C || B > 1) {
        B--;
        C++;
        result += 'C';
      } else if (B < C) {
        B++;
        C--;
        result += 'B';
      } else if (B == 0) {
        return "";
      } else {
        if (i == ms.size() - 1) {
          result += 'B';
        } else {
          auto r = calc(A, B + 1, C - 1, ms, i + 1);
          if (r.size() > 0) {
            return result + 'B' + r;
          }

          r = calc(A, B - 1, C + 1, ms, i + 1);
          if (r.size() > 0) {
            return result + 'C' + r;
          }

          return "";
        }
      }
      break;
    }
  }

  return result;
}

// INSERT ABOVE HERE
signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int N, A, B, C;
  cin >> N >> A >> B >> C;

  vector<Match> ms;
  ms.reserve(N);
  F0R(i, N) {
    string s;
    cin >> s;
    ms.push_back(s == "AB" ? Match::AB : s == "AC" ? Match::AC : Match::BC);
  }

  string result = calc(A, B, C, ms);
  if (result == "") {
    cout << "No" << newl;
  } else {
    cout << "Yes" << newl;
    for (auto c : result) {
      cout << c << newl;
    }
  }
}
#endif


Error: MLE

Fixed submission:

#pragma GCC optimize("Ofast")
#define _USE_MATH_DEFINES
#include "bits/stdc++.h"

using namespace std;

using u8 = uint8_t;
using u16 = uint16_t;
using u32 = uint32_t;
using u64 = uint64_t;
using i8 = int8_t;
using i32 = int32_t;
using i64 = int64_t;
constexpr char newl = '\\n';

template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, b) FOR(i, 0, b)
#define RFO(i, a, b) for (int i = ((b)-1); i >= (a); i--)
#define RF0(i, b) RFO(i, 0, b)
// #define F first
// #define S second
#define debug(x) cout << #x << ": " << x << '\\n';

#if 1

enum class Match : u8 {
  AB,
  AC,
  BC,
};

string calc(int A, int B, int C, const vector<Match> &ms, int start = 0) {
  string result;
  result.reserve(ms.size());

  FOR(i, start, ms.size()) {
    switch (ms[i]) {
    case Match::AB:
      if (A > B || A > 1) {
        A--;
        B++;
        result += 'B';
      } else if (A < B) {
        A++;
        B--;
        result += 'A';
      } else if (A == 0) {
        return "";
      } else {
        if (i == ms.size() - 1) {
          result += 'A';
        } else {
          auto r = calc(A + 1, B - 1, C, ms, i + 1);
          if (r.size() > 0) {
            return result + 'A' + r;
          }

          r = calc(A - 1, B + 1, C, ms, i + 1);
          if (r.size() > 0) {
            return result + 'B' + r;
          }

          return "";
        }
      }
      break;
    case Match::AC:
      if (A > C || A > 1) {
        A--;
        C++;
        result += 'C';
      } else if (A < C) {
        A++;
        C--;
        result += 'A';
      } else if (A == 0) {
        return "";
      } else {
        if (i == ms.size() - 1) {
          result += 'A';
        } else {
          auto r = calc(A + 1, B, C - 1, ms, i + 1);
          if (r.size() > 0) {
            return result + 'A' + r;
          }

          r = calc(A - 1, B, C + 1, ms, i + 1);
          if (r.size() > 0) {
            return result + 'C' + r;
          }

          return "";
        }
      }
      break;
    case Match::BC:
      if (B > C || B > 1) {
        B--;
        C++;
        result += 'C';
      } else if (B < C) {
        B++;
        C--;
        result += 'B';
      } else if (B == 0) {
        return "";
      } else {
        if (i == ms.size() - 1) {
          result += 'B';
        } else {
          auto r = calc(A, B + 1, C - 1, ms, i + 1);
          if (r.size() > 0) {
            return result + 'B' + r;
          }

          r = calc(A, B - 1, C + 1, ms, i + 1);
          if (r.size() > 0) {
            return result + 'C' + r;
          }

          return "";
        }
      }
      break;
    }
  }

  return result;
}

// INSERT ABOVE HERE
signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int N, A, B, C;
  cin >> N >> A >> B >> C;

  vector<Match> ms;
  ms.reserve(N);
  F0R(i, N) {
    string s;
    cin >> s;
    ms.push_back(s == "AB" ? Match::AB : s == "AC" ? Match::AC : Match::BC);
  }

  string result = calc(A, B, C, ms);
  if (result == "") {
    cout << "No" << newl;
  } else {
    cout << "Yes" << newl;
    for (auto c : result) {
      cout << c << newl;
    }
  }
}
#endif