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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, n) for (int i = 1; i < (int)(n); i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define vout(x) rep(i, x.size()) cout << x[i] << " "
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;
using vint = vector<int>;
using vvint = vector<vector<int>>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<int, int>;
const int inf = 1e9;
const ll inf_l = 1e18;
const int MAX = 1e5;

int main() {
  int n, a, b, c;
  cin >> n >> a >> b >> c;
  vector<char> ans;
  vector<string> s(n);
  rep(i, n) cin >> s[i];
  bool ok = true;
  if (a + b + c != 2) {
    rep(i, n) {
      if (s[i] == "AB") {
        if (a >= b) {
          a--;
          b++;
          ans.push_back('B');
        } else {
          a++;
          b--;
          ans.push_back('A');
        }
      }
      if (s[i] == "BC") {
        if (b >= c) {
          b--;
          c++;
          ans.push_back('C');
        } else {
          b++;
          c--;
          ans.push_back('B');
        }
      }
      if (s[i] == "AC") {
        if (a >= c) {
          a--;
          c++;
          ans.push_back('C');
        } else {
          a++;
          c--;
          ans.push_back('A');
        }
      }
      if (a < 0 || b < 0 || c < 0) {
        ok = false;
        break;
      }
    }
  } else {
    rep(i, n) {
      if (s[i] == "AB") {
        if (a == 1 && b == 1 && i != n - 1) {
          if (s[i + 1] == "AB") {
            a++;
            b--;
            ans.push_back('A');
          }
          if (s[i + 1] == "AC") {
            a++;
            b--;
            ans.push_back('A');
          }
          if (s[i + 1] == "BC") {
            b++;
            a--;
            ans.push_back('B');
          }
        } else {
          if (a < b) {
            a++;
            b--;
            ans.push_back('A');
          } else {
            b++;
            a--;
            ans.push_back('B');
          }
        }
      }
      if (s[i] == "AC") {
        if (a == 1 && c == 1 && i != n - 1) {
          if (s[i + 1] == "AB") {
            a++;
            c--;
            ans.push_back('A');
          }
          if (s[i + 1] == "AC") {
            a++;
            c--;
            ans.push_back('A');
          }
          if (s[i + 1] == "BC") {
            c++;
            a--;
            ans.push_back('C');
          }
        } else {
          if (a < c) {
            a++;
            c--;
            ans.push_back('A');
          } else {
            c++;
            a--;
            ans.push_back('C');
          }
        }
      }
      if (s[i] == "BC") {
        if (c == 1 && b == 1 && i != n - 1) {
          if (s[i + 1] == "AB") {
            b++;
            c--;
            ans.push_back('B');
          }
          if (s[i + 1] == "AC") {
            c++;
            b--;
            ans.push_back('C');
          }
          if (s[i + 1] == "BC") {
            b++;
            c--;
            ans.push_back('B');
          }
        } else {
          if (c < b) {
            c++;
            b--;
            ans.push_back('C');
          } else {
            b++;
            c--;
            ans.push_back('B');
          }
        }
      }
      if (a < 0 || b < 0 || c < 0) {
        ok = false;
        break;
      }
    }
  }
  if (ok) {
    cout << "Yes" << endl;
    rep(i, n) cout << ans[i] << endl;
  } else {
    cout << "No" << endl;
  }
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, n) for (int i = 1; i < (int)(n); i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define vout(x) rep(i, x.size()) cout << x[i] << " "
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;
using vint = vector<int>;
using vvint = vector<vector<int>>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<int, int>;
const int inf = 1e9;
const ll inf_l = 1e18;
const int MAX = 1e5;

int main() {
  int n, a, b, c;
  cin >> n >> a >> b >> c;
  vector<string> s(n);
  vector<char> ans;
  bool ok = true;
  rep(i, n) cin >> s[i];
  if (a >= 1 && b >= 1 && c >= 1) {
    rep(i, n) {
      if (s[i] == "AB") {
        if (a < b) {
          a++;
          b--;
          ans.push_back('A');
        } else {
          a--;
          b++;
          ans.push_back('B');
        }
      }
      if (s[i] == "AC") {
        if (a < c) {
          a++;
          c--;
          ans.push_back('A');
        } else {
          a--;
          c++;
          ans.push_back('C');
        }
      }
      if (s[i] == "BC") {
        if (b < c) {
          b++;
          c--;
          ans.push_back('B');
        } else {
          b--;
          c++;
          ans.push_back('C');
        }
      }
    }
  }
  if (a + b + c == 0)
    ok = false;
  if (a + b + c == 1) {
    rep(i, n) {
      if (s[i] == "AB") {
        if (a == 0 && b == 0) {
          ok = false;
          break;
        }
        if (a < b) {
          a++;
          b--;
          ans.push_back('A');
        } else {
          b++;
          a--;
          ans.push_back('B');
        }
      }
      if (s[i] == "AC") {
        if (a == 0 && c == 0) {
          ok = false;
          break;
        }
        if (a < c) {
          a++;
          c--;
          ans.push_back('A');
        } else {
          c++;
          a--;
          ans.push_back('C');
        }
      }
      if (s[i] == "BC") {
        if (c == 0 && b == 0) {
          ok = false;
          break;
        }
        if (c < b) {
          c++;
          b--;
          ans.push_back('C');
        } else {
          b++;
          c--;
          ans.push_back('B');
        }
      }
    }
  }
  if (a + b + c == 2) {
    rep(i, n) {
      if (s[i] == "AB") {
        if (a == 0 && b == 0) {
          ok = false;
          break;
        }
        if (a < b) {
          a++;
          b--;
          ans.push_back('A');
        } else {
          b++;
          a--;
          ans.push_back('B');
        }
      }
      if (s[i] == "AC") {
        if (a == 0 && c == 0) {
          ok = false;
          break;
        }
        if (a < c) {
          a++;
          c--;
          ans.push_back('A');
        } else {
          c++;
          a--;
          ans.push_back('C');
        }
      }
      if (s[i] == "BC") {
        if (c == 0 && b == 0) {
          ok = false;
          break;
        }
        if (c < b) {
          c++;
          b--;
          ans.push_back('C');
        } else {
          b++;
          c--;
          ans.push_back('B');
        }
      }
    }
  }
  if (ok) {
    cout << "Yes" << endl;
    rep(i, n) cout << ans[i] << endl;
  } else {
    cout << "No" << endl;
  }
}