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

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
#define repr(i, a, b) for (ll i = a; i < b; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define invrepr(i, a, b) for (int i = b - 1; i >= a; i--)
#define invrep(i, n) invrepr(i, 0, n)
#define repitr(itr, a) for (auto itr = a.begin(); itr != a.end(); ++itr)
const int MOD = 1e9 + 7;

int main() {
  ios_base::sync_with_stdio(false);

  int n;
  cin >> n;
  vector<ll> a(3);
  rep(i, 3) cin >> a[i];
  map<string, int> mp;
  mp["AB"] = 0;
  mp["BC"] = 1;
  mp["AC"] = 2;
  vector<int> b(n);
  rep(i, n) {
    string s;
    cin >> s;
    b[i] = mp[s];
  }
  bool flag = 1;
  ll sum = 0;
  rep(i, 3) sum += a[i];
  vector<char> ans;
  rep(i, n) {
    int fi = b[i], se = (b[i] + 1) % 3;
    if (a[fi] == 0 && a[se] == 0) {
      flag = 0;
    } else if (a[fi] == 1 && a[se] == 1 && a[(fi + 2) % 3] == 0) {
      if (i < n - 1) {
        if (b[i + 1] == se) {
          --a[fi];
          ++a[se];
          ans.push_back(se + 'A');
        } else {
          ++a[fi];
          --a[se];
          ans.push_back(fi + 'A');
        }
      } else {
        --a[fi];
        ++a[se];
        ans.push_back(se + 'A');
      }
    } else {
      if (a[fi] >= a[se]) {
        --a[fi];
        ++a[se];
        ans.push_back(se + 'A');
      } else {
        ++a[fi];
        --a[se];
        ans.push_back(fi + 'A');
      }
    }
  }

  if (flag) {
    cout << "Yes" << endl;
    rep(i, n) cout << ans[i] << endl;
  } else {
    cout << "No" << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
#define repr(i, a, b) for (ll i = a; i < b; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define invrepr(i, a, b) for (int i = b - 1; i >= a; i--)
#define invrep(i, n) invrepr(i, 0, n)
#define repitr(itr, a) for (auto itr = a.begin(); itr != a.end(); ++itr)
const int MOD = 1e9 + 7;

int main() {
  ios_base::sync_with_stdio(false);

  int n;
  cin >> n;
  vector<ll> a(3);
  rep(i, 3) cin >> a[i];
  map<string, int> mp;
  mp["AB"] = 0;
  mp["BC"] = 1;
  mp["AC"] = 2;
  vector<int> b(n);
  rep(i, n) {
    string s;
    cin >> s;
    b[i] = mp[s];
  }
  bool flag = 1;
  ll sum = 0;
  rep(i, n) sum += a[i];
  vector<char> ans;
  if (sum > 0) {
    rep(i, n) {
      int fi = b[i], se = (b[i] + 1) % 3;
      if (a[fi] == 0 && a[se] == 0) {
        flag = 0;
      } else if (a[fi] == 1 && a[se] == 1 && a[(fi + 2) % 3] == 0) {
        if (i < n - 1) {
          if (b[i + 1] == (fi + 2) % 3) {
            --a[fi];
            ++a[se];
            ans.push_back(se + 'A');
          } else {
            ++a[fi];
            --a[se];
            ans.push_back(fi + 'A');
          }
        } else {
          --a[fi];
          ++a[se];
          ans.push_back(se + 'A');
        }
      } else {
        if (a[fi] >= a[se]) {
          --a[fi];
          ++a[se];
          ans.push_back(se + 'A');
        } else {
          ++a[fi];
          --a[se];
          ans.push_back(fi + 'A');
        }
      }
    }
  } else {
    flag = 0;
  }
  if (flag) {
    cout << "Yes" << endl;
    rep(i, n) cout << ans[i] << endl;
  } else {
    cout << "No" << endl;
  }

  return 0;
}