Problem p03168 (C++)

Buggy submission (status: Time Limit Exceeded):
//  Copyright © 2020 Pradeep Singh. All rights reserved.

/*
Zeckendrof's theorem : Each positive number can be represented as a sum of three
fibonacci numbers such that no two consecutive fib number is used. binomial :
C(n,r) = C(n-1,r-1)+C(n-1,r) , C(n,0)=1 catalan : C(2*n,n)/(n+1);
*/

#include <algorithm>
#include <bits/stdc++.h>
using namespace std;

#define ff first
#define ss second
#define int long long
#define str(a) to_string(a)
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define SORT(c) sort(c.begin(), c.end())
#define mod 1000000007
#define max_heap priority_queue<int>
#define min_heap priority_queue<int, vi, greater<int>>
#define setbits(x) __builtin_popcount(x)
#define ps(x, y) fixed << setprecision(y) << x
#define start(t)                                                               \
  int x;                                                                       \
  cin >> x;                                                                    \
  while (x--)
#define inf LLONG_MAX
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define endl "\n"

vector<vector<double>> dp(3001, vector<double>(3001, -1));
double rec(int i, int heads, vector<double> &prob) {
  if (heads == 0)
    return 1;
  if (i == 0)
    return 0;
  if (dp[i][heads] > -0.9)
    return dp[i][heads];
  return rec(i - 1, heads - 1, prob) * prob[i] +
         rec(i - 1, heads, prob) * (1 - prob[i]);
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int n;
  cin >> n;
  vector<double> prob(n + 1);
  // cerr << dp.size() << " " <<
  for (int i = 1; i <= n; i++)
    cin >> prob[i];
  cout << ps(rec(n, (n + 1) / 2, prob), 10) << endl;
}


Fixed submission:
//  Copyright © 2020 Pradeep Singh. All rights reserved.

/*
Zeckendrof's theorem : Each positive number can be represented as a sum of three
fibonacci numbers such that no two consecutive fib number is used. binomial :
C(n,r) = C(n-1,r-1)+C(n-1,r) , C(n,0)=1 catalan : C(2*n,n)/(n+1);
*/

#include <algorithm>
#include <bits/stdc++.h>
using namespace std;

#define ff first
#define ss second
#define int long long
#define str(a) to_string(a)
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define SORT(c) sort(c.begin(), c.end())
#define mod 1000000007
#define max_heap priority_queue<int>
#define min_heap priority_queue<int, vi, greater<int>>
#define setbits(x) __builtin_popcount(x)
#define ps(x, y) fixed << setprecision(y) << x
#define start(t)                                                               \
  int x;                                                                       \
  cin >> x;                                                                    \
  while (x--)
#define inf LLONG_MAX
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define endl "\n"

vector<vector<double>> dp(3001, vector<double>(3001, -1));
double rec(int i, int heads, vector<double> &prob) {
  if (heads == 0)
    return 1;
  if (i == 0)
    return 0;
  if (dp[i][heads] > -0.9)
    return dp[i][heads];
  return dp[i][heads] = rec(i - 1, heads - 1, prob) * prob[i] +
                        rec(i - 1, heads, prob) * (1 - prob[i]);
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int n;
  cin >> n;
  vector<double> prob(n + 1);
  // cerr << dp.size() << " " <<
  for (int i = 1; i <= n; i++)
    cin >> prob[i];
  cout << ps(rec(n, (n + 1) / 2, prob), 10) << endl;
}