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

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

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

  int N, A, B, C;
  cin >> N >> A >> B >> C;

  int x = A + B + C;

  vector<char> ans(N);

  vector<string> s(N);

  for (int i = 0; i < N; i++)
    cin >> s[i];

  if (x == 0) {
    cout << "No" << endl;
    return 0;
  } else if (x == 1) {
    for (int i = 0; i < N; i++) {
      if (s[i] == "AB") {
        if (A == 0 && B == 0) {
          cout << "No" << endl;
          return 0;
        }
        if (A == 0) {
          A++;
          B--;
          ans[i] = 'A';
        } else {
          A--;
          B++;
          ans[i] = 'B';
        }
      } else if (s[i] == "BC") {
        if (B == 0 && C == 0) {
          cout << "No" << endl;
          return 0;
        }
        if (B == 0) {
          B++;
          C--;
          ans[i] = 'B';
        } else {
          B--;
          C++;
          ans[i] = 'C';
        }
      } else {
        if (A == 0 && C == 0) {
          cout << "No" << endl;
          return 0;
        }
        if (A == 0) {
          A++;
          C--;
          ans[i] = 'A';
        } else {
          A--;
          C++;
          ans[i] = 'C';
        }
      }
    }
  } else {
    if (s[0] == "AB") {
      if (A == 0 && B == 0) {
        cout << "No" << endl;
        return 0;
      }
    } else if (s[0] == "BC") {
      if (B == 0 && C == 0) {
        cout << "No" << endl;
        return 0;
      }
    } else {
      if (A == 0 && C == 0) {
        cout << "No" << endl;
        return 0;
      }
    }
    for (int i = 0; i < N; i++) {
      if (s[i] == "AB") {
        if ((A == 1 && B == 1) && i != N - 1) {
          if (s[i + 1] == "AB") {
            A++;
            B--;
            ans[i] = 'A';
          } else if (s[i + 1] == "BC") {
            A--;
            B++;
            ans[i] = 'B';
          } else {
            A++;
            B--;
            ans[i] = 'A';
          }
        } else if (A < B) {
          A++;
          B--;
          ans[i] = 'A';
        } else {
          A--;
          B++;
          ans[i] = 'B';
        }
      } else if (s[i] == "BC") {
        if ((B == 1 && C == 1) && i != N - 1) {
          if (s[i + 1] == "AB") {
            B++;
            C--;
            ans[i] = 'B';
          } else if (s[i + 1] == "BC") {
            B++;
            C--;
            ans[i] = 'B';
          } else {
            B--;
            C++;
            ans[i] = 'C';
          }
        } else if (B < C) {
          B++;
          C--;
          ans[i] = 'B';
        } else {
          B--;
          C++;
          ans[i] = 'C';
        }
      } else {
        if ((A == 1 && C == 1) && i != N - 1) {
          if (s[i + 1] == "AB") {
            A++;
            C--;
            ans[i] = 'A';
          } else if (s[i + 1] == "BC") {
            A--;
            C++;
            ans[i] = 'C';
          } else {
            A++;
            C--;
            ans[i] = 'A';
          }
        } else if (A < C) {
          A++;
          C--;
          ans[i] = 'A';
        } else {
          A--;
          C++;
          ans[i] = 'C';
        }
      }
    }
  }

  cout << "Yes" << endl;
  for (int i = 0; i < N; i++)
    cout << ans[i] << endl;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

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

  int N;
  cin >> N;

  vector<vector<int>> X(N + 1, vector<int>(3));
  vector<string> ans(N);

  vector<string> s(N + 1);

  for (int i = 0; i < 3; i++) {
    cin >> X[0][i];
  }

  for (int i = 1; i <= N; i++) {
    cin >> s[i];
  }

  int p = 0;

  vector<int> c(N);

  bool front = false;

  for (int i = 1; i <= N; i++) {
    if (s[i] == "AB") {
      if (X[i - 1][0] <= 0 && X[i - 1][1] <= 0) {
        i = p - 1;
        if (p == 0) {
          cout << "No" << endl;
          return 0;
        }
        continue;
      }
      if (X[i - 1][0] < X[i - 1][1]) {
        ans[i] = "A";
        X[i][1] = X[i - 1][1] - 1;
        X[i][0] = X[i - 1][0] + 1;
      } else if (X[i - 1][1] < X[i - 1][0]) {
        ans[i] = "B";
        X[i][0] = X[i - 1][0] - 1;
        X[i][1] = X[i - 1][1] + 1;
      } else {
        if (c[i] == 2) {
          cout << "No" << endl;
          return 0;
        } else if (c[i] == 1) {
          ans[i] = "B";
          X[i][0] = X[i - 1][0] - 1;
          X[i][1] = X[i - 1][1] + 1;
          c[i]++;
        } else {
          p = i;
          ans[i] = "A";
          X[i][1] = X[i - 1][1] - 1;
          X[i][0] = X[i - 1][0] + 1;
          c[i]++;
        }
      }
    } else if (s[i] == "BC") {
      if (X[i - 1][1] <= 0 && X[i - 1][2] <= 0) {
        i = p - 1;
        if (p == 0) {
          cout << "No" << endl;
          return 0;
        }
        continue;
      }
      if (X[i - 1][1] < X[i - 1][2]) {
        ans[i] = "B";
        X[i][2] = X[i - 1][2] - 1;
        X[i][1] = X[i - 1][1] + 1;
      } else if (X[i - 1][2] < X[i - 1][1]) {
        ans[i] = "C";
        X[i][1] = X[i - 1][1] - 1;
        X[i][2] = X[i - 1][2] + 1;
      } else {
        if (c[i] == 2) {
          cout << "No" << endl;
          return 0;
        } else if (c[i] == 1) {
          ans[i] = "C";
          X[i][1] = X[i - 1][1] - 1;
          X[i][2] = X[i - 1][2] + 1;
          c[i]++;
        } else {
          p = i;
          ans[i] = "B";
          X[i][2] = X[i - 1][2] - 1;
          X[i][1] = X[i - 1][1] + 1;
          c[i]++;
        }
      }
    } else {
      if (X[i - 1][2] <= 0 && X[i - 1][0] <= 0) {
        i = p - 1;
        if (p == 0) {
          cout << "No" << endl;
          return 0;
        }
        continue;
      }
      if (X[i - 1][2] < X[i - 1][0]) {
        ans[i] = "C";
        X[i][0] = X[i - 1][0] - 1;
        X[i][2] = X[i - 1][2] + 1;
      } else if (X[i - 1][0] < X[i - 1][2]) {
        ans[i] = "A";
        X[i][2] = X[i - 1][2] - 1;
        X[i][0] = X[i - 1][0] + 1;
      } else {
        if (c[i] == 2) {
          cout << "No" << endl;
          return 0;
        } else if (c[i] == 1) {
          ans[i] = "A";
          X[i][2] = X[i - 1][2] - 1;
          X[i][0] = X[i - 1][0] + 1;
          c[i]++;
        } else {
          p = i;
          ans[i] = "C";
          X[i][0] = X[i - 1][0] - 1;
          X[i][2] = X[i - 1][2] + 1;
          c[i]++;
        }
      }
    }
  }

  cout << "Yes" << endl;

  for (int i = 1; i <= N; i++) {
    cout << ans[i] << endl;
  }
}