Under construction

Still being built. Things may change

Flower's Land

Time limit8sMemory limit2048 MB

Summary
For each city z, find the largest flower total over a connected set of exactly k cities that includes z, where z is the root of the tree.
Level

Hard8 of 10

Topics
Dynamic programming, Tree, Divide and conquer
Solved
No attempts yet

Statement

The Kingdom of Flowers has nn cities, and city ii grows aia_i flowers. There are n−1n-1 roads, and the ii-th road connects cities uiu_i and viv_i. Any two cities are connected by a path.

The kingdom wants to hold a flower exhibition. First, choose a city zz to build the exhibition hall. Then select exactly kk cities x1,x2,…,xkx_1, x_2, \ldots, x_k and transport their flowers to city zz.

The organizers require that if city xx is selected, then every city on the path from xx to zz must also be selected. In particular, city zz must be selected.

For each z=1,2,…,nz = 1, 2, \ldots, n, find the maximum number of flowers that can be transported when city zz is chosen for the exhibition hall.

Input

The first line contains two integers nn and kk (1≤n≤40 0001 \le n \le 40\,000, 1≤k≤min⁡(n,3000)1 \le k \le \min(n, 3000)).

The second line contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (1≤ai≤5⋅1051 \le a_i \le 5 \cdot 10^5).

Each of the next n−1n-1 lines contains two integers xx and yy (1≤x,y≤n1 \le x, y \le n, x≠yx \ne y), meaning there is an edge between vertices xx and yy. The given graph is guaranteed to be a tree.

Output

Output a single line with nn integers f1,f2,…,fnf_1, f_2, \ldots, f_n, where fif_i is the answer for z=iz = i.

Examples2

  1. Example 1

    Input
    5 3
    6 10 4 3 4
    3 4
    4 2
    2 5
    5 1
    
    Expected output
    20 20 17 17 20
    
  2. Example 2

    Input
    7 4
    1 3 2 1 7 12 17
    4 6
    1 4
    5 1
    2 5
    7 6
    3 2
    
    Expected output
    31 13 13 31 21 31 31