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

/*
    Author : DemonStar
*/

#pragma GCC optimize("Ofast")
#pragma GCC target("sse4")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
#define ordered_set                                                            \\
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define all(x) x.begin(), x.end()
#define loop(i, n) for (ll i = 0; i < n; i++)
#define pl pair<ll, ll>
#define vl vector<ll>
#define endl '\\n'
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\\'' << x << '\\''; }
void __print(const char *x) { cerr << '\\"' << x << '\\"'; }
void __print(const string &x) { cerr << '\\"' << x << '\\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}

#ifdef LOCAL
#define debug(x...)                                                            \\
  cerr << "[" << #x << "] = [";                                                \\
  _print(x)
#else
#define debug(x...)
#endif

struct custom_hash {
  static uint64_t splitmix64(uint64_t x) {
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }
  size_t operator()(uint64_t x) const {
    static const uint64_t FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
};

template <typename T, typename TT>
ostream &operator<<(ostream &os, const pair<T, TT> &t) {
  return os << t.first << " " << t.second;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &t) {
  for (auto &i : t)
    os << i << " ";
  return os;
}
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (T &t : v)
    is >> t;
  return is;
}
template <typename T1, typename T2>
istream &operator>>(istream &is, vector<pair<T1, T2>> &v) {
  for (pair<T1, T2> &t : v)
    is >> t.first >> t.second;
  return is;
}

const ll mod = 1e9 + 7;

ll modpow(ll n, ll p) {
  ll ans = 1;
  while (p) {
    if (p & 1)
      (ans *= n) %= mod;
    (n *= n) %= mod, p /= 2;
  }
  return ans;
}

int main() {

#ifdef LOCAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  fast;
  ll n, a, b, c;
  cin >> n >> a >> b >> c;
  string ans = "";
  vector<string> inp(n);
  cin >> inp;
  loop(i, n) {
    string s = inp[i];
    if (s == "AB") {
      if (!a and !b) {
        cout << "No" << endl;
        return 0;
      }
      if (a == 1 and b == 1 and !c) {
        if (i + 1 < n and inp[i + 1] == "BC") {
          a--, b++, ans += 'B';
          continue;
        } else if (i + 1 < n and inp[i + 1] == "AC") {
          a++, b--, ans += 'A';
          continue;
        }
      }
      if (a < b)
        a++, b--, ans += 'A';
      else
        a--, b++, ans += 'B';
    } else if (s == "AC") {
      if (!a and !c) {
        cout << "No" << endl;
        return 0;
      }
      if (a == 1 and c == 1 and !b) {
        if (i + 1 < n and inp[i + 1] == "BC") {
          a--, c++, ans += 'C';
          continue;
        } else if (i + 1 < n and inp[i + 1] == "AB") {
          a++, c--, ans += 'A';
          continue;
        }
      }
      if (a < c)
        a++, c--, ans += 'A';
      else
        a--, c++, ans += 'C';
    } else {
      if (!c and !b) {
        cout << "No" << endl;
        return 0;
      }
      if (c == 1 and b == 1 and !a) {
        if (i + 1 < n and inp[i + 1] == "AC") {
          b--, c++, ans += 'C';
          continue;
        } else if (i + 1 < n and inp[i + 1] == "AB") {
          c--, b++, ans += 'B';
          continue;
        }
      }
      if (c < b)
        c++, b--, ans += 'C';
      else
        c--, b++, ans += 'B';
    }
    debug(ans.back());
  }
  cout << "Yes" << endl;
  for (auto x : ans)
    cout << x << endl;

#ifdef LOCAL
  cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s";
#endif

  return 0;
}

Error: 0

Fixed submission:

/*
    Author : DemonStar
*/

#pragma GCC optimize("Ofast")
#pragma GCC target("sse4")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
#define ordered_set                                                            \\
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define all(x) x.begin(), x.end()
#define loop(i, n) for (ll i = 0; i < n; i++)
#define pl pair<ll, ll>
#define vl vector<ll>
#define endl '\\n'
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\\'' << x << '\\''; }
void __print(const char *x) { cerr << '\\"' << x << '\\"'; }
void __print(const string &x) { cerr << '\\"' << x << '\\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}

#ifdef LOCAL
#define debug(x...)                                                            \\
  cerr << "[" << #x << "] = [";                                                \\
  _print(x)
#else
#define debug(x...)
#endif

struct custom_hash {
  static uint64_t splitmix64(uint64_t x) {
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }
  size_t operator()(uint64_t x) const {
    static const uint64_t FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
};

template <typename T, typename TT>
ostream &operator<<(ostream &os, const pair<T, TT> &t) {
  return os << t.first << " " << t.second;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &t) {
  for (auto &i : t)
    os << i << " ";
  return os;
}
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (T &t : v)
    is >> t;
  return is;
}
template <typename T1, typename T2>
istream &operator>>(istream &is, vector<pair<T1, T2>> &v) {
  for (pair<T1, T2> &t : v)
    is >> t.first >> t.second;
  return is;
}

const ll mod = 1e9 + 7;

ll modpow(ll n, ll p) {
  ll ans = 1;
  while (p) {
    if (p & 1)
      (ans *= n) %= mod;
    (n *= n) %= mod, p /= 2;
  }
  return ans;
}

int main() {

#ifdef LOCAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  fast;
  ll n, a, b, c;
  cin >> n >> a >> b >> c;
  string ans = "";
  vector<string> inp(n);
  cin >> inp;
  loop(i, n) {
    string s = inp[i];
    if (s == "AB") {
      if (!a and !b) {
        cout << "No" << endl;
        return 0;
      }
      if (a == 1 and b == 1 and !c) {
        if (i + 1 < n and inp[i + 1] == "BC") {
          a--, b++, ans[i] = 'B';
          continue;
        } else if (i + 1 < n and inp[i + 1] == "AC") {
          a++, b--, ans[i] = 'A';
          continue;
        }
      }
      if (a < b)
        a++, b--, ans += 'A';
      else
        a--, b++, ans += 'B';
    } else if (s == "AC") {
      if (!a and !c) {
        cout << "No" << endl;
        return 0;
      }
      if (a == 1 and c == 1 and !b) {
        if (i + 1 < n and inp[i + 1] == "BC") {
          a--, c++, ans[i] = 'C';
          continue;
        } else if (i + 1 < n and inp[i + 1] == "AB") {
          a++, c--, ans[i] = 'A';
          continue;
        }
      }
      if (a < c)
        a++, c--, ans += 'A';
      else
        a--, c++, ans += 'C';
    } else {
      if (!c and !b) {
        cout << "No" << endl;
        return 0;
      }
      if (c == 1 and b == 1 and !a) {
        if (i + 1 < n and inp[i + 1] == "AC") {
          b--, c++, ans[i] = 'C';
          continue;
        } else if (i + 1 < n and inp[i + 1] == "AB") {
          c--, b++, ans[i] = 'B';
          continue;
        }
      }
      if (c < b)
        c++, b--, ans += 'C';
      else
        c--, b++, ans += 'B';
    }
    debug(ans.back());
  }
  cout << "Yes" << endl;
  for (auto x : ans)
    cout << x << endl;

#ifdef LOCAL
  cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s";
#endif

  return 0;
}