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

#include <bits/stdc++.h>
using namespace std;

int main() {
  int N, A, B, C;
  cin >> N >> A >> B >> C;
  vector<char> ans;
  bool ok = true;
  if (A + B + C >= 3) {
    for (int i = 0; i < N; i++) {
      string s;
      cin >> s;
      if (s == "AB") {
        if (A > B) {
          ans.push_back('B');
          B++;
          A--;
        } else {
          ans.push_back('A');
          A++;
          B--;
        }
      } else if (s == "AC") {
        if (A > C) {
          ans.push_back('C');
          C++;
          A--;
        } else {
          ans.push_back('A');
          A++;
          C--;
        }
      } else {
        if (B > C) {
          ans.push_back('C');
          C++;
          B--;
        } else {
          ans.push_back('B');
          B++;
          C--;
        }
      }
      if (A < 0 || B < 0 || C < 0) {
        ok = false;
        break;
      }
    }
    if (ok) {
      cout << "Yes" << endl;
      for (int i = 0; i < N; i++)
        cout << ans[i] << endl;
    } else {
      cout << "No" << endl;
    }
  } else {
    vector<string> s(N);
    for (int i = 0; i < N; i++)
      cin >> s[i];
    for (int i = 0; i < N; i++) {
      if (s[i] == "AB") {
        if (A > B) {
          ans.push_back('B');
          B++;
          A--;
        } else {
          if (A == B && A == 1 && i + 1 < s.size() && s[i + 1] != "AB") {
            if (s[i + 1] == "AC") {
              ans.push_back('A');
              A++;
              B--;
            } else {
              ans.push_back('B');
              B++;
              A--;
            }
          } else {
            ans.push_back('A');
            A++;
            B--;
          }
        }
      } else if (s[i] == "AC") {
        if (A > C) {
          ans.push_back('C');
          C++;
          A--;
        } else {
          if (A == C && A == 1 && i + 1 < s.size() && s[i + 1] != "AC") {
            if (s[i + 1] == "AB") {
              ans.push_back('A');
              A++;
              C--;
            } else {
              ans.push_back('C');
              C++;
              A--;
            }
          } else {
            ans.push_back('A');
            A++;
            C--;
          }
        }
      } else {
        if (B > C) {
          ans.push_back('C');
          C++;
          B--;
        } else {
          if (C == B && C == 1 && i + 1 < s.size() && s[i + 1] != "BC") {
            if (s[i + 1] == "AB") {
              ans.push_back('B');
              B++;
              C--;
            } else {
              ans.push_back('C');
              C++;
              B--;
            }
          } else {
            ans.push_back('B');
            B++;
            C--;
          }
        }
      }
      if (A < 0 || B < 0 || C < 0) {
        ok = false;
        break;
      }
    }
    if (ok) {
      cout << "Yes" << endl;
      for (int i = 0; i < N; i++)
        cout << ans[i] << endl;
    } else {
      cout << "No" << endl;
    }
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int main() {
  int N, A, B, C;
  cin >> N >> A >> B >> C;
  vector<char> ans;
  bool ok = true;
  if (A + B + C >= 3) {
    for (int i = 0; i < N; i++) {
      string s;
      cin >> s;
      if (s == "AB") {
        if (A > B) {
          ans.push_back('B');
          B++;
          A--;
        } else {
          ans.push_back('A');
          A++;
          B--;
        }
      } else if (s == "AC") {
        if (A > C) {
          ans.push_back('C');
          C++;
          A--;
        } else {
          ans.push_back('A');
          A++;
          C--;
        }
      } else {
        if (B > C) {
          ans.push_back('C');
          C++;
          B--;
        } else {
          ans.push_back('B');
          B++;
          C--;
        }
      }
      if (A < 0 || B < 0 || C < 0) {
        ok = false;
        break;
      }
    }
    if (ok) {
      cout << "Yes" << endl;
      for (int i = 0; i < N; i++)
        cout << ans[i] << endl;
    } else {
      cout << "No" << endl;
    }
  } else {
    vector<string> s(N);
    for (int i = 0; i < N; i++)
      cin >> s[i];
    for (int i = 0; i < N; i++) {
      if (s[i] == "AB") {
        if (A > B) {
          ans.push_back('B');
          B++;
          A--;
        } else {
          if (A == B && A == 1 && i + 1 < s.size() && s[i + 1] != "AB") {
            if (s[i + 1] == "AC") {
              ans.push_back('A');
              A++;
              B--;
            } else {
              ans.push_back('B');
              B++;
              A--;
            }
          } else {
            ans.push_back('A');
            A++;
            B--;
          }
        }
      } else if (s[i] == "AC") {
        if (A > C) {
          ans.push_back('C');
          C++;
          A--;
        } else {
          if (A == C && A == 1 && i + 1 < s.size() && s[i + 1] != "AC") {
            if (s[i + 1] == "AB") {
              ans.push_back('A');
              A++;
              C--;
            } else {
              ans.push_back('C');
              C++;
              A--;
            }
          } else {
            ans.push_back('A');
            A++;
            C--;
          }
        }
      } else {
        if (B > C) {
          ans.push_back('C');
          C++;
          B--;
        } else {
          if (C == B && C == 1 && i + 1 < s.size() && s[i + 1] != "BC") {
            if (s[i + 1] == "AB") {
              ans.push_back('B');
              B++;
              C--;
            } else {
              ans.push_back('C');
              C++;
              B--;
            }
          } else {
            ans.push_back('B');
            B++;
            C--;
          }
        }
      }
      if (A < 0 || B < 0 || C < 0) {
        ok = false;
        break;
      }
    }
    if (ok) {
      cout << "Yes" << endl;
      for (int i = 0; i < N; i++)
        cout << ans[i] << endl;
    } else {
      cout << "No" << endl;
      for (int i = 0; i < s.size(); i++)
        cout << ans[i] << endl;
    }
  }
}