prithivMLmods/Coder-Stat huggingface dataset:
Problem p03168 (C++)

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

#define pr_double(x) cout << fixed << setprecision(10) << x;

double dp[3001][3001];
double solve(vector<double> &ar, int i, int x) {

  if (x == 0) {
    return 1;
  } else if (i == 0) {
    return 0;
  }

  if (dp[i][x] > 0.9) {
    return dp[i][x];
  }

  return dp[i][x] = ar[i] * solve(ar, i - 1, x - 1) +
                    (1 - ar[i]) * solve(ar, i - 1, x);
}

int main() {

  int n;
  cin >> n;
  memset(dp, -1, sizeof dp);
  vector<double> ar(n + 1);

  for (int i = 1; i < n + 1; i++) {
    cin >> ar[i];
  }
  pr_double(solve(ar, n, (n + 1) / 2));
}

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

#define pr_double(x) cout << fixed << setprecision(10) << x;

double dp[3001][3001];
double solve(vector<double> &ar, int i, int x) {

  if (x == 0) {
    return 1;
  } else if (i == 0) {
    return 0;
  }

  if (dp[i][x] > -0.9) {
    return dp[i][x];
  }

  return dp[i][x] = ar[i] * solve(ar, i - 1, x - 1) +
                    (1 - ar[i]) * solve(ar, i - 1, x);
}

int main() {

  int n;
  cin >> n;
  memset(dp, -1, sizeof dp);
  vector<double> ar(n + 1);

  for (int i = 1; i < n + 1; i++) {
    cin >> ar[i];
  }
  pr_double(solve(ar, n, (n + 1) / 2));
}
Problem p03168 (C++)

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

const int MAXN = 300 + 5;

using namespace std;

int n;
long double dp[MAXN][MAXN], probability[MAXN];

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> probability[i];
  dp[0][0] = 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= i; j++) {
      dp[i + 1][j] += dp[i][j] * (1 - probability[i + 1]);
      dp[i + 1][j + 1] += dp[i][j] * probability[i + 1];
    }
  }

  long double ans = 0;

  for (int j = n / 2 + 1; j <= n; j++)
    ans += dp[n][j];
  cout << setprecision(10) << fixed << ans;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

const int MAXN = 3000 + 5;

using namespace std;

int n;
long double dp[MAXN][MAXN], probability[MAXN];

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> probability[i];
  dp[0][0] = 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= i; j++) {
      dp[i + 1][j] += dp[i][j] * (1 - probability[i + 1]);
      dp[i + 1][j + 1] += dp[i][j] * probability[i + 1];
    }
  }

  long double ans = 0;

  for (int j = n / 2 + 1; j <= n; j++)
    ans += dp[n][j];
  cout << setprecision(10) << fixed << ans;

  return 0;
}
Problem p03168 (Python)

Buggy submission (status: Time Limit Exceeded):
N = int(input())
P = list(map(float, input().split()))

dp = [0] * (N + 1)
dp[0] = 1
for i in range(1, N + 1):
    tmp = dp
    dp = [0] * (N + 1)
    for j in range(N):
        if j < N:
            dp[j + 1] += tmp[j] * P[i - 1]
        dp[j] += tmp[j] * (1 - P[i - 1])

