#P4712. Ecosystem Energy Chain
Ecosystem Energy Chain
Ecosystem Energy Chain
In an ecosystem there are n+2 species numbered from 0 to n+1. Species 0 is the producer which supplies a fixed amount of energy, and species n+1 is the top predator (you) that can prey upon any species. For each species i with 1 ≤ i ≤ n, it is a predator that has a prey preference: it can only prey on species with indices in the range [0, ri]. It is guaranteed that for every i (1 ≤ i ≤ n) the condition ri < i holds and the sequence r1, r2, …, rn is non‐decreasing (i.e. ri ≤ ri+1 for 1 ≤ i < n).
Each species i (0 ≤ i ≤ n) captures an amount of energy bi. In order to survive, species i requires at least ai units of energy, i.e. bi ≥ ai. In addition, an organism may pass some of its captured energy to its predators. However, the total energy transferred from species i cannot exceed ⅕ bi (i.e. at most 20% efficiency): $$\text{transferred energy } c_i \le \frac{1}{5} b_i. $$
In the optimal scenario we assume every species uses its full transmission capacity. Thus, for species i (0 ≤ i ≤ n) we have
which implies
$$b_i = \frac{5}{4} a_i \quad \text{and} \quad f_i = \frac{1}{5}b_i = \frac{a_i}{4}, $$where fi is the maximum extra energy that species i can forward. However, for species i (1 ≤ i ≤ n) the available extra energy is also limited by what they can obtain from their allowed prey. Formally, let Fi denote the actual forwarded energy from species i, then:
- For the producer (i=0): $$ F_0 = \frac{a_0}{4}. $$
- For each species i (1 ≤ i ≤ n): $$ F_i = \min\Biggl(\sum_{j=0}^{r_i} F_j,\; \frac{a_i}{4}\Biggr). $$
The top predator (species n+1) can prey on every species. Therefore, the maximum energy you can obtain is the sum of all the forwarded energies from species 0 to n:
Given the minimum energy requirements a0, a1, …, an and the prey restrictions r1, r2, …, rn, calculate the maximum energy the top predator (species n+1) can acquire, under the condition that all species survive.
inputFormat
The input consists of three lines:
- The first line contains a single integer n (1 ≤ n).
- The second line contains n+1 space‑separated integers: a0, a1, …, an, where ai is the minimum energy required by species i (0 ≤ i ≤ n).
- The third line contains n space‑separated integers: r1, r2, …, rn, representing the maximum index of species that species i (1 ≤ i ≤ n) can prey on. It is guaranteed that for each 1 ≤ i ≤ n, ri < i and ri ≤ ri+1 for 1 ≤ i < n.
outputFormat
Output a single number: the maximum energy the top predator (species n+1) can obtain. The result should be printed with exactly 6 decimal places.
sample
1
4 5
0
2.000000