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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
using less_set = tree<int, null_type, less<int>, rb_tree_tag,
                      tree_order_statistics_node_update>;
using great_set = tree<int, null_type, greater<int>, rb_tree_tag,
                       tree_order_statistics_node_update>;
#define FIO                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);
const int N = 2e3 + 69;
#define ALL(x) begin(x), end(x)
#define sz(x) (ll) x.size()
const int OO = 0x3f3f3f3f;
const int mod = 1e9 + 7;

ll n, a, b, c;
ll mem[100069][4][4][4];
string arr[100069];
int solve(ll idx, ll an, ll bn, ll cn) {
  ll op1, op2;
  if (idx == n) {
    if (an >= 0 && bn >= 0 && cn >= 0)
      return 1;
    return 0;
  }

  if (mem[idx][an][bn][cn] != -1)
    return mem[idx][an][bn][cn];
  op1 = op2 = 0;
  if (arr[idx] == "AB") {
    if (bn > 0)
      op1 = solve(idx + 1, an + 1, bn - 1, cn);
    if (an > 0)
      op2 = solve(idx + 1, an - 1, bn + 1, cn);
  } else if (arr[idx] == "AC") {
    if (cn > 0)
      op1 = solve(idx + 1, an + 1, bn, cn - 1);
    if (an > 0)
      op2 = solve(idx + 1, an - 1, bn, cn + 1);
  } else {
    if (cn > 0)
      op1 = solve(idx + 1, an, bn + 1, cn - 1);
    if (bn > 0)
      op2 = solve(idx + 1, an, bn - 1, cn + 1);
  }

  return mem[idx][an][bn][cn] = max(op1, op2);
}
void print(ll idx, ll an, ll bn, ll cn) {
  ll op1, op2;
  if (idx == n)
    return;

  op1 = op2 = 0;
  if (arr[idx] == "AB") {
    if (bn > 0)
      op1 = solve(idx + 1, an + 1, bn - 1, cn);
    if (an > 0)
      op2 = solve(idx + 1, an - 1, bn + 1, cn);
    if (op1) {
      cout << "A\\n";
      print(idx + 1, an + 1, bn - 1, cn);
      return;
    } else {
      cout << "B\\n";
      print(idx + 1, an - 1, bn + 1, cn);
      return;
    }
  } else if (arr[idx] == "AC") {
    if (cn > 0)
      op1 = solve(idx + 1, an + 1, bn, cn - 1);
    if (an > 0)
      op2 = solve(idx + 1, an - 1, bn, cn + 1);
    if (op1) {
      cout << "A\\n";
      print(idx + 1, an + 1, bn, cn - 1);
      return;
    } else {
      cout << "C\\n";
      print(idx + 1, an - 1, bn, cn + 1);
      return;
    }
  } else {
    if (cn > 0)
      op1 = solve(idx + 1, an, bn + 1, cn - 1);
    if (bn > 0)
      op2 = solve(idx + 1, an, bn - 1, cn + 1);
    if (op1) {
      cout << "B\\n";
      print(idx + 1, an, bn + 1, cn - 1);
      return;
    } else {
      cout << "C\\n";
      print(idx + 1, an, bn - 1, cn + 1);
      return;
    }
  }
}

