Problem p02692 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define REP(i, b, e) for (int i = (b); i < (e); ++i)
#define RREP(i, b, e) for (int i = (b)-1; i >= e; --i)
#define rep(i, e) for (int i = 0; i < (e); ++i)

struct Initializer {
  Initializer() {
    cin.tie(0);
    ios::sync_with_stdio(0);
  }
} initializer;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (auto it = vec.begin(); it != vec.end(); ++it) {
    if (it != vec.begin())
      os << ", ";
    os << *it;
  }
  return os << "]";
}

inline void print(void) { cout << '\n'; }
template <class T> inline void print(const T &x) { cout << x << '\n'; }
template <class T, class... U> inline void print(const T &x, const U &...y) {
  cout << x << " ";
  print(y...);
}

long long n, a, b, c;
vector<string> s = {}, ans = {};
bool good = false;

void rec(int idx, vector<string> &v, long long a, long long b, long long c) {
  if (good)
    return;
  if (idx == n) {
    if (a >= 0 and b >= 0 and c >= 0) {
      copy(v.begin(), v.end(), ans.begin());
      good = true;
    } else {
      return;
    }
  }
  if (a < 0 or b < 0 or c < 0)
    return;
  if (s[idx] == "AB") {
    v.emplace_back("A");
    rec(idx + 1, v, a + 1, b - 1, c);
    if (good)
      return;
    v.pop_back();
    v.emplace_back("B");
    rec(idx + 1, v, a - 1, b + 1, c);
    if (good)
      return;
    v.pop_back();
  } else if (s[idx] == "AC") {
    v.emplace_back("A");
    rec(idx + 1, v, a + 1, b, c - 1);
    if (good)
      return;
    v.pop_back();
    v.emplace_back("C");
    rec(idx + 1, v, a - 1, b, c + 1);
    if (good)
      return;
    v.pop_back();
  } else {
    v.emplace_back("B");
    rec(idx + 1, v, a, b + 1, c - 1);
    if (good)
      return;
    v.pop_back();
    v.emplace_back("C");
    rec(idx + 1, v, a, b - 1, c + 1);
    if (good)
      return;
    v.pop_back();
  }
}

int main() {
  cin >> n >> a >> b >> c;
  rep(i, n) {
    string st;
    cin >> st;
    s.emplace_back(st);
  }
  ans.resize(n);
  vector<string> v = {};
  rec(0, v, a, b, c);

  if (good) {
    print("Yes");
    for (auto x : ans)
      print(x);
  } else {
    print("No");
  }

  return 0;
}

Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
#define REP(i, b, e) for (int i = (b); i < (e); ++i)
#define RREP(i, b, e) for (int i = (b)-1; i >= e; --i)
#define rep(i, e) for (int i = 0; i < (e); ++i)

struct Initializer {
  Initializer() {
    cin.tie(0);
    ios::sync_with_stdio(0);
  }
} initializer;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (auto it = vec.begin(); it != vec.end(); ++it) {
    if (it != vec.begin())
      os << ", ";
    os << *it;
  }
  return os << "]";
}

inline void print(void) { cout << '\n'; }
template <class T> inline void print(const T &x) { cout << x << '\n'; }
template <class T, class... U> inline void print(const T &x, const U &...y) {
  cout << x << " ";
  print(y...);
}

long long n, a, b, c;
vector<string> s = {}, ans = {};
bool good = false;

void rec(int idx, vector<string> &v, long long a, long long b, long long c) {
  if (good)
    return;
  if (idx == n) {
    if (a >= 0 and b >= 0 and c >= 0) {
      copy(v.begin(), v.end(), ans.begin());
      good = true;
    } else {
      return;
    }
  }
  if (a < 0 or b < 0 or c < 0)
    return;
  if (s[idx] == "AB") {
    v.emplace_back("A");
    rec(idx + 1, v, a + 1, b - 1, c);
    if (good)
      return;
    v.pop_back();
    v.emplace_back("B");
    rec(idx + 1, v, a - 1, b + 1, c);
    if (good)
      return;
    v.pop_back();
  } else if (s[idx] == "AC") {
    v.emplace_back("A");
    rec(idx + 1, v, a + 1, b, c - 1);
    if (good)
      return;
    v.pop_back();
    v.emplace_back("C");
    rec(idx + 1, v, a - 1, b, c + 1);
    if (good)
      return;
    v.pop_back();
  } else {
    v.emplace_back("B");
    rec(idx + 1, v, a, b + 1, c - 1);
    if (good)
      return;
    v.pop_back();
    v.emplace_back("C");
    rec(idx + 1, v, a, b - 1, c + 1);
    if (good)
      return;
    v.pop_back();
  }
}

int main() {
  cin >> n >> a >> b >> c;
  rep(i, n) {
    string st;
    cin >> st;
    s.emplace_back(st);
  }
  ans.resize(n);
  vector<string> v = {};
  rec(0, v, a, b, c);

  if (good) {
    print("Yes");
    for (auto x : ans)
      print(x);
  } else {
    print("No");
  }

  return 0;
}