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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using PII = pair<ll, ll>;
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
template <typename T> void chmin(T &a, const T &b) { a = min(a, b); }
template <typename T> void chmax(T &a, const T &b) { a = max(a, b); }
struct FastIO {
  FastIO() {
    cin.tie(0);
    ios::sync_with_stdio(0);
  }
} fastiofastio;
#ifdef DEBUG
#include "program_contest_library/memo/dump.hpp"
#else
#define dump(...)
#endif
const ll INF = 1LL << 60;

int main(void) {
  ll n, a, b, c;
  cin >> n >> a >> b >> c;
  vector<string> s(n);
  REP(i, n) cin >> s[i];

  auto fall = [] {
    cout << "No" << endl;
    exit(0);
  };

  if (a + b + c == 0)
    fall();

  string ans(n, '?');
  if (a + b + c == 2) {
    REP(i, n) {
      if (s[i] == "AB") {
        if (a == 0 && b == 0)
          fall();
        if (a < b) {
          a++;
          b--;
          ans[i] = 'A';
        } else if (a > b) {
          a--;
          b++;
          ans[i] = 'B';
        } else {
          if (i == n - 1 || s[i + 1] == "AB" || s[i + 1] == "AC") {
            a++;
            b--;
            ans[i] = 'A';
          } else {
            b++;
            a--;
            ans[i] = 'B';
          }
        }
      } else if (s[i] == "AC") {
        if (a == 0 && c == 0)
          fall();
        if (a < c) {
          a++;
          c--;
          ans[i] = 'A';
        } else if (a > c) {
          a--;
          c++;
          ans[i] = 'C';
        } else {
          if (i == n - 1 || s[i + 1] == "AB" || s[i + 1] == "AC") {
            a++;
            c--;
            ans[i] = 'A';
          } else {
            a--;
            c++;
            ans[i] = 'C';
          }
        }
      } else if (s[i] == "BC") {
        if (b == 0 && c == 0)
          fall();
        if (b < c) {
          b++;
          c--;
          ans[i] = 'B';
        } else if (b > c) {
          b--;
          c++;
          ans[i] = 'C';
        } else {
          if (i == n - 1 || s[i + 1] == "AB" || s[i + 1] == "BC") {
            b++;
            c--;
            ans[i] = 'B';
          } else {
            b--;
            c++;
            ans[i] = 'C';
          }
        }
      }
    }
  } else {
    REP(i, n) {
      if (s[i] == "AB") {
        if (a == 0 && b == 0)
          fall();
        if (a < b) {
          a++;
          b--;
          ans[i] = 'A';
        } else {
          a--;
          b++;
          ans[i] = 'B';
        }
      } else if (s[i] == "AC") {
        if (a == 0 && c == 0)
          fall();
        if (a < c) {
          a++;
          c--;
          ans[i] = 'A';
        } else {
          a--;
          c++;
          ans[i] = 'C';
        }
      } else if (s[i] == "BC") {
        if (b == 0 && c == 0)
          fall();
        if (b < c) {
          b++;
          c--;
          ans[i] = 'B';
        } else {
          b--;
          c++;
          ans[i] = 'C';
        }
      }
    }
  }

  cout << "Yes" << endl;
  REP(i, n) cout << ans[i] << "\\n";
  return 0;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using PII = pair<ll, ll>;
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
template <typename T> void chmin(T &a, const T &b) { a = min(a, b); }
template <typename T> void chmax(T &a, const T &b) { a = max(a, b); }
struct FastIO {
  FastIO() {
    cin.tie(0);
    ios::sync_with_stdio(0);
  }
} fastiofastio;
#ifdef DEBUG
#include "program_contest_library/memo/dump.hpp"
#else
#define dump(...)
#endif
const ll INF = 1LL << 60;

int main(void) {
  ll n, a, b, c;
  cin >> n >> a >> b >> c;
  vector<string> s(n);
  REP(i, n) cin >> s[i];

  auto fall = [] {
    cout << "No" << endl;
    exit(0);
  };

  if (a + b + c == 0)
    fall();

  string ans(n, '?');
  if (a + b + c == 2) {
    assert(false);
    REP(i, n) {
      if (s[i] == "AB") {
        if (a == 0 && b == 0)
          fall();
        if (a < b) {
          a++;
          b--;
          ans[i] = 'A';
        } else if (b > a) {
          a--;
          b++;
          ans[i] = 'B';
        } else {
          if (i == n - 1 || s[i + 1] == "AB" || s[i + 1] == "AC") {
            a++;
            b--;
            ans[i] = 'A';
          } else {
            b++;
            a--;
            ans[i] = 'B';
          }
        }
      } else if (s[i] == "AC") {
        if (a == 0 && c == 0)
          fall();
        if (a < c) {
          a++;
          c--;
          ans[i] = 'A';
        } else if (a > c) {
          a--;
          c++;
          ans[i] = 'C';
        } else {
          if (i == n - 1 || s[i + 1] == "AB" || s[i + 1] == "AC") {
            a++;
            c--;
            ans[i] = 'A';
          } else {
            a--;
            c++;
            ans[i] = 'C';
          }
        }
      } else if (s[i] == "BC") {
        if (b == 0 && c == 0)
          fall();
        if (b < c) {
          b++;
          c--;
          ans[i] = 'B';
        } else if (b > c) {
          b--;
          c++;
          ans[i] = 'C';
        } else {
          if (i == n - 1 || s[i + 1] == "AB" || s[i + 1] == "BC") {
            b++;
            c--;
            ans[i] = 'B';
          } else {
            b--;
            c++;
            ans[i] = 'C';
          }
        }
      }
    }
  } else {
    REP(i, n) {
      if (s[i] == "AB") {
        if (a == 0 && b == 0)
          fall();
        if (a < b) {
          a++;
          b--;
          ans[i] = 'A';
        } else {
          a--;
          b++;
          ans[i] = 'B';
        }
      } else if (s[i] == "AC") {
        if (a == 0 && c == 0)
          fall();
        if (a < c) {
          a++;
          c--;
          ans[i] = 'A';
        } else {
          a--;
          c++;
          ans[i] = 'C';
        }
      } else if (s[i] == "BC") {
        if (b == 0 && c == 0)
          fall();
        if (b < c) {
          b++;
          c--;
          ans[i] = 'B';
        } else {
          b--;
          c++;
          ans[i] = 'C';
        }
      }
    }
  }

  cout << "Yes" << endl;
  REP(i, n) cout << ans[i] << "\\n";
  return 0;

  return 0;
}