int main() {
  FIO memset(mem, -1, sizeof mem);
  cin >> n >> a >> b >> c;
  for (int i = 0; i < n; i++)
    cin >> arr[i];

  if (a + b + c > 2) {
    if ((a && b) || (a && c) || (b && c) ||
        (a && (arr[0] == "AB" || arr[0] == "AC")) ||
        (b && (arr[0] == "AB" || arr[0] == "BC")) ||
        (c && (arr[0] == "BC" || arr[0] == "AC"))) {

      cout << "Yes\\n";
      for (int i = 0; i < n; i++) {
        if (arr[i] == "AB") {
          if (a >= b) {
            cout << "B\\n";
            a--;
            b++;
          } else {
            cout << "A\\n";
            b--;
            a++;
          }
        } else if (arr[i] == "AC") {
          if (a >= c) {
            cout << "C\\n";
            a--;
            c++;
          } else {
            cout << "A\\n";
            c--;
            a++;
          }
        } else {
          if (b >= c) {
            cout << "C\\n";
            b--;
            c++;
          } else {
            cout << "B\\n";
            c--;
            b++;
          }
        }
      }
    } else
      cout << "No\\n";
    return 0;
  }
  if (solve(0, a, b, c)) {
    cout << "Yes\\n";
    print(0, a, b, c);
  } else
    cout << "No\\n";

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
using less_set = tree<int, null_type, less<int>, rb_tree_tag,
                      tree_order_statistics_node_update>;
using great_set = tree<int, null_type, greater<int>, rb_tree_tag,
                       tree_order_statistics_node_update>;
#define FIO                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);
const int N = 2e3 + 69;
#define ALL(x) begin(x), end(x)
#define sz(x) (ll) x.size()
const int OO = 0x3f3f3f3f;
const int mod = 1e9 + 7;

ll n, a, b, c;
ll mem[100069][4][4][4];
string arr[100069];
int solve(ll idx, ll an, ll bn, ll cn) {
  ll op1, op2;
  if (idx == n) {
    if (an >= 0 && bn >= 0 && cn >= 0)
      return 1;
    return 0;
  }

  if (mem[idx][an][bn][cn] != -1)
    return mem[idx][an][bn][cn];
  op1 = op2 = 0;
  if (arr[idx] == "AB") {
    if (bn > 0)
      op1 = solve(idx + 1, an + 1, bn - 1, cn);
    if (an > 0)
      op2 = solve(idx + 1, an - 1, bn + 1, cn);
  } else if (arr[idx] == "AC") {
    if (cn > 0)
      op1 = solve(idx + 1, an + 1, bn, cn - 1);
    if (an > 0)
      op2 = solve(idx + 1, an - 1, bn, cn + 1);
  } else {
    if (cn > 0)
      op1 = solve(idx + 1, an, bn + 1, cn - 1);
    if (bn > 0)
      op2 = solve(idx + 1, an, bn - 1, cn + 1);
  }

  return mem[idx][an][bn][cn] = max(op1, op2);
}
void print(ll idx, ll an, ll bn, ll cn) {
  ll op1, op2;
  if (idx == n)
    return;

  op1 = op2 = 0;
  if (arr[idx] == "AB") {
    op1 = solve(idx + 1, an + 1, bn - 1, cn);
    op2 = solve(idx + 1, an - 1, bn + 1, cn);
    if (op1) {
      cout << "A\\n";
      print(idx + 1, an + 1, bn - 1, cn);
      return;
    } else {
      cout << "B\\n";
      print(idx + 1, an + 1, bn - 1, cn);
      return;
    }
  } else if (arr[idx] == "AC") {
    op1 = solve(idx + 1, an + 1, bn, cn - 1);
    op2 = solve(idx + 1, an - 1, bn, cn + 1);
    if (op1) {
      cout << "A\\n";
      print(idx + 1, an + 1, bn, cn - 1);
      return;
    } else {
      cout << "C\\n";
      print(idx + 1, an - 1, bn, cn + 1);
      return;
    }
  } else {
    op1 = solve(idx + 1, an, bn + 1, cn - 1);
    op2 = solve(idx + 1, an, bn - 1, cn + 1);
    if (op1) {
      cout << "B\\n";
      print(idx + 1, an, bn + 1, cn - 1);
      return;
    } else {
      cout << "C\\n";
      print(idx + 1, an, bn - 1, cn + 1);
      return;
    }
  }
}

int main() {
  FIO memset(mem, -1, sizeof mem);
  cin >> n >> a >> b >> c;
  for (int i = 0; i < n; i++)
    cin >> arr[i];

  if (a + b + c > 2) {
    if ((a && b) || (a && c) || (b && c) ||
        (a && (arr[0] == "AB" || arr[0] == "AC")) ||
        (b && (arr[0] == "AB" || arr[0] == "BC")) ||
        (c && (arr[0] == "BC" || arr[0] == "AC"))) {

      cout << "Yes\\n";
      for (int i = 0; i < n; i++) {
        if (arr[i] == "AB") {
          if (a >= b) {
            cout << "B\\n";
            a--;
            b++;
          } else {
            cout << "A\\n";
            b--;
            a++;
          }
        } else if (arr[i] == "AC") {
          if (a >= c) {
            cout << "C\\n";
            a--;
            c++;
          } else {
            cout << "A\\n";
            c--;
            a++;
          }
        } else {
          if (b >= c) {
            cout << "C\\n";
            b--;
            c++;
          } else {
            cout << "B\\n";
            c--;
            b++;
          }
        }
      }
    } else
      cout << "No\\n";
    return 0;
  }
  if (solve(0, a, b, c)) {
    cout << "Yes\\n";
    print(0, a, b, c);
  } else
    cout << "No\\n";

  return 0;
}