Problem p03168 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
#pragma GCC optimize "03"
using namespace std;

#define int long long int
#define pb push_back
#define pii pair<int, int>
#define fi first
#define se second
#define rep(i, a, b) for (int i = a; i < b; ++i)
#define dbg(x)                                                                 \
  { cerr << "> " << #x << ": " << x << endl; }
#define dbg2(x, y)                                                             \
  { cerr << "> " << #x << ": " << x << " , " << #y << ": " << y << endl; }
#define dbg3(x, y, z)                                                          \
  {                                                                            \
    cerr << "> " << #x << ": " << x << " , " << #y << ": " << y << " , " << #z \
         << ": " << z << endl;                                                 \
  }
#define IOS                                                                    \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0)
#ifndef LOCAL
#define endl '\n'
#endif
const int inf = INT_MAX;
const double eps = 0.0000001;
const double PI = acos(-1.0);
const int MOD = 1e9 + 7;
const int N = 1e3 + 5;

double a[N], dp[N][N];

signed main() {
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
#endif
  IOS;

  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  dp[1][1] = a[1];
  dp[1][0] = 1 - a[1];
  for (int i = 2; i <= n; i++) {
    dp[i][0] = dp[i - 1][0] * (1 - a[i]);
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (i == 1 && j == 1)
        continue;
      if (j > i) {
        dp[i][j] = 0;
        continue;
      }
      dp[i][j] = dp[i - 1][j] * (1 - a[i]) + dp[i - 1][j - 1] * a[i];
    }
  }
  double ans = 0;
  for (int i = (n + 1) / 2; i <= n; i++)
    ans += dp[n][i];
  cout << fixed << setprecision(10) << ans << endl;
  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
#pragma GCC optimize "03"
using namespace std;

#define int long long int
#define pb push_back
#define pii pair<int, int>
#define fi first
#define se second
#define rep(i, a, b) for (int i = a; i < b; ++i)
#define dbg(x)                                                                 \
  { cerr << "> " << #x << ": " << x << endl; }
#define dbg2(x, y)                                                             \
  { cerr << "> " << #x << ": " << x << " , " << #y << ": " << y << endl; }
#define dbg3(x, y, z)                                                          \
  {                                                                            \
    cerr << "> " << #x << ": " << x << " , " << #y << ": " << y << " , " << #z \
         << ": " << z << endl;                                                 \
  }
#define IOS                                                                    \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0)
#ifndef LOCAL
#define endl '\n'
#endif
const int inf = INT_MAX;
const double eps = 0.0000001;
const double PI = acos(-1.0);
const int MOD = 1e9 + 7;
const int N = 3e3 + 5;

double a[N], dp[N][N];

signed main() {
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
#endif
  IOS;

  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  dp[1][1] = a[1];
  dp[1][0] = 1 - a[1];
  for (int i = 2; i <= n; i++) {
    dp[i][0] = dp[i - 1][0] * (1 - a[i]);
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (i == 1 && j == 1)
        continue;
      if (j > i) {
        dp[i][j] = 0;
        continue;
      }
      dp[i][j] = dp[i - 1][j] * (1 - a[i]) + dp[i - 1][j - 1] * a[i];
    }
  }
  double ans = 0;
  for (int i = (n + 1) / 2; i <= n; i++)
    ans += dp[n][i];
  cout << fixed << setprecision(10) << ans << endl;
  return 0;
}