Problem p03168 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

typedef long long int ll;
typedef long double ld;
typedef pair<ll, ll> isPair;
using vvll = vector<vector<ll>>;
using vll = vector<ll>;
#define Pll pair<ll, ll>
#define vpll vector<pll>
#define sll set<ll>
#define mll map<ll, ll>

const ll inf = 1e18;
const ll maxs = 100005;
const ll mod = 1e9 + 7;

#define mp make_pair
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define ff first
#define ss second
#define LB lower_bound
#define UB upper_bound
#define line cout << endl;
#define fr(i, a, b) for (int i = a; i <= b; ++i)
#define all(n) n.begin(), n.end()
#define present(s, x) (s.find(x) != s.end())
#define cpresent(s, x) (find(all(s), x) != s.end())
#define tr(container, it)                                                      \
  for (__typeof(container.begin()) it = container.begin();                     \
       it != container.end(); it++)
#define fastio()                                                               \
  ;                                                                            \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define mst(x, a) memset(x, a, sizeof(x))

ll n;
double dp[3000][3000];
double p[3000];

double go(ll pos, ll h, ll t) {
  if (pos == n)
    return h > t;
  if (dp[pos][h] != -1)
    return dp[pos][h];

  double ans = 0;
  ans += p[pos] * go(pos + 1, h + 1, t);
  ans += (1 - p[pos]) * go(pos + 1, h, t + 1);
  return ans;
}

int main() {
  fastio();
  cout.precision(20);
  cout << fixed;
  ll t = 1;
  // cin>>t;
  while (t--) {
    for (ll i = 0; i < 3000; i++) {
      for (ll j = 0; j < 3000; j++)
        dp[i][j] = -1;
    }

    cin >> n;
    for (ll i = 0; i < n; i++)
      cin >> p[i];

    cout << go(0, 0, 0);
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

typedef long long int ll;
typedef long double ld;
typedef pair<ll, ll> isPair;
using vvll = vector<vector<ll>>;
using vll = vector<ll>;
#define Pll pair<ll, ll>
#define vpll vector<pll>
#define sll set<ll>
#define mll map<ll, ll>

const ll inf = 1e18;
const ll maxs = 100005;
const ll mod = 1e9 + 7;

#define mp make_pair
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define ff first
#define ss second
#define LB lower_bound
#define UB upper_bound
#define line cout << endl;
#define fr(i, a, b) for (int i = a; i <= b; ++i)
#define all(n) n.begin(), n.end()
#define present(s, x) (s.find(x) != s.end())
#define cpresent(s, x) (find(all(s), x) != s.end())
#define tr(container, it)                                                      \
  for (__typeof(container.begin()) it = container.begin();                     \
       it != container.end(); it++)
#define fastio()                                                               \
  ;                                                                            \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define mst(x, a) memset(x, a, sizeof(x))

ll n;
double dp[3000][3000];
double p[3000];

double go(ll pos, ll h, ll t) {
  if (pos == n)
    return h > t;
  if (dp[pos][h] != -1)
    return dp[pos][h];

  double ans = 0;
  ans += p[pos] * go(pos + 1, h + 1, t);
  ans += (1 - p[pos]) * go(pos + 1, h, t + 1);
  return dp[pos][h] = ans;
}

int main() {
  fastio();
  cout.precision(20);
  cout << fixed;
  ll t = 1;
  // cin>>t;
  while (t--) {
    for (ll i = 0; i < 3000; i++) {
      for (ll j = 0; j < 3000; j++)
        dp[i][j] = -1;
    }

    cin >> n;
    for (ll i = 0; i < n; i++)
      cin >> p[i];

    cout << go(0, 0, 0);
  }

  return 0;
}