num = 0
for i in range(N // 2 + 1, N + 1):
    num += dp[i]

print(num)


Fixed submission:
N = int(input())
P = list(map(float, input().split()))
# dp = [[0] * (N + 1) for i in range(N + 1)]
# dp[0][0] = 1.0
# for i in range(1, N + 1):
#     for j in range(N):
#         if j < N:
#             dp[i][j + 1] += dp[i - 1][j] * P[i - 1]
#         dp[i][j] += dp[i - 1][j] * (1 - P[i - 1])
#
# num = 0
# for i in range(N // 2 + 1, N + 1):
#     num += dp[N][i]
#
# print(num)


dp = [0] * (N + 1)
dp[0] = 1
for i in range(1, N + 1):
    tmp = dp
    dp = [0] * (N + 1)
    for j in range(N):
        if j < N:
            dp[j + 1] += tmp[j] * P[i - 1]
        dp[j] += tmp[j] * (1 - P[i - 1])

num = 0
for i in range(N // 2 + 1, N + 1):
    num += dp[i]

print(num)
Problem p03168 (Python)

Buggy submission (status: Time Limit Exceeded):
# -*- coding: utf-8 -*-
# from collections import deque
from sys import setrecursionlimit

setrecursionlimit(1000000)


# INF = float("inf")

# d = deque()
# ---Input---#
n = int(input())
# n, k = map(int, input().split())
p = list(map(float, input().split()))
p = [-1] + p

memo = [[-1 for _ in [0] * (n + 1)] for _ in [0] * (n + 1)]
memo[0][0] = 1
for i in range(len(p))[1:]:
    memo[0][i] = 0
    memo[i][0] = memo[i - 1][0] * (1 - p[i])
memo[0][0] = 1


def dp(i, j):
    if memo[i][j] == -1:
        memo[i][j] = dp(i - 1, j) * (1 - p[i]) + dp(i - 1, j - 1) * p[i]
    return memo[i][j]


ans = 0
tar = int(n / 2) + 1
for i in range(n + 1)[tar:]:
    ans += dp(n, i)


print("{}".format(ans))


Fixed submission:
# -*- coding: utf-8 -*-
# from collections import deque
from sys import setrecursionlimit

setrecursionlimit(1000000)


# INF = float("inf")

# d = deque()
# ---Input---#
n = int(input())
# n, k = map(int, input().split())
p = list(map(float, input().split()))
p = [-1] + p

memo = [[-1 for _ in [0] * (n + 1)] for _ in [0] * (n + 1)]
memo[0][0] = 1
for i in range(len(p))[1:]:
    memo[0][i] = 0
    memo[i][0] = memo[i - 1][0] * (1 - p[i])
memo[0][0] = 1


def dp(i, j):
    if memo[i][j] == -1:
        memo[i][j] = dp(i - 1, j) * (1 - p[i]) + dp(i - 1, j - 1) * p[i]
    return memo[i][j]


ans = 0
tar = int(n / 2) + 1
for i in range(n + 1):
    dp(i, i)
    dp(i, n)

for i in range(n + 1)[tar:]:
    ans += dp(n, i)


print("{}".format(ans))
Problem p03168 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ll long long
using namespace std;
double dp[3001][3001];
double calc_prob(int n, vector<double> pH, ll HC, vector<vector<double>> &dp) {
  if (HC == 0) {
    return 1;
  }
  if (n < 0) {
    return 0;
  }
  if (dp[n][HC] > -0.9) {
    return dp[n][HC];
  }

  double ans = pH[n] * calc_prob(n - 1, pH, HC - 1, dp) +
               (1 - pH[n]) * calc_prob(n - 1, pH, HC, dp);
  return dp[n][HC] = ans;
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ll n;
  cin >> n;
  vector<double> pH(n);
  for (int i = 0; i < n; i++) {
    double f;
    cin >> f;
    pH[i] = f;
  }

  // for (int i = 0; i < n; i++)
  // {
  // 	for (int j = 0; j < (n + 1) / 2; j++)
  // 	{
  // 		cout << dp[i][j] << " ";
  // 	}
  // 	cout << endl;
  // }
  vector<vector<double>> dp(n + 1, vector<double>((n + 1) / 2 + 1, -1));
  cout << fixed << setprecision(10) << calc_prob(n - 1, pH, (n + 1) / 2, dp)
       << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
using namespace std;
double dp[3001][3001];
double calc_prob(int n, vector<double> &pH, ll HC, vector<vector<double>> &dp) {
  if (HC == 0) {
    return 1;
  }
  if (n < 0) {
    return 0;
  }
  if (dp[n][HC] > -0.9) {
    return dp[n][HC];
  }

  double ans = pH[n] * calc_prob(n - 1, pH, HC - 1, dp) +
               (1 - pH[n]) * calc_prob(n - 1, pH, HC, dp);
  return dp[n][HC] = ans;
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ll n;
  cin >> n;
  vector<double> pH(n);
  for (int i = 0; i < n; i++) {
    double f;
    cin >> f;
    pH[i] = f;
  }

  // for (int i = 0; i < n; i++)
  // {
  // 	for (int j = 0; j < (n + 1) / 2; j++)
  // 	{
  // 		cout << dp[i][j] << " ";
  // 	}
  // 	cout << endl;
  // }
  vector<vector<double>> dp(n + 1, vector<double>((n + 1) / 2 + 1, -1));
  cout << fixed << setprecision(10) << calc_prob(n - 1, pH, (n + 1) / 2, dp)
       << endl;
  return 0;
}
Problem p03168 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ll long long
using namespace std;
double dp[3001][3001];
double calc_prob(int n, vector<double> pH, ll HC) {
  if (HC == 0) {
    return 1;
  }
  if (n < 0) {
    return 0;
  }
  if (dp[n][HC] > -0.9) {
    return dp[n][HC];
  }

  double ans = pH[n] * calc_prob(n - 1, pH, HC - 1) +
               (1 - pH[n]) * calc_prob(n - 1, pH, HC);
  return dp[n][HC] = ans;
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ll n;
  cin >> n;
  vector<double> pH(n);
  for (int i = 0; i < n; i++) {
    double f;
    cin >> f;
    pH[i] = f;
  }

  // for (int i = 0; i < n; i++)
  // {
  // 	for (int j = 0; j < (n + 1) / 2; j++)
  // 	{
  // 		cout << dp[i][j] << " ";
  // 	}
  // 	cout << endl;
  // }
  memset(dp, -1, sizeof(dp));
  cout << fixed << setprecision(10) << calc_prob(n - 1, pH, (n + 1) / 2);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
using namespace std;
double dp[3001][3001];
double calc_prob(int n, vector<double> &pH, ll HC) {
  if (HC == 0) {
    return 1;
  }
  if (n < 0) {
    return 0;
  }
  if (dp[n][HC] > -0.9) {
    return dp[n][HC];
  }

  double ans = pH[n] * calc_prob(n - 1, pH, HC - 1) +
               (1 - pH[n]) * calc_prob(n - 1, pH, HC);
  return dp[n][HC] = ans;
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ll n;
  cin >> n;
  vector<double> pH(n);
  for (int i = 0; i < n; i++) {
    double f;
    cin >> f;
    pH[i] = f;
  }

  // for (int i = 0; i < n; i++)
  // {
  // 	for (int j = 0; j < (n + 1) / 2; j++)
  // 	{
  // 		cout << dp[i][j] << " ";
  // 	}
  // 	cout << endl;
  // }
  memset(dp, -1, sizeof(dp));
  cout << fixed << setprecision(10) << calc_prob(n - 1, pH, (n + 1) / 2);
  return 0;
}
Problem p03168 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define pb push_back
#define mp make_pair
#define M 1000000007
using namespace std;

ld dp[3001][3001];

ld f(vector<ld> &a, int i, int x) {
  if (x == 0)
    return 1;
  if (i == 0)
    return 0;
  if (dp[i][x] > -0.9)
    return dp[i][x];
  return dp[i][x] = a[i] * f(a, i - 1, x - 1) + (1 - a[i]) * f(a, i - 1, x);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int n;
  cin >> n;
  memset(dp, -1, sizeof(dp));
  vector<ld> a(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  cout << fixed << setprecision(10) << f(a, n, (n + 1) / 2);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define pb push_back
#define mp make_pair
#define M 1000000007
using namespace std;

ld dp[3001][3001];

ld f(vector<ld> &a, int i, int x) {
  if (x == 0)
    return 1;
  if (i == 0)
    return 0;
  if (dp[i][x] > -0.9)
    return dp[i][x];
  return dp[i][x] = a[i] * f(a, i - 1, x - 1) + (1 - a[i]) * f(a, i - 1, x);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  // #ifndef ONLINE_JUDGE
  //	freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  // #endif
  int n;
  cin >> n;
  memset(dp, -1, sizeof(dp));
  vector<ld> a(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  cout << fixed << setprecision(10) << f(a, n, (n + 1) / 2);
  return 0;
}
Problem p03168 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ld long double
#define all(x) x.begin(), x.end()
#define fi first
#define se second
#define endl '\n'
const int mod = 1e9 + 7, u = 1e3 + 5;

ld dp[u][u];
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  int n;
  cin >> n;
  ld p[n + 1];
  for (int i = 1; i <= n; i++) {
    cin >> p[i];
  }

  dp[0][0] = 1;
  for (int i = 1; i <= n; i++) {
    dp[i][0] = dp[i - 1][0] * (1 - p[i]);
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      dp[i][j] = (dp[i - 1][j - 1] * p[i] + dp[i - 1][j] * (1 - p[i]));
    }
  }

  ld ans = 0;
  for (int i = n / 2 + 1; i <= n; i++) {
    ans += dp[n][i];
  }
  cout << fixed << setprecision(10) << ans;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ld long double
#define all(x) x.begin(), x.end()
#define fi first
#define se second
#define endl '\n'
const int mod = 1e9 + 7, u = 3e3 + 5;

ld dp[u][u];
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  int n;
  cin >> n;
  ld p[n + 1];
  for (int i = 1; i <= n; i++) {
    cin >> p[i];
  }

  dp[0][0] = 1;
  for (int i = 1; i <= n; i++) {
    dp[i][0] = dp[i - 1][0] * (1 - p[i]);
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      dp[i][j] = (dp[i - 1][j - 1] * p[i] + dp[i - 1][j] * (1 - p[i]));
    }
  }

  ld ans = 0;
  for (int i = n / 2 + 1; i <= n; i++) {
    ans += dp[n][i];
  }
  cout << fixed << setprecision(10) << ans;

  return 0;
}
Problem p03168 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set                                                            \
  tree<int, null_type, less_equal<int>, rb_tree_tag,                           \
       tree_order_statistics_node_update>
using namespace std;
#define ll long long
#define pb push_back
#define F first
#define S second
#define all(c) c.begin(), c.end()
#define llv vector<ll>
#define mp make_pair
#define endl "\n"
const int N = (int)1e5 + 10;
const int M = (int)1000000007;

int main() {
  // #ifndef ONLINE_JUDGE
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  // #endif
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  ll n, i, j;
  cin >> n;
  double p[n + 1];
  for (i = 1; i <= n; i++) {
    cin >> p[i];
  }
  double dp[n + 1][n + 1];
  fill(&dp[0][0], &dp[0][0] + sizeof(dp), 0.0);
  dp[0][0] = 1.0;
  for (i = 1; i <= n; i++) {
    dp[i][0] = dp[i - 1][0] * (1.0 - p[i]);
    for (j = 1; j <= i; j++) {
      dp[i][j] = p[i] * dp[i - 1][j - 1] + (1.0 - p[i]) * dp[i - 1][j];
    }
  }

  double ans = 0.0;
  for (i = (n + 1) / 2; i <= n; i++) {
    ans += dp[n][i];
  }
  cout << setprecision(10) << fixed << ans;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set                                                            \
  tree<int, null_type, less_equal<int>, rb_tree_tag,                           \
       tree_order_statistics_node_update>
using namespace std;
#define ll long long
#define pb push_back
#define F first
#define S second
#define all(c) c.begin(), c.end()
#define llv vector<ll>
#define mp make_pair
#define endl "\n"
const int N = (int)1e5 + 10;
const int M = (int)1000000007;

int main() {
  // #ifndef ONLINE_JUDGE
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  // #endif
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  ll n, i, j;
  cin >> n;
  double p[n + 1];
  for (i = 1; i <= n; i++) {
    cin >> p[i];
  }
  double dp[n + 1][n + 1];
  for (i = 0; i <= n; i++) {
    for (j = 0; j <= n; j++) {
      dp[i][j] = 0.0;
    }
  }
  dp[0][0] = 1.0;
  for (i = 1; i <= n; i++) {
    dp[i][0] = dp[i - 1][0] * (1.0 - p[i]);
    for (j = 1; j <= i; j++) {
      dp[i][j] = p[i] * dp[i - 1][j - 1] + (1.0 - p[i]) * dp[i - 1][j];
    }
  }

  double ans = 0.0;
  for (i = (n + 1) / 2; i <= n; i++) {
    ans += dp[n][i];
  }
  cout << setprecision(10) << fixed << ans;
  return 0;
}
Problem p03168 (C++)

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

using namespace std;
using ll = long long;
const ll N = 3001, mod = 1e9 + 7;

double p[N];
double dp[N][N];
int n;

double dfs(int pos, int k) {
  if (pos == n + 1) {
    return k == 0 ? 1.0 : 0.0;
  }
  double &ret = dp[pos][k];
  if (ret > 0)
    return ret;
  ret = 0.0;
  ret = p[pos] * dfs(pos + 1, k - 1);
  ret += (1.0 - p[pos]) * dfs(pos + 1, k);
  return ret;
}

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

  cin >> n;
  for (int i = 1; i <= n; i += 1)
    cin >> p[i];

  for (int i = 0; i <= n; i += 1)
    for (int j = 0; j <= n; j += 1)
      dp[i][j] = -1.0;

  double ans = 0.0;
  for (int i = n / 2 + 1; i <= n; i += 1)
    ans += dfs(1, i);
  cout << setprecision(10) << fixed << ans << '\n';
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
const ll N = 3001, mod = 1e9 + 7;

double p[N];
double dp[N][N];
int n;

double dfs(int pos, int k) {
  if (pos == n + 1) {
    return k == 0 ? 1.0 : 0.0;
  }
  double &ret = dp[pos][k];
  if (ret != -1.0)
    return ret;
  ret = 0.0;
  ret = p[pos] * dfs(pos + 1, k - 1);
  ret += (1.0 - p[pos]) * dfs(pos + 1, k);
  return ret;
}

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

  cin >> n;
  for (int i = 1; i <= n; i += 1)
    cin >> p[i];

  for (int i = 0; i <= n; i += 1)
    for (int j = 0; j <= n; j += 1)
      dp[i][j] = -1.0;

  double ans = 0.0;
  for (int i = n / 2 + 1; i <= n; i += 1)
    ans += dfs(1, i);
  cout << setprecision(10) << fixed << ans << '\n';
  return 0;
}
Problem p03168 (C++)

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

using namespace std;

const int N = 3e3 + 5;
int n;
double dp[N][N];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  cin >> n;

  dp[0][0] = 1;
  for (int i = 1; i <= n; ++i) {
    double p;
    cin >> p;
    for (int j = 0; j <= i; ++j) {
      int k = i - j;
      dp[j][k] = dp[j - 1][k] * p + dp[j][k - 1] * (1 - p);
    }
  }

  double ans = 0;
  for (int i = 0; i <= n; ++i)
    if (n - i < i)
      ans += dp[i][n - i];
  cout << fixed << setprecision(10) << ans;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

const int N = 3e3 + 5;
int n;
double dp[N][N];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  cin >> n;

  dp[0][0] = 1;
  for (int i = 1; i <= n; ++i) {
    double p;
    cin >> p;
    for (int j = 0; j <= i; ++j) {
      int k = i - j;
      if (j)
        dp[j][k] += dp[j - 1][k] * p;
      if (k)
        dp[j][k] += dp[j][k - 1] * (1 - p);
    }
  }

  double ans = 0;
  for (int i = 0; i <= n; ++i)
    if (n - i < i)
      ans += dp[i][n - i];
  cout << fixed << setprecision(10) << ans;

  return 0;
}
Problem p03168 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define ll long long
#define MOD 1000000007
double probs[3000];

double recur(int i, int n, int heads, int tails, double sa) {
  if (i == n) {
    if (heads > tails)
      return sa;
    else
      return 0;
  }

  double a = recur(i + 1, n, heads + 1, tails, (double)sa * probs[i]);
  double b = recur(i + 1, n, heads, tails + 1, (double)sa * (1 - probs[i]));
  return a + b;
}

int main() {

  ll n;
  cin >> n;

  for (int i = 0; i < n; i++)
    cin >> probs[i];

  double dp[n + 1][n + 1];
  memset(dp, 0, sizeof(dp));
  dp[0][0] = 1;

  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      if (i == 0 && j == 0)
        dp[i][j] = 1;

      if (j > i)
        dp[i][j] = 0;
      if (j - 1 >= 0)
        dp[i][j] += dp[i][j - 1] * (probs[i + j - 1]);

      if (i - 1 >= 0)
        dp[i][j] += dp[i - 1][j] * (1 - probs[i + j - 1]);
    }
  }
  double ans = 0;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      if ((i + j == n) && (j > i))
        ans += dp[i][j];
    }
  }

  cout << setprecision(10) << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define ll long long
