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

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

#define int long long

#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)

#define YES puts("YES")
#define Yes puts("Yes")
#define NO puts("NO")
#define No puts("No")
#define ALL(v) (v).begin(), (v).end()
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

#if 1
//-------------
// DUMPマクロ
// https://www.creativ.xyz/dump-cpp-652/

#define repi(itr, ds) for (auto itr = ds.begin(); itr != ds.end(); itr++)

// vector
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  for (T &x : vec)
    is >> x;
  return is;
}
// pair
template <typename T, typename U>
ostream &operator<<(ostream &os, pair<T, U> &pair_var) {
  os << "(" << pair_var.first << ", " << pair_var.second << ")";
  return os;
}
// vector
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "{";
  for (int i = 0; i < vec.size(); i++) {
    os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
  }
  os << "}";
  return os;
}
// map
template <typename T, typename U>
ostream &operator<<(ostream &os, map<T, U> &map_var) {
  os << "{";
  repi(itr, map_var) {
    os << *itr;
    itr++;
    if (itr != map_var.end())
      os << ", ";
    itr--;
  }
  os << "}";
  return os;
}
// set
template <typename T> ostream &operator<<(ostream &os, set<T> &set_var) {
  os << "{";
  repi(itr, set_var) {
    os << *itr;
    itr++;
    if (itr != set_var.end())
      os << ", ";
    itr--;
  }
  os << "}";
  return os;
}

#define DUMPOUT cerr

void dump_func() { DUMPOUT << endl; }
template <class Head, class... Tail>
void dump_func(Head &&head, Tail &&...tail) {
  DUMPOUT << head;
  if (sizeof...(Tail) > 0) {
    DUMPOUT << ", ";
  }
  dump_func(std::move(tail)...);
}
#ifdef DEBUG
#define DEB
#define dump(...)                                                              \\
  DUMPOUT << "  " << string(#__VA_ARGS__) << ": "                              \\
          << "[" << to_string(__LINE__) << ":" << __FUNCTION__ << "]" << endl  \\
          << "    ",                                                           \\
      dump_func(__VA_ARGS__)
#else
#define DEB if (false)
#define dump(...)
#endif

// http://beet-aizu.hatenablog.com/entry/2018/04/08/145516
template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }

template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}

template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
  t = v;
}

template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
  for (auto &e : t)
    fill_v(e, v);
}

#endif

//*
#define mod                                                                    \\
  1000000007 /*/                                                               \\
#define mod 998244353 //*/

typedef pair<int, int> P;
#define INF (1LL << 60)

void solve() {
  int N, A, B, C;
  cin >> N >> A >> B >> C;

  vector<string> in(N);
  rep(i, N) cin >> in[i];

  vector<string> ans;

  rep(i, N) {
    string s = in[i];
    if (s == "AB") {
      if (A > B) {
        A--;
        B++;
        ans.push_back("B");
      } else if (A < B) {
        B--;
        A++;
        ans.push_back("A");
      } else {
        if (i + 1 < N && in[i + 1] == "BC") {
          A--;
          B++;
          ans.push_back("B");
        } else {
          B--;
          A++;
          ans.push_back("A");
        }
      }
    } else if (s == "AC") {
      if (A > C) {
        A--;
        C++;
        ans.push_back("C");
      } else if (A < C) {
        C--;
        A++;
        ans.push_back("A");
      } else {
        if (i + 1 < N && in[i + 1] == "BC") {
          C++;
          A--;
          ans.push_back("C");
        } else {
          C--;
          A++;
          ans.push_back("A");
        }
      }
    } else if (s == "BC") {
      if (B > C) {
        B--;
        C++;
        ans.push_back("C");
      } else if (B < C) {
        C--;
        B++;
        ans.push_back("B");
      } else {
        if (i + 1 < N && in[i + 1] == "AB") {
          C--;
          B++;
          ans.push_back("B");
        } else {
          B--;
          C++;
          ans.push_back("C");
        }
      }
    }
    if (A < 0 || B < 0 || C < 0) {
      No;
      return;
    }
  }

  Yes;
  for (string s : ans)
    cout << s << endl;
}

signed main() {
  cout << fixed << setprecision(18);
  cerr << fixed << setprecision(18);
  solve();
}


Error: 0

Fixed submission:

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

#define int long long

#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)

#define YES puts("YES")
#define Yes puts("Yes")
#define NO puts("NO")
#define No puts("No")
#define ALL(v) (v).begin(), (v).end()
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

