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

/*
██╗  ██╗ █████╗ ██████╗ ███████╗███████╗███╗   ███╗        ██╗  ██╗██╗  ██╗
█████╗ ██╗     ███████╗██████╗ ██║ ██╔╝██╔══██╗██╔══██╗██╔════╝██╔════╝████╗
████║        ██║ ██╔╝██║  ██║██╔══██╗██║     ██╔════╝██╔══██╗ █████╔╝
███████║██████╔╝█████╗  █████╗  ██╔████╔██║        █████╔╝ ███████║███████║██║
█████╗  ██║  ██║ ██╔═██╗ ██╔══██║██╔══██╗██╔══╝  ██╔══╝  ██║╚██╔╝██║ ██╔═██╗
██╔══██║██╔══██║██║     ██╔══╝  ██║  ██║ ██║  ██╗██║  ██║██║
██║███████╗███████╗██║ ╚═╝ ██║███████╗██║  ██╗██║  ██║██║
██║███████╗███████╗██████╔╝ ╚═╝  ╚═╝╚═╝  ╚═╝╚═╝  ╚═╝╚══════╝╚══════╝╚═╝
╚═╝╚══════╝╚═╝  ╚═╝╚═╝  ╚═╝╚═╝  ╚═╝╚══════╝╚══════╝╚═════╝ Believe in yourself
,( try and try and then try ), You can do it
*/
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <unordered_map>
#define endl "\\n"
#define Ceil(x, y) ((x + y - 1) / y)
#define clr(arr, x) memset(arr, x, sizeof arr)
#define all(v) v.begin(), v.end()
#define allr(s) s.rbegin(), s.rend()
#define rt(s) return cout << s, 0
#define watch(x) cout << (#x) << " = " << x << endl
#define sz(s) (int)(s.size())
#define OO 0x3f3f3f3f3f3f3f3fLL
const int oo = 0x3f3f3f3f, Mod = 1e9 + 7;
using namespace std;
typedef long long ll;
int dx[]{1, -1, 0, 0, 1, 1, -1, -1};
int dy[]{0, 0, 1, -1, 1, -1, 1, -1};
void K_K() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}

int main() {
  K_K();
  string s;
  cin >> s;
  for (int i = 0; i < sz(s); i++) {
    if (s[i] == '?')
      cout << 'D';
    else
      cout << s[i];
  }
}

Error: TLE

Fixed submission:

/*
██╗  ██╗ █████╗ ██████╗ ███████╗███████╗███╗   ███╗        ██╗  ██╗██╗  ██╗
█████╗ ██╗     ███████╗██████╗ ██║ ██╔╝██╔══██╗██╔══██╗██╔════╝██╔════╝████╗
████║        ██║ ██╔╝██║  ██║██╔══██╗██║     ██╔════╝██╔══██╗ █████╔╝
███████║██████╔╝█████╗  █████╗  ██╔████╔██║        █████╔╝ ███████║███████║██║
█████╗  ██║  ██║ ██╔═██╗ ██╔══██║██╔══██╗██╔══╝  ██╔══╝  ██║╚██╔╝██║ ██╔═██╗
██╔══██║██╔══██║██║     ██╔══╝  ██║  ██║ ██║  ██╗██║  ██║██║
██║███████╗███████╗██║ ╚═╝ ██║███████╗██║  ██╗██║  ██║██║
██║███████╗███████╗██████╔╝ ╚═╝  ╚═╝╚═╝  ╚═╝╚═╝  ╚═╝╚══════╝╚══════╝╚═╝
╚═╝╚══════╝╚═╝  ╚═╝╚═╝  ╚═╝╚═╝  ╚═╝╚══════╝╚══════╝╚═════╝ Believe in yourself
,( try and try and then try ), You can do it
*/
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <unordered_map>
#define endl "\\n"
#define Ceil(x, y) ((x + y - 1) / y)
#define clr(arr, x) memset(arr, x, sizeof arr)
#define all(v) v.begin(), v.end()
#define allr(s) s.rbegin(), s.rend()
#define rt(s) return cout << s, 0
#define watch(x) cout << (#x) << " = " << x << endl
#define sz(s) (int)(s.size())
#define OO 0x3f3f3f3f3f3f3f3fLL
const int oo = 0x3f3f3f3f, Mod = 1e9 + 7;
using namespace std;
typedef long long ll;
int dx[]{1, -1, 0, 0, 1, 1, -1, -1};
int dy[]{0, 0, 1, -1, 1, -1, 1, -1};
void K_K() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
string s;
pair<int, string> ans;
map<pair<int, string>, int> dp;
int sol(int idx, string r) {
  if (idx == sz(s)) {
    int cnt = 0;
    for (int i = 0; i < sz(r); i++) {
      if (r[i] == 'D')
        cnt++;
      else if (r[i] == 'P' && i + 1 < sz(r) && r[i + 1] == 'D')
        cnt++;
    }
    if (cnt > ans.first) {
      ans.first = cnt;
      ans.second = r;
    }
    return cnt;
  }
  if (dp.find({idx, r}) != dp.end())
    return dp[{idx, r}];
  int ch1 = sol(idx + 1, r + s[idx]), ch2 = 0, ch3 = 0;
  if (s[idx] == '?') {
    ch2 = sol(idx + 1, r + 'D');
    ch3 = sol(idx + 1, r + 'P');
  }
  return dp[{idx, r}] = max({ch1, ch2, ch3});
}

int main() {
  K_K();
  cin >> s;
  sol(0, "");
  cout << ans.second;
}