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

#include <algorithm>
#include <bitset>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  // freopen("input.txt", "r", stdin); // redirects standard input
  // freopen("output.txt", "w", stdout); // redirects standard output

  int n, a, b, c, i, j, k;
  cin >> n >> a >> b >> c;
  vector<string> ss(n);

  if (a > 5)
    a = 5;
  if (b > 5)
    b = 5;
  if (c > 5)
    c = 5;

  vector<map<tuple<int, int, int>, char>> dp(n + 1);
  dp[0][{a, b, c}] = ' ';

  for (i = 0; i < n; i++) {
    string s;
    cin >> s;
    ss[i] = s;

    for (auto p : dp[i]) {
      tie(a, b, c) = p.first;
      if (s == "AB") {
        if (b > 0)
          dp[i + 1][{a + 1, b - 1, c}] = 'A';
        if (a > 0)
          dp[i + 1][{a - 1, b + 1, c}] = 'B';
      }
      if (s == "AC") {
        if (c > 0)
          dp[i + 1][{a + 1, b, c - 1}] = 'A';
        if (a > 0)
          dp[i + 1][{a - 1, b, c + 1}] = 'C';
      }
      if (s == "BC") {
        if (b > 0)
          dp[i + 1][{a, b - 1, c + 1}] = 'C';
        if (c > 0)
          dp[i + 1][{a, b + 1, c - 1}] = 'B';
      }
    }
  }

  if (dp[n].empty())
    cout << "No\\n";
  else {
    cout << "Yes\\n";

    vector<char> ans;
    tie(a, b, c) = dp[n].begin()->first;
    char ch = dp[n].begin()->second;

    for (i = n - 1; i >= 0; i--) {
      if (ss[i] == "AB")
        a++, b++;
      if (ss[i] == "AC")
        a++, c++;
      if (ss[i] == "BC")
        c++, b++;

      ans.push_back(ch);
      if (ch == 'A')
        a -= 2;
      if (ch == 'B')
        b -= 2;
      if (ch == 'C')
        c -= 2;

      ch = dp[i][{a, b, c}];
    }

    reverse(ans.begin(), ans.end());
    for (auto ch : ans)
      cout << ch << "\\n";
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  // freopen("input.txt", "r", stdin); // redirects standard input
  // freopen("output.txt", "w", stdout); // redirects standard output

  int n, a, b, c, i, j, k;
  cin >> n >> a >> b >> c;
  vector<string> ss(n);

  if (a > 5)
    a = 5;
  if (b > 5)
    b = 5;
  if (c > 5)
    c = 5;

  vector<map<tuple<int, int, int>, char>> dp(n + 1);
  dp[0][{a, b, c}] = ' ';

  for (i = 0; i < n; i++) {
    string s;
    cin >> s;
    ss[i] = s;

    for (auto p : dp[i]) {
      tie(a, b, c) = p.first;
      if (s == "AB") {
        if (b > 0)
          dp[i + 1][{a + 1, b - 1, c}] = 'A';
        if (a > 0)
          dp[i + 1][{a - 1, b + 1, c}] = 'B';
      }
      if (s == "AC") {
        if (c > 0)
          dp[i + 1][{a + 1, b, c - 1}] = 'A';
        if (a > 0)
          dp[i + 1][{a - 1, b, c + 1}] = 'C';
      }
      if (s == "BC") {
        if (b > 0)
          dp[i + 1][{a, b - 1, c + 1}] = 'C';
        if (c > 0)
          dp[i + 1][{a, b + 1, c - 1}] = 'B';
      }
    }
  }

  if (dp[n].empty())
    cout << "No\\n";
  else {
    cout << "Yes\\n";

    vector<char> ans;
    tie(a, b, c) = dp[n].begin()->first;
    char ch = dp[n].begin()->second;

    for (i = n - 1; i >= 0; i--) {
      if (ss[i] == "AB")
        a++, b++;
      if (ss[i] == "AC")
        a++, c++;
      if (ss[i] == "BC")
        c++, b++;

      ans.push_back(ch);
      if (ch == 'A')
        a -= 2;
      if (ch == 'B')
        b -= 2;
      if (ch == 'C')
        c -= 2;

      if (dp[i].find({a, b, c}) == dp[i].end()) {
        while (1)
          ;
      }
      ch = dp[i][{a, b, c}];
      tie(a, b, c) = dp[i].begin()->first;
    }

    reverse(ans.begin(), ans.end());
    for (auto ch : ans)
      cout << ch << "\\n";
  }

  return 0;
}