Mancunian and Birthday Gifts

0

0 votes
Centroid decomposition, Disjoint set, Hard, Inclusion Exclusion, Inclusion exclusion principle, Math, Trees
Problem

It's Mancunian's birthday! His friends will be arriving for the party from all over the country of Neverland. And it goes without saying that they will bring their gifts for Mancunian.

The country of Neverland consists of N cities connected by bidirectional roads. There are exactly N1 roads and it is possible to travel between any two cities using the roads. Given that there are N cities, there are exactly N(N1)/2 distinct unordered pairs of cities. Each such pair corresponds to a path between the two cities. By a huge stroke of coincidence, there are exactly N(N1)/2 guests invited to the party. Each of those guests will choose a distinct path and will travel along it.

There are M types of gifts available in the country of Neverland. The ith such gift (1-indexed) costs i dollars. Each city of the country sells only a single type of gift. While Mancunian's friends are happy for him, they do not want to spend a lot of money on their gift. So, each person will buy the cheapest gift not sold along any city along his/her path.

Can you help Mancunian find out the total value of all the gifts he will receive on his special day?

Input:
The first line contains two integers N and M denoting the number of cities and types of gifts respectively.
The second line contains N integers denoting the types of gifts Gi sold in the cities.
Each of the next N1 lines consists of two integers, the two endpoints of a road.

Output:
Output a single integer which is the answer to the problem.

Constraints:
3N50000
3M10
1Gi<M

Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

There are 5 cities and 10 guests.
The guest travelling between the cities 1 and 2 will buy a gift of cost 3.
The guest travelling between the cities 1 and 3 will buy a gift of cost 2.
The guest travelling between the cities 1 and 4 will buy a gift of cost 3.
The guest travelling between the cities 1 and 5 will buy a gift of cost 3.
The guest travelling between the cities 2 and 3 will buy a gift of cost 4.
The guest travelling between the cities 2 and 4 will buy a gift of cost 1.
The guest travelling between the cities 2 and 5 will buy a gift of cost 1.
The guest travelling between the cities 3 and 4 will buy a gift of cost 5.
The guest travelling between the cities 3 and 5 will buy a gift of cost 4.
The guest travelling between the cities 4 and 5 will buy a gift of cost 1.

Editor Image

?