Problem p03168 (C++)

Buggy submission (status: Runtime Error):
// Write correct codes,then there is no need to debug
// Implement after solving several problems
//                                   - GENNADY KOROTKEVICH

#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define pb push_back
#define mp make_pair
#define ll long long
#define ld long double
#define int int64_t
#define be(v) v.begin(), v.end()
#define ff(i, n) for (int i = 0; i < n; i++)
#define ii(it, v) for (auto it = v.begin(); it != v.end(); it++)
#define vii vector<vector<int>>
#define vi vector<int>
#define vP vector<pair<int, int>>
#define P pair<int, int>
#define case                                                                   \
  cout << "Case #" << hh + 1 << ": ";                                          \
  hh++; // int T;cin>>T;int hh=0;while(T--)
#define fi first
#define se second
#define endl "\n"
#define mem(a, b) memset(a, b, sizeof(a))
#define sf(n) scanf("%d", &n)
#define sff(a, b) scanf("%d %d", &a, &b)
#define sfff(a, b, c) scanf("%d %d %d", &a, &b, &c)
#define pfn(n) printf("%d\n", n)
#define pfs(n) printf("%d ", n)
#define H (ll)(1e9 + 7)
#define bg (ll)998244353

bool *Seive(ll n, bool *p) // boolean 1 means prime
{
  /*
   bool p[21];                    //write this in main()
  *p=Seive(20,p);   //replace 20 by the given value
  */

  mem(p, 1);
  ll i, j;
  for (i = 2; i <= n; i++) {
    if (p[i] == 1)
      for (j = i * i; j <= n; j += i) {
        p[j] = 0;
      }
  }
  p[1] = 0;
  return p;
}

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}

ll modexp(ll x, ll y) // x^y//Inverse modulo where y=H-2(use when H is prime)
{
  if (y == 0)
    return 1;
  else if (y % 2 == 0) {
    return modexp((x * x) % H, y / 2);
  } else {
    return (x * modexp((x * x) % H, (y - 1) / 2)) % H;
  }
}

ll fact(ll n) // n!
{
  ll f = 1;
  for (ll i = 2; i <= n; i++) {
    f = (i % H * f % H) % H;
  }
  return f;
}

ll nCr(ll n, ll r) {
  return (fact(n) * modexp((fact(n - r) * fact(r)) % H, H - 2)) % H;
}
void showlist(list<int> g) {
  list<int>::iterator it;
  for (it = g.begin(); it != g.end(); ++it)
    cout << *it << " ";
  cout << '\n';
}

void swap(int &a, int &b) {
  int t;
  t = a;
  a = b;
  b = t;
}
// Segment tree Functions
void build(int l, int r, int i) // array 'a[4*n+4]' is the segment tree
{
  if (l == r) {
    // a[i]++;
  } else {
    int mid = (l + r) / 2;
    build(l, mid, 2 * i);
    build(mid + 1, r, 2 * i + 1);
    // a[i]=a[2*i]+a[2*i+1];
  }
} // in main() : build(0,n-1,1);
void update(int p, int l, int r, int i) // update at position 'p'
{
  if (l == r) {
    // a[i]--;
    // a[i]++;
  } else {
    int mid = (l + r) / 2;
    if (p <= mid) {
      update(p, l, mid, 2 * i);
    } else {
      update(p, mid + 1, r, 2 * i + 1);
    }
    // a[i]--;
    // a[i]++;
  }
} // in main(): update(p,0,n-1,1);
void query(int y, int z, int l, int r,
           int i) // query: find ans for range y to z
{
  if (y <= l && z >= r) {
  } else {
    int mid = (l + r) / 2;
    if (z <= mid) {
      query(y, z, l, mid, 2 * i);
    } else if (y > mid) {
      query(y, z, mid + 1, r, 2 * i + 1);
    } else {
      query(y, z, l, mid, 2 * i);
      query(y, z, mid + 1, r, 2 * i + 1);
    }
  }
} // in main(): query(y,z,0,n-1);

// dsu Functions
void make_set(int v) {
  /* parent[v] = v;
   size[v] = 1;*/
}
int find_set(int v) {
  /*if (v == parent[v])
      return v;
  return parent[v] = find_set(parent[v]);*/
}
void union_sets(int a, int b) {
  /*a = find_set(a);
  b = find_set(b);
  if (a != b) {
      if (size[a] < size[b])
          swap(a, b);
      parent[b] = a;
      size[a] += size[b];
  }*/
}

int32_t main() {

  IOS
#ifndef ONLINE_JUDGE
      freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  /*    int T,hh=0;
      cin>>T;
      while(T--)
      {

      }
     */
  int n, i, j;
  cin >> n;
  ld a[n], x;
  ld m = 1;
  for (i = 0; i < n; i++) {
    cin >> a[i];
    m *= a[i];
  }
  ld g[n + 1][n + 1];
  for (i = 0; i <= n; i++) {
    g[i][0] = 0;
  }

  for (i = 1; i <= n; i++) {
    g[1][i] = g[1][i - 1] + (m / a[i - 1]) * (1 - a[i - 1]);
  }
  for (i = 2; i <= n / 2; i++) {
    for (j = i; j <= n; j++) {
      g[i][j - i + 1] =
          g[i][j - i] + (g[i - 1][j - i + 1] / a[j - 1]) * (1 - a[j - 1]);
    }
  }
  ld s = m;
  for (i = 1; i <= n / 2; i++) {
    s += g[i][n - i + 1];
  }
  cout << fixed << setprecision(10) << s << endl;

  return 0;
}


