#P8564. ρars/ey
ρars/ey
Problem Description
You are given a rooted tree with nodes, where the root is node . You can perform the following operation any number of times:
- Choose a node, and delete all nodes in its subtree except itself.
The cost of this operation is , where is the size of the subtree of the chosen node.
You want to delete all nodes except node . What is the minimum total cost?
Input Format
The first line contains a positive integer .
The second line contains positive integers. The -th integer represents .
The next lines each contain two positive integers, representing an edge of the tree.
Output Format
Output one positive integer in a single line, representing the answer.
8
11000 18640 32793 36187 45104 64932 66425
6 8
3 6
3 7
1 8
1 4
3 5
2 7
63744
Hint
[Sample Explanation]
First, delete the nodes in the subtree of node except itself, and then delete the nodes in the subtree of node except itself. The total cost is . It can be proven that this is the minimum cost.
[Constraints]
For all testdata, it is guaranteed that and .
$$\def\arraystretch{1.5} \begin{array}{c|c|c}\hline \textbf{Test Point ID}&\bm{~~~~~~~~n\le~~~~~~~~}&~~~~\textbf{Special Constraints}~~~~\cr\hline \textsf1\sim \sf2 & 8& \cr\hline \sf3\sim 6 & 15& \cr\hline \sf7\sim 8 & 400&\textsf{A}\cr\hline \textsf9 & 400 &\sf B\cr\hline \sf10\sim 12 & 400&\cr\hline \sf13\sim 14 & &\sf C\cr\hline \sf15\sim 20 & &\cr\hline \end{array}$$: It is guaranteed that the degree of every node in the tree is at most , and the degree of node is .
: It is guaranteed that only node has degree at least .
: It is guaranteed that the tree is generated randomly. The generation method is: for , randomly choose an integer from and add an edge between and . Then randomly shuffle the labels of all nodes.
Translated by ChatGPT 5