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

#include <bits/stdc++.h>
using namespace std;
#define all(container) (continer).begin(), (container).end()
#define sz(container) (int)(container).size();
#define rall(container) (container).rbegin(), (container).rend()
using ll = long long;
#ifdef LOCAL_DEFINE
#include "../../library/DEBUG.hpp"
#else
#define debug(...) 0
#endif

template <class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template <class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }

int main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int val[3], n;
  cin >> n >> val[0] >> val[1] >> val[2];
  vector<string> q(n);
  for (int i = 0; i < n; i++) {
    cin >> q[i];
  }
  bool ok = true;
  string ans = "";
  auto get = [&](int pos) {
    for (int i = 0; i < 2; i++) {
      for (int j = 0; j < 2; j++) {
        if (q[pos][i] == q[pos + 1][j]) {
          return i;
        }
      }
    }
    return -1;
  };
  for (int i = 0; i < (int)q.size(); i++) {
    if (!val[q[i][0] - 'A'] && !val[q[i][1] - 'A']) {
      ok = false;
      break;
    } else if (val[q[i][0] - 'A'] == 1 && val[q[i][1] - 'A'] == 1) {
      if (i < (int)q.size() - 1 && q[i + 1] != q[i]) {
        int pos = get(i);
        assert(pos != -1);
        ans += q[i][pos];
        val[q[i][pos] - 'A']++;
        --val[q[i][(pos + 1) % 2] - 'A'];
      } else {
        ans += q[i][0];
        ++val[q[i][0] - 'A'];
        --val[q[i][1] - 'A'];
      }
    } else {
      if (val[q[i][0] - 'A'] > val[q[i][1] - 'A']) {
        --val[q[i][0] - 'A'], ++val[q[i][1] - 'A'];
        ans += q[i][1];
      } else {
        --val[q[i][1] - 'A'], ++val[q[i][0] - 'A'];
        ans += q[i][0];
      }
    }
  }
  if (!ok) {
    cout << "No\\n";
  } else {
    cout << "Yes\\n";
    for (int i = 0; i < (int)ans.size(); i++) {
      cout << ans[i] << '\\n';
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

int main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll a, b, c, n;
  cin >> n >> a >> b >> c;
  vector<string> qq(n);
  for (int i = 0; i < n; i++)
    cin >> qq[i];
  vector<char> ans;
  bool ok = true;
  for (int i = 0; i < n; i++) {
    assert(a >= 0 && b >= 0 && c >= 0);
    if (qq[i] == "AB") {
      if (!a && !b) {
        ok = false;
        break;
      }
      if (a == 1 && b == 1) {
        if (i == n - 1) {
          ans.push_back('A');
          break;
        } else if (qq[i] == qq[i + 1]) {
          --b, ++a;
          ans.push_back('A');
        } else if (qq[i + 1] == "AC") {
          ++a, --b;
          ans.push_back('A');
        } else if (qq[i + 1] == "BC") {
          ++b, --a;
          ans.push_back('B');
        }
      } else {
        if (a <= b) {
          ++a, --b;
          ans.push_back('A');
        } else {
          --a, ++b;
          ans.push_back('B');
        }
      }
    } else if (qq[i] == "BC") {
      if (!c && !b) {
        ok = false;
        break;
      }
      if (c == 1 && b == 1) {
        if (i == n - 1) {
          ans.push_back('B');
          break;
        } else if (qq[i] == qq[i + 1]) {
          ++b, --c;
          ans.push_back('B');
        } else if (qq[i + 1] == "AC") {
          ++c, --b;
          ans.push_back('C');
        } else if (qq[i + 1] == "AB") {
          ++b, --a;
          ans.push_back('B');
        }
      } else {
        if (c <= b) {
          ++c, --b;
          ans.push_back('C');
        } else {
          --c, ++b;
          ans.push_back('B');
        }
      }
    } else if (qq[i] == "AC") {
      if (!a && !c) {
        ok = false;
        break;
      }
      if (a == 1 && c == 1) {
        if (i == n - 1) {
          ans.push_back('A');
          break;
        } else if (qq[i] == qq[i + 1]) {
          ++a, --c;
          ans.push_back('A');
        } else if (qq[i + 1] == "AB") {
          ++a, --b;
          ans.push_back('A');
        } else if (qq[i + 1] == "BC") {
          ++c, --a;
          ans.push_back('C');
        }
      } else {
        if (a <= c) {
          ++a, --c;
          ans.push_back('C');
        } else {
          --a, ++c;
          ans.push_back('C');
        }
      }
    }
  }
  if (ok) {
    cout << "Yes\\n";
    for (int i = 0; i < (int)ans.size(); i++)
      cout << ans[i] << '\\n';
  } else
    cout << "No\\n";
  return 0;
}
/*
    if A == B and A != 1
    unless both query vars are 1, all choices are optimal
    1 0 1
    AC --> (2 0 0) or (0 0 2)
    depends on the next query.
    AB
    AC

*/