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

#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)
#define ALL(c) (c).begin(), (c).end()

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<long double> VD;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<VD> VVD;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;

template <typename T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}
template <typename T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}

int in() {
  int x;
  scanf("%d", &x);
  return x;
}
ll lin() {
  ll x;
  scanf("%lld", &x);
  return x;
}
#define INF 1LL << 60

int main() {
  int N, A, B, C;
  cin >> N >> A >> B >> C;
  vector<string> S(N);
  // vector< vector<int> > num(N + 1, vector<int>(3)); // num[i][j]:
  // i回目までのjの登場回数
  FOR(i, 1, N) {
    cin >> S[i - 1];
    // if(S[i][0] == 'A') num[i][0] = num[i-1][0] + 1;
    // else num[i][0] = num[i-1][0];
    // if(S[i][0] == 'B' || S[i][1] == 'B') num[i][1] = num[i-1][1] + 1;
    // else num[i][1] = num[i-1][1];
    // if(S[i][1] == 'C') num[i][2] = num[i-1][2] + 1;
    // else num[i][2] = num[i-1][2];
  }

  // FOR(i, 1, N) {
  //     cerr << num[i][0] << " " << num[i][1] << " " << num[i][2] << endl;
  //     if(A + B ) {
  //         cout << "No" << endl;
  //         return 0;
  //     }
  // }

  if (A + B + C == 0) {
    cout << "No" << endl;
    return 0;
  }

  // 可能な場合
  vector<char> ans;
  vector<int> cur(3);
  cur[0] = A;
  cur[1] = B;
  cur[2] = C;
  REP(i, N) {
    if (A + B + C == 2 && i < N - 1 && S[i] != S[i + 1] &&
        cur[S[i][0] - 'A'] == 1 && cur[S[i][1] - 'A'] == 1) {
      vector<int> now(3), next(3);
      for (char x : S[i])
        now[x - 'A']++;
      for (char x : S[i + 1])
        next[x - 'A']++;
      REP(i, 3) {
        if (now[i] == 1 && next[i] == 1) {
          cur[i]++;
          ans.push_back('A' + i);
        } else if (now[i] == 1) {
          cur[i]--;
        }
      }
    } else {
      if (S[i] == "AB")
        if (cur[0] > cur[1]) {
          ans.push_back('B');
          cur[0]--;
          cur[1]++;
        } else {
          ans.push_back('A');
          cur[0]++;
          cur[1]--;
        }
      else if (S[i] == "BC")
        if (cur[2] > cur[1]) {
          ans.push_back('B');
          cur[2]--;
          cur[1]++;
        } else {
          ans.push_back('C');
          cur[2]++;
          cur[1]--;
        }
      else if (cur[0] > cur[2]) {
        ans.push_back('C');
        cur[0]--;
        cur[2]++;
      } else {
        ans.push_back('A');
        cur[0]++;
        cur[2]--;
      }
    }

    if (cur[0] < 0 || cur[1] < 0 || cur[2] < 0) {
      cout << "No" << endl;
      return 0;
    }
  }

  cout << "Yes" << endl;
  for (char x : ans)
    cout << x << endl;

  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)
#define ALL(c) (c).begin(), (c).end()

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<long double> VD;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<VD> VVD;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;

template <typename T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}
template <typename T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}

int in() {
  int x;
  scanf("%d", &x);
  return x;
}
ll lin() {
  ll x;
  scanf("%lld", &x);
  return x;
}
#define INF 1LL << 60

int main() {
  int N, A, B, C;
  cin >> N >> A >> B >> C;
  vector<string> S(N);
  vector<vector<int>> num(N + 1,
                          vector<int>(3)); // num[i][j]: i回目までのjの登場回数
  FOR(i, 1, N) {
    cin >> S[i - 1];
    if (S[i][0] == 'A')
      num[i][0] = num[i - 1][0] + 1;
    else
      num[i][0] = num[i - 1][0];
    if (S[i][0] == 'B' || S[i][1] == 'B')
      num[i][1] = num[i - 1][1] + 1;
    else
      num[i][1] = num[i - 1][1];
    if (S[i][1] == 'C')
      num[i][2] = num[i - 1][2] + 1;
    else
      num[i][2] = num[i - 1][2];
  }

  // FOR(i, 1, N) {
  //     cerr << num[i][0] << " " << num[i][1] << " " << num[i][2] << endl;
  //     if(A + B ) {
  //         cout << "No" << endl;
  //         return 0;
  //     }
  // }

  // 可能な場合
  vector<char> ans;
  vector<int> cur(3);
  cur[0] = A;
  cur[1] = B;
  cur[2] = C;
  REP(i, N) {
    if (S[i] == "AB")
      if (cur[0] > cur[1]) {
        ans.push_back('B');
        cur[0]--;
        cur[1]++;
      } else {
        ans.push_back('A');
        cur[0]++;
        cur[1]--;
      }
    else if (S[i] == "BC")
      if (cur[2] > cur[1]) {
        ans.push_back('B');
        cur[2]--;
        cur[1]++;
      } else {
        ans.push_back('C');
        cur[2]++;
        cur[1]--;
      }
    else if (cur[0] > cur[2]) {
      ans.push_back('C');
      cur[0]--;
      cur[2]++;
    } else {
      ans.push_back('A');
      cur[0]++;
      cur[2]--;
    }

    if (cur[0] < 0 || cur[1] < 0 || cur[2] < 0) {
      cout << "No" << endl;
      return 0;
    }
  }

  cout << "Yes" << endl;
  for (char x : ans)
    cout << x << endl;

  return 0;
}