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

#include <bits/stdc++.h>
// #define int long long
using namespace std;

int dp[100005][8][8][8];
pair<int, int> parent[100005][8][8][8];

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  if (fopen("INPUT.txt", "r")) {
    freopen("INPUT.txt", "r", stdin);
    freopen("OUTPUT.txt", "w", stdout);
  }
#endif

  int n, a, b, c;
  cin >> n >> a >> b >> c;
  a = min(a, 4);
  b = min(b, 4);
  c = min(c, 4);

  dp[0][a][b][c] = 1;
  string s[n + 1];
  for (int i = 1; i <= n; i++) {
    cin >> s[i];
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < 7; j++) {
      for (int k = 0; k < 7; k++) {
        for (int l = 0; l < 7; l++) {
          if (s[i] == "AC") {
            if (l > 0 && j < 7) {
              dp[i][j][k][l] |= dp[i - 1][j + 1][k][l - 1];
              if (dp[i - 1][j + 1][k][l - 1] > 0) {
                parent[i][j][k][l] = {3, 1};
              }
            }
            if (j > 0 && l < 7) {
              dp[i][j][k][l] |= dp[i - 1][j - 1][k][l + 1];
              if (dp[i - 1][j - 1][k][l + 1] > 0) {
                parent[i][j][k][l] = {1, 3};
              }
            }
          } else if (s[i] == "AB") {
            if (k > 0 && j < 7) {
              dp[i][j][k][l] |= dp[i - 1][j + 1][k - 1][l];
              if (dp[i - 1][j + 1][k - 1][l] > 0) {
                parent[i][j][k][l] = {2, 1};
              }
            }
            if (j > 0 && k < 7) {
              dp[i][j][k][l] |= dp[i - 1][j - 1][k + 1][l];
              if (dp[i - 1][j - 1][k + 1][l] > 0) {
                parent[i][j][k][l] = {1, 2};
              }
            }
          }
          if (s[i] == "BC") {
            if (l > 0 && k < 7) {
              dp[i][j][k][l] |= dp[i - 1][j][k + 1][l - 1];
              if (dp[i - 1][j][k + 1][l - 1] > 0) {
                parent[i][j][k][l] = {3, 2};
              }
            }
            if (k > 0 && l < 7) {
              dp[i][j][k][l] |= dp[i - 1][j][k - 1][l + 1];
              if (dp[i - 1][j][k - 1][l + 1] > 0) {
                parent[i][j][k][l] = {2, 3};
              }
            }
          }
        }
      }
    }
  }
  a = -1;
  b = -1;
  c = -1;
  for (int i = 0; i < 8 && a == -1; i++)
    for (int j = 0; j < 8 && a == -1; j++)
      for (int k = 0; k < 8 && a == -1; k++) {
        if (dp[n][i][j][k] > 0) {
          a = i;
          b = j;
          c = k;
          break;
        }
      }
  if (a == -1) {
    cout << "No";
  } else {
    int x[4];
    x[1] = a;
    x[2] = b;
    x[3] = c;
    cout << "Yes\\n";
    vector<char> ans;
    int ini = n;
    while (ini > 0) {
      int z = parent[ini][a][b][c].first;
      if (z == 1)
        ans.push_back('A');
      else if (z == 2)
        ans.push_back('B');
      else
        ans.push_back('C');
      x[parent[ini][a][b][c].first]--;
      x[parent[ini][a][b][c].second]++;
      a = x[1];
      b = x[2];
      c = x[3];
      ini--;
    }
    reverse(ans.begin(), ans.end());
    for (auto i : ans)
      cout << i << '\\n';
  }
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define int long long
using namespace std;

int dp[100005][8][8][8];
pair<int, int> parent[100005][8][8][8];

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  if (fopen("INPUT.txt", "r")) {
    freopen("INPUT.txt", "r", stdin);
    freopen("OUTPUT.txt", "w", stdout);
  }
#endif

  int n, a, b, c;
  cin >> n >> a >> b >> c;
  a = min(a, 4LL);
  b = min(b, 4LL);
  c = min(c, 4LL);

  dp[0][a][b][c] = 1;
  string s[n + 1];
  for (int i = 1; i <= n; i++) {
    cin >> s[i];
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < 7; j++) {
      for (int k = 0; k < 7; k++) {
        for (int l = 0; l < 7; l++) {
          if (s[i] == "AC") {
            if (l > 0 && j < 7) {
              dp[i][j][k][l] |= dp[i - 1][j + 1][k][l - 1];
              if (dp[i - 1][j + 1][k][l - 1] > 0) {
                parent[i][j][k][l] = {3, 1};
              }
            }
            if (j > 0 && l < 7) {
              dp[i][j][k][l] |= dp[i - 1][j - 1][k][l + 1];
              if (dp[i - 1][j - 1][k][l + 1] > 0) {
                parent[i][j][k][l] = {1, 3};
              }
            }
          } else if (s[i] == "AB") {
            if (k > 0 && j < 7) {
              dp[i][j][k][l] |= dp[i - 1][j + 1][k - 1][l];
              if (dp[i - 1][j + 1][k - 1][l] > 0) {
                parent[i][j][k][l] = {2, 1};
              }
            }
            if (j > 0 && k < 7) {
              dp[i][j][k][l] |= dp[i - 1][j - 1][k + 1][l];
              if (dp[i - 1][j - 1][k + 1][l] > 0) {
                parent[i][j][k][l] = {1, 2};
              }
            }
          }
          if (s[i] == "BC") {
            if (l > 0 && k < 7) {
              dp[i][j][k][l] |= dp[i - 1][j][k + 1][l - 1];
              if (dp[i - 1][j][k + 1][l - 1] > 0) {
                parent[i][j][k][l] = {3, 2};
              }
            }
            if (k > 0 && l < 7) {
              dp[i][j][k][l] |= dp[i - 1][j][k - 1][l + 1];
              if (dp[i - 1][j][k - 1][l + 1] > 0) {
                parent[i][j][k][l] = {2, 3};
              }
            }
          }
        }
      }
    }
  }
  a = -1;
  b = -1;
  c = -1;
  for (int i = 0; i < 8 && a == -1; i++)
    for (int j = 0; j < 8 && a == -1; j++)
      for (int k = 0; k < 8 && a == -1; k++) {
        if (dp[n][i][j][k] > 0) {
          a = i;
          b = j;
          c = k;
          break;
        }
      }
  if (a == -1) {
    cout << "No";
  } else {
    int x[4];
    x[1] = a;
    x[2] = b;
    x[3] = c;
    cout << "Yes\\n";
    vector<char> ans;
    int ini = n;
    while (ini > 0) {
      int z = parent[ini][a][b][c].first;
      if (z == 1)
        ans.push_back('A');
      else if (z == 2)
        ans.push_back('B');
      else
        ans.push_back('C');
      x[parent[ini][a][b][c].first]--;
      x[parent[ini][a][b][c].second]++;
      a = x[1];
      b = x[2];
      c = x[3];
      ini--;
    }
    reverse(ans.begin(), ans.end());
    for (auto i : ans)
      cout << i << '\\n';
  }
}