Problem p03168 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define fio                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define Mod 1000000007
#define ll long long
#define dd double
#define pb push_back
#define ff first
#define ss second
#define Mp make_pair
#define INF 999999999999999999
#define NN (ll)(3e3 + 5)
int mod(string s, int a) {
  int ans = 0;
  for (int i = 0; i < s.length(); i++)
    ans = (ans * 10 + (int)s[i] - '0') % a;
  return ans;
}
ll min(ll x, ll y) {
  if (x < y)
    return x;
  return y;
}
ll max(ll x, ll y) {
  if (x > y)
    return x;
  return y;
}
ll power(ll x, unsigned ll y) {
  if (y == 0)
    return 1;
  else if (y % 2 == 0)
    return power(x, y / 2) * power(x, y / 2);
  else
    return x * power(x, y / 2) * power(x, y / 2);
}
ll hcf(ll a, ll b) {
  if (b == 0)
    return a;
  return hcf(b, a % b);
}
ll n;
vector<dd> v(NN);
ll vis[NN][NN];
dd dp[NN][NN];
dd func(ll ind, ll heads) {
  if (ind == n + 1)
    return (heads * 2 >= n);
  if (vis[ind][heads])
    return dp[ind][heads];
  dd &ans = dp[ind][heads];
  return ans = v[ind] * func(ind + 1, heads + 1) +
               (1 - v[ind]) * func(ind + 1, heads);
}
int main() {
  fio;
  /*#ifndef ONLINE_JUDGE
       freopen("input.txt", "r", stdin);
       freopen("output.txt", "w", stdout);
       freopen("error.txt", "w", stderr);
  #endif*/
  ll TT = 1;
  // cin>>TT;
  while (TT--) {
    cin >> n;
    for (ll i = 1; i <= n; i++)
      cin >> v[i];
    dd ans = func(1, 0);
    cout << fixed << setprecision(12) << ans << "\n";
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define fio                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define Mod 1000000007
#define ll long long
#define dd double
#define pb push_back
#define ff first
#define ss second
#define Mp make_pair
#define INF 999999999999999999
#define NN (ll)(3e3 + 5)
int mod(string s, int a) {
  int ans = 0;
  for (int i = 0; i < s.length(); i++)
    ans = (ans * 10 + (int)s[i] - '0') % a;
  return ans;
}
ll min(ll x, ll y) {
  if (x < y)
    return x;
  return y;
}
ll max(ll x, ll y) {
  if (x > y)
    return x;
  return y;
}
ll power(ll x, unsigned ll y) {
  if (y == 0)
    return 1;
  else if (y % 2 == 0)
    return power(x, y / 2) * power(x, y / 2);
  else
    return x * power(x, y / 2) * power(x, y / 2);
}
ll hcf(ll a, ll b) {
  if (b == 0)
    return a;
  return hcf(b, a % b);
}
ll n;
vector<dd> v(NN);
ll vis[NN][NN];
dd dp[NN][NN];
dd func(ll ind, ll heads) {
  if (ind == n + 1)
    return (heads * 2 >= n);
  if (vis[ind][heads])
    return dp[ind][heads];
  vis[ind][heads] = 1;
  dd &ans = dp[ind][heads];
  return ans = v[ind] * func(ind + 1, heads + 1) +
               (1 - v[ind]) * func(ind + 1, heads);
}
int main() {
  fio;
  /*#ifndef ONLINE_JUDGE
       freopen("input.txt", "r", stdin);
       freopen("output.txt", "w", stdout);
       freopen("error.txt", "w", stderr);
  #endif*/
  ll TT = 1;
  // cin>>TT;
  while (TT--) {
    cin >> n;
    for (ll i = 1; i <= n; i++)
      cin >> v[i];
    dd ans = func(1, 0);
    cout << fixed << setprecision(12) << ans << "\n";
  }
}