Problem p03168 (C++)

Buggy submission (status: Time Limit Exceeded):

/* Pain. Anger. Hatred. Ego.
  Survive. Live. Win. Conquer. */

/*#Quota_Reform .
#We_Want_Safe_Roads .
#We_Want_Justice .
  #Support_Us .
  #Bangladesh .
*/

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>

#define ook order_of_key
#define fbk find_by_order
#define bug printf("Massacre.\n");
#define pf printf
#define sf scanf
#define pb push_back
#define r0 return 0
#define ff first
#define ss second
#define mp make_pair
#define set0(a) memset((a), 0, sizeof(a))
#define set1(a) memset((a), -1, sizeof(a))
#define si(a) scanf("%d", &a)
#define sl(a) scanf("%lld", &a)
#define sii(a, b) scanf("%d %d", &a, &b)
#define siii(a, b, c) scanf("%d %d %d", &a, &b, &c)
#define sll(a, b) scanf("%lld %lld", &a, &b)
#define slll(a, b, c) scanf("%lld %lld %lld", &a, &b, &c)
#define pll pair<LL, LL>
#define pii pair<int, int>
#define vi vector<int>
#define vl vector<LL>
#define all(x) (x).begin(), (x).end()
#define vii vector<pair<int, int>>
#define vll vector<pair<LL, LL>>
#define mod 1000000007

// using namespace __gnu_pbds;
// typedef tree < int, null_type
// ,less<int>,rb_tree_tag,tree_order_statistics_node_update > ordered_set;

void func(void) {
  freopen("input.c", "r", stdin);
  freopen("output.c", "w", stdout);
}

void print(vector<LL> &v) {
  cout << v.size() << endl;
  for (int i = 0; i < v.size(); i++) {
    pf("%lld ", v[i]);
  }
  pf("\n");
}

void print(vector<pll> &v) {
  cout << v.size() << endl;
  for (int i = 0; i < v.size(); i++) {
    pf("%lld %lld\n", v[i].ff, v[i].ss);
  }
}

void print(double d) { cout << fixed << setprecision(10) << d << endl; }

void print(string s, double d) {
  cout << s << " ";
  cout << fixed << setprecision(10) << d << endl;
}

const int N = 3000;
double dp[N][N];
bool vis[N][N];
LL n;
double a[N];
double calc(LL i, LL j) {
  if (i == n) {
    if (j * 2 >= n)
      return 1.0;
    return 0.0;
  }
  if (vis[i][j])
    return dp[i][j];
  return dp[i][j] = a[i] * calc(i + 1, j + 1) + (1.0 - a[i]) * calc(i + 1, j);
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  LL q, i, j = 0, temp, t, k, ans = 0, sum = 0, x, y, z, cnt = 0, m, fg = 0,
           mx = 0, mx1 = 0, mn = 8000000000000000000, mn1 = 8000000000000000000;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  cout << fixed << setprecision(10) << calc(0, 0) << endl;
}


Fixed submission:

/* Pain. Anger. Hatred. Ego.
  Survive. Live. Win. Conquer. */

/*#Quota_Reform .
#We_Want_Safe_Roads .
#We_Want_Justice .
  #Support_Us .
  #Bangladesh .
*/

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>

#define ook order_of_key
#define fbk find_by_order
#define bug printf("Massacre.\n");
#define pf printf
#define sf scanf
#define pb push_back
#define r0 return 0
#define ff first
#define ss second
#define mp make_pair
#define set0(a) memset((a), 0, sizeof(a))
#define set1(a) memset((a), -1, sizeof(a))
#define si(a) scanf("%d", &a)
#define sl(a) scanf("%lld", &a)
#define sii(a, b) scanf("%d %d", &a, &b)
#define siii(a, b, c) scanf("%d %d %d", &a, &b, &c)
#define sll(a, b) scanf("%lld %lld", &a, &b)
#define slll(a, b, c) scanf("%lld %lld %lld", &a, &b, &c)
#define pll pair<LL, LL>
#define pii pair<int, int>
#define vi vector<int>
#define vl vector<LL>
#define all(x) (x).begin(), (x).end()
#define vii vector<pair<int, int>>
#define vll vector<pair<LL, LL>>
#define mod 1000000007

// using namespace __gnu_pbds;
// typedef tree < int, null_type
// ,less<int>,rb_tree_tag,tree_order_statistics_node_update > ordered_set;

void func(void) {
  freopen("input.c", "r", stdin);
  freopen("output.c", "w", stdout);
}

void print(vector<LL> &v) {
  cout << v.size() << endl;
  for (int i = 0; i < v.size(); i++) {
    pf("%lld ", v[i]);
  }
  pf("\n");
}

void print(vector<pll> &v) {
  cout << v.size() << endl;
  for (int i = 0; i < v.size(); i++) {
    pf("%lld %lld\n", v[i].ff, v[i].ss);
  }
}

void print(double d) { cout << fixed << setprecision(10) << d << endl; }

void print(string s, double d) {
  cout << s << " ";
  cout << fixed << setprecision(10) << d << endl;
}

const int N = 3000;
double dp[N][N];
bool vis[N][N];
LL n;
double a[N];
double calc(LL i, LL j) {
  if (i == n) {
    if (j * 2 >= n)
      return 1.0;
    return 0.0;
  }
  if (vis[i][j])
    return dp[i][j];
  vis[i][j] = 1;
  return dp[i][j] = a[i] * calc(i + 1, j + 1) + (1.0 - a[i]) * calc(i + 1, j);
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  LL q, i, j = 0, temp, t, k, ans = 0, sum = 0, x, y, z, cnt = 0, m, fg = 0,
           mx = 0, mx1 = 0, mn = 8000000000000000000, mn1 = 8000000000000000000;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  cout << fixed << setprecision(10) << calc(0, 0) << endl;
}