#define MOD 1000000007
double probs[3000];

double recur(int i, int n, int heads, int tails, double sa) {
  if (i == n) {
    if (heads > tails)
      return sa;
    else
      return 0;
  }

  double a = recur(i + 1, n, heads + 1, tails, (double)sa * probs[i]);
  double b = recur(i + 1, n, heads, tails + 1, (double)sa * (1 - probs[i]));
  return a + b;
}

int main() {

  ll n;
  cin >> n;

  for (int i = 0; i < n; i++)
    cin >> probs[i];

  double dp[n + 1][n + 1];
  memset(dp, 0, sizeof(dp));
  dp[0][0] = 1;

  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      if (i == 0 && j == 0)
        dp[i][j] = 1;

      if (i + j > n)
        break;
      if (j > i)
        dp[i][j] = 0;
      if (j - 1 >= 0)
        dp[i][j] += dp[i][j - 1] * (probs[i + j - 1]);

      if (i - 1 >= 0)
        dp[i][j] += dp[i - 1][j] * (1 - probs[i + j - 1]);
    }
  }
  double ans = 0;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      if ((i + j == n) && (j > i))
        ans += dp[i][j];
    }
  }

  cout << setprecision(10) << ans << endl;
}
Problem p03168 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ll long long int
#define N 100000
#define f(i, a, b) for (ll i = (ll)a; i <= (ll)b; i++)
#define rf(i, a, b) for (ll i = (ll)a; i >= (ll)b; i--)
#define po pop_back
#define pb push_back
#define lb lower_bound
#define fi first
#define se second
#define debug(x) cout << #x << " = " << x << endl
#define ub upper_bound
#define ibs ios_base::sync_with_stdio(false)
#define cti cin.tie(0)
#define all(x) x.begin(), x.end()
#define PI 3.14159265
#define cot cout.tie(0)
using namespace std;
/**
 * @author :: Sawlani
 *
 */
