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

#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <typeinfo>
#include <utility>
#include <vector>
using namespace std;
using Complex = complex<double>;
using ll = long long;
template <typename T>
using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
const ll inf = 1145141919810364364LL;
double PI = acos(-1.0);
#define all(x) (x).begin(), (x).end()
#define puts(x) cout << x << endl
#define rep(i, m, n) for (ll i = m; i < n; ++i)
#define pb push_back
#define fore(i, a) for (auto &i : a)
#define rrep(i, m, n) for (ll i = m; i >= n; --i)

int N, A, B, C;
vector<string> op;
vector<char> ans;

void err() {
  puts("No");
  exit(0);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  cin >> N >> A >> B >> C;
  op.resize(N);
  for (int i = 0; i < N; i++)
    cin >> op[i];

  for (int i = 0; i < N; i++) {
    if (op[i] == "AB") {
      if (A == 0) {
        if (B == 0)
          err();
        A++;
        B--;
        ans.pb('A');
      } else if (B == 0) {
        A--;
        B++;
        ans.pb('B');
      } else {
        if (i == N - 1) {
          A++;
          B--;
          ans.pb('A');
        } else if (op[i + 1] == "AC") {
          A++;
          B--;
          ans.pb('A');
        } else {
          A--;
          B++;
          ans.pb('B');
        }
      }
    } else if (op[i] == "BC") {
      if (B == 0) {
        if (C == 0)
          err();
        B++;
        C--;
        ans.pb('B');
      } else if (C == 0) {
        B--;
        C++;
        ans.pb('C');
      } else {
        if (i == N - 1) {
          B++;
          C--;
          ans.pb('B');
        } else if (op[i + 1] == "AB") {
          B++;
          C--;
          ans.pb('B');
        } else {
          B--;
          C++;
          ans.pb('C');
        }
      }
    } else if (op[i] == "AC") {
      if (C == 0) {
        if (A == 0)
          err();
        A--;
        C++;
        ans.pb('C');
      } else if (A == 0) {
        A++;
        C--;
        ans.pb('A');
      } else {
        if (i == N - 1) {
          A--;
          C++;
          ans.pb('C');
        } else if (op[i + 1] == "AB") {
          A++;
          C--;
          ans.pb('A');
        } else {
          A--;
          C++;
          ans.pb('C');
        }
      }
    }
  }
  puts("Yes");
  fore(x, ans) puts(x);

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <typeinfo>
#include <utility>
#include <vector>
using namespace std;
using Complex = complex<double>;
using ll = long long;
template <typename T>
using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
const ll inf = 1145141919810364364LL;
double PI = acos(-1.0);
#define all(x) (x).begin(), (x).end()
#define puts(x) cout << x << endl
#define rep(i, m, n) for (ll i = m; i < n; ++i)
#define pb push_back
#define fore(i, a) for (auto &i : a)
#define rrep(i, m, n) for (ll i = m; i >= n; --i)

int N, A, B, C;
vector<string> op;
vector<char> ans;

void err() {
  puts("No");
  exit(1);
}

void f() {}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  cin >> N >> A >> B >> C;
  op.resize(N);
  for (int i = 0; i < N; i++)
    cin >> op[i];

  for (int i = 0; i < N; i++) {
    if (op[i] == "AB") {
      if (A == 0) {
        if (B == 0)
          err();
        A++;
        B--;
        ans.pb('A');
      } else if (B == 0) {
        A--;
        B++;
        ans.pb('B');
      } else {
        if (i == N - 1) {
          A++;
          B--;
          ans.pb('A');
        } else if (op[i + 1] == "AC") {
          A++;
          B--;
          ans.pb('A');
        } else {
          A--;
          B++;
          ans.pb('B');
        }
      }
    } else if (op[i] == "BC") {
      if (B == 0) {
        if (C == 0)
          err();
        B++;
        C--;
        ans.pb('B');
      } else if (C == 0) {
        B--;
        C++;
        ans.pb('C');
      } else {
        if (i == N - 1) {
          B++;
          C--;
          ans.pb('B');
        } else if (op[i + 1] == "AB") {
          B++;
          C--;
          ans.pb('B');
        } else {
          B--;
          C++;
          ans.pb('C');
        }
      }
    } else if (op[i] == "AC") {
      if (C == 0) {
        if (A == 0)
          err();
        A--;
        C++;
        ans.pb('C');
      } else if (A == 0) {
        A++;
        C--;
        ans.pb('A');
      } else {
        if (i == N - 1) {
          A--;
          C++;
          ans.pb('C');
        } else if (op[i + 1] == "AB") {
          A++;
          C--;
          ans.pb('A');
        } else {
          A--;
          C++;
          ans.pb('C');
        }
      }
    }
  }
  puts("Yes");
  fore(x, ans) puts(x);

  return 0;
}