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

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define fst first
#define scd second
#define PB push_back
#define MP make_pair
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define omajinai                                                               \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0)
#define rep(i, x) for (int i = 0; i < (int)(x); ++i)
#define rep1(i, x) for (int i = 1; i <= (int)(x); ++i)
#define rrep(i, x) for (int i = (int)x - 1; i >= 0; --i)
#define rrep1(i, x) for (int i = (int)x; i >= 1; --i)

using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
using vpii = vector<pii>;

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> p) {
  os << "(" << p.fst << ", " << p.scd << ")";
  return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> v) {
  rep(i, v.size()) {
    if (i)
      os << ", ";
    os << v[i];
  }
  return os;
}
template <typename T> T &max(T &a, T &b) {
  if (a >= b)
    return a;
  return b;
}
template <typename T> T &min(T &a, T &b) {
  if (a < b)
    return a;
  return b;
}
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
constexpr ll TEN(int n) { return n == 0 ? 1 : 10 * TEN(n - 1); }

constexpr ll inf = TEN(9) + 5;
constexpr ll linf = 3 * TEN(18);

const int mod = TEN(9) + 7;

int N, A, B, C;
vector<string> op;
vector<char> ans;

signed main() {
  cin >> N >> A >> B >> C;
  rep(i, N) {
    string s;
    cin >> s;
    op.emplace_back(s);
  }

  bool f = true;

  if (A + B + C <= 1) {
    rep(i, N) {
      if (op[i] == "AB") {
        if (A == 0 and B == 0) {
          f = false;
        } else if (A > 0) {
          A--;
          B++;
          ans.PB('B');
        } else {
          B--;
          A++;
          ans.PB('A');
        }
      }
      if (op[i] == "AC") {
        if (A == 0 and C == 0) {
          f = false;
        } else if (A > 0) {
          A--;
          C++;
          ans.PB('C');
        } else {
          C--;
          A++;
          ans.PB('A');
        }
      }
      if (op[i] == "BC") {
        if (B == 0 and C == 0) {
          f = false;
        } else if (B > 0) {
          B--;
          C++;
          ans.PB('C');
        } else {
          C--;
          B++;
          ans.PB('B');
        }
      }
    }
  } else {
    f = false;

    if (op[0] == "AB" and (A > 0 or B > 0))
      f = true;
    if (op[0] == "AC" and (A > 0 or C > 0))
      f = true;
    if (op[0] == "BC" and (B > 0 or C > 0))
      f = true;

    if (f) {
      rep(i, N) {
        if (op[i] == "AB") {
          if (A == 0 and B == 0)
            f = false;

          if (A * B == 1) {
            if (i + 1 < N) {
              if (op[i + 1] == "BC") {
                B++, ans.PB('B');
                A--;
              } else {
                A++, ans.PB('A');
                B--;
              }
            }

            continue;
          }

          if (A >= B) {
            B++, ans.PB('B');
            A--;
          } else {
            A++, ans.PB('A');
            B--;
          }
        }
        if (op[i] == "AC") {
          if (A == 0 and C == 0)
            f = false;

          if (A * C == 1) {
            if (i + 1 < N) {
              if (op[i + 1] == "BC") {
                C++, ans.PB('C');
                A--;
              } else {
                A++, ans.PB('A');
                C--;
              }

              continue;
            }
          }

          if (A >= C) {
            C++, ans.PB('C');
            A--;
          } else {
            A++, ans.PB('A');
            C--;
          }
        }
        if (op[i] == "BC") {
          if (B == 0 and C == 0)
            f = false;

          if (B * C == 1) {
            if (i + 1 < N) {
              if (op[i + 1] == "AB") {
                B++, ans.PB('B');
                C--;
              } else {
                C++, ans.PB('C');
                B--;
              }
            }

            continue;
          }

          if (B >= C) {
            C++, ans.PB('C');
            B--;
          } else {
            B++, ans.PB('B');
            C--;
          }
        }
      }
    }
  }

  if (f) {
    puts("Yes");
    rep(i, N) printf("%c\\n", ans[i]);
  } else {
    puts("No");
  }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define fst first
#define scd second
#define PB push_back
#define MP make_pair
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define omajinai                                                               \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0)
#define rep(i, x) for (int i = 0; i < (int)(x); ++i)
#define rep1(i, x) for (int i = 1; i <= (int)(x); ++i)
#define rrep(i, x) for (int i = (int)x - 1; i >= 0; --i)
#define rrep1(i, x) for (int i = (int)x; i >= 1; --i)