ll n;
double p[3000];
double dp[3000][3000];
double rec(ll i, ll head) {

  if (i > n && (head > (n / 2)))
    return 1;
  else if (i > n)
    return 0;
  if (dp[i][head] > -1)
    return dp[i][head];
  return p[i] * rec(i + 1, head + 1) + (1.0 - p[i]) * rec(i + 1, head);
}
int main() {
  cin >> n;

  cout << fixed << setprecision(10);

  memset(dp, -1, sizeof(dp));
  f(i, 1, n) cin >> p[i];

  cout << rec(1, 0);
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long int
#define N 100000
#define f(i, a, b) for (ll i = (ll)a; i <= (ll)b; i++)
#define rf(i, a, b) for (ll i = (ll)a; i >= (ll)b; i--)
#define po pop_back
#define pb push_back
#define lb lower_bound
#define fi first
#define se second
#define debug(x) cout << #x << " = " << x << endl
#define ub upper_bound
#define ibs ios_base::sync_with_stdio(false)
#define cti cin.tie(0)
#define all(x) x.begin(), x.end()
#define PI 3.14159265
#define cot cout.tie(0)
using namespace std;
/**
 * @author :: Sawlani
 *
 */
ll n;
double p[3000];
double dp[3000][3000];
double rec(ll i, ll head) {

  if (i > n && (head > (n / 2)))
    return 1;
  else if (i > n)
    return 0;
  if (dp[i][head] > -1)
    return dp[i][head];
  return dp[i][head] =
             p[i] * rec(i + 1, head + 1) + (1.0 - p[i]) * rec(i + 1, head);
}
int main() {
  cin >> n;

  cout << fixed << setprecision(10);

  memset(dp, -1, sizeof(dp));
  f(i, 1, n) cin >> p[i];

  cout << rec(1, 0);
}
Problem p03168 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
void func(vector<double> &p, double &sum, int n, int i, double mult) {
  int sz = p.size();
  if (i == sz) {
    if (n >= ceil(double(sz) / 2.0)) {
      sum += mult;
    }
    return;
  }
  func(p, sum, n + 1, i + 1, mult * p[i]);
  func(p, sum, n, i + 1, mult * (1 - p[i]));
}
int main() {
  int n;
  cin >> n;
  vector<double> p;
  for (int i = 0; i < n; i++) {
    double x;
    cin >> x;
    p.push_back(x);
  }
  double sum = 0.0;
  func(p, sum, 0, 0, 1);
  cout.precision(16);
  cout << sum << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
void func(vector<double> &p, double &sum, int n, int i, double mult) {
  int sz = p.size();
  if (i == sz) {
    if (n >= ceil(double(sz) / 2.0)) {
      sum += mult;
    }
    return;
  }
  func(p, sum, n + 1, i + 1, mult * p[i]);
  func(p, sum, n, i + 1, mult * (1 - p[i]));
}
int main() {
  int n;
  cin >> n;
  vector<double> p;
  for (int i = 0; i < n; i++) {
    double x;
    cin >> x;
    p.push_back(x);
  }
  double sum = 0.0;
  // func(p,sum,0,0,1);
  double dp[n + 1][n + 1];
  for (int i = 0; i < n + 1; i++) {
    for (int j = 0; j < n + 1; j++) {
      dp[i][j] = 0;
    }
    // dp[0][i]=0;
  }
  dp[0][0] = 1.0;
  for (int i = 1; i < n + 1; i++) {
    for (int j = 0; j <= i; j++) {
      if (j == 0) {
        dp[i][j] = dp[i - 1][j] * (1.0 - p[i - 1]);
      } else {
        dp[i][j] =
            dp[i - 1][j] * (1.0 - p[i - 1]) + dp[i - 1][j - 1] * p[i - 1];
      }
    }
  }
  for (int i = ceil(double(n) / 2.0); i < n + 1; i++) {
    sum += dp[n][i];
  }
  cout.precision(16);
  cout << sum << endl;
}
Problem p03168 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iomanip>
#include <iostream>

using namespace std;

const int N = 3e3 + 3;

int n;
double p[N];
double dp[N][2 * N];

int main() {
  //    freopen("input.inp", "r", stdin);
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> p[i];
  dp[1][1 + N] = p[1];
  dp[1][-1 + N] = 1 - p[1];
  for (int i = 1; i < N; ++i)
    for (int j = -n; j <= n; ++j)
      if (dp[i][j + N] != 0) {
        if (j + 1 <= n)
          dp[i + 1][j + 1 + N] += dp[i][j + N] * p[i + 1];
        if (j - 1 >= -n)
          dp[i + 1][j - 1 + N] += dp[i][j + N] * (1 - p[i + 1]);
      }
  double ans = 0;
  for (int i = 1; i <= n; ++i)
    ans += dp[n][i + N];
  cout << fixed << setprecision(10) << ans;
  return 0;
}


Fixed submission:
#include <cstdio>
#include <iomanip>
#include <iostream>

using namespace std;

const int N = 3e3 + 3;

int n;
double p[N];
double dp[N][2 * N];

int main() {
  //    freopen("input.inp", "r", stdin);
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> p[i];
  dp[1][1 + N] = p[1];
  dp[1][-1 + N] = 1 - p[1];
  for (int i = 1; i < n; ++i)
    for (int j = -n; j <= n; ++j)
      if (dp[i][j + N] != 0) {
        if (j + 1 <= n)
          dp[i + 1][j + 1 + N] += dp[i][j + N] * p[i + 1];
        if (j - 1 >= -n)
          dp[i + 1][j - 1 + N] += dp[i][j + N] * (1 - p[i + 1]);
      }
  double ans = 0;
  for (int i = 1; i <= n; ++i)
    ans += dp[n][i + N];
  cout << fixed << setprecision(10) << ans;
  return 0;
}
Problem p03168 (C++)

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

#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep1(i, n) for (ll i = 1; i < (ll)(n); i++)
#define INF 10000000000
#define MOD 1000000007
using ll = long long;
using pint = pair<int, int>;

int main() {
  int N;
  cin >> N;
  vector<double> p(N);
  rep(i, N) cin >> p[i];
  vector<vector<double>> dp(N + 1, vector<double>(N + 1, 0.0));
  dp[0][0] = 1.0;
  rep(n, N) {
    rep(j, n + 1) {
      dp[n + 1][j + 1] += dp[n][j] * p[n];
      dp[n + 1][j] += dp[n][j] * (1 - p[n]);
    }
  }
  double res = 0.0;
  for (int i = N / 2 + 1; i++; i <= N)
    res += dp[N][i];
  cout << fixed << setprecision(10) << res << endl;
}

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

#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep1(i, n) for (ll i = 1; i < (ll)(n); i++)
#define INF 10000000000
#define MOD 1000000007
using ll = long long;
using pint = pair<int, int>;

int main() {
  int N;
  cin >> N;
  vector<double> p(N);
  rep(i, N) cin >> p[i];
  vector<vector<double>> dp(N + 1, vector<double>(N + 1, 0.0));
  dp[0][0] = 1.0;
  rep(n, N) {
    rep(j, n + 1) {
      dp[n + 1][j + 1] += dp[n][j] * p[n];
      dp[n + 1][j] += dp[n][j] * (1 - p[n]);
    }
  }
  double res = 0.0;
  for (int i = N / 2 + 1; i <= N; i++)
    res += dp[N][i];
  cout << fixed << setprecision(10) << res << endl;
}
Problem p03168 (C++)

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

using namespace std;

typedef long long ll;

const int MAX = 2e3 + 7;

double a[MAX], dp[MAX][MAX];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> a[i];
  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. - a[i]) + dp[i - 1][j - 1] * a[i];
      else
        dp[i][j] = dp[i - 1][j] * (1. - a[i]);
  double ans = 0.;
  for (int i = n / 2 + 1; i <= n; ++i)
    ans += dp[n][i];
  cout << fixed << setprecision(11) << ans << '\n';

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int MAX = 3e3 + 7;

double a[MAX], dp[MAX][MAX];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> a[i];
  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. - a[i]) + dp[i - 1][j - 1] * a[i];
      else
        dp[i][j] = dp[i - 1][j] * (1. - a[i]);
  double ans = 0.;
  for (int i = n / 2 + 1; i <= n; ++i)
    ans += dp[n][i];
  cout << fixed << setprecision(11) << ans << '\n';

  return 0;
}
Problem p03168 (C++)

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

