Under construction

Still being built. Things may change

Arcade!

Time limit2sMemory limit512 MB

Summary
Given a triangular grid of holes with per-hole bounce probabilities and payouts, compute the expected payout of one dropped ball.
Level

Medium7 of 10

Topics
Probability, Dynamic programming, Math, Simulation
Solved
No attempts yet

Problem

Have you been to an arcade lately? Arcade games have needed less and less skill over the years, and the machines you find today decide almost everything by luck. Look at the machine in the picture. Its holes are arranged in a triangle. A ball is dropped near the hole at the top. The ball either falls into that hole, which ends the game, or it bounces to one of the neighboring holes marked by the red arrows (up to 4 of them). Every hole has its own payout, and some payouts are negative. When the ball reaches another hole, the same thing happens again. It either falls in and ends the game, or it bounces to a neighbor. The bouncing can go on forever.

Write a program that computes the expected payout of dropping one ball into the machine.

Input

The first line contains the number of rows NN of the machine (1≤N≤321 \le N \le 32).

The second line contains H=N(N+1)/2H = N(N+1)/2 integers v1,v2,…,vHv_1, v_2, \dots, v_H (−100≤vi≤100-100 \le v_i \le 100), where viv_i is the payout the player receives if the ball falls into hole ii. A payout may be negative. Holes are numbered from top to bottom. Row 1 holds hole 1, row 2 holds holes 2 and 3, and row kk starts with hole k(k−1)/2+1k(k-1)/2 + 1 and holds exactly kk holes.

The next HH lines each contain five real numbers p0 p1 p2 p3 p4. Line ii gives, for hole ii, the probability that the ball bounces to its top-left neighbor (p0), to its top-right neighbor (p1), to its bottom-left neighbor (p2), to its bottom-right neighbor (p3), and the probability that the ball falls into the hole (p4). Each probability is given with at most 3 digits after the decimal point, 0.0≤pi≤1.00.0 \le p_i \le 1.0, and the five values sum to exactly 1.01.0.

For the hole in position cc of row kk, the top-left neighbor is hole c−1c-1 of row k−1k-1, the top-right neighbor is hole cc of row k−1k-1, the bottom-left neighbor is hole cc of row k+1k+1, and the bottom-right neighbor is hole c+1c+1 of row k+1k+1. When a hole sits on the boundary and a neighbor does not exist, the probability of bouncing there is always given as 0.00.0. Hole 1, for instance, has no hole above it, so p0 and p1 are both 0.00.0.

You can assume that after the ball has bounced bb times, the probability that it has not yet fallen into any hole is at most (1−10−3)⌊b/H⌋(1 - 10^{-3})^{\lfloor b/H \rfloor}.

Output

Print the expected payout of one game on a single line, rounded to six digits after the decimal point. Always write all six digits, and when the rounded value is 00, print 0.000000 with no sign.

A Monte Carlo approach, dropping many balls and picking each bounce at random, does not reach this accuracy.

Examples5

  1. Example 1

    Input
    4
    40 30 30 40 20 40 50 30 30 50
    0.0 0.0 0.45 0.45 0.1
    0.0 0.3 0.3 0.3 0.1
    0.3 0.0 0.3 0.3 0.1
    0.0 0.3 0.3 0.3 0.1
    0.2 0.2 0.2 0.2 0.2
    0.3 0.0 0.3 0.3 0.1
    0.0 0.8 0.0 0.0 0.2
    0.4 0.4 0.0 0.0 0.2
    0.4 0.4 0.0 0.0 0.2
    0.8 0.0 0.0 0.0 0.2
    
    Expected output
    32.640545
    
  2. Example 2

    Input
    2
    100 50 50
    0.0 0.0 0.45 0.45 0.1
    0.0 0.90 0.0 0.0 0.10
    0.90 0.0 0.0 0.0 0.10
    
    Expected output
    76.315789
    
  3. Example 3

    Input
    1
    100
    0.000 0.000 0.000 0.000 1.000
    
    Expected output
    100.000000
    
  4. Example 4

    Input
    1
    -100
    0.000 0.000 0.000 0.000 1.000
    
    Expected output
    -100.000000
    
  5. Example 5

    Input
    2
    -81 -100 -100
    0.0 0.0 0.45 0.45 0.1
    0.0 0.90 0.0 0.0 0.10
    0.90 0.0 0.0 0.0 0.10
    
    Expected output
    -90.000000