using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
using vpii = vector<pii>;

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> p) {
  os << "(" << p.fst << ", " << p.scd << ")";
  return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> v) {
  rep(i, v.size()) {
    if (i)
      os << ", ";
    os << v[i];
  }
  return os;
}
template <typename T> T &max(T &a, T &b) {
  if (a >= b)
    return a;
  return b;
}
template <typename T> T &min(T &a, T &b) {
  if (a < b)
    return a;
  return b;
}
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
constexpr ll TEN(int n) { return n == 0 ? 1 : 10 * TEN(n - 1); }

constexpr ll inf = TEN(9) + 5;
constexpr ll linf = 3 * TEN(18);

const int mod = TEN(9) + 7;

int N, A, B, C;
vector<string> op;
vector<char> ans;

signed main() {
  cin >> N >> A >> B >> C;
  rep(i, N) {
    string s;
    cin >> s;
    op.emplace_back(s);
  }

  bool f = true;

  if (A + B + C <= 1) {
    rep(i, N) {
      if (op[i] == "AB") {
        if (A == 0 and B == 0) {
          f = false;
        } else if (A > 0) {
          A--;
          B++;
          ans.PB('B');
        } else {
          B--;
          A++;
          ans.PB(A);
        }
      }
      if (op[i] == "AC") {
        if (A == 0 and C == 0) {
          f = false;
        } else if (A > 0) {
          A--;
          C++;
          ans.PB('C');
        } else {
          C--;
          A++;
          ans.PB('A');
        }
      }
      if (op[i] == "BC") {
        if (B == 0 and C == 0) {
          f = false;
        } else if (B > 0) {
          B--;
          C++;
          ans.PB('C');
        } else {
          C++;
          B--;
          ans.PB('B');
        }
      }
    }
    return 1;
  } else {
    f = false;

    if (op[0] == "AB" and (A > 0 or B > 0))
      f = true;
    if (op[0] == "AC" and (A > 0 or C > 0))
      f = true;
    if (op[0] == "BC" and (B > 0 or C > 0))
      f = true;

    if (f) {
      rep(i, N) {
        if (op[i] == "AB") {
          if (A == 0 and B == 0)
            f = false;

          if (A * B == 1) {
            if (i + 1 < N) {
              if (op[i + 1] == "BC") {
                B++, ans.PB('B');
                A--;
              } else {
                A++, ans.PB('A');
                B--;
              }
            }

            continue;
          }

          if (A >= B) {
            B++, ans.PB('B');
            A--;
          } else {
            A++, ans.PB('A');
            B--;
          }
        }
        if (op[i] == "AC") {
          if (A == 0 and C == 0)
            f = false;

          if (A * C == 1) {
            if (i + 1 < N) {
              if (op[i + 1] == "BC") {
                C++, ans.PB('C');
                A--;
              } else {
                A++, ans.PB('A');
                C--;
              }

              continue;
            }
          }

          if (A >= C) {
            C++, ans.PB('C');
            A--;
          } else {
            A++, ans.PB('A');
            C--;
          }
        }
        if (op[i] == "BC") {
          if (B == 0 and C == 0)
            f = false;

          if (B * C == 1) {
            if (i + 1 < N) {
              if (op[i + 1] == "AB") {
                B++, ans.PB('B');
                C--;
              } else {
                C++, ans.PB('C');
                B--;
              }
            }

            continue;
          }

          if (B >= C) {
            C++, ans.PB('C');
            B--;
          } else {
            B++, ans.PB('B');
            C--;
          }
        }
      }
    }
  }

  if (f) {
    puts("Yes");
    rep(i, N) printf("%c\\n", ans[i]);
  } else {
    puts("No");
  }
}