Problem p02692 (C++)

Buggy submission (status: Runtime Error):
/*#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")*/
#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define int long long // comment it out when MLE
// # define endl "\n"
#define gok                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
// # define m 163577857
// # define MOD 163577857
#define pi acos(-1)
#define fi first
#define se second
#define REP(i, n) for (int i = 1; i <= n; i++)

const ll mod = 1000000007;
const int K = 2;
// typedef vector<vector<int> > matrix;
int add(int a, int b, int mod) {
  a %= mod;
  b %= mod;
  a += b;
  if (a >= mod)
    a -= mod;
  if (a < 0) {
    a += mod;
    a %= mod;
  }
  return a;
}

int mul(ll a, int b, int mod) {
  a %= mod;
  b %= mod;
  a *= b;
  return a % mod;
}
int po(int a, int b, int mod) {
  if (b == 0)
    return 1;
  if (b % 2 == 0)
    return po(mul(a, a, mod), b / 2, mod);
  return mul(a, po(mul(a, a, mod), (b - 1) / 2, mod), mod);
}

ll fact[1000003];
ll inv[1000003];

void fact0() {
  int i, j;
  fact[0] = 1;
  for (i = 1; i <= 200000; i++) {
    fact[i] = i * fact[i - 1] % mod;
  }
  inv[0] = 1;
  inv[1] = 1;
  ll p = mod;
  for (i = 2; i <= 200000; i++)
    inv[i] = (p - (p / i) * inv[p % i] % p) % p;

  for (i = 2; i <= 200000; i++) {
    inv[i] *= inv[i - 1];
    inv[i] %= mod;
  }
}
set<int> v[100005];
signed main() {
  gok
      // fact0();
      int n,
      a, b, c;
  cin >> n >> a >> b >> c;
  map<string, int> mp;
  map<char, int> mp1;
  mp1['A'] = a;
  mp1['B'] = b;
  mp1['C'] = c;
  mp["AC"] = 0;
  mp["AB"] = 0;
  mp["BC"] = 0;
  vector<char> vv;
  string s[n];
  for (int i = 0; i < n; i++) {

    cin >> s[i];

    // mp[s]++;
  }
  bool ch = true;
  for (int i = 0; i < n; i++) {
    if (mp1[s[i][0]] > mp1[s[i][1]]) {
      mp1[s[i][0]]--;
      mp1[s[i][1]]++;
      vv.push_back(s[i][1]);
    } else if (mp1[s[i][0]] < mp1[s[i][1]]) {
      mp1[s[i][0]]++;
      mp1[s[i][1]]--;
      vv.push_back(s[i][0]);
    } else if (mp1[s[i][0]] == mp1[s[i][1]]) {
      if (mp1[s[i][0]] == 0) {
        ch = false;
        break;
      }
      if (s[i][0] == s[i + 1][1] || s[i][0] == s[i + 1][0]) {
        vv.push_back(s[i][0]);
        mp1[s[i][0]]++;
        mp1[s[i][1]]--;
      } else {
        vv.push_back(s[i][1]);
        mp1[s[i][0]]--;
        mp1[s[i][1]]++;
      }
    }
  }
  if (!ch)
    cout << "No";
  else {
    cout << "Yes" << endl;
    for (int i = 0; i < vv.size(); i++)
      cout << vv[i] << endl;
  }

  return 0;
}


Fixed submission:
/*#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")*/
#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define int long long // comment it out when MLE
// # define endl "\n"
#define gok                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
// # define m 163577857
// # define MOD 163577857
#define pi acos(-1)
#define fi first
#define se second
#define REP(i, n) for (int i = 1; i <= n; i++)

const ll mod = 1000000007;
const int K = 2;
// typedef vector<vector<int> > matrix;
int add(int a, int b, int mod) {
  a %= mod;
  b %= mod;
  a += b;
  if (a >= mod)
    a -= mod;
  if (a < 0) {
    a += mod;
    a %= mod;
  }
  return a;
}

int mul(ll a, int b, int mod) {
  a %= mod;
  b %= mod;
  a *= b;
  return a % mod;
}
int po(int a, int b, int mod) {
  if (b == 0)
    return 1;
  if (b % 2 == 0)
    return po(mul(a, a, mod), b / 2, mod);
  return mul(a, po(mul(a, a, mod), (b - 1) / 2, mod), mod);
}

ll fact[1000003];
ll inv[1000003];

void fact0() {
  int i, j;
  fact[0] = 1;
  for (i = 1; i <= 200000; i++) {
    fact[i] = i * fact[i - 1] % mod;
  }
  inv[0] = 1;
  inv[1] = 1;
  ll p = mod;
  for (i = 2; i <= 200000; i++)
    inv[i] = (p - (p / i) * inv[p % i] % p) % p;

  for (i = 2; i <= 200000; i++) {
    inv[i] *= inv[i - 1];
    inv[i] %= mod;
  }
}
set<int> v[100005];
signed main() {
  gok
      // fact0();
      int n,
      a, b, c;
  cin >> n >> a >> b >> c;
  map<string, int> mp;
  map<char, int> mp1;
  mp1['A'] = a;
  mp1['B'] = b;
  mp1['C'] = c;
  mp["AC"] = 0;
  mp["AB"] = 0;
  mp["BC"] = 0;
  vector<char> vv;
  string s[n];
  for (int i = 0; i < n; i++) {

    cin >> s[i];

    // mp[s]++;
  }
  bool ch = true;
  for (int i = 0; i < n; i++) {
    if (mp1[s[i][0]] > mp1[s[i][1]]) {
      mp1[s[i][0]]--;
      mp1[s[i][1]]++;
      vv.push_back(s[i][1]);
    } else if (mp1[s[i][0]] < mp1[s[i][1]]) {
      mp1[s[i][0]]++;
      mp1[s[i][1]]--;
      vv.push_back(s[i][0]);
    } else if (mp1[s[i][0]] == mp1[s[i][1]]) {
      if (mp1[s[i][0]] == 0) {
        ch = false;
        break;
      }

      if (i + 1 != n && (s[i][0] == s[i + 1][1] || s[i][0] == s[i + 1][0])) {
        vv.push_back(s[i][0]);
        mp1[s[i][0]]++;
        mp1[s[i][1]]--;
      } else {
        vv.push_back(s[i][1]);
        mp1[s[i][0]]--;
        mp1[s[i][1]]++;
      }
    }
  }
  if (!ch)
    cout << "No";
  else {
    cout << "Yes" << endl;
    for (int i = 0; i < vv.size(); i++)
      cout << vv[i] << endl;
  }

  return 0;
}