#if 1
//-------------
// DUMPマクロ
// https://www.creativ.xyz/dump-cpp-652/

#define repi(itr, ds) for (auto itr = ds.begin(); itr != ds.end(); itr++)

// vector
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  for (T &x : vec)
    is >> x;
  return is;
}
// pair
template <typename T, typename U>
ostream &operator<<(ostream &os, pair<T, U> &pair_var) {
  os << "(" << pair_var.first << ", " << pair_var.second << ")";
  return os;
}
// vector
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "{";
  for (int i = 0; i < vec.size(); i++) {
    os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
  }
  os << "}";
  return os;
}
// map
template <typename T, typename U>
ostream &operator<<(ostream &os, map<T, U> &map_var) {
  os << "{";
  repi(itr, map_var) {
    os << *itr;
    itr++;
    if (itr != map_var.end())
      os << ", ";
    itr--;
  }
  os << "}";
  return os;
}
// set
template <typename T> ostream &operator<<(ostream &os, set<T> &set_var) {
  os << "{";
  repi(itr, set_var) {
    os << *itr;
    itr++;
    if (itr != set_var.end())
      os << ", ";
    itr--;
  }
  os << "}";
  return os;
}

#define DUMPOUT cerr

void dump_func() { DUMPOUT << endl; }
template <class Head, class... Tail>
void dump_func(Head &&head, Tail &&...tail) {
  DUMPOUT << head;
  if (sizeof...(Tail) > 0) {
    DUMPOUT << ", ";
  }
  dump_func(std::move(tail)...);
}
#ifdef DEBUG
#define DEB
#define dump(...)                                                              \\
  DUMPOUT << "  " << string(#__VA_ARGS__) << ": "                              \\
          << "[" << to_string(__LINE__) << ":" << __FUNCTION__ << "]" << endl  \\
          << "    ",                                                           \\
      dump_func(__VA_ARGS__)
#else
#define DEB if (false)
#define dump(...)
#endif

// http://beet-aizu.hatenablog.com/entry/2018/04/08/145516
template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }

template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}

template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
  t = v;
}

template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
  for (auto &e : t)
    fill_v(e, v);
}

#endif

//*
#define mod                                                                    \\
  1000000007 /*/                                                               \\
#define mod 998244353 //*/

typedef pair<int, int> P;
#define INF (1LL << 60)

void solve() {
  int N, A, B, C;
  cin >> N >> A >> B >> C;

  vector<string> in(N);
  rep(i, N) cin >> in[i];

  if (A == 0 && B == 0 || A == 0 && C == 0 || B == 0 && C == 0) {
    exit(1);
  }
  /*
      // ひたすら試す
      int MAX=min(20LL,N);
      bool good=false;
      rep(bit, 1<<MAX){
          int A_ = A, B_ = B, C_=C;
          rep(i,MAX){
              bool f = (bit & (1LL<<i)) >0  ? true: false;
              string s = in[i];
              if(s=="AB"){
                  if(f){ A_--; B_++;}
                  else{ B_--; A_++;}
              }else if(s=="AC"){
                  if(f){ A_--; C_++;}
                  else{ C_--; A_++;}
              }else if(s=="BC"){
                  if(f){ B_--; C_++;}
                  else{ C_--; B_++;}
              }
          }
          // どうやってもマイナスになるならNoにする
  //        dump(A_, B_, C_);
          if(A_>=0 && B_>=0 && C_>=0){ good=true;}
      }
      if(!good){ No; return;}
  */
  vector<string> ans;
  rep(i, N) {
    string s = in[i];
    if (s == "AB") {
      if (A >= B) {
        A--;
        B++;
        ans.push_back("B");
      } else {
        B--;
        A++;
        ans.push_back("A");
      }
    } else if (s == "AC") {
      if (A >= C) {
        A--;
        C++;
        ans.push_back("C");
      } else {
        C--;
        A++;
        ans.push_back("A");
      }
    } else if (s == "BC") {
      if (B >= C) {
        B--;
        C++;
        ans.push_back("C");
      } else {
        C--;
        B++;
        ans.push_back("B");
      }
    }
    if (A < 0 || B < 0 || C < 0) {
      No;
      return;
    }
  }

  Yes;
  for (string s : ans)
    cout << s << endl;
}

signed main() {
  cout << fixed << setprecision(18);
  cerr << fixed << setprecision(18);
  solve();
}