Problem p03168 (C++)

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

#define int long long
// #define mp      make_pair
#define eb emplace_back
#define pb push_back
#define ss second
#define ff first
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define mod 998244353
#define MOD (1000 * 1000 * 1000 + 7)
#define MN LLONG_MIN
#define MX LLONG_MAX
#define v1d vector<int>
#define v2d vector<vector<int>>
#define vip vector<pair<int, int>>
#define v1s vector<string>
#define pa pair<int, int>
#define mxpq(T) priority_queue<T>
#define mnpq(T) priority_queue<T, vector<T>, greater<T>>
#define print(v)                                                               \
  for (auto i : v)                                                             \
    cout << i << " ";                                                          \
  cout << "\n";
#define p2d(v)                                                                 \
  for (auto a : v) {                                                           \
    for (auto b : a)                                                           \
      cout << b << " ";                                                        \
    cout << endl;                                                              \
  }
#define p1d(v)                                                                 \
  for (auto a : v)                                                             \
    cout << a << " ";                                                          \
  cout << endl;
#define ppd(v)                                                                 \
  for (auto a : v)                                                             \
    cout << a.ff << " " << a.ss << endl;
#define endl "\n"
#define input(b, n)                                                            \
  for (int i = 0; i < n; i++)                                                  \
    cin >> b[i + 1];
#define Sort(v) sort(v.begin(), v.end())
#define RSort(v) sort(v.rbegin(), v.rend())
#define all(v) v.begin(), v.end()
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
// string s = bitset<64>(a).to_string();

void azmuth(int n = 12) {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << setprecision(n) << fixed;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}
int lb = 10000000;

//____________________________________________________________________________________

int n;
vector<double> p;
vector<vector<double>> dp;

double fun(int idx, int head) {
  if (idx > n) {
    if (head >= (n + 1) / 2)
      return 1.0;
    else
      return 0;
  }
  if (dp[idx][head] != -1)
    return dp[idx][head];
  double a = p[idx] * fun(idx + 1, head + 1);
  double b = (1 - p[idx]) * fun(idx + 1, head);
  return dp[idx][head] = a + b;
}

void solve() {
  cin >> n;
  dp.resize(n + 1, vector<double>(n + 1, -1));
  p.resize(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> p[i];
  cout << fixed << fun(0, 0);
}

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

  return 0;
}

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

#define int long long
// #define mp      make_pair
#define eb emplace_back
#define pb push_back
#define ss second
#define ff first
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define mod 998244353
#define MOD (1000 * 1000 * 1000 + 7)
#define MN LLONG_MIN
#define MX LLONG_MAX
#define v1d vector<int>
#define v2d vector<vector<int>>
#define vip vector<pair<int, int>>
#define v1s vector<string>
#define pa pair<int, int>
#define mxpq(T) priority_queue<T>
#define mnpq(T) priority_queue<T, vector<T>, greater<T>>
#define print(v)                                                               \
  for (auto i : v)                                                             \
    cout << i << " ";                                                          \
  cout << "\n";
#define p2d(v)                                                                 \
  for (auto a : v) {                                                           \
    for (auto b : a)                                                           \
      cout << b << " ";                                                        \
    cout << endl;                                                              \
  }
#define p1d(v)                                                                 \
  for (auto a : v)                                                             \
    cout << a << " ";                                                          \
  cout << endl;
#define ppd(v)                                                                 \
  for (auto a : v)                                                             \
    cout << a.ff << " " << a.ss << endl;
#define endl "\n"
#define input(b, n)                                                            \
  for (int i = 0; i < n; i++)                                                  \
    cin >> b[i + 1];
#define Sort(v) sort(v.begin(), v.end())
#define RSort(v) sort(v.rbegin(), v.rend())
#define all(v) v.begin(), v.end()
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
// string s = bitset<64>(a).to_string();

void azmuth(int n = 12) {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << setprecision(n) << fixed;
}
int lb = 10000000;

//____________________________________________________________________________________

int n;
vector<double> p;
vector<vector<double>> dp;

double fun(int idx, int head) {
  if (idx > n) {
    if (head >= (n + 1) / 2)
      return 1.0;
    else
      return 0;
  }
  if (dp[idx][head] != -1)
    return dp[idx][head];
  double a = p[idx] * fun(idx + 1, head + 1);
  double b = (1 - p[idx]) * fun(idx + 1, head);
  return dp[idx][head] = a + b;
}

void solve() {
  cin >> n;
  dp.resize(n + 1, vector<double>(n + 1, -1));
  p.resize(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> p[i];
  cout << fixed << fun(0, 0);
}

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

  return 0;
}