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

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define per(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define pb push_back
#define mp make_pair
#define bg begin()
#define en end()
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define sz(v) (int)(v).size()

using namespace std;

typedef long long ll;
typedef long double ld;

using P = pair<int, int>;
template <typename T> struct Edge {
  int u, v;
  T cost;
  Edge(int u, int v, T c) : u(u), v(v), cost(c) {}
  bool operator<(const Edge &e) const { return cost < e.cost; }
};

static const long long MOD = 1000000007;
static const long long LINF = (ll)(1e18 + 99);
static const int INF = 1e9 + 99;

string ans;
vector<string> cmds;
map<char, ll> num;

int main(void) {
  int N;
  cin >> N >> num['A'] >> num['B'] >> num['C'];
  rep(i, 0, N) {
    string cmd;
    cin >> cmd;
    cmds.pb(cmd);
  }

  if (num['A'] + num['B'] + num['C'] != 2LL) {
    rep(i, 0, N) {

      string cmd = cmds[i];
      char X = cmd[0], Y = cmd[1];
      if (num[X] >= num[Y]) {
        num[X]--;
        num[Y]++;
        ans += Y;
      } else {
        num[Y]--;
        num[X]++;
        ans += X;
      }
      if (num['A'] < 0 || num['B'] < 0 || num['C'] < 0) {
        puts("No");
        return 0;
      }
      // printf("%lld %lld %lld\\n", num['A'], num['B'], num['C']);
    }

  } else {
    vector<int> idxs;
    string notchoice;
    rep(i, 0, N) {
      string cmd = cmds[i];
      char X = cmd[0], Y = cmd[1];
      if (num[X] > num[Y]) {
        num[X]--;
        num[Y]++;
        ans += Y;
        notchoice += X;
      } else if (num[X] < num[Y]) {
        num[Y]--;
        num[X]++;
        ans += X;
        notchoice += Y;
      } else if (num[X] == 1 && num[Y] == 1) {
        num[X]++;
        num[Y]--;
        ans += X;
        notchoice += Y;
        idxs.pb(i);
      } else {
        if (sz(idxs) == 0) {
          puts("No");
          return 0;
        }
        int idx = idxs[sz(idxs) - 1];
        idxs.pop_back();

        num[notchoice[idx]] = 2;
        num[ans[idx]] = 0;
        swap(ans[idx], notchoice[idx]);

        if (ans[idx] == X) {
          num[Y]++;
          num[X]--;
          ans += Y;
          notchoice += X;
        } else {
          num[X]++;
          num[Y]--;
          ans += X;
          notchoice += Y;
        }
      }
      // printf("%lld %lld %lld\\n", num['A'], num['B'], num['C']);
    }
  }

  puts("Yes");
  rep(i, 0, N) cout << ans[i] << endl;
  return 0;
}


Error: -8

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define per(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define pb push_back
#define mp make_pair
#define bg begin()
#define en end()
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define sz(v) (int)(v).size()

using namespace std;

typedef long long ll;
typedef long double ld;

using P = pair<int, int>;
template <typename T> struct Edge {
  int u, v;
  T cost;
  Edge(int u, int v, T c) : u(u), v(v), cost(c) {}
  bool operator<(const Edge &e) const { return cost < e.cost; }
};

static const long long MOD = 1000000007;
static const long long LINF = (ll)(1e18 + 99);
static const int INF = 1e9 + 99;

string ans;
vector<string> cmds;
map<char, ll> num;

int main(void) {
  int N;
  cin >> N >> num['A'] >> num['B'] >> num['C'];
  rep(i, 0, N) {
    string cmd;
    cin >> cmd;
    cmds.pb(cmd);
  }

  if (num['A'] + num['B'] + num['C'] != 2LL) {
    rep(i, 0, N) {
      int x = 0;
      int y = 5 / x;
      cout << y << endl;

      string cmd = cmds[i];
      char X = cmd[0], Y = cmd[1];
      if (num[X] >= num[Y]) {
        num[X]--;
        num[Y]++;
        ans += Y;
      } else {
        num[Y]--;
        num[X]++;
        ans += X;
      }
      if (num['A'] < 0 || num['B'] < 0 || num['C'] < 0) {
        puts("No");
        return 0;
      }
      // printf("%lld %lld %lld\\n", num['A'], num['B'], num['C']);
    }

  } else {
    vector<int> idxs;
    string notchoice;
    rep(i, 0, N) {
      string cmd = cmds[i];
      char X = cmd[0], Y = cmd[1];
      if (num[X] > num[Y]) {
        num[X]--;
        num[Y]++;
        ans += Y;
        notchoice += X;
      } else if (num[X] < num[Y]) {
        num[Y]--;
        num[X]++;
        ans += X;
        notchoice += Y;
      } else if (num[X] == 1 && num[Y] == 1) {
        num[X]++;
        num[Y]--;
        ans += X;
        notchoice += Y;
        idxs.pb(i);
      } else {
        if (sz(idxs) == 0) {
          puts("No");
          return 0;
        }
        int idx = idxs[sz(idxs) - 1];
        idxs.pop_back();
        swap(ans[idx], notchoice[idx]);
        num[notchoice[idx]]--;

        if (ans[idx] == X) {
          num[Y]++;
          ans += Y;
          notchoice += X;
        } else {
          num[X]++;
          ans += X;
          notchoice += Y;
        }
      }
      // printf("%lld %lld %lld\\n", num['A'], num['B'], num['C']);
    }
  }

  puts("Yes");
  rep(i, 0, N) cout << ans[i] << endl;
  return 0;
}