Problem p02692 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
typedef long long ll;
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define clr(a, h) memset(a, (h), sizeof(a))
#define mem(a, h) memset(a, (h), sizeof(a))
#define fi first
#define se second
#define por(a, b) (((a % MOD) * (b % MOD)) % MOD)
#define forg(i, b, e, c) for (ll i = (ll)b; i < (ll)e; i += c)
#define forr(i, b, e) for (ll i = b; i < e; i++)

using namespace std;
// using namespace __gnu_pbds;
typedef double lldb;
typedef pair<ll, ll> ii;
typedef pair<ii, ll> iii;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef vector<ll> vll;
// typedef
// tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
// ordered_set;
const ll INF = 1e9;
const double PI = acos(-1);
#define tam 220000
#define offset
const ll MOD = 1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int v[300];
int ans[tam];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n >> v['A'] >> v['B'] >> v['C'];
  string txt, ant;
  int m = v['A'] + v['B'] + v['C'];
  bool valid = 1;
  forr(i, 0, n) {
    cin >> txt;
    if (m <= 1) {
      if (v[txt[0]] + v[txt[1]] == 0)
        valid = 0;
      else {
        if (v[txt[0]] == 1)
          ans[i] = txt[1], v[txt[0]]--, v[txt[1]]++;
        else
          ans[i] = txt[0], v[txt[0]]++, v[txt[1]]--;
      }
    } else {
      if (v[txt[0]] + v[txt[1]] == 0) {
        if (i - 1 < 0)
          valid = 0;
        else {
          if (ans[i - 1] == ant[0]) {
            ans[i - 1] = ant[1];
            v[ant[0]] -= 2, v[ant[1]] += 2;
          } else {
            ans[i - 1] = ant[0];
            v[ant[1]] -= 2, v[ant[0]] += 2;
          }
          assert(v[ant[0]] >= 0);
          assert(v[ant[1]] >= 0);
        }
      }
      if (v[txt[0]] > 0) {
        v[txt[0]]--, v[txt[1]]++;
        ans[i] = txt[1];
      } else {
        v[txt[1]]--, v[txt[0]]++;
        ans[i] = txt[0];
      }
    }
    ant = txt;
  }
  if (valid) {
    cout << "Yes\n";
    forr(i, 0, n) { cout << (char)ans[i] << endl; }
  } else
    cout << "No\n";
}
// PLUS ULTRA!


Fixed submission:
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
typedef long long ll;
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define clr(a, h) memset(a, (h), sizeof(a))
#define mem(a, h) memset(a, (h), sizeof(a))
#define fi first
#define se second
#define por(a, b) (((a % MOD) * (b % MOD)) % MOD)
#define forg(i, b, e, c) for (ll i = (ll)b; i < (ll)e; i += c)
#define forr(i, b, e) for (ll i = b; i < e; i++)

using namespace std;
// using namespace __gnu_pbds;
typedef double lldb;
typedef pair<ll, ll> ii;
typedef pair<ii, ll> iii;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef vector<ll> vll;
// typedef
// tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
// ordered_set;
const ll INF = 1e9;
const double PI = acos(-1);
#define tam 220000
#define offset
const ll MOD = 1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int v[300];
int ans[tam];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n >> v['A'] >> v['B'] >> v['C'];
  string txt, ant;
  int m = v['A'] + v['B'] + v['C'];
  bool valid = 1;
  forr(i, 0, n) {
    cin >> txt;
    if (m <= 1) {
      if (v[txt[0]] + v[txt[1]] == 0)
        valid = 0;
      else {
        if (v[txt[0]] == 1)
          ans[i] = txt[1], v[txt[0]]--, v[txt[1]]++;
        else
          ans[i] = txt[0], v[txt[0]]++, v[txt[1]]--;
      }
    } else {
      if (v[txt[0]] + v[txt[1]] == 0) {
        if (i - 1 < 0)
          valid = 0;
        else {
          if (ans[i - 1] == ant[0]) {
            ans[i - 1] = ant[1];
            v[ant[0]] -= 2, v[ant[1]] += 2;
          } else {
            ans[i - 1] = ant[0];
            v[ant[1]] -= 2, v[ant[0]] += 2;
          }
          if (v[ant[0]] < 0 || v[ant[1]] < 0)
            valid = 0;
          // assert(v[ant[0]]>=0);
          // assert(v[ant[1]]>=0);
        }
      }
      if (v[txt[0]] > 0) {
        v[txt[0]]--, v[txt[1]]++;
        ans[i] = txt[1];
      } else {
        v[txt[1]]--, v[txt[0]]++;
        ans[i] = txt[0];
      }
    }
    ant = txt;
  }
  if (valid) {
    cout << "Yes\n";
    forr(i, 0, n) { cout << (char)ans[i] << endl; }
  } else
    cout << "No\n";
}
// PLUS ULTRA!