Problem p02664 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define ll long long
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define pii pair<ll, ll>
#define vi vector<ll>
#define vii vector<pii>
#define mi map<ll, ll>
#define mii map<pii, ll>
#define all(a) (a).begin(), (a).end()
#define fi first
#define si second
#define sz(x) (ll) x.size()
#define endl '\n'
#define checkbit(n, b) ((n >> b) & 1)
#define mod 1000000007
#define inf 1000000000000000000
#define puts(n) cout << n;
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define setbits(x) __builtin_popcount(x)
#define mset(m, v) memset(m, v, sizeof(m))
using namespace std;
string s;
ll n;
ll dp[20005][2];
ll solve(ll i, ll last) {
  if (i == n)
    return 0;
  if (dp[i][last] != -1)
    return dp[i][last];
  ll ans = 0;
  if (i == 0) {
    if (s[i] == 'D')
      ans = max(ans, solve(i + 1, 0) + 1);
    else if (s[i] == 'P')
      ans = max(ans, solve(i + 1, 1));
    else {
      ll v1, v2;
      v1 = solve(i + 1, 0) + 1;
      v2 = solve(i + 1, 1);
      if (v1 > ans) {
        ans = v1;
        s[i] = 'D';
      }
      if (v2 > ans) {
        ans = v2;
        s[i] = 'P';
      }
    }
  } else {
    if (s[i] == 'D')
      ans = max(ans, solve(i + 1, 0) + 1 + (last == 1));
    else if (s[i] == 'P')
      ans = max(ans, solve(i + 1, 1));
    else {
      ll v1, v2;
      v1 = solve(i + 1, 0) + 1 + (last == 1);
      v2 = solve(i + 1, 1);
      if (v1 > ans) {
        ans = v1;
        s[i] = 'D';
      }
      if (v2 > ans) {
        ans = v2;
        s[i] = 'P';
      }
    }
  }
  dp[i][last] = ans;
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> s;
  n = s.size();
  for (ll i = 0; i < n; i++) {
    dp[i][0] = dp[i][1] = -1;
  }
  ll ans = solve(0, 0);
  // cout<<ans<<"\n";
  cout << s;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define ll long long
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define pii pair<ll, ll>
#define vi vector<ll>
#define vii vector<pii>
#define mi map<ll, ll>
#define mii map<pii, ll>
#define all(a) (a).begin(), (a).end()
#define fi first
#define si second
#define sz(x) (ll) x.size()
#define endl '\n'
#define checkbit(n, b) ((n >> b) & 1)
#define mod 1000000007
#define inf 1000000000000000000
#define puts(n) cout << n;
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define setbits(x) __builtin_popcount(x)
#define mset(m, v) memset(m, v, sizeof(m))
using namespace std;
string s;
ll n;
ll dp[200005][2];
ll solve(ll i, ll last) {
  if (i == n)
    return 0;
  if (dp[i][last] != -1)
    return dp[i][last];
  ll ans = 0;
  if (i == 0) {
    if (s[i] == 'D')
      ans = max(ans, solve(i + 1, 0) + 1);
    else if (s[i] == 'P')
      ans = max(ans, solve(i + 1, 1));
    else {
      ll v1, v2;
      v1 = solve(i + 1, 0) + 1;
      v2 = solve(i + 1, 1);
      if (v1 > ans) {
        ans = v1;
        s[i] = 'D';
      }
      if (v2 > ans) {
        ans = v2;
        s[i] = 'P';
      }
    }
  } else {
    if (s[i] == 'D')
      ans = max(ans, solve(i + 1, 0) + 1 + (last == 1));
    else if (s[i] == 'P')
      ans = max(ans, solve(i + 1, 1));
    else {
      ll v1, v2;
      v1 = solve(i + 1, 0) + 1 + (last == 1);
      v2 = solve(i + 1, 1);
      if (v1 > ans) {
        ans = v1;
        s[i] = 'D';
      }
      if (v2 > ans) {
        ans = v2;
        s[i] = 'P';
      }
    }
  }
  dp[i][last] = ans;
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> s;
  n = s.size();
  for (ll i = 0; i < n; i++) {
    dp[i][0] = dp[i][1] = -1;
  }
  ll ans = solve(0, 0);
  // cout<<ans<<"\n";
  cout << s;

  return 0;
}