Problem p03168 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pullull;
typedef pair<ll, int> plli;
typedef pair<int, pii> pipii;
typedef vector<vector<int>> mati;
typedef vector<vector<double>> matd;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;

#define rep(i, x, y) for (int i = (x); i < (y); ++i)

template <typename T> void vec_print(vector<T> VEC) {
  rep(i, 0, VEC.size()) { cout << VEC[i] << " "; }
  cout << "\n";
};

template <typename T>
void mat_print(vector<vector<T>> MAT){
    rep(i, 0, MAT.size()){rep(j, 0, MAT[i].size()){cout << MAT[i][j] << " ";
}
cout << "\n";
}
}
;

template <typename CLASS1, typename CLASS2> class HOGE {
public:
  CLASS1 key;
  CLASS2 value;
  HOGE(ll key, ll value) {
    this->key = index;
    this->value = value;
  };
  ~HOGE(void) { return; };

  void print(void) {
    cout << "key : " << key << ", value : " << value << "\n";
    return;
  };

  bool operator==(const HOGE &obj) { return (this->value == obj.value); };
  bool operator<(const HOGE &obj) { return (this->value < obj.value); };
  bool operator>(const HOGE &obj) { return (this->value > obj.value); };
};

constexpr int INF = (1 << 30);
constexpr ll INFLL = 1LL << 62;
constexpr long double EPS = 1e-12;

void calc_dp(const int start, const vector<double> &p, vector<double> &dp) {
  if (start == p.size() - 1) {
    dp[0] = 1.0 - p[start];
    dp[1] = p[start];
    return;
  }
  calc_dp(start + 1, p, dp);
  dp[p.size() - start] = dp[p.size() - start - 1] * p[start];
  for (int i = p.size() - start - 1; i > 0; --i) {
    dp[i] = dp[i] * (1.0 - p[start]) + dp[i - 1] * p[start];
  }
  dp[0] = dp[0] * (1.0 - p[start]);
  return;
}

int main() {
  cin.tie(0); // cut the cin and cout (default, std::flush is performed after
              // std::cin)
  ios::sync_with_stdio(
      false); // cut the iostream and stdio (DON'T endl; BUT "\n";)

  ll N;
  cin >> N;
  // ans 1
  /*
  vector<double> p(N);
  rep(i, 0, N){
    cin >> p[i];
  }
  vector<double> dp(N+1, 0.0);
  calc_dp(0, p, dp);
  */

  // ans 2
  vector<double> dp(N / 2 + 1, 0.0);
  double tmp;
  cin >> tmp;
  dp[0] = tmp;
  dp[1] = 1.0 - tmp;
  rep(i, 1, N / 2 + 1) {
    cin >> tmp;
    dp[i + 1] = dp[i] * (1.0 - tmp);
    for (int j = i; j > 0; --j) {
      dp[j] = dp[j] * tmp + dp[j - 1] * (1.0 - tmp);
    }
    dp[0] = dp[0] * tmp;
  }
  rep(i, N / 2 + 1, N) {
    cin >> tmp;
    for (int j = N / 2; j > 0; --j) {
      dp[j] = dp[j] * tmp + dp[j - 1] * (1.0 - tmp);
    }
    dp[0] = dp[0] * tmp;
  }

  double ans = 0.0;
  rep(i, 0, N / 2 + 1) { ans += dp[i]; }

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

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pullull;
typedef pair<ll, int> plli;
typedef pair<int, pii> pipii;
typedef vector<vector<int>> mati;
typedef vector<vector<double>> matd;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;

#define rep(i, x, y) for (int i = (x); i < (y); ++i)

template <typename T> void vec_print(vector<T> VEC) {
  rep(i, 0, VEC.size()) { cout << VEC[i] << " "; }
  cout << "\n";
};

template <typename T>
void mat_print(vector<vector<T>> MAT){
    rep(i, 0, MAT.size()){rep(j, 0, MAT[i].size()){cout << MAT[i][j] << " ";
}
cout << "\n";
}
}
;

template <typename CLASS1, typename CLASS2> class HOGE {
public:
  CLASS1 key;
  CLASS2 value;
  HOGE(ll key, ll value) {
    this->key = index;
    this->value = value;
  };
  ~HOGE(void) { return; };

  void print(void) {
    cout << "key : " << key << ", value : " << value << "\n";
    return;
  };

  bool operator==(const HOGE &obj) { return (this->value == obj.value); };
  bool operator<(const HOGE &obj) { return (this->value < obj.value); };
  bool operator>(const HOGE &obj) { return (this->value > obj.value); };
};

constexpr int INF = (1 << 30);
constexpr ll INFLL = 1LL << 62;
constexpr long double EPS = 1e-12;

void calc_dp(const int start, const vector<double> &p, vector<double> &dp) {
  if (start == p.size() - 1) {
    dp[0] = 1.0 - p[start];
    dp[1] = p[start];
    return;
  }
  calc_dp(start + 1, p, dp);
  dp[p.size() - start] = dp[p.size() - start - 1] * p[start];
  for (int i = p.size() - start - 1; i > 0; --i) {
    dp[i] = dp[i] * (1.0 - p[start]) + dp[i - 1] * p[start];
  }
  dp[0] = dp[0] * (1.0 - p[start]);
  return;
}

int main() {
  cin.tie(0); // cut the cin and cout (default, std::flush is performed after
              // std::cin)
  ios::sync_with_stdio(
      false); // cut the iostream and stdio (DON'T endl; BUT "\n";)

  ll N;
  cin >> N;
  // ans 1
  /*
  vector<double> p(N);
  rep(i, 0, N){
    cin >> p[i];
  }
  vector<double> dp(N+1, 0.0);
  calc_dp(0, p, dp);
  */

  // ans 2
  vector<double> dp(N / 2 + 2, 0.0);
  double tmp;
  cin >> tmp;
  dp[0] = tmp;
  dp[1] = 1.0 - tmp;
  rep(i, 1, N / 2 + 1) {
    cin >> tmp;
    dp[i + 1] = dp[i] * (1.0 - tmp);
    for (int j = i; j > 0; --j) {
      dp[j] = dp[j] * tmp + dp[j - 1] * (1.0 - tmp);
    }
    dp[0] = dp[0] * tmp;
  }
  rep(i, N / 2 + 1, N) {
    cin >> tmp;
    for (int j = N / 2; j > 0; --j) {
      dp[j] = dp[j] * tmp + dp[j - 1] * (1.0 - tmp);
    }
    dp[0] = dp[0] * tmp;
  }

  double ans = 0.0;
  rep(i, 0, N / 2 + 1) { ans += dp[i]; }

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

  return 0;
}