## COCI '20 Contest 2 #4 Sjekira

View as PDF

Points: 12 (partial)
Time limit: 1.0s
Memory limit: 512M

Problem types

Mirko is tired of his stressful CEO job in a well-known multinational software company. With a golden parachute of several million dollars, he decided to live a simple life and move to Gorski Kotar. However, winters in the remote village he just moved in are tough. None of his neighbours were born after WWII, so he is destined to chop firewood himself.

Today, he is going to chop his first trunk. Before chopping, he labels the parts of the trunk which are small enough to fit in a fireplace, and measures their hardness.

Mirko is a programmer, so he notices that the parts and the connections between them form a tree graph.

The damage on his axe resulting from cutting a connection on the trunk is equal to the sum of the maximal hardnesses in the two connected components formed by cutting the connection.

Mirko has only one axe, so he wants the total damage to be as small as possible. He wants to know the minimal total damage on the axe, if he cuts the whole trunk into single parts which fit in a fireplace.

#### Input

The first line contains an integer , the number of parts. The parts are labeled by integers from to .

The second line contains integers . The number is the hardness of the part labeled .

Each of the following lines contains two integers and – labels of parts that are directly connected.

#### Output

Output the minimal total damage after cuts.

#### Scoring

In all subtasks it holds that .

Parts and are directly connected.

#### Sample Input 1

3
1 2 3
1 2
2 3

#### Sample Output 1

8

#### Explanation for Sample Output 1

There are two ways to cut this trunk. He can first cut the connection , which causes damage, and then cut the connection , which causes damage. The total damage is in this case.

Otherwise, he can first cut , and then . The total damage in that case .

#### Sample Input 2

4
2 2 3 2
1 3
3 2
4 3

#### Sample Output 2

15

#### Sample Input 3

5
5 2 3 1 4
2 1
3 1
2 4
2 5

#### Sample Output 3

26