#P8564. ρars/ey

ρars/ey

Problem Description

You are given a rooted tree with nn nodes, where the root is node 11. 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 fif_i, where ii is the size of the subtree of the chosen node.

You want to delete all nodes except node 11. What is the minimum total cost?

Input Format

The first line contains a positive integer nn.

The second line contains n1n-1 positive integers. The ii-th integer represents fi+1f_{i+1}.

The next n1n-1 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 55 nodes in the subtree of node 88 except itself, and then delete the 22 nodes in the subtree of node 11 except itself. The total cost is f6+f3=63744f_6 + f_3 = 63744. It can be proven that this is the minimum cost.

[Constraints]

For all testdata, it is guaranteed that 1n50001 \le n \le 5000 and 1fi1091 \le f_i \le 10^9.

$$\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}$$

A\textsf A: It is guaranteed that the degree of every node in the tree is at most 22, and the degree of node 11 is 11.

B\textsf B: It is guaranteed that only node 11 has degree at least 22.

C\textsf C: It is guaranteed that the tree is generated randomly. The generation method is: for i2i \ge 2, randomly choose an integer xx from [1,i1][1, i-1] and add an edge between xx and ii. Then randomly shuffle the labels of all nodes.

Translated by ChatGPT 5