Problem:
Score : 100 points

Problem Statement

Let N be a positive odd number.

There are N coins, numbered 1, 2, \ldots, N.
For each i (1 \leq i \leq N), when Coin i is tossed, it comes up heads with probability p_i and tails with probability 1 - p_i.

Taro has tossed all the N coins.
Find the probability of having more heads than tails.

Constraints

N is an odd number.

1 \leq N \leq 2999

p_i is a real number and has two decimal places.

0 < p_i < 1

Input

Input is given from Standard Input in the following format:

N
p_1 p_2 \ldots p_N

Output

Print the probability of having more heads than tails.
The output is considered correct when the absolute error is not greater than 10^{-9}.

Sample Input 1

3
0.30 0.60 0.80

Sample Output 1

0.612

The probability of each case where we have more heads than tails is as follows:

The probability of having (Coin 1, Coin 2, Coin 3) = (Head, Head, Head) is 0.3 × 0.6 × 0.8 = 0.144;

The probability of having (Coin 1, Coin 2, Coin 3) = (Tail, Head, Head) is 0.7 × 0.6 × 0.8 = 0.336;

The probability of having (Coin 1, Coin 2, Coin 3) = (Head, Tail, Head) is 0.3 × 0.4 × 0.8 = 0.096;

The probability of having (Coin 1, Coin 2, Coin 3) = (Head, Head, Tail) is 0.3 × 0.6 × 0.2 = 0.036.

Thus, the probability of having more heads than tails is 0.144 + 0.336 + 0.096 + 0.036 = 0.612.

Sample Input 2

1
0.50

Sample Output 2

0.5

Outputs such as 0.500, 0.500000001 and 0.499999999 are also considered correct.

Sample Input 3

5
0.42 0.01 0.42 0.99 0.42

Sample Output 3

0.3821815872

Solution in Rust (1.15.1) (judge verdict: Accepted)

use std::cmp;
use std::collections::HashMap;
use std::collections::VecDeque;
use std::usize::MAX;

macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
  }

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};
    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
  }

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };
    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };
    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };
    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };
    // 3
    // 1 2
    // 1 3
    // 1 4
    // のようなタイプ
    ($next:expr, [ $t:tt ]) => {{
        let len = read_value!($next, usize);
        (0..len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    }};
    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
  }

// 最大公約数
fn gcd(a: u64, b: u64) -> u64 {
    let mut aa: u64 = if a > b { a } else { b };
    let mut bb: u64 = if a > b { b } else { a };
    while bb != 0 {
        let tmp = bb;
        bb = aa % tmp;
        aa = tmp;
    }
    return aa;
}

// 最小公倍数
fn lcm(a: u64, b: u64) -> u64 {
    a / gcd(a, b) * b
}

// DFS,BFS
fn BFS() {
    let mut searchResult = vec![vec![MAX; 100]; 100];
    let mut q: VecDeque<(usize, usize)> = VecDeque::new();
    q.push_front((0, 0));
    searchResult[0][0] = 0;

    let moves = [(1, 0), (-1, 0), (0, 1), (0, -1)];

    while !q.is_empty() {
        let pos = q.pop_back().unwrap(); // これがpop_frontならstackになって深さ優先になる
        for m in moves.iter() {
            let dx: i64 = pos.0 as i64 + m.0;
            let dy: i64 = pos.1 as i64 + m.1;
            if dx >= 0
                && dx < 100
                && dy >= 0
                && dy < 100
                && searchResult[dx as usize][dy as usize] == MAX
            {
                q.push_front((dx as usize, dy as usize));
                searchResult[dx as usize][dy as usize] = searchResult[pos.0][pos.1] + 1;
            }
        }
    }
}

// 文字列操作

const LARGE_PRIME: u64 = 1_000_000_007;

fn inverse_mod(element: u64, modulo: u64) -> u64 {
    // フェルマーの小定理からpが素数なら a^(p-1) = 1
    // よってa*a^(p-2) = 1 より a^(p-2)がaの逆元

    fn power(base: u64, exp: u64, modulo: u64) -> u64 {
        if exp == 0 {
            return 1;
        } else if exp & 1 == 0 {
            return (power(base, exp / 2, modulo) % modulo).pow(2) % modulo;
        } else {
            return power(base, exp - 1, modulo) % modulo * base % modulo;
        }
    }

    power(element, modulo - 2, modulo) % modulo
}

fn combination_mod(m: u64, n: u64, modulo: u64) -> u64 {
    // m C n = m! / ( n! * (m - n)! )
    //       = m*(m-1)*(m-2)*...*(m-n+1)/n!
    //       = m*(m-1)*(m-2)*...*(m-n+1) * (n!)^-1

    let mut numerator = 1;
    for i in 0..n {
        numerator *= m - i;
        numerator %= modulo;
    }

    // (m-n)!
    let mut element = 1;
    for j in 1..(n + 1) {
        element *= j;
        element %= modulo;
    }

    numerator * inverse_mod(element, modulo) % modulo
}

fn topological_sort(n: usize, edges: Vec<(usize, usize)>) -> Vec<usize> {
    let mut result = vec![];
    let mut isolated = VecDeque::new();

    // to[x] = [a,b] x -> a, x -> b が存在
    let mut to = vec![vec![]; n + 1];

    // deg[x] = a -> x, b -> xのような辺の数
    let mut deg = vec![0; n + 1];
    for e in edges {
        to[e.0].push(e.1);
        deg[e.1] += 1;
    }

    for i in 1..n + 1 {
        if deg[i] == 0 {
            isolated.push_front(i);
        }
    }

    while isolated.len() != 0 {
        let next = isolated.pop_back().unwrap();
        result.push(next);
        for i in &to[next] {
            deg[*i] -= 1;
            if deg[*i] == 0 {
                isolated.push_front(*i);
            }
        }
    }

    result
}

fn solve() {
    input! {
        n: usize,
        p: [f64;n]
    }

    // dp[x][y] = x枚コインを投げてy枚表の確率
    let mut dp: Vec<Vec<f64>> = vec![vec![0.0; n + 1]; n + 1];
    dp[0][0] = 1.0;

    for i in 0..n {
        dp[i + 1][0] = dp[i][0] * (1.0 - p[i]);
        for j in 1..i + 2 {
            dp[i + 1][j] = dp[i][j - 1] * p[i] + dp[i][j] * (1.0 - p[i]);
        }
    }

    let half = n / 2 + 1;

    let mut ans = 0.0;
    for i in half..n + 1 {
        ans += dp[n][i];
    }

    println!("{}", ans);
}

fn main() {
    let thd = std::thread::Builder::new().stack_size(104_857_600);
    thd.spawn(|| solve()).unwrap().join().unwrap();
}