Problem p02692 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
string s[100005];
ll d[100005][10][10][10];
int main() {
  ll n, x, y, z;
  cin >> n >> x >> y >> z;
  for (int i = 1; i <= n; i++) {
    cin >> s[i];
  }
  x = min(x, (ll)3);
  y = min(y, (ll)3);
  z = min(z, (ll)3);
  d[0][x][y][z] = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= 4; j++) {
      for (int k = 0; k <= 4; k++) {
        for (int t = 0; t <= 4; t++) {
          if (s[i] == "AB") {
            if (k - 1 >= 0 && j + 1 <= 4)
              d[i][j + 1][k - 1][t] |= d[i - 1][j][k][t];
            if (j - 1 >= 0 && k + 1 <= 4)
              d[i][j - 1][k + 1][t] |= d[i - 1][j][k][t];
          }
          if (s[i] == "AC") {
            if (t - 1 >= 0 && j + 1 <= 4)
              d[i][j + 1][k][t - 1] |= d[i - 1][j][k][t];
            if (j - 1 >= 0 && t + 1 <= 4)
              d[i][j - 1][k][t + 1] |= d[i - 1][j][k][t];
          }
          if (s[i] == "BC") {
            if (t - 1 >= 0 && k + 1 <= 4)
              d[i][j][k + 1][t - 1] |= d[i - 1][j][k][t];
            if (k - 1 >= 0 && t + 1 <= 4)
              d[i][j][k - 1][t + 1] |= d[i - 1][j][k][t];
          }
        }
      }
    }
  }
  int nx = -1, ny = -1, nv = -1;
  for (int j = 0; j < 5; j++) {
    for (int k = 0; k < 5; k++) {
      for (int t = 0; t < 5; t++) {
        if (d[n][j][k][t]) {
          nx = j;
          ny = k;
          nv = t;
          break;
        }
      }
    }
  }
  if (nx == -1 || ny == -1 || nv == -1) {
    cout << "No" << endl;
    return 0;
  }
  cout << "Yes" << endl;
  vector<char> ans;
  for (int i = n; i >= 1; i--) {
    if (s[i] == "AB") {
      if (nx - 1 >= 0 && ny + 1 <= 4 && d[i - 1][nx - 1][ny + 1][nv]) {
        ans.push_back('A');
        nx--;
        ny++;
      } else {
        ans.push_back('B');
        nx++;
        ny--;
      }
    } else if (s[i] == "AC") {
      if (nx - 1 >= 0 && nv + 1 <= 4 && d[i - 1][nx - 1][ny][nv + 1]) {
        ans.push_back('A');
        nx--;
        nv++;
      } else {
        ans.push_back('C');
        nx++;
        nv--;
      }
    } else if (s[i] == "BC") {
      if (nv - 1 >= 0 && ny + 1 <= 4 && d[i - 1][nx][ny + 1][nv - 1]) {
        ans.push_back('C');
        nv--;
        ny++;
      } else {
        ans.push_back('B');
        nv++;
        ny--;
      }
    }
  }
  reverse(ans.begin(), ans.end());
  for (int i = 0; i < ans.size(); i++) {
    cout << ans[i] << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
string s[100005];
ll d[100005][10][10][10];
int main() {
  ll n, x, y, z;
  cin >> n >> x >> y >> z;
  for (int i = 1; i <= n; i++) {
    cin >> s[i];
  }
  x = min(x, (ll)3);
  y = min(y, (ll)3);
  z = min(z, (ll)3);
  d[0][x][y][z] = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= 9; j++) {
      for (int k = 0; k <= 9; k++) {
        for (int t = 0; t <= 9; t++) {
          if (s[i] == "AB") {
            if (k - 1 >= 0)
              d[i][j + 1][k - 1][t] |= d[i - 1][j][k][t];
            if (j - 1 >= 0)
              d[i][j - 1][k + 1][t] |= d[i - 1][j][k][t];
          }
          if (s[i] == "AC") {
            if (t - 1 >= 0)
              d[i][j + 1][k][t - 1] |= d[i - 1][j][k][t];
            if (j - 1 >= 0)
              d[i][j - 1][k][t + 1] |= d[i - 1][j][k][t];
          }
          if (s[i] == "BC") {
            if (t - 1 >= 0)
              d[i][j][k + 1][t - 1] |= d[i - 1][j][k][t];
            if (k - 1 >= 0)
              d[i][j][k - 1][t + 1] |= d[i - 1][j][k][t];
          }
        }
      }
    }
  }
  int nx = -1, ny = -1, nv = -1;
  for (int j = 0; j < 10; j++) {
    for (int k = 0; k < 10; k++) {
      for (int t = 0; t < 10; t++) {
        if (d[n][j][k][t]) {
          nx = j;
          ny = k;
          nv = t;
          break;
        }
      }
    }
  }
  if (nx == -1 || ny == -1 || nv == -1) {
    cout << "No" << endl;
    return 0;
  }
  cout << "Yes" << endl;
  vector<char> ans;
  for (int i = n; i >= 1; i--) {
    if (s[i] == "AB") {
      if (nx - 1 >= 0 && d[i - 1][nx - 1][ny + 1][nv]) {
        ans.push_back('A');
        nx--;
        ny++;
      } else {
        ans.push_back('B');
        nx++;
        ny--;
      }
    } else if (s[i] == "AC") {
      if (nx - 1 >= 0 && d[i - 1][nx - 1][ny][nv + 1]) {
        ans.push_back('A');
        nx--;
        nv++;
      } else {
        ans.push_back('C');
        nx++;
        nv--;
      }
    } else if (s[i] == "BC") {
      if (nv - 1 >= 0 && d[i - 1][nx][ny + 1][nv - 1]) {
        ans.push_back('C');
        nv--;
        ny++;
      } else {
        ans.push_back('B');
        nv++;
        ny--;
      }
    }
  }
  reverse(ans.begin(), ans.end());
  for (int i = 0; i < ans.size(); i++) {
    cout << ans[i] << endl;
  }
}