P14919 [GESP202512 Level 6] Path Cover

Background

Corresponding multiple-choice and true/false questions:

Description

Given a rooted tree $T$ with $n$ nodes, where the nodes are numbered $1,2,\ldots,n$ and the root is node $1$. For convenience, the node numbered $i$ is called node $i$. Initially, all the nodes in $T$ are white. You need to color several nodes of $T$ black so that the path from every leaf to the root contains at least one black node. Coloring node $i$ black costs $c_i$. You need to minimize the total coloring cost under the above condition. A leaf is a node in $T$ that has no child nodes.

Input Format

The first line contains a positive integer $n$, the number of nodes. The second line contains $n-1$ positive integers $f_2,f_3,\ldots,f_n$, where $f_i$ is the number of the parent node of node $i$, and it is guaranteed that $f_i

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.

Explanation/Hint

For $40\%$ of the test data, it is guaranteed that $2\le n\le 16$. For another $20\%$ of the test data, it is guaranteed that $f_i=i-1$. For all the test data, it is guaranteed that $2\le n\le 10^5$ and $1\le c_i\le 10^9$. Translated by QwenWork