#P7221. Vegetable Price Game on Mars
Vegetable Price Game on Mars
Vegetable Price Game on Mars
JYY has been captured by Martians after stealing some treasure – and now his life depends on winning a strange game played on a vegetable square. Initially, a giant genetically–modified pumpkin is placed in the square and then n vegetables are arranged one after another. They form a tree: the first vegetable (the giant pumpkin) has no parent, and every later vegetable i is connected by a rope to a previously placed vegetable p_i. In Martian terms, vegetable i is a Dlihc of p_i and vegetable p_i is its Tnerap.
Each vegetable i has an initial price vi. The game proceeds by allowing the player to make an arbitrary number of operations. In an operation the player may choose any vegetable i that has both at least one child and a parent, and update its price as follows:
Here, p is the Tnerap (parent) of i and c is an arbitrarily chosen one of its Dlihc (children).
The game ends when the player stops making operations. Then the Martian government buys every vegetable at their marked prices – and the money goes to pay off JYY’s debt. JYY wonders: Can he maximize the total sale price, or even make it arbitrarily high? In other words, given the tree structure and initial prices, determine either the maximum total sale price achievable by applying a sequence of operations, or report that the total price can be made arbitrarily large (i.e. "Infinity").
Note. Any formula appearing in the statement is given in \(\LaTeX\) format.
inputFormat
The first line contains an integer n (1 ≤ n ≤ 15) – the total number of vegetables.
The second line contains n integers v1, v2, ... , vn (the initial prices).
The third line contains n − 1 integers: p2, p3, ... , pn, where pi (1 ≤ pi < i) denotes the index of the vegetable connected to the ith vegetable. It is guaranteed that the vegetables form a tree with vegetable 1 as the root.
Remark: A vegetable is eligible for an operation if and only if it has a parent and at least one child. Note that the operations are involutions (applying the same operation twice reverts the change).
outputFormat
If the total sale price can be made arbitrarily high, output Infinity
(without quotes). Otherwise, output the maximum total sale price that JYY can obtain.
sample
1
10
10