Problem p03168 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

#define d(x) cerr << #x ":" << x << endl;
#define dd(x, y) cerr << "(" #x "," #y "):(" << x << "," << y << ")" << endl
#define rep(i, n) for (int i = (int)(0); i < (int)(n); i++)
#define repp(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define all(v) v.begin(), v.end()
#define dump(v)                                                                \
  cerr << #v ":[ ";                                                            \
  for (auto macro_vi : v) {                                                    \
    cerr << macro_vi << " ";                                                   \
  }                                                                            \
  cerr << "]" << endl;
#define ddump(v)                                                               \
  cerr << #v ":" << endl;                                                      \
  for (auto macro_row : v) {                                                   \
    cerr << "[";                                                               \
    for (auto macro__vi : macro_row) {                                         \
      cerr << macro__vi << " ";                                                \
    }                                                                          \
    cerr << "]" << endl;                                                       \
  }
using lint = long long;
const int INF = 1e9;
const lint LINF = 1e18;
const lint MOD = 1e9 + 7;
const double EPS = 1e-10;

int main() {
  int N;
  cin >> N;
  vector<long double> p(N);
  rep(i, N) cin >> p[i];

  // 状態 (i,j) : i 枚コインを投げて，j 枚 表面がでた状態
  // per(i,j) : 状態(i,j) となる確率
  vector<vector<long double>> per(N + 1, vector<long double>(N + 1, 0));
  ddump(per);
  per[0][0] = 1;

  for (int i = 1; i <= N; i++) {
    for (int j = 0; j <= N; j++) {
      if (0 <= j - 1)
        per[i][j] += per[i - 1][j - 1] * p[i - 1];
      per[i][j] += per[i - 1][j] * (1 - p[i - 1]);
    }
  }

  // ddump(per);

  long double ans = 0;
  for (int i = 0; i <= N; i++) {
    if (i > N / 2)
      ans += per[N][i];
  }
  cout << setprecision(10) << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define d(x) cerr << #x ":" << x << endl;
#define dd(x, y) cerr << "(" #x "," #y "):(" << x << "," << y << ")" << endl
#define rep(i, n) for (int i = (int)(0); i < (int)(n); i++)
#define repp(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define all(v) v.begin(), v.end()
#define dump(v)                                                                \
  cerr << #v ":[ ";                                                            \
  for (auto macro_vi : v) {                                                    \
    cerr << macro_vi << " ";                                                   \
  }                                                                            \
  cerr << "]" << endl;
#define ddump(v)                                                               \
  cerr << #v ":" << endl;                                                      \
  for (auto macro_row : v) {                                                   \
    cerr << "[";                                                               \
    for (auto macro__vi : macro_row) {                                         \
      cerr << macro__vi << " ";                                                \
    }                                                                          \
    cerr << "]" << endl;                                                       \
  }
using lint = long long;
const int INF = 1e9;
const lint LINF = 1e18;
const lint MOD = 1e9 + 7;
const double EPS = 1e-10;

int main() {
  int N;
  cin >> N;
  vector<long double> p(N);
  rep(i, N) cin >> p[i];

  // 状態 (i,j) : i 枚コインを投げて，j 枚 表面がでた状態
  // per(i,j) : 状態(i,j) となる確率
  vector<vector<long double>> per(N + 1, vector<long double>(N + 1, 0));
  // ddump(per);
  per[0][0] = 1;

  for (int i = 1; i <= N; i++) {
    for (int j = 0; j <= N; j++) {
      if (0 <= j - 1)
        per[i][j] += per[i - 1][j - 1] * p[i - 1];
      per[i][j] += per[i - 1][j] * (1 - p[i - 1]);
    }
  }

  // ddump(per);

  long double ans = 0;
  for (int i = 0; i <= N; i++) {
    if (i > N / 2)
      ans += per[N][i];
  }
  cout << setprecision(10) << ans << endl;

  return 0;
}