The Qualifying Bracket (medium version)
Suki (dog 1) is competing in a single-elimination dog show tournament. There are n dogs total, each with a fixed starting position in the bracket (dog i at position i). The bracket is a complete binary tree: in the first round, positions 1 vs 2, 3 vs 4, ..., (n-1) vs n compete; winners advance to the next round, and so on until one dog remains.
For every pair of distinct dogs i and j, we know the probability p[i][j] that dog i beats dog j in a head-to-head match. We want to compute the probability that Suki (dog 1) wins the entire tournament.
Input
The first line contains n (2 ≤ n ≤ 512, and n is a power of 2), the number of dogs.
The next n·(n−1)/2 lines each contain three space-separated values: integers i and j (1 ≤ i, j ≤ n, i < j) and a decimal number p (0 ≤ p ≤ 1), meaning dog i beats dog j with probability p. The probability that dog j beats dog i is therefore 1 − p.
All pairs (i, j) with i < j appear exactly once.
Output
Print a single decimal number: the probability that dog 1 wins the tournament. Your answer must be within 10^−6 of the correct value (absolute or relative error).
Notes
The bracket structure is fixed and determined solely by the dog positions. In the first round, dogs at positions 1 and 2 face each other; dogs at positions 3 and 4 face each other; and so on. Winners of each match advance to face the winners of the adjacent match at the next level, up the bracket tree.