Fixed submission:
// Write correct codes,then there is no need to debug
// Implement after solving several problems
//                                   - GENNADY KOROTKEVICH

#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define pb push_back
#define mp make_pair
#define ll long long
#define ld long double
#define int int64_t
#define be(v) v.begin(), v.end()
#define ff(i, n) for (int i = 0; i < n; i++)
#define ii(it, v) for (auto it = v.begin(); it != v.end(); it++)
#define vii vector<vector<int>>
#define vi vector<int>
#define vP vector<pair<int, int>>
#define P pair<int, int>
#define case                                                                   \
  cout << "Case #" << hh + 1 << ": ";                                          \
  hh++; // int T;cin>>T;int hh=0;while(T--)
#define fi first
#define se second
#define endl "\n"
#define mem(a, b) memset(a, b, sizeof(a))
#define sf(n) scanf("%d", &n)
#define sff(a, b) scanf("%d %d", &a, &b)
#define sfff(a, b, c) scanf("%d %d %d", &a, &b, &c)
#define pfn(n) printf("%d\n", n)
#define pfs(n) printf("%d ", n)
#define H (ll)(1e9 + 7)
#define bg (ll)998244353

bool *Seive(ll n, bool *p) // boolean 1 means prime
{
  /*
   bool p[21];                    //write this in main()
  *p=Seive(20,p);   //replace 20 by the given value
  */

  mem(p, 1);
  ll i, j;
  for (i = 2; i <= n; i++) {
    if (p[i] == 1)
      for (j = i * i; j <= n; j += i) {
        p[j] = 0;
      }
  }
  p[1] = 0;
  return p;
}

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}

ll modexp(ll x, ll y) // x^y//Inverse modulo where y=H-2(use when H is prime)
{
  if (y == 0)
    return 1;
  else if (y % 2 == 0) {
    return modexp((x * x) % H, y / 2);
  } else {
    return (x * modexp((x * x) % H, (y - 1) / 2)) % H;
  }
}

ll fact(ll n) // n!
{
  ll f = 1;
  for (ll i = 2; i <= n; i++) {
    f = (i % H * f % H) % H;
  }
  return f;
}

ll nCr(ll n, ll r) {
  return (fact(n) * modexp((fact(n - r) * fact(r)) % H, H - 2)) % H;
}
void showlist(list<int> g) {
  list<int>::iterator it;
  for (it = g.begin(); it != g.end(); ++it)
    cout << *it << " ";
  cout << '\n';
}

void swap(int &a, int &b) {
  int t;
  t = a;
  a = b;
  b = t;
}
// Segment tree Functions
void build(int l, int r, int i) // array 'a[4*n+4]' is the segment tree
{
  if (l == r) {
    // a[i]++;
  } else {
    int mid = (l + r) / 2;
    build(l, mid, 2 * i);
    build(mid + 1, r, 2 * i + 1);
    // a[i]=a[2*i]+a[2*i+1];
  }
} // in main() : build(0,n-1,1);
void update(int p, int l, int r, int i) // update at position 'p'
{
  if (l == r) {
    // a[i]--;
    // a[i]++;
  } else {
    int mid = (l + r) / 2;
    if (p <= mid) {
      update(p, l, mid, 2 * i);
    } else {
      update(p, mid + 1, r, 2 * i + 1);
    }
    // a[i]--;
    // a[i]++;
  }
} // in main(): update(p,0,n-1,1);
void query(int y, int z, int l, int r,
           int i) // query: find ans for range y to z
{
  if (y <= l && z >= r) {
  } else {
    int mid = (l + r) / 2;
    if (z <= mid) {
      query(y, z, l, mid, 2 * i);
    } else if (y > mid) {
      query(y, z, mid + 1, r, 2 * i + 1);
    } else {
      query(y, z, l, mid, 2 * i);
      query(y, z, mid + 1, r, 2 * i + 1);
    }
  }
} // in main(): query(y,z,0,n-1);

// dsu Functions
void make_set(int v) {
  /* parent[v] = v;
   size[v] = 1;*/
}
int find_set(int v) {
  /*if (v == parent[v])
      return v;
  return parent[v] = find_set(parent[v]);*/
}
void union_sets(int a, int b) {
  /*a = find_set(a);
  b = find_set(b);
  if (a != b) {
      if (size[a] < size[b])
          swap(a, b);
      parent[b] = a;
      size[a] += size[b];
  }*/
}

int32_t main() {

  IOS

      /*    int T,hh=0;
          cin>>T;
          while(T--)
          {

          }
         */
      int n,
      i, j;
  cin >> n;
  ld a[n], x;
  ld m = 1;
  for (i = 0; i < n; i++) {
    cin >> a[i];
    m *= a[i];
  }
  ld g[n + 1][n + 1];
  for (i = 0; i <= n; i++) {
    g[i][0] = 0;
  }

  for (i = 1; i <= n; i++) {
    g[1][i] = g[1][i - 1] + (m / a[i - 1]) * (1 - a[i - 1]);
  }
  for (i = 2; i <= n / 2; i++) {
    for (j = i; j <= n; j++) {
      g[i][j - i + 1] =
          g[i][j - i] + (g[i - 1][j - i + 1] / a[j - 1]) * (1 - a[j - 1]);
    }
  }
  ld s = m;
  for (i = 1; i <= n / 2; i++) {
    s += g[i][n - i + 1];
  }
  cout << fixed << setprecision(10) << s << endl;

  return 0;
}