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

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;
const int maxn = 1e5 + 10;
int n, a, b, c, d;
struct da {
  int a, b, c;
  char d;
} pre[maxn][5][5][5];
bool dp[maxn][5][5][5];
char s[5], ans[maxn];
int main() {
  scanf("%d%d%d%d%d", &n, &a, &b, &c, &d);
  dp[0][min(a, 4)][min(b, 4)][min(c, 4)] = 1;
  for (int i = 1; i <= n; i++) {
    scanf("%s", s + 1);
    if (s[1] == 'A' && s[2] == 'B') {
      for (int j = 0; j <= 4; j++)
        for (int k = 0; k <= 4; k++)
          for (int l = 0; l <= 4; l++)
            if (dp[i - 1][j][k][l]) {
              if (j >= 1)
                dp[i][j - 1][min(k + 1, 4)][l] = 1,
                                        pre[i][j - 1][min(k + 1, 4)][l] = {
                                            j, k, l, 'B'};
              if (k >= 1)
                dp[i][min(j + 1, 4)][k - 1][l] = 1,
                                         pre[i][min(j + 1, 4)][k - 1][l] = {
                                             j, k, l, 'A'};
            }
    }
    if (s[1] == 'A' && s[2] == 'C') {
      for (int j = 0; j <= 4; j++)
        for (int k = 0; k <= 4; k++)
          for (int l = 0; l <= 4; l++)
            if (dp[i - 1][j][k][l]) {
              if (j >= 1)
                dp[i][j - 1][k][min(l + 1, 4)] = 1,
                                           pre[i][j - 1][k][min(l + 1, 4)] = {
                                               j, k, l, 'C'};
              if (l >= 1)
                dp[i][min(j + 1, 4)][k][l - 1] = 1,
                                            pre[i][min(j + 1, 4)][k][l - 1] = {
                                                j, k, l, 'A'};
            }
    }
    if (s[1] == 'B' && s[2] == 'C') {
      for (int j = 0; j <= 4; j++)
        for (int k = 0; k <= 4; k++)
          for (int l = 0; l <= 4; l++)
            if (dp[i - 1][j][k][l]) {
              if (k >= 1)
                dp[i][j][k - 1][min(l + 1, 4)] = 1,
                                           pre[i][j][k - 1][min(l + 1, 4)] = {
                                               j, k, l, 'C'};
              if (l >= 1)
                dp[i][j][min(k + 1, 4)][l - 1] = 1,
                                            pre[i][j][min(k + 1, 4)][l - 1] = {
                                                j, k, l, 'B'};
            }
    }
  }
  bool ok = 0;
  da now = {0, 0, 0, 'A'};
  for (int i = 0; i <= 4; i++)
    for (int j = 0; j <= 4; j++)
      for (int k = 0; k <= 4; k++)
        if (dp[n][i][j][k]) {
          ok = 1;
          now = {i, j, k, 'A'};
        }
  if (!ok) {
    printf("No\\n");
    return 0;
  }
  printf("Yes\\n");
  for (int i = n; i >= 1; i--)
    ans[i] = pre[i][now.a][now.b][now.c].d, now = pre[i][now.a][now.b][now.c];
  for (int i = 1; i <= n; i++)
    printf("%c\\n", ans[i]);
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
const int maxn = 1e6 + 10;
int n, ans[maxn], a, b, c, id[5][maxn];
char s[maxn][5];
map<char, int> num, sum;
inline bool ok() {
  if (num['A'] < 0 || num['B'] < 0 || num['C'] < 0)
    return 0;
  return 1;
}
int main() {
  scanf("%d%d%d%d", &n, &a, &b, &c);
  num['A'] = a;
  num['B'] = b;
  num['C'] = c;
  sum['A'] = 0;
  sum['B'] = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%s", s[i] + 1);
    if (s[i][2] == 'C') {
      if (num[s[i][1]] == 0)
        num[s[i][1]]++, num[s[i][2]]--, ans[i] = 0;
      else
        num[s[i][1]]--, num[s[i][2]]++, sum[s[i][1]]++,
            id[s[i][1] - 'A'][sum[s[i][1]]] = i, ans[i] = 1;
      if (!ok()) {
        printf("No\\n");
        return 0;
      }
    } else {
      if (num[s[i][1]] == 0 && num[s[i][2]] == 0) {
        if (sum['A'] && sum['B'] && num['C'] >= 2) {
          ans[id['A'][sum['A']]] ^= 1;
          sum['A']--;
          ans[id['B'][sum['B']]] ^= 1;
          sum['B']--;
          num['C'] -= 2;
          num['A']++;
          num['B']++;
        } else {
          printf("No\\n");
          return 0;
        }
      }
      if (num[s[i][1]] > num[s[i][2]])
        num[s[i][1]]--, num[s[i][2]]++, ans[i] = 1;
      else
        num[s[i][1]]++, num[s[i][2]]--, ans[i] = 0;
      // printf("%d %d %d\\n",num['A'],num['B'],num['C']);
      if (!ok()) {
        printf("No\\n");
        return 0;
      }
    }
  }
  printf("Yes\\n");
  for (int i = 1; i <= n; i++)
    printf("%c\\n", s[i][ans[i] + 1]);
  return 0;
}