Problem p03168 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (ll i = (ll)(a); i <= (ll)(b); i++)
#define NFOR(i, a, b) for (ll i = (ll)(a); i >= (ll)(b); --i)
#define rep(i, a) for (ll i = 0; i < a; i++)
#define endl "\n"
#define fi first
#define se second
#define MOD 1000000007
#define inf 1e12
#define pb push_back
#define Case cout << "Case #" << ++cas << ": ";
#define fastio                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define all(v) v.begin(), v.end()

typedef long long ll;
typedef pair<ll, ll> pll;
typedef vector<ll> vl;
typedef vector<vl> vvl;

#define pr(...) dbs(#__VA_ARGS__, __VA_ARGS__)
template <class T> void dbs(string str, T t) {
  cerr << str << " : " << t << "\n";
}
template <class T, class... S> void dbs(string str, T t, S... s) {
  int idx = str.find(',');
  cerr << str.substr(0, idx) << " : " << t << ",";
  dbs(str.substr(idx + 1), s...);
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
  return os << "(" << p.first << ", " << p.second << ")";
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &p) {
  os << "[ ";
  for (auto &it : p)
    os << it << " ";
  return os << "]";
}
template <class T> ostream &operator<<(ostream &os, const multiset<T> &p) {
  os << "[ ";
  for (auto &it : p)
    os << it << " ";
  return os << "]";
}
template <class S, class T>
ostream &operator<<(ostream &os, const map<S, T> &p) {
  os << "[ ";
  for (auto &it : p)
    os << it << " ";
  return os << "]";
}
template <class T> void prc(T a, T b) {
  cerr << "[";
  for (T i = a; i != b; ++i) {
    if (i != a)
      cerr << ", ";
    cerr << *i;
  }
  cerr << "]\n";
}

void solve() {
  ll mod = 1000000007;
  ll n;
  cin >> n;
  vector<double> p(n);
  rep(i, n) cin >> p[i];

  vector<vector<double>> dp(2, vector<double>(n + 1, 0));

  dp[0][0] = 1.0;
  FOR(i, 1, n)
  FOR(j, 0, i) {
    dp[i % 2][j] = dp[(i + 1) % 2][j] * (1.0 - p[i - 1]) +
                   ((j >= 1) ? dp[(i + 1) % 2][j - 1] * p[i - 1] : 0);
    pr(i, j, dp[i % 2][j]);
  }

  double ans = 0;
  FOR(i, n / 2 + 1, n) ans += dp[n % 2][i];

  cout << setprecision(10) << fixed << ans << endl;
}

int main() {
  fastio;
  ll t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (ll i = (ll)(a); i <= (ll)(b); i++)
#define NFOR(i, a, b) for (ll i = (ll)(a); i >= (ll)(b); --i)
#define rep(i, a) for (ll i = 0; i < a; i++)
#define endl "\n"
#define fi first
#define se second
#define MOD 1000000007
#define inf 1e12
#define pb push_back
#define Case cout << "Case #" << ++cas << ": ";
#define fastio                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define all(v) v.begin(), v.end()

typedef long long ll;
typedef pair<ll, ll> pll;
typedef vector<ll> vl;
typedef vector<vl> vvl;

#define pr(...) dbs(#__VA_ARGS__, __VA_ARGS__)
template <class T> void dbs(string str, T t) {
  cerr << str << " : " << t << "\n";
}
template <class T, class... S> void dbs(string str, T t, S... s) {
  int idx = str.find(',');
  cerr << str.substr(0, idx) << " : " << t << ",";
  dbs(str.substr(idx + 1), s...);
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
  return os << "(" << p.first << ", " << p.second << ")";
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &p) {
  os << "[ ";
  for (auto &it : p)
    os << it << " ";
  return os << "]";
}
template <class T> ostream &operator<<(ostream &os, const multiset<T> &p) {
  os << "[ ";
  for (auto &it : p)
    os << it << " ";
  return os << "]";
}
template <class S, class T>
ostream &operator<<(ostream &os, const map<S, T> &p) {
  os << "[ ";
  for (auto &it : p)
    os << it << " ";
  return os << "]";
}
template <class T> void prc(T a, T b) {
  cerr << "[";
  for (T i = a; i != b; ++i) {
    if (i != a)
      cerr << ", ";
    cerr << *i;
  }
  cerr << "]\n";
}

void solve() {
  ll mod = 1000000007;
  ll n;
  cin >> n;
  vector<double> p(n);
  rep(i, n) cin >> p[i];

  vector<vector<double>> dp(2, vector<double>(n + 1, 0));

  dp[0][0] = 1.0;
  FOR(i, 1, n)
  FOR(j, 0, i)
  dp[i % 2][j] = dp[(i + 1) % 2][j] * (1.0 - p[i - 1]) +
                 ((j >= 1) ? dp[(i + 1) % 2][j - 1] * p[i - 1] : 0);

  double ans = 0;
  FOR(i, n / 2 + 1, n) ans += dp[n % 2][i];

  cout << setprecision(10) << fixed << ans << endl;
}

int main() {
  fastio;
  ll t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
}