Problem p03168 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h> //Carefully Crafted by hetp111
using namespace std;
#define int long long
#define double long double
#define all(v) (v).begin(), (v).end()
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int, int>
#define vii vector<pii>
#define MOD 1000000007
#define PI acos(-1)
#define eps (1e-8)
#define INF (1e18)
#define FASTER                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0)
template <class A, class B>
ostream &operator<<(ostream &out, const pair<A, B> &a) {
  return out << "(" << a.first << "," << a.second << ")";
}
template <class A> ostream &operator<<(ostream &out, const vector<A> &a) {
  for (const A &it : a)
    out << it << " ";
  return out;
}
template <class A, class B> istream &operator>>(istream &in, pair<A, B> &a) {
  return in >> a.first >> a.second;
}
template <class A> istream &operator>>(istream &in, vector<A> &a) {
  for (A &i : a)
    in >> i;
  return in;
}
// ifstream cinn("input.txt");ofstream coutt("output.txt");

double dp[3000][3000];
int n;
vector<double> v;

double f(int i, int head) {
  if (i == n)
    return head > (n / 2);
  double ans = dp[i][head];
  if (ans > -1)
    return ans;
  ans = 0;
  ans += v[i] * f(i + 1, head + 1);
  ans += (1 - v[i]) * f(i + 1, head);
  return ans;
}

signed main() {
  FASTER;
  for (int i = 0; i < 3000; i++)
    for (int j = 0; j < 3000; j++)
      dp[i][j] = -1000.0;
  cin >> n;
  v = vector<double>(n);
  cin >> v;
  double ans = 0;
  ans += f(0, 0);
  cout << fixed << setprecision(10) << ans;
}


Fixed submission:
#include <bits/stdc++.h> //Carefully Crafted by hetp111
using namespace std;
#define int long long
#define double long double
#define all(v) (v).begin(), (v).end()
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int, int>
#define vii vector<pii>
#define MOD 1000000007
#define PI acos(-1)
#define eps (1e-8)
#define INF (1e18)
#define FASTER                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0)
template <class A, class B>
ostream &operator<<(ostream &out, const pair<A, B> &a) {
  return out << "(" << a.first << "," << a.second << ")";
}
template <class A> ostream &operator<<(ostream &out, const vector<A> &a) {
  for (const A &it : a)
    out << it << " ";
  return out;
}
template <class A, class B> istream &operator>>(istream &in, pair<A, B> &a) {
  return in >> a.first >> a.second;
}
template <class A> istream &operator>>(istream &in, vector<A> &a) {
  for (A &i : a)
    in >> i;
  return in;
}
// ifstream cinn("input.txt");ofstream coutt("output.txt");

double dp[3000][3000];
int n;
vector<double> v;

double f(int i, int head) {
  if (i == n)
    return head > (n / 2);
  double &ans = dp[i][head];
  if (ans > -1)
    return ans;
  ans = 0;
  ans += v[i] * f(i + 1, head + 1);
  ans += (1 - v[i]) * f(i + 1, head);
  return ans;
}

signed main() {
  FASTER;
  for (int i = 0; i < 3000; i++)
    for (int j = 0; j < 3000; j++)
      dp[i][j] = -1000.0;
  cin >> n;
  v = vector<double>(n);
  cin >> v;
  double ans = 0;
  ans += f(0, 0);
  cout << fixed << setprecision(10) << ans;
}