1717. Vera and Engineering Buildings

时间限制 2000 ms   内存限制 512 MB

There are N engineering buildings at the University of Waterloo numbered from 1 to N. For i ≥ 2, there is a two-way bridge from building i to building xi . Two buildings are called neighbours if there is a bridge between them. 

Each building has a unique aesthetic value which can be any integer. It is difficult to determine the exact aesthetic value of a building, so Vera would like to find a nice building, one that has higher aesthetic value than all of its neighbours. It takes Vera ti seconds to inspect building i and the bridges to all of its neighbours. After the inspection, for each neighbour j of building i, Vera will know which of building i or j has higher aesthetic value. 

What is the minimum total seconds such that it is guaranteed that Vera can find a nice building in that time no matter what the aesthetic values are? Vera can decide which building to inspect next based on the results of previous inspections. Ignore the time needed to travel between buildings. 

输入数据

Line 1 contains integer N (2 ≤ N ≤ 16). 

Line 2 contains N integers t1, . . . , tN (1 ≤ ti ≤ 10^8 ). 

Line 3 contains N − 1 integers x2, . . . , xN (1 ≤ xi < i).

输出数据

Print one line with one integer, the minimum total seconds that guarantees a nice building can be found. 

样例输入

复制
3
10 40 20
1 2
 \n
  ·  ·  \n
 · \n

样例输出

复制
30
  \n

样例说明

For the example, inspecting buildings 1 and 3 guarantees a nice building will be found

Another input: 5
2 4 1 2 1
1 1 2 2
The output should be:
5

提交

请先 登录

© 2025 FAQs Contact About