Problem p03168 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
#define sz(a) int(a.size())
#define ll long long
#define ull unsigned long long
// #define mod int(1e9+7)
#define endl '\n'
#define Engz                                                                   \
  ios::sync_with_stdio(0);                                                     \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0), cout.tie(0);
#define pi double(acos(-1))
#define S second
#define F first

//  freopen("input.txt","r",stdin);

//------------------- BESH ----------------------//
long long mod;
long long p(long long a, long long b) {
  if (b == 0)
    return 1;

  long long z = p(a, b / 2);

  if (b % 2 == 0)
    return ((z % mod) * (z % mod)) % mod;
  else
    return (((z * z) % mod) * a) % mod;
}

long long g(long long a, long long b) {
  if (a == 0)
    return b;
  else
    return g(b % a, a);
}

//-----------sieve --------------------//

bool prime[1000001];

void sieve() {
  memset(prime, true, sizeof prime);
  prime[0] = prime[1] = 0;

  for (int i = 2; i * i <= 1000000; i++) {
    if (prime[i] == true)
      for (int j = i * i; j <= 100000; j += i)
        prime[j] = false;
  }
}

ll lcm(ll a, ll b) { return (a * b) / __gcd(a, b); }

long double a[3005];
long double ans[1003][1003];
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];

  ans[1][0] = 1.0 - a[1];
  ans[1][1] = a[1];

  for (int i = 2; i <= n; i++) {

    for (int j = 0; j <= n; j++) {
      if (j <= i) {
        ans[i][j] = ans[i - 1][j] * (1 - a[i]);
        if (j)
          ans[i][j] += ans[i - 1][j - 1] * a[i];
      }

      else
        ans[i][j] = 0;
    }
  }

  long double res = 0;
  for (int i = ceil((long double)n / 2); i <= n; i++)
    res += ans[n][i];

  cout << setprecision(12) << fixed << res;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
#define sz(a) int(a.size())
#define ll long long
#define ull unsigned long long
// #define mod int(1e9+7)
#define endl '\n'
#define Engz                                                                   \
  ios::sync_with_stdio(0);                                                     \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0), cout.tie(0);
#define pi double(acos(-1))
#define S second
#define F first

//  freopen("input.txt","r",stdin);

//------------------- BESH ----------------------//
long long mod;
long long p(long long a, long long b) {
  if (b == 0)
    return 1;

  long long z = p(a, b / 2);

  if (b % 2 == 0)
    return ((z % mod) * (z % mod)) % mod;
  else
    return (((z * z) % mod) * a) % mod;
}

long long g(long long a, long long b) {
  if (a == 0)
    return b;
  else
    return g(b % a, a);
}

//-----------sieve --------------------//

bool prime[1000001];

void sieve() {
  memset(prime, true, sizeof prime);
  prime[0] = prime[1] = 0;

  for (int i = 2; i * i <= 1000000; i++) {
    if (prime[i] == true)
      for (int j = i * i; j <= 100000; j += i)
        prime[j] = false;
  }
}

ll lcm(ll a, ll b) { return (a * b) / __gcd(a, b); }

long double a[3005];
long double ans[3005][3005];
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];

  ans[1][0] = 1.0 - a[1];
  ans[1][1] = a[1];

  for (int i = 2; i <= n; i++) {

    for (int j = 0; j <= n; j++) {
      if (j <= i) {
        ans[i][j] = ans[i - 1][j] * (1 - a[i]);
        if (j)
          ans[i][j] += ans[i - 1][j - 1] * a[i];
      }

      else
        ans[i][j] = 0;
    }
  }

  long double res = 0;
  for (int i = ceil((long double)n / 2); i <= n; i++)
    res += ans[n][i];

  cout << setprecision(12) << fixed << res;
  return 0;
}