int n;

bool visited[3000][3000];
double dp[3000][3000];

double solve(int h, int t, double *p) {

  if (visited[h][t] == true)
    return dp[h][t];

  else {

    if (h > t) {
      dp[h][t] = 0;
      visited[h][t] = true;
      return 0;
    }

    else if (h == t && t == 1) {
      dp[h][t] = p[t - 1];
      visited[h][t] = true;
      return dp[h][t];
    }

    else if (h == 0 && t == 1) {
      dp[h][t] = 1 - p[t - 1];
      visited[h][t] = true;
      return dp[h][t];
    }

    else {

      dp[h][t] = p[t - 1] * solve(h - 1, t - 1, p) +
                 (1 - p[t - 1]) * solve(h, t - 1, p);
      visited[h][t] = true;
      return dp[h][t];
    }
  }
}
int main() {

  cin >> n;

  double p[n];
  for (int i = 0; i < n; i++)
    cin >> p[i];
  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= n; j++)
      visited[i][j] = false;

  double pro = 0;

  for (int i = n / 2 + 1; i <= n; i++) {

    pro += solve(i, n, p);
  }
  cout.precision(9);
  cout << fixed << pro << endl;
}


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

int n;

bool visited[3000][3000];
double dp[3000][3000];

double solve(int h, int t, double *p) {

  if (visited[h][t] == true)
    return dp[h][t];

  else {

    if (h > t) {
      dp[h][t] = 0;
      visited[h][t] = true;
      return 0;
    }

    else if (h == t && t == 1) {
      dp[h][t] = p[t - 1];
      visited[h][t] = true;
      return dp[h][t];
    }

    else if (h == 0 && t == 1) {
      dp[h][t] = 1 - p[t - 1];
      visited[h][t] = true;
      return dp[h][t];
    }

    else {

      dp[h][t] = ((h == 0) ? 0 : (p[t - 1] * solve(h - 1, t - 1, p))) +
                 (1 - p[t - 1]) * solve(h, t - 1, p);
      visited[h][t] = true;
      return dp[h][t];
    }
  }
}
int main() {

  cin >> n;

  double p[n];
  for (int i = 0; i < n; i++)
    cin >> p[i];
  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= n; j++)
      visited[i][j] = false;

  double pro = 0;

  for (int i = n / 2 + 1; i <= n; i++) {

    pro += solve(i, n, p);
  }
  cout.precision(9);
  cout << fixed << pro << endl;
}
Problem p03168 (C++)

