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

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

int main() {
  int n, a, b, c;
  cin >> n >> a >> b >> c;

  vector<string> s(n);
  for (int i = 0; i < n; i++) {
    cin >> s[i];
  }

  if (a + b + c == 0) {
    cout << "No" << '\\n';
    return 0;
  }

  if (a + b + c == 1) {
    bool flag = true;
    vector<char> ans(n);
    for (int i = 0; i < n; i++) {
      if (s[i] == "AB") {
        if (a == b)
          flag = false;
        else if (a == 0)
          ans[i] = 'A', a++, b--;
        else
          ans[i] = 'B', a--, b++;
      }
      if (s[i] == "AC") {
        if (a == c)
          flag = false;
        else if (a == 0)
          ans[i] = 'A', a++, c--;
        else
          ans[i] = 'C', a--, c++;
      }
      if (s[i] == "BC") {
        if (b == c)
          flag = false;
        else if (b == 0)
          ans[i] = 'B', b++, c--;
        else
          ans[i] = 'C', b--, c++;
      }
    }
    if (flag) {
      cout << "Yes" << '\\n';
      for (auto it : ans)
        cout << it << '\\n';
    } else {
      cout << "No" << '\\n';
    }
    return 0;
  }

  if (s[0] == "AB" && a == 0 && b == 0) {
    cout << "No" << endl;
    return 0;
  }
  if (s[0] == "BC" && b == 0 && c == 0) {
    cout << "No" << endl;
    return 0;
  }
  if (s[0] == "AC" && a == 0 && c == 0) {
    cout << "No" << endl;
    return 0;
  }

  vector<char> ans(n);
  for (int i = 0; i < n; i++) {
    if (s[i] == "AB") {
      if (a == 0) {
        ans[i] = 'A', a++, b--;
      } else if (b == 0) {
        ans[i] = 'B', a--, b++;
      } else if (i != n - 1 && a == 1 &&
                 (s[i + 1] == "AB" || s[i + 1] == "AC")) {
        ans[i] = 'A', a++, b--;
      } else if (i != n - 1 && b == 1 &&
                 (s[i + 1] == "AB" || s[i + 1] == "BC")) {
        ans[i] = 'B', a--, b++;
      } else {
        ans[i] = 'A', a++, b--;
      }
    }
    if (s[i] == "AC") {
      if (a == 0) {
        ans[i] = 'A', a++, c--;
      } else if (c == 0) {
        ans[i] = 'C', a--, c++;
      } else if (i != n - 1 && a == 1 &&
                 (s[i + 1] == "AB" || s[i + 1] == "AC")) {
        ans[i] = 'A', a++, c--;
      } else if (i != n - 1 && c == 1 &&
                 (s[i + 1] == "AC" || s[i + 1] == "BC")) {
        ans[i] = 'C', a--, c++;
      } else {
        ans[i] = 'A', a++, c--;
      }
    }
    if (s[i] == "BC") {
      if (b == 0) {
        ans[i] = 'B', b++, c--;
      } else if (c == 0) {
        ans[i] = 'C', b--, c++;
      } else if (i != n - 1 && b == 1 &&
                 (s[i + 1] == "BC" || s[i + 1] == "AB")) {
        ans[i] = 'B', b++, c--;
      } else if (i != n - 1 && c == 1 &&
                 (s[i + 1] == "AC" || s[i + 1] == "BC")) {
        ans[i] = 'C', b--, c++;
      } else {
        ans[i] = 'B', b++, c--;
      }
    }
  }
  cout << "Yes" << endl;
  for (auto it : ans)
    cout << it << endl;
  return 0;
}

Error: -11

Fixed submission:

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

constexpr int N = 100010;
int dp[N][3];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int n, a, b, c;
  cin >> n >> a >> b >> c;

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

  vector<char> ans;

  for (int i = n; i >= 1; i--) {
    if (s[i] == "AB") {
      if (dp[i][0] <= dp[i][1]) {
        dp[i][0]++;
        ans.push_back('A');
      } else {
        dp[i][1]++;
        ans.push_back('B');
      }
    }
    if (s[i] == "AC") {
      if (dp[i][0] <= dp[i][2]) {
        dp[i][0]++;
        ans.push_back('A');
      } else {
        dp[i][2]++;
        ans.push_back('C');
      }
    }
    if (s[i] == "BC") {
      if (dp[i][1] <= dp[i][2]) {
        dp[i][1]++;
        ans.push_back('B');
      } else {
        dp[i][2]++;
        ans.push_back('C');
      }
    }
  }

  if (dp[1][0] < 0 || dp[1][1] < 0 || dp[1][2] < 0) {
    cout << "No" << endl;
    return 0;
  }
  cout << "Yes" << endl;
  reverse(ans.begin(), ans.end());
  for (int i = 0; i < n; i++) {
    cout << ans[i] << '\\n';
  }
}