#P14919. [GESP202512 六级] 路径覆盖
[GESP202512 六级] 路径覆盖
Background
Corresponding multiple-choice and true/false questions: https://ti.luogu.com.cn/problemset/1202
Problem Description
Given a rooted tree with nodes, where the nodes are numbered and the root is node . For convenience, the node numbered is called node .
Initially, all the nodes in are white. You need to color several nodes of black so that the path from every leaf to the root contains at least one black node. Coloring node black costs . You need to minimize the total coloring cost under the above condition.
A leaf is a node in that has no child nodes.
Input Format
The first line contains a positive integer , the number of nodes.
The second line contains positive integers , where is the number of the parent node of node , and it is guaranteed that .
The third line contains positive integers , where is the cost of coloring node 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 of the test data, it is guaranteed that .
For another of the test data, it is guaranteed that .
For all the test data, it is guaranteed that and .
Translated by QwenWork