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

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

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define ddrep(i, n) for (int i = n; i > 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define ssrep(i, s, t) for (int i = s; i <= t; ++i)
#define chmax(x, y) (x = max(x, y))
#define chmin(x, y) (x = min(x, y))

using ll = long long;
using pi = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using ld = long double;

long int M = 1e+9 + 7;
int inf = 1e+9;
int main(void) {
  int n;
  map<char, int> abc;
  vector<char> ans;
  cin >> n >> abc['A'] >> abc['B'] >> abc['C'];
  vector<string> s(n);
  rep(i, n) { cin >> s[i]; }
  ll sum = abc['A'] + abc['B'] + abc['C'];
  if (sum == 0) {
    cout << "No" << endl;
    return 0;
  }
  rep(i, n) {
    if (abc[s[i][0]] == 0 && abc[s[i][1]] == 0) {
      cout << "No" << endl;
      return 0;
    }
    if (i + 1 < n && abc[s[i][0]] == 1 && abc[s[i][1]] == 1) {
      if (s[i][0] == s[i + 1][0] || s[i][0] == s[i + 1][1]) {
        ans.push_back(s[i][0]);
        abc[s[i][0]]++;
        abc[s[i][1]]--;
      } else {
        ans.push_back(s[i][1]);
        abc[s[i][0]]--;
        abc[s[i][1]]++;
      }
    } else if (abc[s[i][0]] < abc[s[i][1]]) {
      ans.push_back(s[i][0]);
      abc[s[i][0]]++;
      abc[s[i][1]]--;
    } else {
      ans.push_back(s[i][1]);
      abc[s[i][0]]--;
      abc[s[i][1]]++;
    }
  }

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

Error: 0

Fixed submission:

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

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define ddrep(i, n) for (int i = n; i > 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define ssrep(i, s, t) for (int i = s; i <= t; ++i)
#define chmax(x, y) (x = max(x, y))
#define chmin(x, y) (x = min(x, y))

using ll = long long;
using pi = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using ld = long double;

long int M = 1e+9 + 7;
int inf = 1e+9;
int main(void) {
  int n;
  map<char, int> abc;
  vector<char> ans;
  cin >> n >> abc['A'] >> abc['B'] >> abc['C'];
  vector<string> s(n);
  rep(i, n) { cin >> s[i]; }
  ll sum = abc['A'] + abc['B'] + abc['C'];
  if (sum == 0) {
    cout << "No" << endl;
  }

  if (sum == 1) {
    rep(i, n) {
      if (abc[s[i][0]] == 1) {
        abc[s[i][0]] = 0;
        abc[s[i][1]] = 1;
        ans.push_back(s[i][1]);
      } else if (abc[s[i][1]] == 1) {
        abc[s[i][0]] = 1;
        abc[s[i][1]] = 0;
        ans.push_back(s[i][0]);
      } else {
        cout << "No" << endl;
        return 0;
      }
    }
  }

  if (sum >= 2) {
    if (abc[s[0][0]] == 0 && abc[s[0][1]] == 0) {
      cout << "No" << endl;
      return 0;
    }

    rep(i, n) {
      if (i + 1 < n && abc[s[i][0]] == 1 && abc[s[i][1]] == 1) {
        if (s[i][0] == s[i + 1][0] || s[i][0] == s[i + 1][1]) {
          ans.push_back(s[i][0]);
          abc[s[i][0]]++;
          abc[s[i][1]]--;
        } else {
          ans.push_back(s[i][1]);
          abc[s[i][0]]--;
          abc[s[i][1]]++;
        }
      } else if (abc[s[i][0]] < abc[s[i][1]]) {
        ans.push_back(s[i][0]);
        abc[s[i][0]]++;
        abc[s[i][1]]--;
      } else {
        ans.push_back(s[i][1]);
        abc[s[i][0]]--;
        abc[s[i][1]]++;
      }
    }
  }

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