Problem p02692 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define ll int64_t
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <math.h>
#define FOR(I, A, B) for (ll I = (A); I <= (B); I++)
#define IOS                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define fo(i, n) for (ll i = 0; i < n; i++)
#define sz(a) ll((a).size())
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define tr(c, i) for (auto i = (c).begin(); i != (c).end(); i++)
#define present(c, x) ((c).find(x) != (c).end())
#define cpresent(c, x) (find(all(c), x) != (c).end())
#define dbg(x) cout << #x << " = " << x << endl
#define dbg2(x, y) cout << #x << " = " << x << ", " << #y << " = " << y << endl
#define dbg3(x, y, z)                                                          \\
  cout << #x << " = " << x << ", " << #y << " = " << y << ", " << #z << " = "  \\
       << z << endl
#define dbg4(x, y, z, q)                                                       \\
  cout << #x << " = " << x << ", " << #y << " = " << y << ", " << #z << " = "  \\
       << z << ", " << #q << " = " << q << endl
#define scan(char_array) scanf("%[^\\n]s", &char_array);

int main() {

  IOS
      // freopen("output.txt", "w", stdout);

      // freopen("input.txt", "r", stdin);
      // for writing output to output.txt
      // this can be opted out if you want to print the output to the sublime
      // console
          ll n;
  cin >> n;
  std::vector<ll> v(3);
  fo(i, 3) cin >> v[i];

  std::vector<string> s(n);
  string tmp;
  fo(i, n) { cin >> s[i]; }
  ll t1, t2;
  ll aa, bb;
  // dbg3(v[0],v[1],v[2]);

  std::vector<ll> ans;
  fo(i, n) {
    tmp = s[i];
    aa = (ll)(s[i][0] - 'A');
    bb = (ll)(s[i][1] - 'A');
    // dbg2(aa,bb);
    // dbg2(v[aa],v[bb]);
    if (v[aa] <= 0 && v[bb] <= 0) {
      cout << "No" << endl;
      return 0;
    } else if (v[aa] == 1 && v[bb] == 1 && v[3 - (aa + bb)] == 0) {
      if (i == n - 1) {
        ans.pb(aa);
        break;
      }
      t1 = s[i + 1][0] - 'A';
      t2 = s[i + 1][1] - 'A';
      if (aa == t1 || aa == t2) {
        v[bb]--;
        v[aa]++;
        ans.pb(aa);

      } else {
        v[aa]--;
        v[bb]++;
        ans.pb(bb);
      }

    } else if (v[aa] >= v[bb]) {
      v[aa]--;
      v[bb]++;
      ans.pb(bb);

    } else {
      v[aa]++;
      v[bb]--;
      ans.pb(aa);
    }
    // dbg2(v[aa],v[bb]);
  }
  cout << "Yes" << endl;
  fo(i, n) { cout << char(ans[i] + 'A') << endl; }
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll int64_t
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <math.h>
#define FOR(I, A, B) for (ll I = (A); I <= (B); I++)
#define IOS                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define fo(i, n) for (ll i = 0; i < n; i++)
#define sz(a) ll((a).size())
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define tr(c, i) for (auto i = (c).begin(); i != (c).end(); i++)
#define present(c, x) ((c).find(x) != (c).end())
#define cpresent(c, x) (find(all(c), x) != (c).end())
#define dbg(x) cout << #x << " = " << x << endl
#define dbg2(x, y) cout << #x << " = " << x << ", " << #y << " = " << y << endl
#define dbg3(x, y, z)                                                          \\
  cout << #x << " = " << x << ", " << #y << " = " << y << ", " << #z << " = "  \\
       << z << endl
#define dbg4(x, y, z, q)                                                       \\
  cout << #x << " = " << x << ", " << #y << " = " << y << ", " << #z << " = "  \\
       << z << ", " << #q << " = " << q << endl
#define scan(char_array) scanf("%[^\\n]s", &char_array);

int main() {

  IOS
      // freopen("output.txt", "w", stdout);

      freopen("input2.txt", "r", stdin);
  // for writing output to output.txt
  // this can be opted out if you want to print the output to the sublime
  // console
  ll n;
  cin >> n;
  std::vector<ll> v(3);
  fo(i, 3) cin >> v[i];

  std::vector<string> s(n);
  string tmp;
  fo(i, n) { cin >> s[i]; }
  ll t1, t2;
  ll aa, bb;
  // dbg3(v[0],v[1],v[2]);

  std::vector<ll> ans;
  fo(i, n) {
    tmp = s[i];
    aa = (ll)(s[i][0] - 'A');
    bb = (ll)(s[i][1] - 'A');
    // dbg2(aa,bb);
    // dbg2(v[aa],v[bb]);
    if (v[aa] <= 0 && v[bb] <= 0) {
      cout << "No" << endl;
      return 0;
    } else if (v[aa] == 1 && v[bb] == 1 && v[3 - (aa + bb)] == 0) {
      if (i == n - 1) {
        ans.pb(aa);
        break;
      }
      t1 = s[i + 1][0] - 'A';
      t2 = s[i + 1][0] - 'A';
      if (aa == t1 || aa == t2) {
        v[bb]--;
        v[aa]++;
        ans.pb(aa);
      }

    } else if (v[aa] >= v[bb]) {
      v[aa]--;
      v[bb]++;
      ans.pb(bb);

    } else {
      v[aa]++;
      v[bb]--;
      ans.pb(aa);
    }
    // dbg2(v[aa],v[bb]);
  }
  cout << "YES" << endl;
  fo(i, n) { cout << char(ans[i] + 'A') << endl; }
}