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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef pair<ll, ll> pi;
typedef vector<pi> vpi;
typedef long double ld;
#define pb emplace_back
#define emp emplace
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define ALL(x) x.begin(), x.end()
#define SZ(x) (ll) x.size()
#define f first
#define s second
const ll MAXN = 200100;
const ll INF = 1e9;
const ll MOD = 1e9 + 7;

ll N, Q, a, b, c, d, e, M, X;
ll A, B, C;
map<ll, ll> p;
ll ans;
string S;
vector<char> out;
vector<string> V;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> Q >> A >> B >> C;
  if (A + B + C != 2) {
    while (Q--) {
      cin >> S;
      if (S == "AB") {
        if (A == 0 && B == 0) {
          cout << "No";
          return 0;
        }
        if (A > B) {
          out.pb('B');
          --A;
          ++B;
        } else {
          out.pb('A');
          --B;
          ++A;
        }
      } else if (S == "AC") {
        if (A == 0 && C == 0) {
          cout << "No";
          return 0;
        }
        if (A > C) {
          out.pb('C');
          --A;
          ++C;
        } else {
          out.pb('A');
          --C;
          ++A;
        }
      } else {
        if (C == 0 && B == 0) {
          cout << "No";
          return 0;
        }
        if (C > B) {
          out.pb('B');
          --C;
          ++B;
        } else {
          out.pb('C');
          --B;
          ++C;
        }
      }
    }
    cout << "Yes\\n";
    for (auto i : out)
      cout << i << '\\n';
    return 0;
  }
  for (int i = 0; i < Q; ++i) {
    cin >> S;
    V.pb(S);
  }
  for (int cur = 0; cur < Q; ++cur) {
    S = V[cur];
    // fail con
    if (S == "AB" && A + B == 0) {
      cout << "No";
      return 0;
    }
    if (S == "AC" && A + C == 0) {
      cout << "No";
      return 0;
    }
    if (S == "BC" && B + C == 0) {
      cout << "No";
      return 0;
    }

    if (S == "AB" && A == 0) {
      out.pb('A');
      --B;
      ++A;
      continue;
    }
    if (S == "AB" && B == 0) {
      out.pb('B');
      --A;
      ++B;
      continue;
    }
    if (S == "AC" && A == 0) {
      out.pb('A');
      --C;
      ++A;
      continue;
    }
    if (S == "AC" && C == 0) {
      out.pb('C');
      --A;
      ++C;
      continue;
    }
    if (S == "BC" && B == 0) {
      out.pb('B');
      --C;
      ++B;
      continue;
    }
    if (S == "BC" && C == 0) {
      out.pb('C');
      --B;
      ++C;
      continue;
    }
    if (cur == Q - 1) {
      out.pb(S[0]);
      continue;
    }
    string nxt = V[cur + 1];
    if (S == nxt) {
      out.pb(S[0]);
      if (S == "AB") {
        A = 2;
        B = 0;
      } else if (S == "AC") {
        A = 2;
        C = 0;
      } else {
        B = 2;
        C = 0;
      }
    }
    if (S == "AB" && nxt == "AC") {
      out.pb('A');
      A = 2;
      B = 0;
      continue;
    }
    if (S == "AB" && nxt == "BC") {
      out.pb('B');
      B = 2;
      A = 0;
      continue;
    }
    if (S == "AC" && nxt == "AB") {
      out.pb('A');
      A = 2;
      C = 0;
      continue;
    }
    if (S == "AC" && nxt == "BC") {
      out.pb('C');
      C = 2;
      A = 0;
      continue;
    }
    if (S == "BC" && nxt == "AB") {
      out.pb('B');
      B = 2;
      C = 0;
      continue;
    }
    if (S == "BC" && nxt == "AC") {
      out.pb('C');
      C = 2;
      B = 0;
      continue;
    }
  }
  cout << "Yes\\n";
  for (auto i : out)
    cout << i << '\\n';
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef pair<ll, ll> pi;
typedef vector<pi> vpi;
typedef long double ld;
#define pb emplace_back
#define emp emplace
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define ALL(x) x.begin(), x.end()
#define SZ(x) (ll) x.size()
#define f first
#define s second
const ll MAXN = 200100;
const ll INF = 1e9;
const ll MOD = 1e9 + 7;

ll N, Q, a, b, c, d, e, M, X;
ll A, B, C;
map<ll, ll> p;
ll ans;
string S;
vector<char> out;
vector<string> V;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> Q >> A >> B >> C;
  if (A + B + C != 2) {
    while (Q--) {
      cin >> S;
      if (S == "AB") {
        if (A == 0 && B == 0) {
          cout << "No";
          return 0;
        }
        if (A > B) {
          out.pb('B');
          --A;
          ++B;
        } else {
          out.pb('A');
          --B;
          ++A;
        }
      } else if (S == "AC") {
        if (A == 0 && C == 0) {
          cout << "No";
          return 0;
        }
        if (A > C) {
          out.pb('C');
          --A;
          ++C;
        } else {
          out.pb('A');
          --C;
          ++A;
        }
      } else {
        if (C == 0 && B == 0) {
          cout << "No";
          return 0;
        }
        if (C > B) {
          out.pb('B');
          --C;
          ++B;
        } else {
          out.pb('C');
          --B;
          ++C;
        }
      }
    }
    cout << "Yes\\n";
    for (auto i : out)
      cout << i << '\\n';
    return 0;
  }
  assert(0);
  for (int i = 0; i < Q; ++i) {
    cin >> S;
    V.pb(S);
  }
  for (int cur = 0; cur < Q; ++cur) {
    S = V[cur];
    // fail con
    if (S == "AB" && A + B == 0) {
      cout << "No";
      return 0;
    }
    if (S == "AC" && A + C == 0) {
      cout << "No";
      return 0;
    }
    if (S == "BC" && B + C == 0) {
      cout << "No";
      return 0;
    }
    if (S == "AB" && A == 2) {
      out.pb('B');
      A = B = 1;
      continue;
    }
    if (S == "AB" && B == 2) {
      out.pb('B');
      A = B = 1;
      continue;
    }
    if (S == "AC" && A == 2) {
      out.pb('C');
      A = C = 1;
      continue;
    }
    if (S == "AC" && C == 2) {
      out.pb('A');
      A = C = 1;
      continue;
    }
    if (S == "BC" && B == 2) {
      out.pb('C');
      B = C = 1;
      continue;
    }
    if (S == "BC" && C == 2) {
      out.pb('B');
      B = C = 1;
      continue;
    }
    if (cur == Q - 1) {
      out.pb(S[0]);
      continue;
    }
    string nxt = V[cur + 1];
    if (S == nxt) {
      out.pb(S[0]);
      if (S == "AB") {
        A = 2;
        B = 0;
      } else if (S == "AC") {
        A = 2;
        C = 0;
      } else {
        B = 2;
        C = 0;
      }
    }
    if (S == "AB" && nxt == "AC") {
      out.pb('A');
      A = 2;
      continue;
    }
    if (S == "AB" && nxt == "BC") {
      out.pb('B');
      B = 2;
      continue;
    }
    if (S == "AC" && nxt == "AB") {
      out.pb('A');
      A = 2;
      continue;
    }
    if (S == "AC" && nxt == "BC") {
      out.pb('C');
      C = 2;
      continue;
    }
    if (S == "BC" && nxt == "AB") {
      out.pb('B');
      B = 2;
      continue;
    }
    if (S == "BC" && nxt == "AC") {
      out.pb('C');
      C = 2;
      continue;
    }
  }
  cout << "Yes\\n";
  for (auto i : out)
    cout << i << '\\n';
  return 0;
}