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

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

int n, a, b, c;
string s[100001];
char ans[100001];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  cin >> n >> a >> b >> c;
  for (int i = 0; i < n; i++)
    cin >> s[i];

  for (int i = 0; i < n; i++) {
    if (s[i] == "AB") {
      if (a == 0 && b == 0) {
        cout << "No" << '\\n';
        return 0;
      } else if (a == 1 && b == 1 && c == 0 && i != n && s[i + 1] == "AC") {
        b--;
        a++;
        ans[i] = 'A';
      } else if (a == 1 && b == 1 && c == 0 && i != n && s[i + 1] == "BC") {
        a--;
        b++;
        ans[i] = 'B';
      } else if (a > b) {
        a--;
        b++;
        ans[i] = 'B';
      } else {
        a++;
        b--;
        ans[i] = 'A';
      }
    }

    if (s[i] == "AC") {
      if (a == 0 && c == 0) {
        cout << "No" << '\\n';
        return 0;
      } else if (a == 1 && c == 1 && b == 0 && i != n && s[i + 1] == "AB") {
        c--;
        a++;
        ans[i] = 'A';
      } else if (a == 1 && c == 1 && b == 0 && i != n && s[i + 1] == "BC") {
        a--;
        c++;
        ans[i] = 'C';
      } else if (a > c) {
        a--;
        c++;
        ans[i] = 'C';
      } else {
        a++;
        c--;
        ans[i] = 'A';
      }
    }

    if (s[i] == "BC") {
      if (b == 0 && c == 0) {
        cout << "No" << '\\n';
        return 0;
      } else if (b == 1 && c == 1 && a == 0 && i != n && s[i + 1] == "AB") {
        c--;
        b++;
        ans[i] = 'B';
      } else if (b == 1 && c == 1 && a == 0 && i != n && s[i + 1] == "AC") {
        b--;
        c++;
        ans[i] = 'C';
      } else if (b > c) {
        b--;
        c++;
        ans[i] = 'C';
      } else {
        b++;
        c--;
        ans[i] = 'B';
      }
    }
  }

  cout << "Yes" << '\\n';
  for (int i = 0; i < n; i++)
    cout << ans[i] << '\\n';
  return 0;
}


Error: -11

Fixed submission:

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

int n, a, b, c, cnt[3];
string str[100001];
vector<char> ans;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  cin >> n >> a >> b >> c;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    if (s == "AB")
      cnt[0]++, cnt[1]++;
    if (s == "AC")
      cnt[0]++, cnt[2]++;
    if (s == "BC")
      cnt[1]++, cnt[2]++;
  }

  bool ok = true;
  for (int i = 0; i < n; i++) {
    if (str[i] == "AB") {
      if (a > b)
        a--, b++, ans.push_back('B');
      else if (a == b) {
        if (cnt[0] > cnt[1])
          a--, b++, ans.push_back('B');
        else
          a++, b--, ans.push_back('A');
      } else
        a++, b--, ans.push_back('A');
      cnt[0]--, cnt[1]--;
    }
    if (str[i] == "AC") {
      if (a > c)
        a--, c++, ans.push_back('C');
      else if (a == c) {
        if (cnt[0] > cnt[2])
          a--, c++, ans.push_back('C');
        else
          a++, c--, ans.push_back('A');
      } else
        a++, c--, ans.push_back('A');
      cnt[0]--, cnt[2]--;
    }
    if (str[i] == "BC") {
      if (b > c)
        b--, c++, ans.push_back('C');
      else if (b == c) {
        if (cnt[1] > cnt[2])
          b--, c++, ans.push_back('C');
        else
          b++, c--, ans.push_back('B');
      } else
        b++, c--, ans.push_back('B');
      cnt[1]--, cnt[2]--;
    }
    if (a < 0 || b < 0 || c < 0)
      ok = false;
  }

  if (ok) {
    cout << "Yes" << '\\n';
    for (int i = 0; i < n; i++)
      cout << ans[i] << '\\n';
  } else
    cout << "No" << '\\n';
  return 0;
}