Problem p03168 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define ALL(c) (c).begin(), (c).end()
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v, x) (std::find(v.begin(), v.end(), x) != v.end())
using namespace std;

using ll = long long;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline void dump(vector<T> v) {
  for (auto &x : v)
    cerr << x << " ";
  cerr << endl;
}
template <class T> inline void dump(vector<pair<T, T>> v) {
  for (auto &p : v)
    cerr << p.first << " " << p.second << endl;
}
template <class T> inline void dump(vector<vector<T>> vv) {
  for (auto &v : vv) {
    for (auto &x : v)
      cerr << x << " ";
    cerr << endl;
  }
}

constexpr int INF = 1e9 + 5;
constexpr long long INFLL = 1LL << 60;
constexpr double eps = (1e-9);

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;

  vector<double> p(n + 1, 0);
  rep(i, n) cin >> p[i + 1];

  vector<vector<double>> dp(
      n + 1, vector<double>(n + 1, 0)); // i 枚投げて表が j 枚出る確率
  dp[0][0] = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= i; j++) {
      if (j > 0)
        dp[i][j] = dp[i - 1][j] * (1 - p[i]) + dp[i - 1][j - 1] * p[i];
      else
        dp[i][j] = dp[i - 1][j] * (1 - p[i]);
    }
  }

  double ans = 0;
  for (int j = n / 2 + 1; j <= n; j++)
    ans += dp[n][j];

  dump(dp);

  printf("%.10f\n", ans);

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define ALL(c) (c).begin(), (c).end()
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v, x) (std::find(v.begin(), v.end(), x) != v.end())
using namespace std;

using ll = long long;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline void dump(vector<T> v) {
  for (auto &x : v)
    cerr << x << " ";
  cerr << endl;
}
template <class T> inline void dump(vector<pair<T, T>> v) {
  for (auto &p : v)
    cerr << p.first << " " << p.second << endl;
}
template <class T> inline void dump(vector<vector<T>> vv) {
  for (auto &v : vv) {
    for (auto &x : v)
      cerr << x << " ";
    cerr << endl;
  }
}

constexpr int INF = 1e9 + 5;
constexpr long long INFLL = 1LL << 60;
constexpr double eps = (1e-9);

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;

  vector<double> p(n + 1, 0);
  rep(i, n) cin >> p[i + 1];

  vector<vector<double>> dp(
      n + 1, vector<double>(n + 1, 0)); // i 枚投げて表が j 枚出る確率
  dp[0][0] = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= i; j++) {
      if (j > 0)
        dp[i][j] = dp[i - 1][j] * (1 - p[i]) + dp[i - 1][j - 1] * p[i];
      else
        dp[i][j] = dp[i - 1][j] * (1 - p[i]);
    }
  }

  double ans = 0;
  for (int j = n / 2 + 1; j <= n; j++)
    ans += dp[n][j];

  printf("%.10f\n", ans);

  return 0;
}