The Great KL

3.6

25 votes
Medium, Tree, Greedy algorithm
Problem

View Russian Translation

The Great KL lives in city KLand. KLand has n junctions and n-1 bidirectional roads. One can reach any city from any other city using only these roads.

Each junction has a cupid in it. Cupids are of m different types. Type of the cupid in junction i is ci. KL wants to know for each of m types that what is the maximum distance between two cupids of that type. Distance between two junctions is the length of their shortest path (number of roads in the shortest path between them).

KL is not that smart and can't figure this out himself. Please help him!

Input

The first line of input contains two integers n and m (1 ≤ m ≤ n ≤ 105).

The second line contains n integers c1, c2, ..., cn separated by spaces (1 ≤ ci ≤ m).

The next n-1 lines contain the roads. Each line contains two integers v and u; Endpoints of a road (1 ≤ v, u ≤ n).

For each i between 1 to m there is at least one cupid whit that type.

Output

Print m integers in one line; i-th of them should be the answer for cupid-type i.

Sample Input
4 2
1 1 1 2
1 4
1 3
2 3

Sample Output
2 0 
Time Limit: 2
Memory Limit: 256
Source Limit:
Editor Image

?