Buggy submission (status: Time Limit Exceeded):
/************
 * 	@Just Another Source code by: ankit.sangwan1999
 *	@created on: 25 May 2020
 */
#include <bits/stdc++.h>
using namespace std;
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL) //;cout.tie(NULL)
#define ll long long
const int mod = 1e9 + 7;
#define endl '\n'

int n;
int t_max;
vector<double> prr;
double dp[3000][3000];
bool vis[3000][3000];

double fun(int ind, int curr_tails) {

  // if(vis[ind][curr_tails]) {
  // 	return dp[ind][curr_tails];
  // }
  // vis[ind][curr_tails] = 1;

  double pos1 = 0, pos2 = 0;

  if (curr_tails < t_max) {
    pos1 = (1 - prr[ind]);
    if (ind - 1 >= 0) {
      if (vis[ind - 1][curr_tails + 1] == 0) {
        dp[ind - 1][curr_tails + 1] = fun(ind - 1, curr_tails + 1);
      }
      pos1 *= dp[ind - 1][curr_tails + 1];
    }
  }

  pos2 = prr[ind];
  if (ind - 1 >= 0) {
    if (vis[ind - 1][curr_tails] == 0) {
      dp[ind - 1][curr_tails] = fun(ind - 1, curr_tails);
    }
    pos2 *= dp[ind - 1][curr_tails];
  }

  return dp[ind][curr_tails] = pos1 + pos2;
}

signed main() {

  fastio;

  cin >> n;
  for (int i = 0; i < 3000; i++)
    for (int j = 0; j < 3000; j++)
      vis[i][j] = 0;

  if (n & 1)
    t_max = n / 2;
  else
    t_max = n / 2 - 1;

  prr = vector<double>(n);

  for (int i = 0; i < n; i++)
    cin >> setprecision(10) >> prr[i];

  cout << setprecision(10) << fun(n - 1, 0);

  return 0;
}

Fixed submission:
/************
 * 	@Just Another Source code by: ankit.sangwan1999
 *	@created on: 25 May 2020
 */
#include <bits/stdc++.h>
using namespace std;
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL) //;cout.tie(NULL)
#define ll long long
const int mod = 1e9 + 7;
#define endl '\n'

int n;
int t_max;
vector<double> prr;
double dp[3000][3000];
bool vis[3000][3000];

double fun(int ind, int curr_tails) {

  if (vis[ind][curr_tails]) {
    return dp[ind][curr_tails];
  }
  vis[ind][curr_tails] = 1;

  double pos1 = 0, pos2 = 0;

  if (curr_tails < t_max) {
    pos1 = (1 - prr[ind]);
    if (ind - 1 >= 0) {
      if (vis[ind - 1][curr_tails + 1] == 0) {
        dp[ind - 1][curr_tails + 1] = fun(ind - 1, curr_tails + 1);
      }
      pos1 *= dp[ind - 1][curr_tails + 1];
    }
  }

  pos2 = prr[ind];
  if (ind - 1 >= 0) {
    if (vis[ind - 1][curr_tails] == 0) {
      dp[ind - 1][curr_tails] = fun(ind - 1, curr_tails);
    }
    pos2 *= dp[ind - 1][curr_tails];
  }

  return dp[ind][curr_tails] = pos1 + pos2;
}

signed main() {

  fastio;

  cin >> n;
  for (int i = 0; i < 3000; i++)
    for (int j = 0; j < 3000; j++)
      vis[i][j] = 0;

  if (n & 1)
    t_max = n / 2;
  else
    t_max = n / 2 - 1;

  prr = vector<double>(n);

  for (int i = 0; i < n; i++)
    cin >> setprecision(10) >> prr[i];

  cout << setprecision(10) << fun(n - 1, 0);

  return 0;
}
Problem p03168 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;

typedef long long ll;
typedef long double ld;
typedef array<ll, 2> pll;
const ll NMAX = 1005, MOD = 1e9 + 7;
ld dp[NMAX][NMAX];

