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<string> S(N);
  for (auto &e : S)
    cin >> e;
  vector<char> ans;

  auto solve = [&]() -> bool {
    for (int i = 0; i < N; ++i) {
      if (S[i] == "AB") {
        if (A == 0) {
          A++, B--, ans.push_back('A');
        } else if (B == 0) {
          A--, B++, ans.push_back('B');
        } else if (C == 0) {
          if (i < N - 1 and S[i + 1] == "BC") {
            A--, B++, ans.push_back('B');
          } else if (i < N - 1 and S[i + 1] == "AC") {
            A++, B--, ans.push_back('A');
          } else {
            A++, B--, ans.push_back('A');
          }
        } else {
          A++, B--, ans.push_back('A');
        }
      }
      if (S[i] == "BC") {
        if (B == 0) {
          B++, C--, ans.push_back('B');
        } else if (C == 0) {
          B--, C++, ans.push_back('C');
        } else if (A == 0) {
          if (i < N - 1 and S[i + 1] == "AC") {
            B--, C++, ans.push_back('C');
          } else if (i < N - 1 and S[i + 1] == "AB") {
            B++, C--, ans.push_back('B');
          } else {
            B++, C--, ans.push_back('B');
          }
        } else {
          B++, C--, ans.push_back('B');
        }
      }
      if (S[i] == "AC") {
        if (A == 0) {
          A++, C--, ans.push_back('A');
        } else if (C == 0) {
          A--, C++, ans.push_back('C');
        } else if (B == 0) {
          if (i < N - 1 and S[i + 1] == "BC") {
            A--, C++, ans.push_back('C');
          } else if (i < N - 1 and S[i + 1] == "AB") {
            A++, C--, ans.push_back('A');
          } else {
            C++, A--, ans.push_back('C');
          }
        } else {
          C++, A--, ans.push_back('C');
        }
      }
      if (A < 0 or B < 0 or C < 0)
        return false;
    }
    return true;
  };

  if (solve()) {
    cout << "Yes" << '\\n';
    for (auto &e : ans)
      cout << e << '\\n';
  } else {
    cout << "No" << '\\n';
  }
  return 0;
}


Error: 0

Fixed submission:

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

int main() {
  int N, A, B, C;
  cin >> N >> A >> B >> C;
  vector<string> S(N);
  for (auto &e : S)
    cin >> e;
  vector<int> a(N + 1, A), b(N + 1, B), c(N + 1, C);
  for (int i = 0; i < N; ++i) {
    a[i + 1] = a[i];
    b[i + 1] = b[i];
    c[i + 1] = c[i];
    if (S[i] == "AB")
      a[i + 1]--, b[i + 1]--;
    else if (S[i] == "BC")
      b[i + 1]--, c[i + 1]--;
    else
      a[i + 1]--, c[i + 1]--;
  }
  A = 0, B = 0, C = 0;
  bool ok = true;
  vector<char> ans;
  for (int i = i; i < N + 1; ++i) {
    int p = i - 1;
    a[i] += A, b[i] += B, c[i] += C;
    if (S[p] == "AB") {
      if (a[i] <= 0 and b[i] <= 0)
        ok = false;
      if (a[i] < b[i])
        ans.push_back('A'), A += 2;
      else
        ans.push_back('B'), B += 2;
    }
    if (S[p] == "BC") {
      if (b[i] <= 0 and c[i] <= 0)
        ok = false;
      if (b[i] < c[i])
        ans.push_back('B'), B += 2;
      else
        ans.push_back('C'), C += 2;
    }
    if (S[p] == "AC") {
      if (a[i] <= 0 and c[i] <= 0)
        ok = false;
      if (a[i] < c[i])
        ans.push_back('A'), A += 2;
      else
        ans.push_back('C'), C += 2;
    }
  }
  if (ok) {
    cout << "Yes" << '\\n';
    for (auto &s : ans)
      cout << s << '\\n';
  } else {
    cout << "No" << '\\n';
  }
  return 0;
}