Problem p03168 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define F first
#define S second
#define ld long douroote
#define pb push_back
#define sz size
#define ll long long
#define ull unsigned long long
#define INF 0x7f7f7f7f
#define sc(a) scanf("%d", &a)
#define scll(a) scanf("%lld", &a)
#define scd(a) scanf("%lf", &a)
#define scc(a) scanf(" %c", &a)
#define scs(a) scanf(" %s", a)
#define me(a, b) memset(a, b, sizeof a)
#define all(a) a.begin(), a.end()
#define allr(a, n) a, a + n
#define loop(a, s, e) for (ll a = s; a <= e; a++)
#define read_arr(a, s, n)                                                      \
  for (int i = s; i < n + s; i++) {                                            \
    sc(a[i]);                                                                  \
  }
#define read_arr_ll(a, s, n)                                                   \
  for (int i = s; i < n + s; i++) {                                            \
    scll(a[i]);                                                                \
  }
#define err(a, s) cerr << a << s;
#define err_arr(a, s, n)                                                       \
  for (int i = s; i < n + s; i++) {                                            \
    cerr << a[i] << " ";                                                       \
  }                                                                            \
  cerr << endl;
#define prtll(x) printf("%lld", x);
#define prt(x) printf("%d", x);
using namespace std;
const int N = 1e4 + 10;
int n, m;
bool vis[N][N];
double mem[N][N], a[N];
double dp(int i, int co) {
  if ((co > ((n) / 2))) {
    return 0.0;
  }
  if (i == n) {
    return (co <= ((n) / 2));
  }
  if (vis[i][co]) {
    return mem[i][co];
  }
  double &ret = mem[i][co];
  ret = 0.0;
  ret += 1.0 * a[i] * (dp(i + 1, co));
  ret += 1.0 * (1.0 - a[i]) * (dp(i + 1, co + 1));
  return ret;
}
int main() {
  cin >> n;
  loop(i, 0, n - 1) { cin >> a[i]; }
  double ans = dp(0, 0);
  printf("%.9lf\n", ans);
  return 0;
}
/**
 **/


Fixed submission:
#include <bits/stdc++.h>
#define F first
#define S second
#define ld long douroote
#define pb push_back
#define sz size
#define ll long long
#define ull unsigned long long
#define INF 0x7f7f7f7f
#define sc(a) scanf("%d", &a)
#define scll(a) scanf("%lld", &a)
#define scd(a) scanf("%lf", &a)
#define scc(a) scanf(" %c", &a)
#define scs(a) scanf(" %s", a)
#define me(a, b) memset(a, b, sizeof a)
#define all(a) a.begin(), a.end()
#define allr(a, n) a, a + n
#define loop(a, s, e) for (ll a = s; a <= e; a++)
#define read_arr(a, s, n)                                                      \
  for (int i = s; i < n + s; i++) {                                            \
    sc(a[i]);                                                                  \
  }
#define read_arr_ll(a, s, n)                                                   \
  for (int i = s; i < n + s; i++) {                                            \
    scll(a[i]);                                                                \
  }
#define err(a, s) cerr << a << s;
#define err_arr(a, s, n)                                                       \
  for (int i = s; i < n + s; i++) {                                            \
    cerr << a[i] << " ";                                                       \
  }                                                                            \
  cerr << endl;
#define prtll(x) printf("%lld", x);
#define prt(x) printf("%d", x);
using namespace std;
const int N = 1e4 + 10;
int n, m;
bool vis[N][N];
double mem[N][N], a[N];
double dp(int i, int co) {
  if ((co > ((n) / 2))) {
    return 0.0;
  }
  if (i == n) {
    return (co <= ((n) / 2));
  }
  if (vis[i][co]) {
    return mem[i][co];
  }
  double &ret = mem[i][co];
  ret = 0.0;
  ret += 1.0 * a[i] * (dp(i + 1, co));
  ret += 1.0 * (1.0 - a[i]) * (dp(i + 1, co + 1));
  vis[i][co] = 1;
  return ret;
}
int main() {
  cin >> n;
  loop(i, 0, n - 1) { cin >> a[i]; }
  double ans = dp(0, 0);
  printf("%.9lf\n", ans);
  return 0;
}
/**
 **/