A Tree Problem Made By Me

Revision en1, by Georeth, 2017-05-04 21:53:56

Hello, I made a very classic tree problem. I want to get some clean solutions, because my solution seems very complicated. I made this problem on Polygon, but cannot make it public to every one. So I publish this problem here.

I hope you can give me good solutions (maybe by ideone.com / or join my group).

I create a group called "problem sharing" at http://codeforces.net/group/t0m8sIZmgT. You can join this group to solve this problem.

Maximum weight vertex for a fixed distance
time limit per test
2 seconds
memory limit per test
64 megabytes
input
standard input
output
standard output

Given a tree with n vertices and a positive integer D. Vertices are numbered from 1 to n. Each vertex u has a positive weight wu.

For each vertex u, compute ans[u] = max{wv: dist(u, v) = D}. if such vertex v does not exist, ans[u] =  - 1.

Where dist(u, v) is length of shortest path between u and v.

Input

First line contains integer n and D. (1 ≤ n ≤ 100000, 1 ≤ D ≤ 100).

Second line contains n integers w1, w2, ..., wn. (1 ≤ wi ≤ 109)

Then (n - 1) lines follow. Each line contains two integer u and v, which means there is an edge (u, v).

Output

Output n intergers in a single line. i-th of them should be ans[i].

Examples
Input
5 2
1 3 4 2 5
1 2
2 3
3 4
4 5
Output
4 2 5 3 4 
Input
5 2
1 3 7 2 5
1 5
2 5
3 5
4 5
Output
7 7 3 7 -1 

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English Georeth 2017-05-04 21:53:56 3574 Initial revision (published)