Problem p02664 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define FAST                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
typedef long long ll;
typedef long double ld;
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define mod 1000000007
#define inf 1000000000000000000
#define bpc(x) __builtin_popcountll(x)
#define autoit(x, it) for (auto it = x.begin(); it != x.end(); it++)
#define rep(n) for (ll i = 0; i < n; i++)
#define repi(i, n) for (ll i = 0; i < n; i++)

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ordered_set                                                            \\
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>

using namespace std;

char get(ll n) {
  if (n == 1)
    return 'P';
  return 'D';
}

int main() {
  FAST /**/

      string s;
  cin >> s;

  ll n = s.length();
  ll dp[n + 1][2]; // 0 - D
                   // 1 - P
  rep(n + 1) dp[i][0] = dp[i][1] = -inf;
  dp[0][0] = dp[0][1] = 0;
  ll prev[n + 1][2];
  rep(n + 1) repi(j, 2) prev[i][j] = 1;
  rep(n + 1) {
    if (i == 0)
      continue;
    if (i == 1) {
      if (s[i - 1] == '?')
        dp[i][1] = 0, dp[i][0] = 1;
      else {
        if (s[i - 1] == 'D')
          dp[i][0] = 1;
        else
          dp[i][1] = 0;
      }
    }

    else {
      if (s[i - 1] == '?') {
        dp[i][0] = max(dp[i - 1][0] + 1, dp[i - 1][1] + 2);
        dp[i][1] = max(dp[i - 1][0], dp[i - 1][1]);
        if (dp[i][0] == dp[i - 1][0] + 1)
          prev[i][0] = 0;

        if (dp[i][1] == dp[i - 1][0])
          prev[i][1] = 0;
      }

      else {
        if (s[i - 1] == 'D')
          dp[i][0] = max(dp[i - 1][0] + 1, dp[i - 1][1] + 2);
        else
          dp[i][1] = max(dp[i - 1][0], dp[i - 1][1]);

        if (dp[i][0] == dp[i - 1][0] + 1)
          prev[i][0] = 0;
        if (dp[i][1] == dp[i - 1][0])
          prev[i][1] = 0;
      }
    }

    // cout<<"at "<<i<<" = "<<dp[i][0]<<" "<<dp[i][1]<<"\\n";
  }

  ll fans = max(dp[n][0], dp[n][1]);
  ll las;
  string fin;
  if (dp[n][0] > dp[n][1])
    las = 0;
  else
    las = 1;
  ll curr = n;
  while (curr > 0) {
    fin += get(las);
    las = prev[curr][las];
    curr--;
  }
  reverse(fin.begin(), fin.end());
  cout << fin;

  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define FAST                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
typedef long long ll;
typedef long double ld;
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define mod 1000000007
#define inf 1000000000000000000
#define bpc(x) __builtin_popcountll(x)
#define autoit(x, it) for (auto it = x.begin(); it != x.end(); it++)
#define rep(n) for (ll i = 0; i < n; i++)
#define repi(i, n) for (ll i = 0; i < n; i++)

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ordered_set                                                            \\
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>

using namespace std;

char get(ll n) {
  if (n == 1)
    return 'P';
  return 'D';
}

int main() {
  FAST /**/

      string s;
  cin >> s;

  ll n = s.length();
  ll dp[n + 1][2]; // 0 - D
                   // 1 - P
  rep(n + 1) dp[i][0] = dp[i][1] = -inf;
  dp[0][0] = dp[0][1] = 0;
  ll prev[n + 1][2];
  rep(n) repi(j, 2) prev[i][j] = 1;
  rep(n + 1) {
    if (i == 0)
      continue;
    if (i == 1) {
      if (s[i - 1] == '?')
        dp[i][1] = 0, dp[i][0] = 1;
      else {
        if (s[i - 1] == 'D')
          dp[i][0] = 1;
        else
          dp[i][1] = 0;
      }
    }

    else {
      if (s[i - 1] == '?') {
        dp[i][0] = max(dp[i - 1][0], dp[i - 1][1]) + 1;
        dp[i][1] = max(dp[i - 1][0] + 1, dp[i - 1][1]);
        if (dp[i][0] == dp[i - 1][0] + 1)
          prev[i][0] = 0;

        if (dp[i][1] == dp[i - 1][0] + 1)
          prev[i][1] = 0;
      }

      else {
        if (s[i - 1] == 'D')
          dp[i][0] = max(dp[i - 1][0], dp[i - 1][1]) + 1;
        else
          dp[i][1] = max(dp[i - 1][0] + 1, dp[i - 1][1]);

        if (dp[i][0] == dp[i - 1][0] + 1)
          prev[i][0] = 0;
        if (dp[i][1] == dp[i - 1][0] + 1)
          prev[i][1] = 0;
      }
    }

    // cout<<"at "<<i<<" = "<<dp[i][0]<<" "<<dp[i][1]<<"\\n";
  }

  ll fans = max(dp[n][0], dp[n][1]);
  ll las;
  string fin;
  if (dp[n][0] > dp[n][1])
    las = 0;
  else
    las = 1;
  ll curr = n;
  while (curr > 0) {
    fin += get(las);
    las = prev[curr][las];
    curr--;
  }
  reverse(fin.begin(), fin.end());
  cout << fin;

  return 0;
}