int main() {
  ll n;
  ld p, ans = 0;
  cin >> n;
  dp[0][0] = 1;
  for (ll i = 1; i <= n; i++) {
    cin >> p;
    for (ll cnt = 0; cnt <= i; cnt++) {
      dp[i][cnt] = dp[i - 1][cnt - 1] * p + dp[i - 1][cnt] * (1 - p);
    }
  }
  for (ll i = n / 2 + 1; i <= n; i++)
    ans += dp[n][i];
  cout << fixed << setprecision(9) << ans;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;

typedef long long ll;
typedef long double ld;
typedef array<ll, 2> pll;
const ll NMAX = 3005, MOD = 1e9 + 7;
ld dp[NMAX][NMAX];

int main() {
  ll n;
  ld p, ans = 0;
  cin >> n;
  dp[0][0] = 1;
  for (ll i = 1; i <= n; i++) {
    cin >> p;
    for (ll cnt = 0; cnt <= i; cnt++) {
      dp[i][cnt] = dp[i - 1][cnt - 1] * p + dp[i - 1][cnt] * (1 - p);
    }
  }
  for (ll i = n / 2 + 1; i <= n; i++)
    ans += dp[n][i];
  cout << fixed << setprecision(9) << ans;
  return 0;
}
Problem p03168 (C++)

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

#define FAST_IO                                                                \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);

using namespace std;

typedef long long ll;
typedef pair<int, int> pi;

const int MOD = 1e9 + 7;

void add_self(int &a, int b) {
  a += b;
  if (a >= MOD) {
    a -= MOD;
  }
}

void sub_self(int &a, int b) {
  a -= b;
  if (a < 0) {
    a += MOD;
  }
}

const int MAXN = 1e3 + 5;

int main() {
#ifndef ONLINE_JUDGE
  (void)freopen("input.txt", "r", stdin);
  (void)freopen("output.txt", "w", stdout);
#endif
  FAST_IO;
  // dp[k] // the prob that there are k heads so far
  // if we toss i times, k times heads -> i-k times tail s
  int n;
  cin >> n;
  double p;
  vector<double> dp(n + 1);
  dp[0] = 1;
  for (int coin = 0; coin < n; ++coin) {
    cin >> p;
    for (int i = coin + 1; i >= 0; --i) {
      dp[i] = (i == 0 ? 0 : dp[i - 1] * p) + dp[i] * (1 - p);
    }
  }
  double answer = 0.0;
  for (int heads = 0; heads <= n; ++heads) {
    int tails = n - heads;
    if (heads > tails)
      answer += dp[heads];
  }
  printf("%.10lf", answer);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#define FAST_IO                                                                \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);

using namespace std;

typedef long long ll;
typedef pair<int, int> pi;

const int MOD = 1e9 + 7;

void add_self(int &a, int b) {
  a += b;
  if (a >= MOD) {
    a -= MOD;
  }
}

void sub_self(int &a, int b) {
  a -= b;
  if (a < 0) {
    a += MOD;
  }
}

const int MAXN = 1e3 + 5;

int main() {

  FAST_IO;
  // dp[k] // the prob that there are k heads so far
  // if we toss i times, k times heads -> i-k times tail s
  int n;
  cin >> n;
  double p;
  vector<double> dp(n + 1);
  dp[0] = 1;
  for (int coin = 0; coin < n; ++coin) {
    cin >> p;
    for (int i = coin + 1; i >= 0; --i) {
      dp[i] = (i == 0 ? 0 : dp[i - 1] * p) + dp[i] * (1 - p);
    }
  }
  double answer = 0.0;
  for (int heads = 0; heads <= n; ++heads) {
    int tails = n - heads;
    if (heads > tails)
      answer += dp[heads];
  }
  printf("%.10lf", answer);
  return 0;
}
Problem p03168 (C++)

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

#define F first
#define S second
#define MP make_pair
#define PB emplace_back

using namespace std;

const int N = 1e3 + 100, mod = 1e9 + 7;

long double dp[N];

int main() {
  cout << setprecision(12) << fixed;
  dp[0] = 1;
  long long int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    long double x;
    cin >> x;
    for (int j = i + 1; j >= 0; j--) {
      dp[j] = dp[j] * (1 - x);
      if (j != 0)
        dp[j] += dp[j - 1] * x;
    }
  }
  long long int sag = (n + 1) / 2;
  long double ans = 0;
  for (int i = sag; i <= n; i++)
    ans += dp[i];
  cout << ans;
}


Fixed submission:
#include <bits/stdc++.h>

#define F first
#define S second
#define MP make_pair
#define PB emplace_back

using namespace std;

const int N = 1e4 + 100, mod = 1e9 + 7;

long double dp[N];

int main() {
  cout << setprecision(12) << fixed;
  dp[0] = 1;
  long long int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    long double x;
    cin >> x;
    for (int j = i + 1; j >= 0; j--) {
      dp[j] = dp[j] * (1 - x);
      if (j != 0)
        dp[j] += dp[j - 1] * x;
    }
  }
  long long int sag = (n + 1) / 2;
  long double ans = 0;
  for (int i = sag; i <= n; i++)
    ans += dp[i];
  cout << ans;
}
Problem p03168 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cctype>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  std::cout << fixed << setprecision(10);
  int n;
  cin >> n;
  vector<double> dp((n + 1) / 2, 0);
  dp[0] = 1.0;
  for (int i = 0; i < n; i++) {
    double p;
    cin >> p;
    p = 1 - p;
    for (int j = min(i + 1, (n + 1) / 2); j > 0; j--) {
      dp[j] = dp[j] * (1 - p) + dp[j - 1] * p;
    }
    dp[0] = dp[0] * (1 - p);
  }
  cout << accumulate(dp.begin(), dp.end(), 0.0);

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cctype>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  std::cout << fixed << setprecision(10);
  int n;
  cin >> n;
  vector<double> dp((n + 1) / 2, 0);
  dp[0] = 1.0;
  for (int i = 0; i < n; i++) {
    double p;
    cin >> p;
    p = 1 - p;
    for (int j = min(i + 1, n / 2); j > 0; j--) {
      dp[j] = dp[j] * (1 - p) + dp[j - 1] * p;
    }
    dp[0] = dp[0] * (1 - p);
  }
  cout << accumulate(dp.begin(), dp.end(), 0.0);

  return 0;
}
Problem p03168 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-ffloat-store")
#pragma GCC optimize("-fno-defer-pop")
#define all(a) a.begin(), a.end()
#define ll long long int
#define ps push_back
#define fs first
#define sc second
#define mkp make_pair
#define mod 1000000007
double dp[3000][3000];
double maxval(double ar[], ll i, ll n, ll h, ll t) {
  // ll t=i-h;
  if (i == n) {
    if (h > t)
      return 1.0;
    else
      return 0.0;
  }

  // cout<<i<<" "<<h<<" "<<dp[i][h]<<endl;
  if (dp[i][h] != 100.0) {
    return dp[i][h];
  }

  double a = 0.0, b = 0.0;
  a = ar[i] * maxval(ar, i + 1, n, h + 1, t);
  b = (1.0 - ar[i]) * maxval(ar, i + 1, n, h, t + 1);

  dp[i][h] = (a + b);
  return dp[i][h];
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  ll i, j, k, l, n;
  cin >> n;

  for (i = 0; i < 3000; i++) {
    for (j = 0; j < 3000; j++) {
      dp[i][j] = 100.0;
    }
  }

  double ar[n];
  for (i = 0; i < n; i++) {
    cin >> ar[i];
  }

  double an = maxval(ar, 0, n, 0, 0);
  cout << setprecision(10) << an;
  return 0;
}
// ctrl H replace

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-ffloat-store")
#pragma GCC optimize("-fno-defer-pop")
#define all(a) a.begin(), a.end()
#define ll long long int
#define ps push_back
#define fs first
#define sc second
#define mkp make_pair
#define mod 1000000007
double dp[3000][3000];
double maxval(double ar[], ll i, ll n, ll h, ll t) {
  // ll t=i-h;
  if (i == n) {
    if (h > t)
      return 1.0;
    else
      return 0.0;
  }

  // cout<<i<<" "<<h<<" "<<dp[i][h]<<endl;
  if (dp[i][h] != 100.0) {
    return dp[i][h];
  }

  double a = 0.0, b = 0.0;
  a = ar[i] * maxval(ar, i + 1, n, h + 1, t);
  b = (1.0 - ar[i]) * maxval(ar, i + 1, n, h, t + 1);

  dp[i][h] = (a + b);
  return dp[i][h];
}
int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  ll i, j, k, l, n;
  cin >> n;

  for (i = 0; i < 3000; i++) {
    for (j = 0; j < 3000; j++) {
      dp[i][j] = 100.0;
    }
  }

  double ar[n];
  for (i = 0; i < n; i++) {
    cin >> ar[i];
  }

  double an = maxval(ar, 0, n, 0, 0);
  cout << setprecision(10) << an;
  return 0;
}
// ctrl H replace
Problem p03168 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define fr(i, n) for (int i = 0; i < n; i++)
#define frab(i, a, b) for (int i = a; i < b; i++)
#define pb push_back

