Problem p02692 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef pair<ll, ll> PLL;
#define chmax(x, y)                                                            \
  do {                                                                         \
    x = max(x, y);                                                             \
  } while (0)
#define chmin(x, y)                                                            \
  do {                                                                         \
    x = min(x, y);                                                             \
  } while (0)
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b)                                                          \
  for (ll i = static_cast<ll>(a); i < static_cast<ll>(b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__) // NOLINT
template <typename T>
using reversed_priority_queue =
    std::priority_queue<T, std::vector<T>, std::greater<T>>;

ll N;
vector<ll> a(3);
vector<pair<ll, ll>> v;

signed main() {
  cin >> N;
  rep(i, 3) cin >> a[i];
  rep(i, N) {
    string s;
    cin >> s;
    v.push_back({s[0] - 'A', s[1] - 'A'});
  }

  if (a[0] + a[1] + a[2] == 0) {
    // そもそもダメ
    cout << "No" << endl;
    return 0;
  } else if (a[0] + a[1] + a[2] <= 2) {
    // 何とかすればYesになるかも => DP
    map<vector<ll>, string> mp; // map[{0,1,1}] = "ABC"
    mp[a] = "";
    vector<char> ans;
    rep(i, N) {
      ll x = v[i].first, y = v[i].second;
      map<vector<ll>, string> mp2;

      for (auto t : mp) {
        auto val = t.first;
        auto str = t.second;
        if (val[x] > 0) {
          vector<ll> val2 = val;
          string str2 = str + (string() + (char)(y + 'A'));
          val2[x]--;
          val2[y]++;
          mp2[val2] = str2;
        }
        if (val[y] > 0) {
          vector<ll> val2 = val;
          string str2 = str + (string() + (char)(x + 'A'));
          val2[x]++;
          val2[y]--;
          mp2[val2] = str2;
        }
      }

      if (mp2.size() == 0) {
        cout << "No" << endl;
      }
      swap(mp, mp2);
    }
    cout << "Yes" << endl;
    for (auto c : mp.begin()->second) {
      cout << c << endl;
    }
  } else {
    vector<char> ans;
    rep(i, N) {
      ll x = v[i].first, y = v[i].second;
      // cout << "a, b, c = " << a[0] <<  " " << a[1] << " " << a[2] << endl;
      // cout << "x, y = " << x <<  " " << y << endl;
      if (a[x] == 0 && a[y] == 0) {
        cout << "No" << endl;
        return 0;
      } else if (a[x] > a[y]) {
        a[x]--;
        a[y]++;
        ans.push_back(y + 'A');
      } else {
        a[y]--;
        a[x]++;
        ans.push_back(x + 'A');
      }
    }
    cout << "Yes" << endl;
    for (auto c : ans) {
      cout << c << endl;
    }
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef pair<ll, ll> PLL;
#define chmax(x, y)                                                            \
  do {                                                                         \
    x = max(x, y);                                                             \
  } while (0)
#define chmin(x, y)                                                            \
  do {                                                                         \
    x = min(x, y);                                                             \
  } while (0)
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b)                                                          \
  for (ll i = static_cast<ll>(a); i < static_cast<ll>(b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__) // NOLINT
template <typename T>
using reversed_priority_queue =
    std::priority_queue<T, std::vector<T>, std::greater<T>>;

ll N;
vector<ll> a(3);
vector<pair<ll, ll>> v;

signed main() {
  cin >> N;
  rep(i, 3) cin >> a[i];
  rep(i, N) {
    string s;
    cin >> s;
    v.push_back({s[0] - 'A', s[1] - 'A'});
  }

  if (a[0] + a[1] + a[2] == 0) {
    // そもそもダメ
    cout << "No" << endl;
    return 0;
  } else if (a[0] + a[1] + a[2] <= 2) {
    // 何とかすればYesになるかも => DP
    map<vector<ll>, string> mp; // map[{0,1,1}] = "ABC"
    mp[a] = "";
    vector<char> ans;
    rep(i, N) {
      ll x = v[i].first, y = v[i].second;
      map<vector<ll>, string> mp2;

      for (auto t : mp) {
        auto val = t.first;
        auto str = t.second;
        if (val[x] > 0) {
          vector<ll> val2 = val;
          string str2 = str + (string() + (char)(y + 'A'));
          val2[x]--;
          val2[y]++;
          mp2[val2] = str2;
        }
        if (val[y] > 0) {
          vector<ll> val2 = val;
          string str2 = str + (string() + (char)(x + 'A'));
          val2[x]++;
          val2[y]--;
          mp2[val2] = str2;
        }
      }

      if (mp2.size() == 0) {
        cout << "No" << endl;
        return 0;
      }
      swap(mp, mp2);
    }
    cout << "Yes" << endl;
    for (auto c : mp.begin()->second) {
      cout << c << endl;
    }
  } else {
    vector<char> ans;
    rep(i, N) {
      ll x = v[i].first, y = v[i].second;
      // cout << "a, b, c = " << a[0] <<  " " << a[1] << " " << a[2] << endl;
      // cout << "x, y = " << x <<  " " << y << endl;
      if (a[x] == 0 && a[y] == 0) {
        cout << "No" << endl;
        return 0;
      } else if (a[x] > a[y]) {
        a[x]--;
        a[y]++;
        ans.push_back(y + 'A');
      } else {
        a[y]--;
        a[x]++;
        ans.push_back(x + 'A');
      }
    }
    cout << "Yes" << endl;
    for (auto c : ans) {
      cout << c << endl;
    }
  }
  return 0;
}