Problem p02692 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<int, int> pa;

#define fst first
#define snd second
#define pb push_back

const int maxn = 300005, maxm = 106;
const double pi = acos(-1.0);
const ll mod = 1000000007;
const double eps = 1e-6;

int n, x[3];
bool fl = 1;
char c[maxn] = {0}, tp[maxn];
int ii[maxn][2];

void dfs(int aa, int bb, int cc, int dep) {
  int i, j, k, l;
  int tmp[3] = {aa, bb, cc};
  if (dep == n) {
    for (i = 0; i < n; i++)
      c[i] = tp[i];
    return;
  }
  if (c[0] != 0)
    return;
  if (tmp[ii[dep][0]] > tmp[ii[dep][1]]) {
    tp[dep] = ii[dep][1] + 'A';
    --tmp[ii[dep][0]];
    ++tmp[ii[dep][1]];
    dfs(tmp[0], tmp[1], tmp[2], dep + 1);
    ++tmp[ii[dep][0]];
    --tmp[ii[dep][1]];
  }
  if (c[0] != 0)
    return;
  if (tmp[ii[dep][1]] > tmp[ii[dep][0]]) {
    tp[dep] = ii[dep][0] + 'A';
    --tmp[ii[dep][1]];
    ++tmp[ii[dep][0]];
    dfs(tmp[0], tmp[1], tmp[2], dep + 1);
    ++tmp[ii[dep][1]];
    --tmp[ii[dep][0]];
  }
  if (c[0] != 0)
    return;
  tp[dep] = ii[dep][1] + 'A';
  --tmp[ii[dep][0]];
  ++tmp[ii[dep][1]];
  if (tmp[0] >= 0 && tmp[1] >= 0 && tmp[2] >= 0)
    dfs(tmp[0], tmp[1], tmp[2], dep + 1);
  ++tmp[ii[dep][0]];
  --tmp[ii[dep][1]];
  if (c[0] != 0)
    return;
  tp[dep] = ii[dep][0] + 'A';
  --tmp[ii[dep][1]];
  ++tmp[ii[dep][0]];
  if (tmp[0] >= 0 && tmp[1] >= 0 && tmp[2] >= 0)
    dfs(tmp[0], tmp[1], tmp[2], dep + 1);
  ++tmp[ii[dep][1]];
  --tmp[ii[dep][0]];
}

int main() {
  int i, j, k, l;
  scanf("%d", &n);
  for (i = 0; i < 3; i++)
    scanf("%d", x + i);
  for (i = 0; i < n; i++) {
    char s[5];
    scanf("%s", s);
    ii[i][0] = s[0] - 'A', ii[i][1] = s[1] - 'A';
  }
  dfs(x[0], x[1], x[2], 0);
  printf("%s\n", c[0] != 0 ? "Yes" : "No");
  if (c[0] != 0) {
    for (i = 0; i < n; i++)
      printf("%c\n", c[i]);
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<int, int> pa;

#define fst first
#define snd second
#define pb push_back

const int maxn = 300005, maxm = 106;
const double pi = acos(-1.0);
const ll mod = 1000000007;
const double eps = 1e-6;

int n, x[3];
bool fl = 1;
char c[maxn] = {0}, tp[maxn];
int ii[maxn][2];

void dfs(int aa, int bb, int cc, int dep) {
  int i, j, k, l;
  int tmp[3] = {aa, bb, cc};
  if (dep == n) {
    for (i = 0; i < n; i++)
      c[i] = tp[i];
    return;
  }
  if (c[0] != 0)
    return;
  if (tmp[ii[dep][0]] > tmp[ii[dep][1]]) {
    tp[dep] = ii[dep][1] + 'A';
    --tmp[ii[dep][0]];
    ++tmp[ii[dep][1]];
    dfs(tmp[0], tmp[1], tmp[2], dep + 1);
    ++tmp[ii[dep][0]];
    --tmp[ii[dep][1]];
  }
  if (c[0] != 0)
    return;
  if (tmp[ii[dep][1]] > tmp[ii[dep][0]]) {
    tp[dep] = ii[dep][0] + 'A';
    --tmp[ii[dep][1]];
    ++tmp[ii[dep][0]];
    dfs(tmp[0], tmp[1], tmp[2], dep + 1);
    ++tmp[ii[dep][1]];
    --tmp[ii[dep][0]];
  }
  if (c[0] != 0)
    return;
  if (tmp[ii[dep][1]] != tmp[ii[dep][0]])
    return;
  tp[dep] = ii[dep][1] + 'A';
  --tmp[ii[dep][0]];
  ++tmp[ii[dep][1]];
  if (tmp[0] >= 0 && tmp[1] >= 0 && tmp[2] >= 0)
    dfs(tmp[0], tmp[1], tmp[2], dep + 1);
  ++tmp[ii[dep][0]];
  --tmp[ii[dep][1]];
  if (c[0] != 0)
    return;
  tp[dep] = ii[dep][0] + 'A';
  --tmp[ii[dep][1]];
  ++tmp[ii[dep][0]];
  if (tmp[0] >= 0 && tmp[1] >= 0 && tmp[2] >= 0)
    dfs(tmp[0], tmp[1], tmp[2], dep + 1);
  ++tmp[ii[dep][1]];
  --tmp[ii[dep][0]];
}

int main() {
  int i, j, k, l;
  scanf("%d", &n);
  for (i = 0; i < 3; i++)
    scanf("%d", x + i);
  for (i = 0; i < n; i++) {
    char s[5];
    scanf("%s", s);
    ii[i][0] = s[0] - 'A', ii[i][1] = s[1] - 'A';
  }
  dfs(x[0], x[1], x[2], 0);
  printf("%s\n", c[0] != 0 ? "Yes" : "No");
  if (c[0] != 0) {
    for (i = 0; i < n; i++)
      printf("%c\n", c[i]);
  }
  return 0;
}