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

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

int main() {
  int N;
  cin >> N;
  int A, B, C;
  cin >> A >> B >> C;

  if (A + B + C == 2) {
    // 先読みが必要
    vector<string> s(N);
    for (int i = 0; i < N; i++) {
      cin >> s.at(i);
    }

    string result;

    bool ok = true;
    for (int i = 0; i < N; i++) {

      if (s.at(i) == "AB") {

        if (A == 1 && B == 1 && i != N - 1) {
          if (s.at(i + 1) == "AC") {
            A++;
            B--;
            result.push_back('A');
          } else {
            A--;
            B++;
            result.push_back('B');
          }
        }

        else if (A == 0 && B == 0) {
          cout << "No" << endl;
          ok = false;
          break;
        }

        else if (A >= B) {
          A--;
          B++;
          result.push_back('B');
        } else {
          A++;
          B--;
          result.push_back('A');
        }

      } else if (s.at(i) == "AC") {

        if (A == 1 && C == 1 && i != N - 1) {
          if (s.at(i + 1) == "AB") {
            A++;
            C--;
            result.push_back('A');
          } else {
            A--;
            C++;
            result.push_back('C');
          }
        }

        else if (A == 0 && C == 0) {
          cout << "No" << endl;
          ok = false;
          break;
        }

        else if (A >= C) {
          A--;
          C++;
          result.push_back('C');
        } else {
          A++;
          C--;
          result.push_back('A');
        }
      } else {

        if (B == 1 && C == 1 && i != N - 1) {
          if (s.at(i + 1) == "AB") {
            B++;
            C--;
            result.push_back('B');
          } else {
            B--;
            C++;
            result.push_back('C');
          }
        }

        else if (B == 0 && C == 0) {
          cout << "No" << endl;
          ok = false;
          break;
        }

        else if (B >= C) {
          B--;
          C++;
          result.push_back('C');
        } else {
          B++;
          C--;
          result.push_back('B');
        }
      }
    } // for

    if (ok) {

      cout << "Yes" << endl;

      for (int i = 0; i < N; i++) {
        cout << result.at(i) << endl;
      }
    }

  } else {

    string s;
    string result;

    bool ok = true;
    for (int i = 0; i < N; i++) {
      cin >> s;

      if (s == "AB") {
        if (A == 0 && B == 0) {
          cout << "No" << endl;
          ok = false;
          break;
        }

        if (A >= B) {
          A--;
          B++;
          result.push_back('B');
        } else {
          A++;
          B--;
          result.push_back('A');
        }

      } else if (s == "AC") {

        if (A == 0 && C == 0) {
          cout << "No" << endl;
          ok = false;
          break;
        }

        if (A >= C) {
          A--;
          C++;
          result.push_back('C');
        } else {
          A++;
          C--;
          result.push_back('A');
        }
      } else {

        if (B == 0 && C == 0) {
          cout << "No" << endl;
          ok = false;
          break;
        }

        if (B >= C) {
          B--;
          C++;
          result.push_back('C');
        } else {
          B++;
          C--;
          result.push_back('B');
        }
      }
    } // for

    if (ok) {

      cout << "Yes" << endl;

      for (int i = 0; i < N; i++) {
        cout << result.at(i) << endl;
      }
    }

  } // else
  // cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  int N;
  cin >> N;
  int A, B, C;
  cin >> A >> B >> C;

  vector<string> s(N);
  for (int i = 0; i < N; i++) {
    cin >> s.at(i);
  }

  string result;

  bool ok = true;
  for (int i = 0; i < N; i++) {

    if (s.at(i) == "AB") {

      if (A == 1 && B == 1) {
        if (s.at(i + 1) == "AC") {
          A++;
          B--;
          result.push_back('A');
        } else if (s.at(i + 1) == "BC") {
          A--;
          B++;
          result.push_back('B');
        }
      }

      else if (A == 0 && B == 0) {
        cout << "No" << endl;
        ok = false;
        break;
      }

      else if (A >= B) {
        A--;
        B++;
        result.push_back('B');
      } else {
        A++;
        B--;
        result.push_back('A');
      }

    } else if (s.at(i) == "AC") {

      if (A == 1 && C == 1) {
        if (s.at(i + 1) == "AB") {
          A++;
          C--;
          result.push_back('A');
        } else if (s.at(i + 1) == "BC") {
          A--;
          C++;
          result.push_back('C');
        }
      }

      else if (A == 0 && C == 0) {
        cout << "No" << endl;
        ok = false;
        break;
      }

      else if (A >= C) {
        A--;
        C++;
        result.push_back('C');
      } else {
        A++;
        C--;
        result.push_back('A');
      }
    } else {

      if (B == 1 && C == 1) {
        if (s.at(i + 1) == "AB") {
          B++;
          C--;
          result.push_back('B');
        } else if (s.at(i + 1) == "AC") {
          B--;
          C++;
          result.push_back('C');
        }
      }

      else if (B == 0 && C == 0) {
        cout << "No" << endl;
        ok = false;
        break;
      }

      else if (B >= C) {
        B--;
        C++;
        result.push_back('C');
      } else {
        B++;
        C--;
        result.push_back('B');
      }
    }
  } // for

  if (ok) {

    cout << "Yes" << endl;

    for (int i = 0; i < (int)result.size(); i++) {
      cout << result.at(i) << endl;
    }
  }
  // cout << ans << endl;
  return 0;
}