Problem p03168 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
const int MOD = 1e9 + 7;
const double EPS = 1e-11;
const double PI = 2 * acos(0.0);
using ll = long long;
using ull = unsigned long long;

#define input freopen("input.txt", "r", stdin)
#define output freopen("output.txt", "w", stdout)
#define fast_io                                                                \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(nullptr);                                                            \
  cout.tie(nullptr);
#define pf printf
#define si(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
#define sii(x, y) scanf("%d %d", &x, &y)
#define sll(x, y) scanf("%lld %lld", &x, &y)

#define pii pair<int, int>
#define nl '\n'
#define mp make_pair
#define x first
#define y second
#define pb push_back
#define ALL(v) v.begin(), v.end()
#define uniq(a) a.erase(unique(a.begin(), a.end()), a.end())
#define out(x) cout << "#x = " << x << endl
#define outt(x, y) cout << "# " << x << " " << y << endl
#define mem(array, value) memset(array, value, sizeof(array))

const int MX = 1e3 + 2;
double coin[MX];
double dp[MX][MX];
int n;
double solve(int i, int head) {
  if (i == n) {
    if (head > n / 2)
      return 1;
    else
      return 0;
  }
  if (dp[i][head] != -1.0)
    return dp[i][head];
  double a =
      (coin[i] * solve(i + 1, head + 1)) + ((1 - coin[i]) * solve(i + 1, head));
  return dp[i][head] = a;
}
int main() {
  fast_io

      for (int i = 0; i < MX; i++) {
    for (int j = 0; j < MX; j++) {
      dp[i][j] = -1.0;
    }
  }
  // int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> coin[i];
  }
  double ans = solve(0, 0);
  pf("%.10lf\n", ans);
}


Fixed submission:
#include "bits/stdc++.h"
using namespace std;
const int MOD = 1e9 + 7;
const double EPS = 1e-11;
const double PI = 2 * acos(0.0);
using ll = long long;
using ull = unsigned long long;

#define input freopen("input.txt", "r", stdin)
#define output freopen("output.txt", "w", stdout)
#define fast_io                                                                \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(nullptr);                                                            \
  cout.tie(nullptr);
#define pf printf
#define si(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
#define sii(x, y) scanf("%d %d", &x, &y)
#define sll(x, y) scanf("%lld %lld", &x, &y)

#define pii pair<int, int>
#define nl '\n'
#define mp make_pair
#define x first
#define y second
#define pb push_back
#define ALL(v) v.begin(), v.end()
#define uniq(a) a.erase(unique(a.begin(), a.end()), a.end())
#define out(x) cout << "#x = " << x << endl
#define outt(x, y) cout << "# " << x << " " << y << endl
#define mem(array, value) memset(array, value, sizeof(array))

const int MX = 3e3 + 2;
double coin[MX];
double dp[MX][MX];
int n;
double solve(int i, int head) {
  if (i == n) {
    if (head > n / 2)
      return 1;
    else
      return 0;
  }
  if (dp[i][head] != -1.0)
    return dp[i][head];
  double a =
      (coin[i] * solve(i + 1, head + 1)) + ((1 - coin[i]) * solve(i + 1, head));
  return dp[i][head] = a;
}
int main() {
  fast_io

      for (int i = 0; i < MX; i++) {
    for (int j = 0; j < MX; j++) {
      dp[i][j] = -1.0;
    }
  }
  // int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> coin[i];
  }
  double ans = solve(0, 0);
  pf("%.10lf\n", ans);
}