luogu#P10555. [ICPC2024 Xi'an I] Fix the Tree
[ICPC2024 Xi'an I] Fix the Tree
题目描述
You are given a tree consisting of vertices. Each vertex of this tree has a value assigned to it.
Now the vertex will be broken. Once it's broken, vertex and all edges with one end at vertex will be removed from the tree.
To make the tree connected, you can do the following operation any number of times(possibly zero) in any order:
- First choose two vertices and from the tree;
- Then pay coins, and add an edge between vertices and ;
- At last, replace with , replace with .
Your task is to calculate the minimum number of coins to be paid.
But you don't know which vertex is, so you need to find the answer for each . Please answer all the queries independently.
输入格式
The first line contains a single integer --- the number of vertices in this tree.
The next line contains numbers, the -th number is .
The next lines contain a description of the tree's edges. The -th of these lines contains two integers and --- the numbers of vertices connected by the -th edge.
It is guaranteed that the given edges form a tree.
输出格式
Print integers, the -th integer is the answer when .
6
1 1 1 1 1 1
1 2
1 3
1 4
2 5
2 6
4 4 0 0 0 0
4
1 2 3 4
1 2
1 3
1 4
12 0 0 0
7
1 2 3 4 5 6 7
1 2
1 3
2 4
2 5
3 6
3 7
5 12 16 0 0 0 0