33OJ
登录 注册

#P14919. [GESP202512 六级] 路径覆盖

[GESP202512 六级] 路径覆盖

Background

Corresponding multiple-choice and true/false questions: https://ti.luogu.com.cn/problemset/1202

Problem Description

Given a rooted tree TT with nn nodes, where the nodes are numbered 1,2,…,n1,2,\ldots,n and the root is node 11. For convenience, the node numbered ii is called node ii.

Initially, all the nodes in TT are white. You need to color several nodes of TT black so that the path from every leaf to the root contains at least one black node. Coloring node ii black costs cic_i. You need to minimize the total coloring cost under the above condition.

A leaf is a node in TT that has no child nodes.

Input Format

The first line contains a positive integer nn, the number of nodes.

The second line contains n−1n-1 positive integers f2,f3,…,fnf_2,f_3,\ldots,f_n, where fif_i is the number of the parent node of node ii, and it is guaranteed that fi<if_i<i.

The third line contains nn positive integers c1,c2,…,cnc_1,c_2,\ldots,c_n, where cic_i is the cost of coloring node ii black.

Output Format

Output a single line with an integer, the minimum total coloring cost under the condition that the path from every leaf to the root contains at least one black node.

4
1 2 3
5 6 2 3
2
7
1 1 2 2 3 3
64 16 15 4 3 2 1
10

Hint

For 40%40\% of the test data, it is guaranteed that 2≤n≤162\le n\le 16.

For another 20%20\% of the test data, it is guaranteed that fi=i−1f_i=i-1.

For all the test data, it is guaranteed that 2≤n≤1052\le n\le 10^5 and 1≤ci≤1091\le c_i\le 10^9.

Translated by QwenWork