/*YYC is Thinking Here*/
#include<bits/stdc++.h>
#define mids(l,r) const auto mid = (l + r) >> 1
#define lb(x) (x&-x)
#define ls (x<<1)
#define rs (ls|1)
#define int long long
#define pii pair<int,int>
#define inf 0x3f3f3f3f3f3f3f3f
using namespace std;
const int maxn = 4e4+10,maxk = 4e3+10;
int n,k;
int a[maxn];
struct { int nxt,v; } edge[maxn<<1];
int head[maxn],ec;
void add(int u,int v) {edge[++ec]={head[u],v};head[u]=ec;}
void adds(int u,int v) {add(u,v),add(v,u);}
int siz[maxn],dfn[maxn],dfc,dfu[maxn];
bool vis[maxn];
int rt,rv;
void dfsiz(int u,int fa,int n) {
int mv = 0;
siz[u] = 1;
for(int i = head[u];i;i=edge[i].nxt) {
int v = edge[i].v;
if(v == fa || vis[v]) continue;
dfsiz(v,u,n);
siz[u] += siz[v];
mv = max(mv,siz[v]);
}
mv = max(mv,n - siz[u]);
// deb(u,fa,mv);
if(mv < rv) rt = u,rv = mv;
}
void dfsn(int u,int fa) {
siz[u] = 1;
dfu[dfn[u] = ++dfc] = u;
for(int i = head[u];i;i=edge[i].nxt) {
int v = edge[i].v;
if(v == fa || vis[v]) continue;
dfsn(v,u);
siz[u] += siz[v];
}
}
int getrt(int u,int n) {
rt = 0, rv = n;
dfsiz(u,0,n);
return rt;
}
void getdfn(int u) { dfsn(u,dfc = 0); }
int f[maxn][maxk],g[maxn][maxk]; //f: [i...n]; g:[1...i]
void calf(int n) {
memset(f,-0x3f,(n+2) * sizeof f[0]);
f[n+1][0] = 0;
for(int u = n;u;--u) {
f[u][0] = 0;
for(int i = 1;i <= k;++i)
f[u][i] = max(f[u+1][i-1] + a[dfu[u]],f[u + siz[dfu[u]]][i]);
}
}
struct {
int nxt,v;
}lk[maxn];
int fl[maxn],lc;
void lnk(int u,int v) {
// deb(u,v);
lk[++lc] = {fl[u],v};
fl[u] = lc;
}
void calg(int n) {
memset(g,-0x3f,(n+2) * sizeof g[0]);
// for(int i = 1;i <= n;++i) deb(siz[i]);
for(int u = 1;u <= n;++u) lnk(u + siz[dfu[u]] - 1,u - 1);
g[0][0] = 0;
g[1][1] = a[dfu[1]];
// cerr<<"_\n";
for(int u = 2;u <= n;++u) {
for(int i = 1;i <= k;++i) g[u][i] = g[u-1][i-1] + a[dfu[u]];
for(int e = fl[u];e;e=lk[e].nxt) {
int v = lk[e].v;
// deb(u,v);
for(int i = 0;i <= k;++i)
g[u][i] = max(g[u][i],g[v][i]);
}
} memset(fl,lc = 0,(n + 2) * sizeof fl[0]);
}
int ans[maxn],tmp[maxn];
void dfz(int u,int n) {
// infun(u,n);
if(n < k) return;
getdfn(u = getrt(u,n));
calf(n),calg(n),f[n+1][0]=g[0][0]=0;
//
// deb(rt);
// tap;cerr<<"dfu:\n";
// tap;for(int i = 1;i <= n;++i) cerr<<dfu[i]<<' ';
// tap;cerr<<"\ng:\n";
// tap;for(int i = 1;i <= n;++i,cerr<<'\n',tap)for(int j = 0;j <= k;++j)cerr<<g[i][j]<<' ';
// tap;cerr<<"\nf:\n";
// tap;for(int i = 1;i <= n;++i,cerr<<'\n',tap)for(int j = 0;j <= k;++j)cerr<<f[i][j]<<' ';
//
for(int u = 1;u <= n;++u) {
tmp[u] = g[u-1][0] + f[u+1][k-1] + a[dfu[u]];
for(int i = 1;i <= k;++i) tmp[u] = max(tmp[u],g[u-1][i] + f[u+1][k-i-1] + a[dfu[u]]);
}
for(int i = 1;i <= n;++i) ans[dfu[i]] = max(ans[dfu[i]],tmp[i]);
vis[u] = 1;
for(int i = head[u];i;i=edge[i].nxt){
int v = edge[i].v;
if(vis[v]) continue;
dfz(v,siz[v]);
}
}
signed main() {
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
ios::sync_with_stdio(0),cin.tie(0);
cin>>n>>k;
for(int i = 1;i <= n;++i) cin>>a[i];
for(int i = 1,u,v;i < n;++i)
cin>>u>>v,adds(u,v);
dfz(1,n);
for(int i = 1;i <= n;++i) cout<<ans[i]<<' ';
}
/*
* I love it all
* Go forward,move forward !
*/