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

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

using namespace std;
typedef long long ll;
int N;
int A, B, C;
string s[100005];
char ans[100000];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << setprecision(10) << fixed;
  cin >> N;
  cin >> A >> B >> C;
  for (int i = 0; i < N; i++) {
    cin >> s[i];
  }
  s[N] = "AA";
  for (int i = 0; i < N; i++) {
    // cout << A << ' ' << B << ' ' << C << endl;
    if (s[i] == "AB") {
      if (A == 0 && B == 0) {
        cout << "No" << endl;
        return 0;
      } else {
        if (A == 0) {
          ans[i] = 'A';
          A++;
          B--;
        } else if (B == 0) {
          ans[i] = 'B';
          A--;
          B++;
        } else if (A == 1 && B == 1) {
          if (s[i + 1] == "AC") {
            ans[i] = 'A';
            A++;
            B--;
          } else {
            ans[i] = 'B';
            A--;
            B++;
          }
        } else if (B > A) {
          ans[i] = 'A';
          A++;
          B--;
        } else {
          ans[i] = 'B';
          A--;
          B++;
        }
      }
    }
    if (s[i] == "AC") {
      if (A == 0 && C == 0) {
        cout << "No" << endl;
        return 0;
      } else {
        if (A == 0) {
          ans[i] = 'A';
          A++;
          C--;
        } else if (C == 0) {
          ans[i] = 'C';
          A--;
          C++;
        } else if (A == 1 && C == 1) {
          if (s[i + 1] == "AB") {
            ans[i] = 'A';
            A++;
            C--;
          } else {
            ans[i] = 'C';
            A--;
            C++;
          }
        } else if (C > A) {
          ans[i] = 'A';
          A++;
          C--;
        } else {
          ans[i] = 'C';
          A--;
          C++;
        }
      }
    }
    if (s[i] == "BC") {
      if (B == 0 && C == 0) {
        cout << "No" << endl;
        return 0;
      } else {
        if (B == 0) {
          ans[i] = 'B';
          B++;
          C--;
        } else if (C == 0) {
          ans[i] = 'C';
          B--;
          C++;
        } else if (B == 1 && C == 1) {
          if (s[i + 1] == "AB") {
            ans[i] = 'B';
            B++;
            C--;
          } else {
            ans[i] = 'C';
            B--;
            C++;
          }
        } else if (C > B) {
          ans[i] = 'B';
          B++;
          C--;
        } else {
          ans[i] = 'C';
          B--;
          C++;
        }
      }
    }
  }
  cout << "Yes" << endl;
  for (int i = 0; i < N; i++) {
    cout << ans[i] << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

using namespace std;
typedef long long ll;
int N;
int A, B, C;
string s[10005];
char ans[10000];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << setprecision(10) << fixed;
  cin >> N;
  cin >> A >> B >> C;
  for (int i = 0; i < N; i++) {
    cin >> s[i];
  }
  s[N] = "AA";
  for (int i = 0; i < N; i++) {
    // cout << A << ' ' << B << ' ' << C << endl;
    if (s[i] == "AB") {
      if (A == 0 && B == 0) {
        cout << "No" << endl;
        return 0;
      } else {
        if (A == 0) {
          ans[i] = 'A';
          A++;
          B--;
        } else if (B == 0) {
          ans[i] = 'B';
          A--;
          B++;
        } else if (A == 1 && B == 1) {
          if (s[i + 1] == "AC") {
            ans[i] = 'A';
            A++;
            B--;
          } else {
            ans[i] = 'B';
            A--;
            B++;
          }
        } else if (B > A) {
          ans[i] = 'A';
          A++;
          B--;
        } else {
          ans[i] = 'B';
          A--;
          B++;
        }
      }
    }
    if (s[i] == "AC") {
      if (A == 0 && C == 0) {
        cout << "No" << endl;
        return 0;
      } else {
        if (A == 0) {
          ans[i] = 'A';
          A++;
          C--;
        } else if (C == 0) {
          ans[i] = 'C';
          A--;
          C++;
        } else if (A == 1 && C == 1) {
          if (s[i + 1] == "AC") {
            ans[i] = 'A';
            A++;
            C--;
          } else {
            ans[i] = 'C';
            A--;
            C++;
          }
        } else if (C > A) {
          ans[i] = 'A';
          A++;
          C--;
        } else {
          ans[i] = 'C';
          A--;
          C++;
        }
      }
    }
    if (s[i] == "BC") {
      if (B == 0 && C == 0) {
        cout << "No" << endl;
        return 0;
      } else {
        if (B == 0) {
          ans[i] = 'B';
          B++;
          C--;
        } else if (C == 0) {
          ans[i] = 'C';
          B--;
          C++;
        } else if (B == 1 && C == 1) {
          if (s[i + 1] == "BC") {
            ans[i] = 'B';
            B++;
            C--;
          } else {
            ans[i] = 'C';
            B--;
            C++;
          }
        } else if (C > B) {
          ans[i] = 'B';
          B++;
          C--;
        } else {
          ans[i] = 'C';
          B--;
          C++;
        }
      }
    }
  }
  cout << "Yes" << endl;
  for (int i = 0; i < N; i++) {
    cout << ans[i] << endl;
  }
}