using namespace std;

typedef long long ll;
typedef long double ld;

const ll MOD = 1e9 + 7;
const ll INF = 2e15 + 10;
const ll MAX = 1e14 + 10;
const ld EPS = 1e-9;
const int N = 1e3 + 10;
const int M = 1e5 + 10;

ld p[N];

void solve() {
  int n;
  cin >> n;
  fr(i, n) cin >> p[i];
  vector<ld> cnt(n + 1, 0);
  cnt[0] = 1;
  fr(i, n) {
    vector<ld> ncnt(n + 1, 0);
    fr(j, n) {
      ncnt[j + 1] += cnt[j] * p[i];
      ncnt[j] += cnt[j] * (1 - p[i]);
    }
    cnt = ncnt;
  }

  ld ans = accumulate(cnt.begin() + (n + 1) / 2, cnt.end(), 0.0);
  cout << fixed << setprecision(12) << ans;
}

int main() {
  // freopen("a.in", "r", stdin);
  ios_base::sync_with_stdio(false);
  solve();
}


Fixed submission:
#include <bits/stdc++.h>
#define fr(i, n) for (int i = 0; i < n; i++)
#define frab(i, a, b) for (int i = a; i < b; i++)
#define pb push_back

using namespace std;

typedef long long ll;
typedef long double ld;

const ll MOD = 1e9 + 7;
const ll INF = 2e15 + 10;
const ll MAX = 1e14 + 10;
const ld EPS = 1e-9;
const int N = 3e3 + 10;
const int M = 1e5 + 10;

ld p[N];

void solve() {
  int n;
  cin >> n;
  fr(i, n) cin >> p[i];
  vector<ld> cnt(n + 1, 0);
  cnt[0] = 1;
  fr(i, n) {
    vector<ld> ncnt(n + 1, 0);
    fr(j, n) {
      ncnt[j + 1] += cnt[j] * p[i];
      ncnt[j] += cnt[j] * (1 - p[i]);
    }
    cnt = ncnt;
  }

  ld ans = accumulate(cnt.begin() + (n + 1) / 2, cnt.end(), 0.0);
  cout << fixed << setprecision(12) << ans;
}

int main() {
  // freopen("a.in", "r", stdin);
  ios_base::sync_with_stdio(false);
  solve();
}
Problem p03168 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int N;
vector<double> v;
double memo[1009][1009];
bool vis[1009][1009];
bool g[1009][1009];
ll mod = 1e9 + 7;
double sol(int pos, int k) {
  if (pos == N)
    return k > (N / 2);
  if (vis[pos][k])
    return memo[pos][k];
  vis[pos][k] = 1;
  return memo[pos][k] =
             sol(pos + 1, k + 1) * v[pos] + sol(pos + 1, k) * (1 - v[pos]);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  cin >> N;
  for (int i = 0; i < N; i++) {
    double a;
    cin >> a;
    v.push_back(a);
  }
  cout << fixed << setprecision(20) << sol(0, 0);
  return 0;
}

Fixed submission:
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int N;
vector<double> v;
double memo[3009][3009];
bool vis[3009][3009];
ll mod = 1e9 + 7;
double sol(int pos, int k) {
  if (pos == N)
    return k > (N / 2);
  if (vis[pos][k])
    return memo[pos][k];
  vis[pos][k] = 1;
  return memo[pos][k] =
             sol(pos + 1, k + 1) * v[pos] + sol(pos + 1, k) * (1 - v[pos]);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  cin >> N;
  for (int i = 0; i < N; i++) {
    double a;
    cin >> a;
    v.push_back(a);
  }
  cout << fixed << setprecision(20) << sol(0, 0);
  return 0;
}
Problem p03168 (C++)

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

const int N = 1e3 + 10;

double a[N], sum, d[N][N];
int n, kek;

int main() {
  cin >> n;
  d[0][0] = 1;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    d[i][0] = 1;
  }
  d[n][0] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      d[i][j] = d[i - 1][j - 1] * a[i] + d[i - 1][j] * (1 - a[i]);
    }
  }
  if (n % 2 == 1) {
    kek = int(n / 2) + 1;
  } else {
    kek = int(n / 2);
  }
  for (int i = kek; i <= n; i++) {
    sum += d[n][i];
  }
  printf("%.10f", d[n][kek]);
  return 0;
}

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

const int N = 3e3 + 10;

double a[N], sum, d[N][N];
int n, kek;

int main() {
  cin >> n;
  d[0][0] = 1;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    d[i][0] = 1;
  }
  d[n][0] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      d[i][j] = d[i - 1][j - 1] * a[i] + d[i - 1][j] * (1 - a[i]);
    }
  }
  if (n % 2 == 1) {
    kek = int(n / 2) + 1;
  } else {
    kek = int(n / 2);
  }
  for (int i = kek; i <= n; i++) {
    sum += d[n][i];
  }
  printf("%.10f", d[n][kek]);
  return 0;
}
Problem p03168 (C++)

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

using namespace std;

using ll = long long;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<double> p(n);
  vector<double> dp(n, 0);
  dp[0] = 1;
  for (int i = 0; i < n; i++) {
    cin >> p[i];
    for (int j = i; j >= 0; j--) {
      dp[j + 1] += dp[j] * p[i];
      dp[j] *= 1 - p[i];
    }
  }

  double ans = 0;
  for (int i = n; i > n / 2; i--) {
    ans += dp[i];
  }
  cout << fixed << setprecision(15) << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

using ll = long long;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<double> p(n);
  vector<double> dp(n + 1, 0);
  dp[0] = 1;
  for (int i = 0; i < n; i++) {
    cin >> p[i];
    for (int j = i; j >= 0; j--) {
      dp[j + 1] += dp[j] * p[i];
      dp[j] *= 1 - p[i];
    }
  }

  double ans = 0;
  for (int i = n; i > n / 2; i--) {
    ans += dp[i];
  }
  cout << fixed << setprecision(15) << ans << endl;
  return 0;
}