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

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

int main() {
  int n;
  int64_t x[3];
  int s[100000];
  cin >> n >> x[0] >> x[1] >> x[2];
  for (int i = 0; i < n; ++i) {
    string si;
    cin >> si;
    if (si == "AB")
      s[i] = 2;
    if (si == "BC")
      s[i] = 0;
    if (si == "AC")
      s[i] = 1;
  }
  vector<int> ans;
  if (x[0] + x[1] + x[2] == 0) {
    cout << "No" << endl;
    return 0;
  } else if (x[0] + x[1] + x[2] == 1) {
    for (int i = 0; i < n; ++i) {
      if (x[s[i]] == 1) {
        cout << "No" << endl;
        return 0;
      }
      if (x[(s[i] + 1) % 3] == 0) {
        ans.push_back((s[i] + 1) % 3);
        x[(s[i] + 1) % 3] = 1;
        x[(s[i] + 2) % 3] = 0;
      } else {
        ans.push_back((s[i] + 2) % 3);
        x[(s[i] + 2) % 3] = 1;
        x[(s[i] + 1) % 3] = 0;
      }
    }
  } else {
    for (int i = 0; i < n; ++i) {
      if (x[s[i]] == x[0] + x[1] + x[2]) {
        cout << "No" << endl;
        return 0;
      }
      if (x[(s[i] + 1) % 3] == 0) {
        ans.push_back((s[i] + 1) % 3);
        ++x[(s[i] + 1) % 3];
        --x[(s[i] + 2) % 3];
      } else if (x[(s[i] + 2) % 3] == 0) {
        ans.push_back((s[i] + 2) % 3);
        ++x[(s[i] + 2) % 3];
        --x[(s[i] + 1) % 3];
      } else if (i + 1 < n && s[i] != s[i + 1]) {
        ans.push_back(3 - s[i] - s[i + 1]);
        ++x[3 - s[i] - s[i + 1]];
        --x[s[i + 1]];
      } else {
        if (x[(s[i] + 1) % 3] < x[(s[i] + 2) % 3]) {
          ans.push_back((s[i] + 1) % 3);
          ++x[(s[i] + 1) % 3];
          --x[(s[i] + 2) % 3];
        } else {
          ans.push_back((s[i] + 2) % 3);
          ++x[(s[i] + 2) % 3];
          --x[(s[i] + 1) % 3];
        }
      }
    }
  }
  cout << "Yes" << endl;
  for (int a : ans) {
    cout << "ABC"[a] << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  int n;
  int64_t x[3];
  int s[100000];
  cin >> n >> x[0] >> x[1] >> x[2];
  for (int i = 0; i < n; ++i) {
    string si;
    cin >> si;
    if (si == "AB")
      s[i] = 2;
    if (si == "BC")
      s[i] = 0;
    if (si == "AC")
      s[i] = 1;
  }
  vector<int> ans;
  if (x[0] + x[1] + x[2] == 0) {
    cout << "No" << endl;
    return 0;
  } else if (x[0] + x[1] + x[2] == 1) {
    for (int i = 0; i < n; ++i) {
      if (x[s[i]] == 1) {
        cout << "No" << endl;
        return 0;
      }
      if (x[(s[i] + 1) % 3] == 0) {
        ans.push_back((s[i] + 1) % 3);
        x[(s[i] + 1) % 3] = 1;
        x[(s[i] + 2) % 3] = 0;
      } else {
        ans.push_back((s[i] + 2) % 3);
        x[(s[i] + 2) % 3] = 1;
        x[(s[i] + 1) % 3] = 0;
      }
    }
  } else {
    for (int i = 0; i < n; ++i) {
      if (x[s[i]] == x[0] + x[1] + x[2]) {
        cout << "No" << endl;
        return 0;
      }
      if (x[(s[i] + 1) % 3] == 0) {
        ans.push_back((s[i] + 1) % 3);
        ++x[(s[i] + 1) % 3];
        --x[(s[i] + 2) % 3];
      } else if (x[(s[i] + 2) % 3] == 0) {
        ans.push_back((s[i] + 2) % 3);
        ++x[(s[i] + 2) % 3];
        --x[(s[i] + 1) % 3];
      } else if (i + 1 < n && s[i] != s[i - 1]) {
        ans.push_back(3 - s[i] - s[i + 1]);
        ++x[3 - s[i] - s[i + 1]];
        --x[s[i + 1]];
      } else {
        if (x[(s[i] + 1) % 3] < x[(s[i] + 2) % 3]) {
          ans.push_back(x[(s[i] + 1) % 3]);
          ++x[(s[i] + 1) % 3];
          --x[(s[i] + 2) % 3];
        } else {
          ans.push_back(x[(s[i] + 2) % 3]);
          ++x[(s[i] + 2) % 3];
          --x[(s[i] + 1) % 3];
        }
      }
    }
  }
  cout << "Yes" << endl;
  for (int a : ans) {
    cout << "ABC"[a] << endl;
  }

  return 0;
}