This page is still under construction.

Parts of this page are still being built. What you see may change.

Vera and the Engineering Buildings

Time limit2sMemory limit512 MB

Summary
Given a tree of N nodes with distinct hidden values and inspection costs, find the minimum total cost of an adaptive strategy that is guaranteed to locate a local maximum.
Level

Hard8 of 10

Topics
Tree, Dynamic programming, Game theory, Bit manipulation
Solved
No attempts yet

Problem

The University of Waterloo has NN engineering buildings numbered from 1 to NN. For every ii with 2≤i≤N2 \le i \le N there is a two way bridge between building ii and building xix_i. Two buildings are neighbours when a bridge joins them.

Every building has its own aesthetic value, and no two buildings share a value. A value can be any integer. Measuring a value exactly is hard, so Vera only wants to find one nice building, a building whose aesthetic value is higher than the value of each of its neighbours.

Inspecting building ii takes tit_i seconds and covers the bridges to all of its neighbours. Once the inspection is over, Vera knows for each neighbour jj of building ii which of building ii and building jj has the higher aesthetic value.

Vera picks the next building to inspect after seeing the results of the earlier inspections. Travel time between buildings is ignored. Find the smallest total inspection time that guarantees Vera finds a nice building, whatever the aesthetic values are.

Input

The first line contains one integer NN (2≤N≤162 \le N \le 16).

The second line contains NN integers t1,t2,…,tNt_1, t_2, \dots, t_N (1≤ti≤1081 \le t_i \le 10^8).

The third line contains N−1N - 1 integers x2,x3,…,xNx_2, x_3, \dots, x_N (1≤xi<i1 \le x_i < i).

Output

Print one line with one integer, the minimum total number of seconds that guarantees a nice building is found.

Hint

In the first example, inspecting buildings 1 and 3 guarantees a nice building is found.

In the second example, one optimal strategy always inspects building 3 first.

Examples2

  1. Example 1

    Input
    3
    10 40 20
    1 2
    
    Expected output
    30
    
  2. Example 2

    Input
    5
    2 4 1 2 1
    1 1 2 2
    
    Expected output
    5