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++)
using ll = long long;

int main() {
  int n;
  ll sum;
  vector<ll> abc(3);
  cin >> n >> abc[0] >> abc[1] >> abc[2];
  sum = abc[0] + abc[1] + abc[2];

  vector<string> s(n);
  bool can = true;
  vector<char> res(n);
  rep(i, n) { cin >> s[i]; }

  if (sum != 1 && sum != 2) {
    rep(i, n) {
      int m = s[i][0] - 'A';
      int l = s[i][1] - 'A';

      if (abc[m] == 0 && abc[l] == 0) {
        can = false;
        break;
      }
      if (abc[m] == 0) {
        res[i] = s[i][0];
        abc[m] += 1, abc[l] -= 1;
      } else if (abc[l] == 0) {
        res[i] = s[i][1];
        abc[m] -= 1, abc[l] += 1;
      } else if (abc[m] == 1) {
        res[i] = s[i][0];
        abc[m] += 1, abc[l] -= 1;
      } else if (abc[l] == 1) {
        res[i] = s[i][1];
        abc[m] -= 1, abc[l] += 1;
      } else {
        res[i] = s[i][0];
        abc[m] += 1, abc[l] -= 1;
      }
    }
  }
  if (sum == 1 || sum == 2) {
    rep(i, n) {
      int m = s[i][0] - 'A';
      int l = s[i][1] - 'A';
      if (abc[m] == 0 && abc[l] == 0) {
        can = false;
        break;
      }
      if (abc[l] == 0) {
        res[i] = s[i][1];
        abc[m] -= 1, abc[l] += 1;
      } else if (abc[m] == 0) {
        res[i] = s[i][0];
        abc[m] += 1, abc[l] -= 1;
      } else {
        if (i != n - 1) {
          if (s[i] == s[i + 1]) {
            res[i] = s[i][0];
            abc[m] += 1, abc[l] -= 1;
          } else {
            int m_1 = s[i + 1][0] - 'A';
            int n_1 = s[i + 1][1] - 'A';
            int d = m + l + m_1 + n_1 - 3;
            res[i] = char(d + 'A');
            abc[d] += 1, abc[m + l - d] -= 1;
          }
        }
        if (i == n - 1) {
          res[i] = s[i][0];
          abc[m] += 1, abc[l] -= 1;
        }
      }
    }
  }

  if (can) {
    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;
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;

int main() {
  int n, sum;
  vector<int> abc(3);
  cin >> n >> abc[0] >> abc[1] >> abc[2];
  sum = abc[0] + abc[1] + abc[2];

  vector<string> s(n);
  bool can = true;
  vector<char> res(n);
  rep(i, n) { cin >> s[i]; }

  if (sum != 1 && sum != 2) {
    rep(i, n) {
      int m = s[i][0] - 'A';
      int n = s[i][1] - 'A';

      if (abc[m] == 0 && abc[n] == 0) {
        can = false;
        break;
      }
      if (abc[m] == 0) {
        res[i] = s[i][0];
        abc[m] += 1, abc[n] -= 1;
      } else if (abc[n] == 0) {
        res[i] = s[i][1];
        abc[m] -= 1, abc[n] += 1;
      } else if (abc[m] == 1) {
        res[i] = s[i][0];
        abc[m] += 1, abc[n] -= 1;
      } else if (abc[n] == 1) {
        res[i] = s[i][1];
        abc[m] -= 1, abc[n] += 1;
      } else {
        if (abc[m] > abc[n]) {
          res[i] = s[i][1];
          abc[m] -= 1, abc[n] += 1;
        }
        if (abc[m] < abc[n]) {
          res[i] = s[i][0];
          abc[m] += 1, abc[n] -= 1;
        }
      }
    }
  }
  if (sum == 1 || sum == 2) {
    rep(i, n) {
      int m = s[i][0] - 'A';
      int n = s[i][1] - 'A';
      if (abc[m] == 0 && abc[n] == 0) {
        can = false;
        break;
      }
      if (abc[n] == 0) {
        res[i] = s[i][1];
        abc[m] -= 1, abc[n] += 1;
      } else if (abc[m] == 0) {
        res[i] = s[i][0];
        abc[m] += 1, abc[n] -= 1;
      } else {
        if (i != n - 1) {
          if (s[i] == s[i + 1]) {
            res[i] = s[i][0];
            abc[m] += 1, abc[n] -= 1;
          } else {
            int m_1 = s[i + 1][0] - 'A';
            int n_1 = s[i + 1][1] - 'A';
            int d = m + n + m_1 + n_1 - ('A' + 'B' + 'C') - 'A';
            res[i] = char(d + 'A');
            abc[d] += 1, abc[m + n - d] -= 1;
          }
        }
        if (i == n - 1) {
          res[i] = s[i][0];
          abc[m] += 1, abc[n] -= 1;
        }
      }
    }
  }

  if (can) {
    cout << "Yes" << endl;
    rep(i, n) { cout << res[i] << endl; }
  } else {
    cout << "No" << endl;
  }

  return 0;
}