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

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

typedef long long LL;

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define ALL(x) (x).begin(), (x).end()
const int IINF = 1e9;
const LL LINF = 1e18;
const LL MOD = 1e9 + 7;

int main() {
  LL N;
  map<char, LL> dic;
  cin >> N >> dic['A'] >> dic['B'] >> dic['C'];
  LL s = dic['A'] + dic['B'] + dic['C'];
  vector<string> query(N);
  vector<string> res(N, "");

  REP(i, N) { cin >> query[i]; }

  bool yn = true;

  REP(i, N) {
    char x, y;
    tie(x, y) = tie(query[i][0], query[i][1]);
    if (dic[x] == 0 && dic[y] == 0) {
      yn = false;
      break;
    }
    if (dic[x] * dic[y] == 0 && 0 < dic[x]) {
      dic[x]--;
      dic[y]++;
      res[i] = y;
      continue;
    }
    if (dic[x] * dic[y] == 0 && 0 < dic[y]) {
      dic[x]++;
      dic[y]--;
      res[i] = x;
      continue;
    }
    if (s == 2 && dic[x] == dic[y] && i < N - 1) {
      auto n = query[i + 1];
      if (n.find(x) != string::npos) {
        dic[x]++;
        dic[y]--;
        res[i] = x;
        continue;
      }
      if (n.find(y) != string::npos) {
        dic[x]--;
        dic[y]++;
        res[i] = y;
        continue;
      }
    }
    if (dic[x] <= dic[y]) {
      dic[x]++;
      dic[y]--;
      res[i] = x;
      continue;
    }
    if (dic[y] < dic[x]) {
      dic[x]--;
      dic[y]++;
      res[i] = y;
      continue;
    }
  }

  assert((dic[query[0][0]] == 0 && dic[query[0][1]] == 0) || s < 2 ||
         (2 <= s && yn));

  if (yn) {
    cout << "Yes" << endl;
    REP(i, N) { cout << res[i] << endl; }
  } else {
    cout << "No" << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

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

typedef long long LL;

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define ALL(x) (x).begin(), (x).end()
const int IINF = 1e9;
const LL LINF = 1e18;
const LL MOD = 1e9 + 7;

int main() {
  LL N;
  map<char, LL> dic;
  cin >> N >> dic['A'] >> dic['B'] >> dic['C'];
  LL s = dic['A'] + dic['B'] + dic['C'];
  vector<string> query(N);
  vector<string> res(N, "");

  REP(i, N) { cin >> query[i]; }

  bool yn = true;

  REP(i, N) {
    char x, y;
    tie(x, y) = tie(query[i][0], query[i][1]);
    if (dic[x] == 0 && dic[y] == 0) {
      yn = false;
      break;
    }
    if (dic[x] * dic[y] == 0 && 0 < dic[x]) {
      dic[x]--;
      dic[y]++;
      res[i] = y;
      continue;
    }
    if (dic[x] * dic[y] == 0 && 0 < dic[y]) {
      dic[x]++;
      dic[y]--;
      res[i] = x;
      continue;
    }
    if (s == 2 && dic[x] == dic[y] && i < N - 1) {
      auto n = query[i + 1];
      if (n.find(x) != string::npos) {
        dic[x]--;
        dic[y]++;
        res[i] = y;
        continue;
      }
      if (n.find(y) != string::npos) {
        dic[x]++;
        dic[y]--;
        res[i] = x;
        continue;
      }
    }
    if (dic[x] <= dic[y]) {
      dic[x]++;
      dic[y]--;
      res[i] = x;
      continue;
    }
    if (dic[y] < dic[x]) {
      dic[x]--;
      dic[y]++;
      res[i] = y;
      continue;
    }
  }

  assert((dic[query[0][0]] == 0 && dic[query[0][1]] == 0) || s < 2 ||
         (2 <= s && yn));

  if (yn) {
    cout << "Yes" << endl;
    REP(i, N) { cout << res[i] << endl; }
  } else {
    cout << "No" << endl;
  }
  return 0;
}