Problem p03168 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define REP(i, a, b) for (int i = a; i < b; i++)
typedef long long ll;
#define int ll
typedef vector<int> vi;
typedef vector<long long> vl;
typedef pair<int, int> pi;
#define trace(x) cout << #x << "=" << x << "\n";
#define sz(x) (int)(x.size())
#define mod 1000000007

template <typename F, typename S>
ostream &operator<<(ostream &os, const pair<F, S> &p) {
  return os << "(" << p.first << ", " << p.second << ")";
}

template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "{";
  typename vector<T>::const_iterator it;
  for (it = v.begin(); it != v.end(); it++) {
    if (it != v.begin())
      os << ", ";
    os << *it;
  }
  return os << "}";
}

template <typename T> ostream &operator<<(ostream &os, const set<T> &v) {
  os << "[";
  typename set<T>::const_iterator it;
  for (it = v.begin(); it != v.end(); it++) {
    if (it != v.begin())
      os << ", ";
    os << *it;
  }
  return os << "]";
}

template <typename F, typename S>
ostream &operator<<(ostream &os, const map<F, S> &v) {
  os << "[";
  typename map<F, S>::const_iterator it;
  for (it = v.begin(); it != v.end(); it++) {
    if (it != v.begin())
      os << ", ";
    os << it->first << " = " << it->second;
  }
  return os << "]";
}

#define deb(x) cerr << #x << " = " << x << endl;

vector<double> a;
double dp[3005][3005];
double fun(int i, int k) {
  // deb(i);
  // deb(k);
  if (k > i) {
    return 0;
  }
  if (i == 1) {
    if (k == 1) {
      return a[i];
    }
    if (k == 0) {
      double ans = 1 - a[i];
      return ans;
    }
  }
  if (dp[i][k] != -1) {
    return dp[i][k];
  }
  dp[i][k] = a[i] * fun(i - 1, k - 1) + (1 - a[i]) * fun(i - 1, k);
  return dp[i][k];
}
int32_t main() {
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  a.resize(n + 1);
  for (int i = 0; i < 3005; i++) {
    for (int j = 0; j < 3005; j++) {
      dp[i][j] = -1;
    }
  }

  REP(i, 1, n + 1) { cin >> a[i]; }
  double ans = 0;
  for (int i = (n / 2) + 1; i <= n; i++) {
    ans += fun(n, i);
  }
  printf("%0.12lf\n", ans);

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define REP(i, a, b) for (int i = a; i < b; i++)
typedef long long ll;
#define int ll
typedef vector<int> vi;
typedef vector<long long> vl;
typedef pair<int, int> pi;
#define trace(x) cout << #x << "=" << x << "\n";
#define sz(x) (int)(x.size())
#define mod 1000000007

template <typename F, typename S>
ostream &operator<<(ostream &os, const pair<F, S> &p) {
  return os << "(" << p.first << ", " << p.second << ")";
}

template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "{";
  typename vector<T>::const_iterator it;
  for (it = v.begin(); it != v.end(); it++) {
    if (it != v.begin())
      os << ", ";
    os << *it;
  }
  return os << "}";
}

template <typename T> ostream &operator<<(ostream &os, const set<T> &v) {
  os << "[";
  typename set<T>::const_iterator it;
  for (it = v.begin(); it != v.end(); it++) {
    if (it != v.begin())
      os << ", ";
    os << *it;
  }
  return os << "]";
}

template <typename F, typename S>
ostream &operator<<(ostream &os, const map<F, S> &v) {
  os << "[";
  typename map<F, S>::const_iterator it;
  for (it = v.begin(); it != v.end(); it++) {
    if (it != v.begin())
      os << ", ";
    os << it->first << " = " << it->second;
  }
  return os << "]";
}

#define deb(x) cerr << #x << " = " << x << endl;

vector<double> a;
double dp[3005][3005];
double fun(int i, int k) {
  // deb(i);
  // deb(k);
  if (k < 0) {
    return 0;
  }
  if (k > i) {
    return 0;
  }
  if (i == 1) {
    if (k == 1) {
      return a[i];
    }
    if (k == 0) {
      double ans = 1 - a[i];
      return ans;
    }
  }
  if (dp[i][k] != -1) {
    return dp[i][k];
  }
  dp[i][k] = a[i] * fun(i - 1, k - 1) + (1 - a[i]) * fun(i - 1, k);
  return dp[i][k];
}
int32_t main() {
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  a.resize(n + 1);
  for (int i = 0; i < 3005; i++) {
    for (int j = 0; j < 3005; j++) {
      dp[i][j] = -1;
    }
  }

  REP(i, 1, n + 1) { cin >> a[i]; }
  double ans = 0;
  for (int i = (n / 2) + 1; i <= n; i++) {
    ans += fun(n, i);
  }
  printf("%0.12lf\n", ans);

  return 0;
}