#include <bits/stdc++.h>
using namespace std;
int a[201], p[201], s[201][201], k;
vector<int> g[201];

int sol(int n, int d){
	if(s[n][d]) return s[n][d];
	for(int i=0;i<g[n].size();++i){
		if(g[n][i] != p[n])
			p[g[n][i]] = n;
	}
	int r=0,tmp;
	if(d == 0){
		for(int i=0;i<g[n].size();++i){
			if(g[n][i] != p[n]){
				for(int dis = k-1;dis>-1;--dis){
					tmp = sol(g[n][i], dis);
					for(int j=0;j<g[n].size();++j){
						if(i!=j && g[n][j] != p[n]) tmp+=sol(g[n][j],max(k-1-dis,dis));
					}
					r = max(r,tmp);
				}
			}
		}

		tmp = a[n];
		for(int i=0;i<g[n].size();++i){
			if(g[n][i] != p[n]){
				tmp += sol(g[n][i],k);
			}
		}
		r = max(r,tmp);
	}
	else{
		for(int i=0;i<g[n].size();++i){
			if(g[n][i] != p[n]){
				tmp = sol(g[n][i], d-1);
				for(int j=0;j<g[n].size();++j){
					if(i!=j && g[n][j] != p[n]) tmp+=sol(g[n][j],max(k-d,d-1));
				}
				r = max(r,tmp);
			}
		}
	}

	s[n][d] = r;
	for(int i=k-1;i>d;--i){
		s[n][i-1]= max(s[n][i-1], sol(n,i));
	}

	return s[n][d];
}
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int n;
	cin>>n>>k;
	for(int i=1;i<=n;++i){
		cin>>a[i];
	}
	for(int i=1;i<n;++i){
		int u,v;
		cin>>u>>v;
		g[u].push_back(v);
		g[v].push_back(u);
	}

	cout<<sol(1,0);
    return 0;
}


#include <bits/stdc++.h>W
using namespace std;
int a[201], p[201], s[201][201], k;
vector<int> g[201];

int sol(int n, int d){
	if(s[n][d]!=-1) return s[n][d];

	for(int i=0;i<g[n].size();++i){
		if(g[n][i] != p[n])
			p[g[n][i]] = n;
	}

	int r=0,tmp;
	if(d == 0){
		r = a[n];
		for(int i=0;i<g[n].size();++i){
			if(g[n][i] != p[n]){
				r += sol(g[n][i],k);
			}
		}
	}
	else{
		for(int i=0;i<g[n].size();++i){
			if(g[n][i] != p[n]){
				tmp = sol(g[n][i], d-1);
				for(int j=0;j<g[n].size();++j){
					if(i!=j && g[n][j] != p[n]) tmp+=sol(g[n][j],max(k-d,d-1));
				}
				r = max(r,tmp);
			}
		}
	}

	s[n][d] = r;
	s[n][d] = max(s[n][d], sol(n,min(k,d+1)));
	return s[n][d];
}
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int n;
	cin>>n>>k;
	for(int i=1;i<=n;++i){
		cin>>a[i];
	}

	for(int i=1;i<n;++i){
		int u,v;
		cin>>u>>v;
		g[u].push_back(v);
		g[v].push_back(u);
	}

	for(int i=1;i<=n;++i){
		for(int j=0;j<=k;++j){
			s[i][j] = -1;
		}
	}
	cout<<sol(1,0);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int a[201], p[201], s[201][201], k;
vector<int> g[201];

int sol(int n, int d){
	if(s[n][d]) return s[n][d];
	for(int i=0;i<g[n].size();++i){
		if(g[n][i] != p[n])
			p[g[n][i]] = n;
	}
	int r=0,tmp;
	if(d == 0){
		for(int i=0;i<g[n].size();++i){
			if(g[n][i] != p[n]){
				for(int dis = k-1;dis>-1;--dis){
					tmp = sol(g[n][i], dis);
					for(int j=0;j<g[n].size();++j){
						if(i!=j && g[n][j] != p[n]) tmp+=sol(g[n][j],max(k-1-dis,dis));
					}
					r = max(r,tmp);
				}
			}
		}

		tmp = a[n];
		for(int i=0;i<g[n].size();++i){
			if(g[n][i] != p[n]){
				tmp += sol(g[n][i],k);
			}
		}
		r = max(r,tmp);
	}
	else{
		for(int i=0;i<g[n].size();++i){
			if(g[n][i] != p[n]){
				r += sol(g[n][i],d-1);
			}
		}
	}
	s[n][d] = r;
	return r;
}
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int n;
	cin>>n>>k;
	for(int i=1;i<=n;++i){
		cin>>a[i];
	}
	for(int i=1;i<n;++i){
		int u,v;
		cin>>u>>v;
		g[u].push_back(v);
		g[v].push_back(u);
	}

	cout<<sol(1,0);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int a[201], p[201], s[201][201], k;
vector<int> g[201];

int sol(int n, int d){
	if(s[n][d]) return s[n][d];
	for(int i=0;i<g[n].size();++i){
		if(g[n][i] != p[n])
			p[g[n][i]] = n;
	}
	int r=0,tmp;
	if(d == 0){
		for(int i=0;i<g[n].size();++i){
			if(g[n][i] != p[n]){
				for(int dis = k-1;dis>-1;--dis){
					tmp = sol(g[n][i], dis);
					for(int j=0;j<g[n].size();++j){
						if(i!=j && g[n][j] != p[n]) tmp+=sol(g[n][j],max(k-1-dis,dis));
					}
					r = max(r,tmp);
				}
			}
		}

		tmp = a[n];
		for(int i=0;i<g[n].size();++i){
			if(g[n][i] != p[n]){
				tmp += sol(g[n][i],k);
			}
		}
		r = max(r,tmp);
	}
	else{
		for(int i=0;i<g[n].size();++i){
			if(g[n][i] != p[n]){
				tmp = sol(g[n][i], d-1);
				for(int j=0;j<g[n].size();++j){
					if(i!=j && g[n][j] != p[n]) tmp+=sol(g[n][j],max(k-d,d-1));
				}
				r = max(r,tmp);
			}
		}
//		for(int i=0;i<g[n].size();++i){
//			if(g[n][i] != p[n]){
//				r += sol(g[n][i],d-1);
//			}
//		}
	}
	s[n][d] = r;
	return r;
}
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int n;
	cin>>n>>k;
	for(int i=1;i<=n;++i){
		cin>>a[i];
	}
	for(int i=1;i<n;++i){
		int u,v;
		cin>>u>>v;
		g[u].push_back(v);
		g[v].push_back(u);
	}

	cout<<sol(1,0);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int a[201], p[201], s[201][201], k;
vector<int> g[201];

int sol(int n, int d){
	if(s[n][d]) return s[n][d];
	for(int i=0;i<g[n].size();++i){
		if(g[n][i] != p[n])
			p[g[n][i]] = n;
	}
	int r=0,tmp;
	if(d == 0){
		r = a[n];
		for(int i=0;i<g[n].size();++i){
			if(g[n][i] != p[n]){
				r += sol(g[n][i],k);
			}
		}
	}
	else{
		for(int i=0;i<g[n].size();++i){
			if(g[n][i] != p[n]){
				tmp = sol(g[n][i], d-1);
				for(int j=0;j<g[n].size();++j){
					if(i!=j && g[n][j] != p[n]) tmp+=sol(g[n][j],max(k-d,d-1));
				}
				r = max(r,tmp);
			}
		}
	}

	s[n][d] = r;
	for(int i=k;i>d;--i){
		s[n][i-1] = max(s[n][i-1],sol(n,i));
	}
	return s[n][d];
}
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int n;
	cin>>n>>k;
	for(int i=1;i<=n;++i){
		cin>>a[i];
	}
	for(int i=1;i<n;++i){
		int u,v;
		cin>>u>>v;
		g[u].push_back(v);
		g[v].push_back(u);
	}

	cout<<sol(1,0);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int a[201], p[201], s[201][201], k;
vector<int> g[201];

int sol(int n, int d){
	if(s[n][d]) return s[n][d];
	for(int i=0;i<g[n].size();++i){
		if(g[n][i] != p[n])
			p[g[n][i]] = n;
	}
	int r=0,tmp;
	if(d == 0){
		for(int i=0;i<g[n].size();++i){
			if(g[n][i] != p[n]){
				for(int dis = k-1;dis>-1;--dis){
					tmp = sol(g[n][i], dis);
					for(int j=0;j<g[n].size();++j){
						if(i!=j && g[n][j] != p[n]) tmp+=sol(g[n][j],k-1-dis);
					}
					r = max(r,tmp);
				}
			}
		}

		//
		tmp = a[n];
		for(int i=0;i<g[n].size();++i){
			if(g[n][i] != p[n]){
				tmp += sol(g[n][i],k);
			}
		}
		r = max(r,tmp);
	}
	else{
		for(int i=0;i<g[n].size();++i){
			if(g[n][i] != p[n]){
				r += sol(g[n][i],d-1);
			}
		}
	}
	s[n][d] = r;
	return r;
}
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int n;
	cin>>n>>k;
	for(int i=1;i<=n;++i){
		cin>>a[i];
	}
	for(int i=1;i<n;++i){
		int u,v;
		cin>>u>>v;
		g[u].push_back(v);
		g[v].push_back(u);
	}

	cout<<sol(1,0);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef pair< int , int >PII;
const int MAXN = 205;

int a[MAXN];
vector< int >edg[MAXN];
int dst[MAXN];
int n, k;

vector< int >order;
int position[MAXN];

//vector< int >taken;
int ans;
int run;
int optimism;
int calls;

int reachable[MAXN];
int dfs(int u, int p, int depth) {
    if (depth == 0) return 1;
    int rt = 1;
    for (int v : edg[u]) {
        if (v==p) continue;
        rt += dfs(v, u, depth-1);
    }
    return rt;
}

void dfs(int i) {
    calls++;
    if (run > ans) {
        ans = run;
//        cout << run << " with";
//        for (int i : taken) cout << " " << i;
//        cout << endl;
    }
    if (i == n) return;
    if (run + optimism <= ans) return;

    int s = order[i];

    if (dst[s] > k) {
        vector< PII >changes;
        changes.emplace_back(s, dst[s]);

        vector< int >q;
        q.push_back(s);
        dst[s] = 0;
        int blocked = a[s];
        for (int j = 0; j < k; j++) {
            vector< int >r;
            for (int u : q) {
                for (int v : edg[u]) {
                    if (dst[v] > dst[u]+1) {
                        if (dst[v] > k && position[v] > i) blocked += a[v];
                        changes.emplace_back(v, dst[v]);
                        dst[v] = dst[u]+1;
                        r.push_back(v);
                    }
                }
            }
            swap(r, q);
        }

//        taken.push_back(s);
        run += a[s];
        optimism -= blocked;
        dfs(i+1);
//        taken.pop_back();
        optimism += blocked;
        run -= a[s];
        for (PII p : changes) {
            dst[p.first] = p.second;
        }
    }

    if (dst[s] > k) optimism -= a[s];
    dfs(i+1);
    if (dst[s] > k) optimism += a[s];
}

int main() {
    cin >> n >> k;

    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }

    for (int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        edg[u].push_back(v);
        edg[v].push_back(u);
    }

    for (int i = 1; i <= n; i++) {
        dst[i] = MAXN;
        optimism += a[i];
        order.push_back(i);

        reachable[i] = dfs(i, 0, k);
    }

    sort(order.begin(), order.end(), [&](int i, int j){
        return reachable[i] > reachable[j];
    });

//    sort(order.begin(), order.end(), [&](int i, int j){
//        return a[i] > a[j];
//    });

//    sort(order.begin(), order.end(), [&](int i, int j){
//        return make_pair(reachable[i], a[i]) > make_pair(reachable[j], a[j]);
//    });

    for (int i = 0; i < n; i++) position[order[i]] = i;
    dfs(0);

    cout << ans << "\n";
//    cout << "calls: " << calls << endl;

    return 0;
}
///35 61


#include<bits/stdc++.h>
using namespace std;
typedef pair< int , int >PII;
const int MAXN = 205;

int a[MAXN];
vector< int >edg[MAXN];
int dst[MAXN];
int n, k;

vector< int >order;
int position[MAXN];

//vector< int >taken;
int ans;
int run;
int optimism;
int calls;

int reachable[MAXN];
int dfs(int u, int p, int depth) {
    if (depth == 0) return 1;
    int rt = 1;
    for (int v : edg[u]) {
        if (v==p) continue;
        rt += dfs(v, u, depth-1);
    }
    return rt;
}

void dfs(int i) {
    calls++;
    if (run > ans) {
        ans = run;
//        cout << run << " with";
//        for (int i : taken) cout << " " << i;
//        cout << endl;
    }
    if (i == n) return;
    if (run + optimism <= ans) return;

    int s = order[i];

    if (dst[s] > k) {
        vector< PII >changes;
        changes.emplace_back(s, dst[s]);

        vector< int >q;
        q.push_back(s);
        dst[s] = 0;
        int blocked = a[s];
        for (int j = 0; j < k; j++) {
            vector< int >r;
            for (int u : q) {
                for (int v : edg[u]) {
                    if (dst[v] > dst[u]+1) {
                        if (dst[v] > k && position[v] > i) blocked += a[v];
                        changes.emplace_back(v, dst[v]);
                        dst[v] = dst[u]+1;
                        r.push_back(v);
                    }
                }
            }
            swap(r, q);
        }

//        taken.push_back(s);
        run += a[s];
        optimism -= blocked;
        dfs(i+1);
//        taken.pop_back();
        optimism += blocked;
        run -= a[s];
        for (PII p : changes) {
            dst[p.first] = p.second;
        }
    }

    if (dst[s] > k) optimism -= a[s];
    dfs(i+1);
    if (dst[s] > k) optimism += a[s];
}

int main() {
    cin >> n >> k;

    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }

    for (int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        edg[u].push_back(v);
        edg[v].push_back(u);
    }

    for (int i = 1; i <= n; i++) {
        dst[i] = MAXN;
        optimism += a[i];
        order.push_back(i);

        reachable[i] = dfs(i, 0, k);
    }

//    sort(order.begin(), order.end(), [&](int i, int j){
//        return reachable[i] > reachable[j];
//    });

    sort(order.begin(), order.end(), [&](int i, int j){
        return a[i] > a[j];
    });

//    sort(order.begin(), order.end(), [&](int i, int j){
//        return make_pair(reachable[i], a[i]) > make_pair(reachable[j], a[j]);
//    });

    for (int i = 0; i < n; i++) position[order[i]] = i;
    dfs(0);

    cout << ans << "\n";
    //cout << "calls: " << calls << endl;

    return 0;
}
///35 61


#include<bits/stdc++.h>
using namespace std;
typedef pair< int , int >PII;
const int MAXN = 205;

int a[MAXN];
vector< int >edg[MAXN];
int dst[MAXN];
int n, k;

vector< int >order;

//vector< int >taken;
int ans;
int run;
int optimism;
int calls;

int reachable[MAXN];
int dfs(int u, int p, int depth) {
    if (depth == 0) return 1;
    int rt = 1;
    for (int v : edg[u]) {
        if (v==p) continue;
        rt += dfs(v, u, depth-1);
    }
    return rt;
}

void dfs(int i) {
    calls++;
    if (run > ans) {
        ans = run;
//        cout << run << " with";
//        for (int i : taken) cout << " " << i;
//        cout << endl;
    }
    if (i == n) return;
    if (run + optimism <= ans) return;

    int s = order[i];

    if (dst[s] > k) {
        vector< PII >changes;
        changes.emplace_back(s, dst[s]);

        vector< int >q;
        q.push_back(s);
        dst[s] = 0;
        int blocked = a[s];
        for (int i = 0; i < k; i++) {
            vector< int >r;
            for (int u : q) {
                for (int v : edg[u]) {
                    if (dst[v] > dst[u]+1) {
                        if (dst[v] > k) blocked += a[v];
                        changes.emplace_back(v, dst[v]);
                        dst[v] = dst[u]+1;
                        r.push_back(v);
                    }
                }
            }
            swap(r, q);
        }

//        taken.push_back(s);
        run += a[s];
        optimism -= blocked;
        dfs(i+1);
//        taken.pop_back();
        optimism += blocked;
        run -= a[s];
        for (PII p : changes) {
            dst[p.first] = p.second;
        }
    }

    if (dst[s] > k) optimism -= a[s];
    dfs(i+1);
    if (dst[s] > k) optimism += a[s];
}

int main() {
    cin >> n >> k;

    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }

    for (int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        edg[u].push_back(v);
        edg[v].push_back(u);
    }

    for (int i = 1; i <= n; i++) {
        dst[i] = MAXN;
        optimism += a[i];
        order.push_back(i);

        reachable[i] = dfs(i, 0, k);
    }

    sort(order.begin(), order.end(), [&](int i, int j){
        return reachable[i] > reachable[j];
    });

    dfs(0);

    cout << ans << "\n";
//    cout << "calls: " << calls << endl;

    return 0;
}
///35 61


#include<bits/stdc++.h>
using namespace std;
const int MAXN = 201;
const int INF = 1e9+7;

int a[MAXN];
int dp[MAXN][MAXN][MAXN];
int sbtr[MAXN];
vector< int >edg[MAXN];

void maxi(int &a, int b) {
    if (a < b) a = b;
}
int n, k;

void dfs(int u, int p) {
    sbtr[u] = 1;
    dp[u][1][0] = a[u];
    for (int v : edg[u]) {
        if (v==p) continue;
        dfs(v, u);
        for (int n = 0; n <= sbtr[v]; n++) {
            for (int d = sbtr[v]; d > 0; d--) dp[v][n][d] = dp[v][n][d-1];
            dp[v][n][0] = -INF;
        }

        for (int sum = sbtr[u]+sbtr[v]; sum > 1; sum--) {
            for (int nu = min(sbtr[u], sum-1); nu > 0; nu--) {
                int nv = sum-nu;
                if (nv > sbtr[v]) break;
                assert(nu < sum);
                assert(nv < sum);
                for (int du = sbtr[u]; du >= 0; du--) {
                    for (int dv = sbtr[v]; dv >= 0; dv--) {
                        if (du+dv <= k) break;
                        maxi(dp[u][sum][min(du, dv)], dp[u][nu][du]+dp[v][nv][dv]);
                    }
                }
            }
        }

        for (int n = 0; n <= sbtr[v]; n++) {
            for (int d = 0; d <= sbtr[v]; d++) {
                maxi(dp[u][n][d], dp[v][n][d]);
            }
        }

        sbtr[u] += sbtr[v];
    }

//    for (int n = 0; n <= sbtr[u]; n++) {
//        for (int d = 0; d < sbtr[u]; d++) {
//            if (dp[u][n][d] >= 0) {
//                cout << "dp[" << u << "][" << n << "][" << d << "] = " << dp[u][n][d] << endl;
//            }
//        }
//    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    cin >> n >> k;

    for (int i = 1; i <= n; i++) cin >> a[i];
    for (int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        edg[u].push_back(v);
        edg[v].push_back(u);
    }

    for (int i = 0; i < MAXN; i++)
        for (int j = 0; j < MAXN; j++)
            for (int k = 0; k < MAXN; k++)
                dp[i][j][k] = -INF;

    dfs(1, 0);

    int ans = 0;
    for (int j = 1; j <= sbtr[1]; j++) {
        for (int k = 0; k < sbtr[1]; k++) {
            maxi(ans, dp[1][j][k]);
        }
    }

    cout << ans << "\n";

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef pair< int , int >PII;
const int MAXN = 205;

int a[MAXN];
vector< int >edg[MAXN];
int dst[MAXN];
int n, k;

vector< int >order;

//vector< int >taken;
int ans;
int run;
int optimism;
int calls;

int reachable[MAXN];
int dfs(int u, int p, int depth) {
    if (depth == 0) return 1;
    int rt = 1;
    for (int v : edg[u]) {
        if (v==p) continue;
        rt += dfs(v, u, depth-1);
    }
    return rt;
}

void dfs(int i) {
    calls++;
    if (run > ans) {
        ans = run;
//        cout << run << " with";
//        for (int i : taken) cout << " " << i;
//        cout << endl;
    }
    if (i == n) return;
    if (run + optimism <= ans) return;

    int s = order[i];

    if (dst[s] > k) {
        vector< PII >changes;
        changes.emplace_back(s, dst[s]);

        vector< int >q;
        q.push_back(s);
        dst[s] = 0;
        int blocked = a[s];
        for (int i = 0; i < k; i++) {
            vector< int >r;
            for (int u : q) {
                for (int v : edg[u]) {
                    if (dst[v] > dst[u]+1) {
                        if (dst[v] > k) blocked += a[v];
                        changes.emplace_back(v, dst[v]);
                        dst[v] = dst[u]+1;
                        r.push_back(v);
                    }
                }
            }
            swap(r, q);
        }

//        taken.push_back(s);
        run += a[s];
        optimism -= blocked;
        dfs(i+1);
//        taken.pop_back();
        optimism += blocked;
        run -= a[s];
        for (PII p : changes) {
            dst[p.first] = p.second;
        }
    }

    dfs(i+1);
}

int main() {
    cin >> n >> k;

    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }

    for (int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        edg[u].push_back(v);
        edg[v].push_back(u);
    }

    for (int i = 1; i <= n; i++) {
        dst[i] = MAXN;
        optimism += a[i];
        order.push_back(i);

        reachable[i] = dfs(i, 0, k);
    }

    sort(order.begin(), order.end(), [&](int i, int j){
        return reachable[i] > reachable[j];
    });

    sort(order.begin(), order.end(), [&](int i, int j){
        return a[i] > a[j];
    });

    dfs(0);

    cout << ans << "\n";
//    cout << "calls: " << calls << endl;

    return 0;
}
///35 61


#include<bits/stdc++.h>
using namespace std;
typedef pair< int , int >PII;
const int MAXN = 205;

int a[MAXN];
vector< int >edg[MAXN];
int dst[MAXN];
int n, k;

vector< int >order;
int position[MAXN];

//vector< int >taken;
int ans;
int run;
int optimism;
int calls;

int reachable[MAXN];
int dfs(int u, int p, int depth) {
    if (depth == 0) return 1;
    int rt = 1;
    for (int v : edg[u]) {
        if (v==p) continue;
        rt += dfs(v, u, depth-1);
    }
    return rt;
}

void dfs(int i) {
    calls++;
    if (run > ans) {
        ans = run;
//        cout << run << " with";
//        for (int i : taken) cout << " " << i;
//        cout << endl;
    }
    if (i == n) return;
    if (run + optimism <= ans) return;

    int s = order[i];

    if (dst[s] > k) {
        vector< PII >changes;
        changes.emplace_back(s, dst[s]);

        vector< int >q;
        q.push_back(s);
        dst[s] = 0;
        int blocked = a[s];
        for (int j = 0; j < k; j++) {
            vector< int >r;
            for (int u : q) {
                for (int v : edg[u]) {
                    if (dst[v] > dst[u]+1) {
                        if (dst[v] > k && position[v] > i) blocked += a[v];
                        changes.emplace_back(v, dst[v]);
                        dst[v] = dst[u]+1;
                        r.push_back(v);
                    }
                }
            }
            swap(r, q);
        }

//        taken.push_back(s);
        run += a[s];
        optimism -= blocked;
        dfs(i+1);
//        taken.pop_back();
        optimism += blocked;
        run -= a[s];
        for (PII p : changes) {
            dst[p.first] = p.second;
        }
    }

    if (dst[s] > k) optimism -= a[s];
    dfs(i+1);
    if (dst[s] > k) optimism += a[s];
}

int main() {
    cin >> n >> k;

    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }

    for (int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        edg[u].push_back(v);
        edg[v].push_back(u);
    }

    double mxa = 0, mxr = 0;
    for (int i = 1; i <= n; i++) {
        dst[i] = MAXN;
        optimism += a[i];
        order.push_back(i);

        reachable[i] = dfs(i, 0, k);
        mxa = max(mxa, (double)a[i]);
        mxr = max(mxr, (double)reachable[i]);
    }

//    sort(order.begin(), order.end(), [&](int i, int j){
//        return reachable[i] > reachable[j];
//    });
//
//    sort(order.begin(), order.end(), [&](int i, int j){
//        return a[i] > a[j];
//    });

//    sort(order.begin(), order.end(), [&](int i, int j){
//        return make_pair(reachable[i], a[i]) > make_pair(reachable[j], a[j]);
//    });
//    sort(order.begin(), order.end(), [&](int i, int j){
//        return make_pair(a[i], reachable[i]) > make_pair(a[j], reachable[j]);
//    });

    sort(order.begin(), order.end(), [&](int i, int j){
        return (reachable[i]/mxr+a[i]/mxa)<(reachable[j]/mxr+a[j]/mxa);
    });

    for (int i = 0; i < n; i++) position[order[i]] = i;
    dfs(0);

    cout << ans << "\n";
//    cout << "calls: " << calls << endl;

    return 0;
}
///35 61


#include<bits/stdc++.h>
using namespace std;
typedef pair< int , int >PII;
const int MAXN = 205;

int a[MAXN];
vector< int >edg[MAXN];
int dst[MAXN];
int n, k;

vector< int >order;
int position[MAXN];

//vector< int >taken;
int ans;
int run;
int optimism;
int calls;

int reachable[MAXN];
int dfs(int u, int p, int depth) {
    if (depth == 0) return 1;
    int rt = 1;
    for (int v : edg[u]) {
        if (v==p) continue;
        rt += dfs(v, u, depth-1);
    }
    return rt;
}

void dfs(int i) {
    calls++;
    if (run > ans) {
        ans = run;
//        cout << run << " with";
//        for (int i : taken) cout << " " << i;
//        cout << endl;
    }
    if (i == n) return;
    if (run + optimism <= ans) return;

    int s = order[i];

    if (dst[s] > k) {
        vector< PII >changes;
        changes.emplace_back(s, dst[s]);

        vector< int >q;
        q.push_back(s);
        dst[s] = 0;
        int blocked = a[s];
        for (int j = 0; j < k; j++) {
            vector< int >r;
            for (int u : q) {
                for (int v : edg[u]) {
                    if (dst[v] > dst[u]+1) {
                        if (dst[v] > k && position[v] > i) blocked += a[v];
                        changes.emplace_back(v, dst[v]);
                        dst[v] = dst[u]+1;
                        r.push_back(v);
                    }
                }
            }
            swap(r, q);
        }

//        taken.push_back(s);
        run += a[s];
        optimism -= blocked;
        dfs(i+1);
//        taken.pop_back();
        optimism += blocked;
        run -= a[s];
        for (PII p : changes) {
            dst[p.first] = p.second;
        }
    }

    if (dst[s] > k) optimism -= a[s];
    dfs(i+1);
    if (dst[s] > k) optimism += a[s];
}

int main() {
    cin >> n >> k;

    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }

    for (int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        edg[u].push_back(v);
        edg[v].push_back(u);
    }

    for (int i = 1; i <= n; i++) {
        dst[i] = MAXN;
        optimism += a[i];
        order.push_back(i);

        reachable[i] = dfs(i, 0, k);
    }

//    sort(order.begin(), order.end(), [&](int i, int j){
//        return reachable[i] > reachable[j];
//    });
//
//    sort(order.begin(), order.end(), [&](int i, int j){
//        return a[i] > a[j];
//    });

//    sort(order.begin(), order.end(), [&](int i, int j){
//        return make_pair(reachable[i], a[i]) > make_pair(reachable[j], a[j]);
//    });
    sort(order.begin(), order.end(), [&](int i, int j){
        return make_pair(a[i], reachable[i]) > make_pair(a[j], reachable[j]);
    });

    for (int i = 0; i < n; i++) position[order[i]] = i;
    dfs(0);

    cout << ans << "\n";
//    cout << "calls: " << calls << endl;

    return 0;
}
///35 61


#include<bits/stdc++.h>
using namespace std;
typedef pair< int , int >PII;
const int MAXN = 205;

int a[MAXN];
vector< int >edg[MAXN];
int dst[MAXN];
int n, k;

vector< int >order;
int position[MAXN];

//vector< int >taken;
int ans;
int run;
int optimism;
int calls;

int reachable[MAXN];
int dfs(int u, int p, int depth) {
    if (depth == 0) return 1;
    int rt = 1;
    for (int v : edg[u]) {
        if (v==p) continue;
        rt += dfs(v, u, depth-1);
    }
    return rt;
}

void dfs(int i) {
    calls++;
    if (run > ans) {
        ans = run;
//        cout << run << " with";
//        for (int i : taken) cout << " " << i;
//        cout << endl;
    }
    if (i == n) return;
    if (run + optimism <= ans) return;

    int s = order[i];

    if (dst[s] > k) {
        vector< PII >changes;
        changes.emplace_back(s, dst[s]);

        vector< int >q;
        q.push_back(s);
        dst[s] = 0;
        int blocked = a[s];
        for (int i = 0; i < k; i++) {
            vector< int >r;
            for (int u : q) {
                for (int v : edg[u]) {
                    if (dst[v] > dst[u]+1) {
                        if (dst[v] > k && position[v] > i) blocked += a[v];
                        changes.emplace_back(v, dst[v]);
                        dst[v] = dst[u]+1;
                        r.push_back(v);
                    }
                }
            }
            swap(r, q);
        }

//        taken.push_back(s);
        run += a[s];
        optimism -= blocked;
        dfs(i+1);
//        taken.pop_back();
        optimism += blocked;
        run -= a[s];
        for (PII p : changes) {
            dst[p.first] = p.second;
        }
    }

    if (dst[s] > k) optimism -= a[s];
    dfs(i+1);
    if (dst[s] > k) optimism += a[s];
}

int main() {
    cin >> n >> k;

    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }

    for (int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        edg[u].push_back(v);
        edg[v].push_back(u);
    }

    for (int i = 1; i <= n; i++) {
        dst[i] = MAXN;
        optimism += a[i];
        order.push_back(i);

        reachable[i] = dfs(i, 0, k);
    }

    sort(order.begin(), order.end(), [&](int i, int j){
        return reachable[i] > reachable[j];
    });

//    sort(order.begin(), order.end(), [&](int i, int j){
//        return a[i] > a[j];
//    });

//    sort(order.begin(), order.end(), [&](int i, int j){
//        return make_pair(reachable[i], a[i]) > make_pair(reachable[j], a[j]);
//    });

    for (int i = 0; i < n; i++) position[order[i]] = i;
    dfs(0);

    cout << ans << "\n";
//    cout << "calls: " << calls << endl;

    return 0;
}
///35 61


#include<bits/stdc++.h>
using namespace std;
const int MAXN = 201;
const int INF = 1e9+7;

int a[MAXN];
int dp[MAXN][MAXN];
int sbtr[MAXN];
vector< int >edg[MAXN];

void maxi(int &a, int b) {
    if (a < b) a = b;
}
int n, k;

void dfs(int u, int p) {
    sbtr[u] = 1;
    dp[u][0] = a[u];
    for (int v : edg[u]) {
        if (v==p) continue;
        dfs(v, u);
        for (int d = sbtr[v]; d > 0; d--) dp[v][d] = dp[v][d-1];
        dp[v][0] = -INF;

        vector< int >tmp(sbtr[u]+sbtr[v], -INF);
        for (int i = 0; i <= sbtr[u]; i++) tmp[i] = dp[u][i];

        for (int du = sbtr[u]; du >= 0; du--) {
            for (int dv = sbtr[v]; dv >= 0; dv--) {
                if (du+dv <= k) break;
                maxi(tmp[min(du, dv)], dp[u][du]+dp[v][dv]);
            }
        }

        for (int d = 0; d <= sbtr[v]; d++) {
            maxi(tmp[d], dp[v][d]);
        }

        sbtr[u] += sbtr[v];
        for (int i = 0; i < sbtr[u]; i++) dp[u][i] = tmp[i];
    }

//    for (int n = 0; n <= sbtr[u]; n++) {
//        for (int d = 0; d < sbtr[u]; d++) {
//            if (dp[u][n][d] >= 0) {
//                cout << "dp[" << u << "][" << n << "][" << d << "] = " << dp[u][n][d] << endl;
//            }
//        }
//    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    cin >> n >> k;

    for (int i = 1; i <= n; i++) cin >> a[i];
    for (int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        edg[u].push_back(v);
        edg[v].push_back(u);
    }

    for (int i = 0; i < MAXN; i++)
        for (int j = 0; j < MAXN; j++)
                dp[i][j] = -INF;

    dfs(1, 0);

    int ans = 0;
    for (int k = 0; k < sbtr[1]; k++) {
        maxi(ans, dp[1][k]);
    }

    cout << ans << "\n";

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef pair< int , int >PII;
const int MAXN = 205;

int a[MAXN];
vector< int >edg[MAXN];
int dst[MAXN];
int n, k;

vector< int >order;

//vector< int >taken;
int ans;
int run;
int optimism;
int calls;

int reachable[MAXN];
int dfs(int u, int p, int depth) {
    if (depth == 0) return 1;
    int rt = 1;
    for (int v : edg[u]) {
        if (v==p) continue;
        rt += dfs(v, u, depth-1);
    }
    return rt;
}

void dfs(int i) {
    calls++;
    if (run > ans) {
        ans = run;
//        cout << run << " with";
//        for (int i : taken) cout << " " << i;
//        cout << endl;
    }
    if (i == n) return;
    if (run + optimism <= ans) return;

    int s = order[i];

    if (dst[s] > k) {
        vector< PII >changes;
        changes.emplace_back(s, dst[s]);

        vector< int >q;
        q.push_back(s);
        dst[s] = 0;
        int blocked = a[s];
        for (int i = 0; i < k; i++) {
            vector< int >r;
            for (int u : q) {
                for (int v : edg[u]) {
                    if (dst[v] > dst[u]+1) {
                        if (dst[v] > k) blocked += a[v];
                        changes.emplace_back(v, dst[v]);
                        dst[v] = dst[u]+1;
                        r.push_back(v);
                    }
                }
            }
            swap(r, q);
        }

//        taken.push_back(s);
        run += a[s];
        optimism -= blocked;
        dfs(i+1);
//        taken.pop_back();
        optimism += blocked;
        run -= a[s];
        for (PII p : changes) {
            dst[p.first] = p.second;
        }
    }

    dfs(i+1);
}

int main() {
    cin >> n >> k;

    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }

    for (int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        edg[u].push_back(v);
        edg[v].push_back(u);
    }

    for (int i = 1; i <= n; i++) {
        dst[i] = MAXN;
        optimism += a[i];
        order.push_back(i);

        reachable[i] = dfs(i, 0, k);
    }

    sort(order.begin(), order.end(), [&](int i, int j){
        return reachable[i] > reachable[j];
    });

    dfs(0);

    cout << ans << "\n";
//    cout << "calls: " << calls << endl;

    return 0;
}
///35 61


#include<bits/stdc++.h>
using namespace std;
typedef pair< int , int >PII;
const int MAXN = 205;

int a[MAXN];
vector< int >edg[MAXN];
int dst[MAXN];
int n, k;

vector< int >order;

//vector< int >taken;
int ans;
int run;
int optimism;
int calls;

int reachable[MAXN];
int dfs(int u, int p, int depth) {
    if (depth == 0) return 1;
    int rt = 1;
    for (int v : edg[u]) {
        if (v==p) continue;
        rt += dfs(v, u, depth-1);
    }
    return rt;
}

void dfs(int i) {
    calls++;
    if (run > ans) {
        ans = run;
//        cout << run << " with";
//        for (int i : taken) cout << " " << i;
//        cout << endl;
    }
    if (i == n) return;
    int s = order[i];

    if (dst[s] > k) {
        vector< PII >changes;
        changes.emplace_back(s, dst[s]);

        vector< int >q;
        q.push_back(s);
        dst[s] = 0;
        int blocked = a[s];
        for (int i = 0; i < k; i++) {
            vector< int >r;
            for (int u : q) {
                for (int v : edg[u]) {
                    if (dst[v] > dst[u]+1) {
                        if (dst[v] > k) blocked += a[v];
                        changes.emplace_back(v, dst[v]);
                        dst[v] = dst[u]+1;
                        r.push_back(v);
                    }
                }
            }
            swap(r, q);
        }

//        taken.push_back(s);
        run += a[s];
        optimism -= blocked;
        dfs(i+1);
//        taken.pop_back();
        optimism += blocked;
        run -= a[s];
        for (PII p : changes) {
            dst[p.first] = p.second;
        }
    }

    dfs(i+1);
}

int main() {
    cin >> n >> k;

    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }

    for (int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        edg[u].push_back(v);
        edg[v].push_back(u);
    }

    for (int i = 1; i <= n; i++) {
        dst[i] = MAXN;
        optimism += a[i];
        order.push_back(i);

        reachable[i] = dfs(i, 0, k);
    }

    sort(order.begin(), order.end(), [&](int i, int j){
        return reachable[i] > reachable[j];
    });

    dfs(0);

    cout << ans << "\n";
//    cout << "calls: " << calls << endl;

    return 0;
}
///35 61


#include<bits/stdc++.h>
using namespace std;
typedef pair< int , int >PII;
const int MAXN = 205;

int a[MAXN];
vector< int >edg[MAXN];
int dst[MAXN];
int n, k;

vector< int >order;
int position[MAXN];

//vector< int >taken;
int ans;
int run;
int optimism;
int calls;

int reachable[MAXN];
int dfs(int u, int p, int depth) {
    if (depth == 0) return 1;
    int rt = 1;
    for (int v : edg[u]) {
        if (v==p) continue;
        rt += dfs(v, u, depth-1);
    }
    return rt;
}

void dfs(int i) {
    calls++;
    if (run > ans) {
        ans = run;
//        cout << run << " with";
//        for (int i : taken) cout << " " << i;
//        cout << endl;
    }
    if (i == n) return;
    if (run + optimism <= ans) return;

    int s = order[i];

    if (dst[s] > k) {
        vector< PII >changes;
        changes.emplace_back(s, dst[s]);

        vector< int >q;
        q.push_back(s);
        dst[s] = 0;
        int blocked = a[s];
        for (int j = 0; j < k; j++) {
            vector< int >r;
            for (int u : q) {
                for (int v : edg[u]) {
                    if (dst[v] > dst[u]+1) {
                        if (dst[v] > k && position[v] > i) blocked += a[v];
                        changes.emplace_back(v, dst[v]);
                        dst[v] = dst[u]+1;
                        r.push_back(v);
                    }
                }
            }
            swap(r, q);
        }

//        taken.push_back(s);
        run += a[s];
        optimism -= blocked;
        dfs(i+1);
//        taken.pop_back();
        optimism += blocked;
        run -= a[s];
        for (PII p : changes) {
            dst[p.first] = p.second;
        }
    }

    if (dst[s] > k) optimism -= a[s];
    dfs(i+1);
    if (dst[s] > k) optimism += a[s];
}

int main() {
    cin >> n >> k;

    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }

    for (int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        edg[u].push_back(v);
        edg[v].push_back(u);
    }

    for (int i = 1; i <= n; i++) {
        dst[i] = MAXN;
        optimism += a[i];
        order.push_back(i);

        reachable[i] = dfs(i, 0, k);
    }

//    sort(order.begin(), order.end(), [&](int i, int j){
//        return reachable[i] > reachable[j];
//    });
//
//    sort(order.begin(), order.end(), [&](int i, int j){
//        return a[i] > a[j];
//    });

//    sort(order.begin(), order.end(), [&](int i, int j){
//        return make_pair(reachable[i], a[i]) > make_pair(reachable[j], a[j]);
//    });
    sort(order.begin(), order.end(), [&](int i, int j){
        return make_pair(a[i], reachable[i]) > make_pair(a[j], reachable[j]);
    });

    for (int i = 0; i < n; i++) position[order[i]] = i;
    dfs(0);

    cout << ans << "\n";
    cout << "calls: " << calls << endl;

    return 0;
}
///35 61


#include<bits/stdc++.h>
using namespace std;
typedef pair< int , int >PII;
const int MAXN = 205;

int a[MAXN];
vector< int >edg[MAXN];
int dst[MAXN];
int n, k;

vector< int >order;

//vector< int >taken;
int ans;
int run;
int optimism;
int calls;

void dfs(int i) {
    calls++;
    if (run > ans) {
        ans = run;
//        cout << run << " with";
//        for (int i : taken) cout << " " << i;
//        cout << endl;
    }
    if (i == n) return;
    int s = order[i];

    if (dst[s] > k) {
        vector< PII >changes;
        changes.emplace_back(s, dst[s]);

        vector< int >q;
        q.push_back(s);
        dst[s] = 0;
        int blocked = a[s];
        for (int i = 0; i < k; i++) {
            vector< int >r;
            for (int u : q) {
                for (int v : edg[u]) {
                    if (dst[v] > dst[u]+1) {
                        if (dst[v] > k) blocked += a[v];
                        changes.emplace_back(v, dst[v]);
                        dst[v] = dst[u]+1;
                        r.push_back(v);
                    }
                }
            }
            swap(r, q);
        }

//        taken.push_back(s);
        run += a[s];
        optimism -= blocked;
        dfs(i+1);
//        taken.pop_back();
        optimism += blocked;
        run -= a[s];
        for (PII p : changes) {
            dst[p.first] = p.second;
        }
    }

    dfs(i+1);
}

int main() {
    cin >> n >> k;

    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }

    for (int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        edg[u].push_back(v);
        edg[v].push_back(u);
    }

    for (int i = 1; i <= n; i++) {
        dst[i] = MAXN;
        optimism += a[i];
        order.push_back(i);
    }

    sort(order.begin(), order.end(), [&](int i, int j){
        return a[i] > a[j];
    });

    dfs(0);

    cout << ans << "\n";
//    cout << "calls: " << calls << endl;

    return 0;
}
///35 61


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define pb push_back
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>
#include <functional> // for less
using namespace __gnu_pbds;
using namespace std;
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> set1;
ll find(ll a,ll p[])
{
   //cout<<"find "<<a<<endl;
   if(a==p[a])
   return a;
   else return p[a]=find(p[a],p);

}
void join(ll a,ll b,ll p[],ll sz[])
{
   a=find(a,p);
   b=find(b,p);
   if(a!=b)
   {
      if(sz[a]<sz[b])
      swap(a,b);
      sz[a]+=sz[b];
      p[b]=a;
   }
}
ll power(ll a,ll b,ll mod)
{
   ll res=1;
   while(b>0)
   {
      if(b&1)
      res=(res*a)%mod;
      b=b/2;
      a=(a*a)%mod;
   }
   return res;
}
ll inverse(ll x, ll p)
{
   return power(x,p-2,p);
}
ll hash1(ll x,ll y)
{
   return x+y+x*y;
}
ll f[200000]={0};
ll mod;
void ini()
{
   f[0]=1;
   for(ll i=1;i<=200000;i++)
   f[i]=(i*f[i-1])%mod;
}
void dfs(ll v,ll p,vector<ll> adj[],ll k,ll dp[205][205],ll a[],ll n)
{
   ll size=adj[v].size();
  // cout<<"dfs of "<<v<<endl;

   for(ll i=0;i<size;i++)
   {
      ll s=adj[v].at(i);
      if(s!=p)
      dfs(s,v,adj,k,dp,a,n);
   }
   dp[v][0]=a[v];
   for(ll i=0;i<size;i++)
   {
      ll s=adj[v].at(i);
   //   cout<<s<<" hqwh "<<dp[s][k]<<" "<<endl;
      if(s!=p)
      dp[v][0]+=dp[s][k];

   }
  // cout<<" done "<<v<<endl;
   for(ll i=1;i<=n;i++)
   {
      for(ll j=0;j<size;j++)
      {
         ll s=adj[v].at(j);
         if(s!=p)
         {
            ll sum=dp[s][i-1];
           // cout<<s<<" <-s and sum-> "<<sum<<endl;

            for(ll l=0;l<size;l++)
            {
           //    cout<<adj[v].at(l)<<" "<<i<<" "<<k<<" "<<k-1-i<<" "<<endl;
               if(l!=j && adj[v].at(l)!=p)
               sum=sum+dp[adj[v].at(l)][max(i-1,k-i)];
            }
            dp[v][i]=max(dp[v][i],sum);
           // cout<<"for s = "<<s<<" sum is "<<sum<<endl;
         }
      }
   }
 //  cout<<"vertex "<<v<<endl;
   //for(ll i=0;i<=n;i++)
   //cout<<i<<" "<<dp[v][i]<<endl;
   for(ll i=n-1;i>=0;i--)
   dp[v][i]=max(dp[v][i],dp[v][i+1]);

}

ll ncr(ll n,ll r)
{
   ll res=1;
   ll k=f[n];
   ll v=inverse(f[n-r],mod);
   ll u=inverse(f[r],mod);
   ll x=(u*v)%mod;
   k=(k*x)%mod;
   return k;
}

int main()
{
  // ini();
   ios_base::sync_with_stdio(false);
    cin.tie(NULL);
 ll n;
 ll k;

 cin>>n>>k;
 ll a[n+1];
 for(ll i=1;i<=n;i++)
 cin>>a[i];
 vector<ll> adj[n+1];
 for(ll i=0;i<n-1;i++)
 {
    ll u,v;
    cin>>u>>v;
    adj[u].pb(v);
    adj[v].pb(u);

 }

 ll dp[205][205];
 for(ll i=0;i<=200;i++)
 {
    for(ll j=0;j<=201;j++)
    dp[i][j]=0;
 }
dfs(1,-1,adj,k,dp,a,n);
cout<<dp[1][0]<<endl;
return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define pb push_back
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>
#include <functional> // for less
using namespace __gnu_pbds;
using namespace std;
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> set1;
ll find(ll a,ll p[])
{
   //cout<<"find "<<a<<endl;
   if(a==p[a])
   return a;
   else return p[a]=find(p[a],p);

}
void join(ll a,ll b,ll p[],ll sz[])
{
   a=find(a,p);
   b=find(b,p);
   if(a!=b)
   {
      if(sz[a]<sz[b])
      swap(a,b);
      sz[a]+=sz[b];
      p[b]=a;
   }
}
ll power(ll a,ll b,ll mod)
{
   ll res=1;
   while(b>0)
   {
      if(b&1)
      res=(res*a)%mod;
      b=b/2;
      a=(a*a)%mod;
   }
   return res;
}
ll inverse(ll x, ll p)
{
   return power(x,p-2,p);
}
ll hash1(ll x,ll y)
{
   return x+y+x*y;
}
ll f[200000]={0};
ll mod;
void ini()
{
   f[0]=1;
   for(ll i=1;i<=200000;i++)
   f[i]=(i*f[i-1])%mod;
}
void dfs(ll v,vector<ll> adj[],ll vis[],ll dp[201][202],ll a[],ll k)
{
   ll k1=adj[v].size();
   dp[v][0]=a[v];
 //  dp[v][k+1];
   for(ll i=0;i<k1;i++)
   {
      ll s=adj[v].at(i);
     // cout<<"parent "<<v<<" son "<<s<<endl;
      if(vis[s]==0)
      {
          vis[s]=1;

          dfs(s,adj,vis,dp,a,k);
          for(ll i=1;i<=k;i++)
          dp[v][i]+=(dp[s][i-1]);
          dp[v][0]+=dp[s][k];
      }
   }
  // if(dp[v][k+1]>0)
   //dp[v][k+1]+=a[v];
 //  cout<<"for vertex "<<v<<endl;
 //  for(ll i=0;i<=k;i++)
//   cout<<"for distance "<<i<<" "<<dp[v][i]<<endl;
}
ll ncr(ll n,ll r)
{
   ll res=1;
   ll k=f[n];
   ll v=inverse(f[n-r],mod);
   ll u=inverse(f[r],mod);
   ll x=(u*v)%mod;
   k=(k*x)%mod;
   return k;
}

int main()
{
  // ini();
   ios_base::sync_with_stdio(false);
    cin.tie(NULL);
 ll n;
 ll k;

 cin>>n>>k;
 ll a[n+1];
 for(ll i=1;i<=n;i++)
 cin>>a[i];
 vector<ll> adj[n+1];
 for(ll i=0;i<n-1;i++)
 {
    ll u,v;
    cin>>u>>v;
    adj[u].pb(v);
    adj[v].pb(u);

 }

 ll dp[201][202];
 for(ll i=0;i<=200;i++)
 {
    for(ll j=0;j<=201;j++)
    dp[i][j]=0;
 }
ll vis[n+1]={0};
vis[1]=1;
dfs(1,adj,vis,dp,a,k);

ll ans=dp[1][0];
//cout<<"initial ans "<<ans<<endl;
ll z=adj[1].size();
for(ll i=0;i<z;i++)
{

   ll u=adj[1].at(i);
   for(ll j=1;j<=k;j++)
   {
      ll sum=dp[u][j-1];
      for(ll l=0;l<z;l++)
      {
         if(l!=i)
         {
            ll s=adj[1].at(l);
            sum=sum+dp[s][k-j];
         }
      }
      ans=max(ans,sum);
   }

}
cout<<ans<<endl;
return 0;
}


#include <bits/stdc++.h>
using namespace std;
int a[205], sz[205], n, k;
deque<int> g[205], dp[205];
void dfs(int u, int p){
    dp[u].push_back(a[u]);
    for(int i=0;i<g[u].size();i++){
        int v = g[u][i];
        if(v != p){
            dfs(v, u);

            dp[v].push_front(dp[v].front());
            vector<int> result;
            if(dp[v].size() < dp[u].size()){
                result.resize(dp[v].size());
                for(int i=0;i<dp[v].size();i++){
                    if(i < dp[u].size() && k-i+1 < dp[v].size() && k-i+1 >= 0) result[i] = max(result[i], dp[u][i] + dp[v][k-i+1]);
                    if(i < dp[v].size() && k-i+1 < dp[u].size() && k-i+1 >= 0) result[i] = max(result[i], dp[u][k-i+1] + dp[v][i]);
                    result[i] = max(result[i], dp[v][i]);
                }

                int mx = 0;
                for(int i=result.size()-1;i>=0;i--){
                    mx = max(mx, result[i]);
                    dp[u][i] = max(dp[u][i], mx);
                }
            }
            else{
                swap(dp[v], dp[u]);
                result.resize(dp[v].size());
                for(int i=0;i<dp[v].size();i++){
                    if(i < dp[u].size() && k-i+1 < dp[v].size() && k-i+1 >= 0) result[i] = max(result[i], dp[u][i] + dp[v][k-i+1]);
                    if(i < dp[v].size() && k-i+1 < dp[u].size() && k-i+1 >= 0) result[i] = max(result[i], dp[u][k-i+1] + dp[v][i]);
                    result[i] = max(result[i], dp[v][i]);
                }

                int mx = 0;
                for(int i=result.size()-1;i>=0;i--){
                    mx = max(mx, result[i]);
                    dp[u][i] = max(dp[u][i], mx);
                }
            }
        }
    }
}
int main(){
    scanf("%d%d", &n, &k);
    for(int i=1;i<=n;i++) scanf("%d", &a[i]);
    for(int i=2;i<=n;i++){
        int a, b;
        scanf("%d%d", &a, &b);
        g[a].push_back(b);
        g[b].push_back(a);
    }
    dfs(1, 0);
    printf("%d\n", dp[1][0]);
}


#include <bits/stdc++.h>
using namespace std;
int a[205], sz[205], n, k;
deque<int> g[205], dp[205];
void dfs(int u, int p){
    dp[u].push_back(a[u]);
    for(int i=0;i<g[u].size();i++){
        int v = g[u][i];
        if(v != p){
            dfs(v, u);

            dp[v].push_front(dp[v].front());
            if(dp[v].size() > dp[u].size()) swap(dp[u], dp[v]);
            vector<int> result;
            result.resize(dp[v].size());
            for(int i=0;i<dp[v].size();i++){
                int j = max(k-i+1, i);
                if(i < dp[u].size() && j < dp[v].size() && j >= 0) result[i] = max(result[i], dp[u][i] + dp[v][j]);
                if(i < dp[v].size() && j < dp[u].size() && j >= 0) result[i] = max(result[i], dp[u][j] + dp[v][i]);
                result[i] = max(result[i], dp[v][i]);
            }

            int mx = 0;
            for(int i=result.size()-1;i>=0;i--){
                mx = max(mx, result[i]);
                dp[u][i] = max(dp[u][i], mx);
            }
        }
    }
}
int main(){
    scanf("%d%d", &n, &k);
    for(int i=1;i<=n;i++) scanf("%d", &a[i]);
    for(int i=2;i<=n;i++){
        int a, b;
        scanf("%d%d", &a, &b);
        g[a].push_back(b);
        g[b].push_back(a);
    }
    dfs(1, 0);
    printf("%d\n", dp[1][0]);
}


#include <bits/stdc++.h>
using namespace std;
int a[205], sz[205], n, k;
deque<int> g[205], dp[205];
void dfs(int u, int p){
    dp[u].push_back(a[u]);
    for(int i=0;i<g[u].size();i++){
        int v = g[u][i];
        if(v != p){
            dfs(v, u);

            vector<int> result;
            if(dp[v].size() < dp[u].size()){
                result.resize(dp[v].size()+1);
                for(int i=0;i<dp[v].size();i++){
                    if(k-i >= 0 && k-i < dp[u].size()) result[min(i+1, k-i)] = max(result[min(i+1, k-i)], dp[u][k-i] + dp[v][i]);
                    result[i+1] = max(result[i+1], dp[v][i]);
                }

                int mx = 0;
                while(dp[u].size() < result.size()) dp[u].push_back(0);
                for(int i=result.size()-1;i>=0;i--){
                    mx = max(mx, result[i]);
                    dp[u][i] = max(dp[u][i], mx);
                }
            }
            else{
                swap(dp[v], dp[u]);
                result.resize(dp[v].size()+1);
                for(int i=0;i<dp[v].size();i++){
                    if(k-i >= 0 && k-i < dp[u].size()) result[min(i, k-i+1)] = max(result[min(i, k-i+1)], dp[u][k-i] + dp[v][i]);
                    result[i] = max(result[i], dp[v][i]);
                }
                dp[u].push_front(dp[u].front());

                int mx = 0;
                for(int i=result.size()-1;i>=0;i--){
                    mx = max(mx, result[i]);
                    dp[u][i] = max(dp[u][i], mx);
                }
            }
        }
    }
}
int main(){
    scanf("%d%d", &n, &k);
    for(int i=1;i<=n;i++) scanf("%d", &a[i]);
    for(int i=2;i<=n;i++){
        int a, b;
        scanf("%d%d", &a, &b);
        g[a].push_back(b);
        g[b].push_back(a);
    }
    dfs(1, 0);
    printf("%d\n", dp[1][0]);
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
#define rep(i,p1,p2) for(int i=(p1);i<=(p2);++i)
#define per(i,p1,p2) for(int i=(p1);i>=(p2);--i)

const int INF = 1061109567;
const ll LINF = 4557430888798830399ll;
const int MOD = 1000000007;

int qpow(ll x, ll n) {
    ll res = 1;
    while(n) {
        if(n & 1)
            res = res * x % MOD;
        x = x * x % MOD;
        n >>= 1;
    }
    return res;
}

/*---*/

int n, k;
int w[205];
int dp[205][205];
int tmp[205];

vector<int>G[205];

void dfs(int u, int p) {
    if(G[u].size() == 1 && p != -1) {
        dp[u][0] = w[u];
        return;
    }
    bool first = 1;
    for(int i = 0; i < G[u].size(); ++i) {
        int v = G[u][i];
        if(v == p)
            continue;
        dfs(v, u);
        if(first) {
            dp[u][0] = w[u] + max(dp[v][k], dp[v][k + 1]);
            for(int j = 0; j <= k; ++j)
                dp[u][j + 1] = dp[v][j];
            dp[u][k + 1] = max(dp[u][k + 1], dp[v][k + 1]);
            first = 0;
        } else {
            tmp[0] = dp[u][0] + max(dp[v][k], dp[v][k + 1]);
            for(int j = 0; j <= k; ++j)
                tmp[j + 1] = dp[v][j];
            tmp[k + 1] = max(tmp[k + 1], dp[v][k + 1]);
            for(int j = 0; j <= k; ++j) {
                for(int t = k - j; t <= k + 1; ++t)
                    tmp[min(j + 1, t)] = max(tmp[min(j + 1, t)], dp[u][t] + dp[v][j]);
            }
            for(int j = 0; j <= k + 1; ++j)
                dp[u][j] = max(dp[u][j], tmp[j]);
        }
    }
    /*for(int j = 0; j <= k; ++j)
        printf("dp[%d][%d]=%d\n", u, j, dp[u][j]);
    puts("");*/
}

void test_case() {
    scanf("%d%d", &n, &k);
    for(int i = 1; i <= n; ++i)
        scanf("%d", &w[i]);
    if(n == 1) {
        printf("%d\n", w[1]);
        return;
    }
    for(int i = 1; i <= n - 1; ++i) {
        int u, v;
        scanf("%d%d", &u, &v);
        G[u].push_back(v);
        G[v].push_back(u);
    }
    dfs(1, -1);
    int ans = 0;
    for(int j = 0; j <= k + 1; ++j)
        ans = max(ans, dp[1][j]);
    printf("%d\n", ans);
}

/*---*/

int main() {
#ifdef KisekiPurin
    freopen("KisekiPurin.in", "r", stdin);
#endif // KisekiPurin
    int t = 1;
    //scanf("%d", &t);
    for(int ti = 1; ti <= t; ++ti) {
        //printf("Case #%d: ", ti);
        test_case();
    }
}

/*
    1. С���������˻���
        ����Ϊ0��1�᲻������������������ı�����С��Ҫ�������㣬ǿ��ͨ�����������һ����ȵȡ�
    2. �ڴ棺
        �ڴ�Ŀռ���û�п�������ʱ�з���ߣ���ʱ�ж����¼ӵıߡ��߶�������4������
        �ɳ־û����ݽṹ�᲻���ڴ��������������ʱvector�᲻�ᷭ�����ַ���Сд�п�����
        ���������н��г�ʼ����memset�᲻�ᷭ����
    3. ���������
        �˷�������ˣ�����ȡģ�ˣ������������%d�����޷�����Խ�������ˣ�
    4. ϰ�ߣ�
        ��������ָ������ֵ�𣿷���ֵ��void������ÿһ����֧������ʽ�ķ���ֵ��ȷ���������ķ�֧����assertһ�ѡ�
        Yes��No��û�з�����Сд��û�д���û�и��n��m����ɢ��֮���cn��û�д����кͿո�Ҫ��ô����priorityҪ�ѷ��ŷ�������
        id��aid�ǲ���û�п��ǣ��ǲ���Ū���ˣ��������ǻ���֮��ģ����ʱӦ���ǻ���֮ǰ�ġ�
    5. ����bug��
        �����������ӻ����������ӻ���
*/


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
#define rep(i,p1,p2) for(int i=(p1);i<=(p2);++i)
#define per(i,p1,p2) for(int i=(p1);i>=(p2);--i)

const int INF = 1061109567;
const ll LINF = 4557430888798830399ll;
const int MOD = 1000000007;

int qpow(ll x, ll n) {
    ll res = 1;
    while(n) {
        if(n & 1)
            res = res * x % MOD;
        x = x * x % MOD;
        n >>= 1;
    }
    return res;
}

/*---*/

int n, k;
int w[205];
int dp[205][205];
int tmp[205];

vector<int>G[205];

void dfs(int u, int p) {
    if(G[u].size() == 1 && p != -1) {
        dp[u][0] = w[u];
        return;
    }
    bool first = 1;
    for(int i = 0; i < G[u].size(); ++i) {
        int v = G[u][i];
        if(v == p)
            continue;
        dfs(v, u);
        if(first) {
            dp[u][0] = w[u] + max(dp[v][k], dp[v][k + 1]);
            for(int j = 0; j <= k - 1; ++j)
                dp[u][j + 1] = dp[v][j];
            dp[u][k + 1] = max(dp[v][k], dp[v][k + 1]);
            first = 0;
        } else {
            tmp[0] = dp[u][0] + max(dp[v][k], dp[v][k + 1]);
            for(int j = 0; j <= k - 1; ++j)
                tmp[j + 1] = dp[v][j];
            tmp[k + 1] = max(dp[v][k], dp[v][k + 1]);
            for(int j = 0; j <= k + 1; ++j) {
                for(int t = max(0, k - j); t <= k + 1; ++t)
                    tmp[min(j + 1, t)] = max(tmp[min(j + 1, t)], dp[u][t] + dp[v][j]);
            }
            for(int j = 0; j <= k + 1; ++j)
                dp[u][j] = max(dp[u][j], tmp[j]);
        }
    }
    /*for(int j = 0; j <= k; ++j)
        printf("dp[%d][%d]=%d\n", u, j, dp[u][j]);
    puts("");*/
}

void test_case() {
    scanf("%d%d", &n, &k);
    for(int i = 1; i <= n; ++i)
        scanf("%d", &w[i]);
    if(n == 1) {
        printf("%d\n", w[1]);
        return;
    }
    for(int i = 1; i <= n - 1; ++i) {
        int u, v;
        scanf("%d%d", &u, &v);
        G[u].push_back(v);
        G[v].push_back(u);
    }
    dfs(1, -1);
    int ans = 0;
    for(int j = 0; j <= k + 1; ++j)
        ans = max(ans, dp[1][j]);
    printf("%d\n", ans);
}

/*---*/

int main() {
#ifdef KisekiPurin
    freopen("KisekiPurin.in", "r", stdin);
#endif // KisekiPurin
    int t = 1;
    //scanf("%d", &t);
    for(int ti = 1; ti <= t; ++ti) {
        //printf("Case #%d: ", ti);
        test_case();
    }
}

/*
    1. С���������˻���
        ����Ϊ0��1�᲻������������������ı�����С��Ҫ�������㣬ǿ��ͨ�����������һ����ȵȡ�
    2. �ڴ棺
        �ڴ�Ŀռ���û�п�������ʱ�з���ߣ���ʱ�ж����¼ӵıߡ��߶�������4������
        �ɳ־û����ݽṹ�᲻���ڴ��������������ʱvector�᲻�ᷭ�����ַ���Сд�п�����
        ���������н��г�ʼ����memset�᲻�ᷭ����
    3. ���������
        �˷�������ˣ�����ȡģ�ˣ������������%d�����޷�����Խ�������ˣ�
    4. ϰ�ߣ�
        ��������ָ������ֵ�𣿷���ֵ��void������ÿһ����֧������ʽ�ķ���ֵ��ȷ���������ķ�֧����assertһ�ѡ�
        Yes��No��û�з�����Сд��û�д���û�и��n��m����ɢ��֮���cn��û�д����кͿո�Ҫ��ô����priorityҪ�ѷ��ŷ�������
        id��aid�ǲ���û�п��ǣ��ǲ���Ū���ˣ��������ǻ���֮��ģ����ʱӦ���ǻ���֮ǰ�ġ�
    5. ����bug��
        �����������ӻ����������ӻ���
*/


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
#define rep(i,p1,p2) for(int i=(p1);i<=(p2);++i)
#define per(i,p1,p2) for(int i=(p1);i>=(p2);--i)

const int INF = 1061109567;
const ll LINF = 4557430888798830399ll;
const int MOD = 1000000007;

int qpow(ll x, ll n) {
    ll res = 1;
    while(n) {
        if(n & 1)
            res = res * x % MOD;
        x = x * x % MOD;
        n >>= 1;
    }
    return res;
}

/*---*/

int n, k;
int w[205];
int dp[205][205];
int tmp[205];

vector<int>G[205];

void dfs(int u, int p) {
    if(G[u].size() == 1 && p != -1) {
        dp[u][0] = w[u];
        return;
    }
    bool first = 1;
    for(int i = 0; i < G[u].size(); ++i) {
        int v = G[u][i];
        if(v == p)
            continue;
        dfs(v, u);
        if(first) {
            dp[u][0] = w[u] + dp[v][k];
            for(int j = 0; j <= k; ++j)
                dp[u][j + 1] = dp[v][j];
            for(int j = k; j >= 0; --j)
                dp[u][j] = max(dp[u][j + 1], dp[u][j]);
            first = 0;
        } else {
            tmp[0] = dp[u][0] + dp[v][k];
            for(int j = 0; j <= k; ++j)
                tmp[j + 1] = dp[v][j];
            for(int j = 0; j <= k; ++j) {
                for(int t = k - j; t <= k + 1; ++t)
                    tmp[min(j + 1, t)] = max(tmp[min(j + 1, t)], dp[u][t] + dp[v][j]);
            }
            for(int j = k; j >= 0; --j)
                tmp[j] = max(tmp[j + 1], tmp[j]);
            for(int j = 0; j <= k + 1; ++j)
                dp[u][j] = max(dp[u][j], tmp[j]);
        }
    }
    /*for(int j = 0; j <= k; ++j)
        printf("dp[%d][%d]=%d\n", u, j, dp[u][j]);
    puts("");*/
}

void test_case() {
    scanf("%d%d", &n, &k);
    for(int i = 1; i <= n; ++i)
        scanf("%d", &w[i]);
    if(n == 1) {
        printf("%d\n", w[1]);
        return;
    }
    for(int i = 1; i <= n - 1; ++i) {
        int u, v;
        scanf("%d%d", &u, &v);
        G[u].push_back(v);
        G[v].push_back(u);
    }
    dfs(1, -1);
    int ans = 0;
    for(int j = 0; j <= k + 1; ++j)
        ans = max(ans, dp[1][j]);
    printf("%d\n", ans);
}

/*---*/

int main() {
#ifdef KisekiPurin
    freopen("KisekiPurin.in", "r", stdin);
#endif // KisekiPurin
    int t = 1;
    //scanf("%d", &t);
    for(int ti = 1; ti <= t; ++ti) {
        //printf("Case #%d: ", ti);
        test_case();
    }
}

/*
    1. С���������˻���
        ����Ϊ0��1�᲻������������������ı�����С��Ҫ�������㣬ǿ��ͨ�����������һ����ȵȡ�
    2. �ڴ棺
        �ڴ�Ŀռ���û�п�������ʱ�з���ߣ���ʱ�ж����¼ӵıߡ��߶�������4������
        �ɳ־û����ݽṹ�᲻���ڴ��������������ʱvector�᲻�ᷭ�����ַ���Сд�п�����
        ���������н��г�ʼ����memset�᲻�ᷭ����
    3. ���������
        �˷�������ˣ�����ȡģ�ˣ������������%d�����޷�����Խ�������ˣ�
    4. ϰ�ߣ�
        ��������ָ������ֵ�𣿷���ֵ��void������ÿһ����֧������ʽ�ķ���ֵ��ȷ���������ķ�֧����assertһ�ѡ�
        Yes��No��û�з�����Сд��û�д���û�и��n��m����ɢ��֮���cn��û�д����кͿո�Ҫ��ô����priorityҪ�ѷ��ŷ�������
        id��aid�ǲ���û�п��ǣ��ǲ���Ū���ˣ��������ǻ���֮��ģ����ʱӦ���ǻ���֮ǰ�ġ�
    5. ����bug��
        �����������ӻ����������ӻ���
*/


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
#define rep(i,p1,p2) for(int i=(p1);i<=(p2);++i)
#define per(i,p1,p2) for(int i=(p1);i>=(p2);--i)

const int INF = 1061109567;
const ll LINF = 4557430888798830399ll;
const int MOD = 1000000007;

int qpow(ll x, ll n) {
    ll res = 1;
    while(n) {
        if(n & 1)
            res = res * x % MOD;
        x = x * x % MOD;
        n >>= 1;
    }
    return res;
}

/*---*/

int n, k;
int w[205];
int dp[205][205];

vector<int>G[205];

void dfs(int u, int p) {
    if(G[u].size() == 1 && p != -1) {
        dp[u][0] = w[u];
        return;
    }
    bool first = 1;
    for(int i = 0; i < G[u].size(); ++i) {
        int v = G[u][i];
        if(v == p)
            continue;
        dfs(v, u);
        if(first) {
            for(int j = 0; j + 1 <= k; ++j)
                dp[u][j + 1] = dp[v][j];
            dp[u][0] = w[u] + dp[v][k];
            first = 0;
        } else {
            for(int j = 0; j <= k; ++j) {
                for(int t = k - j; t <= k; ++t)
                    dp[u][min(j + 1, t)] = max(dp[u][min(j + 1, t)], dp[u][t] + dp[v][j]);
            }
        }
    }
    /*for(int j = 0; j <= k; ++j)
        printf("dp[%d][%d]=%d\n", u, j, dp[u][j]);*/
}

void test_case() {
    scanf("%d%d", &n, &k);
    for(int i = 1; i <= n; ++i)
        scanf("%d", &w[i]);
    if(n == 1) {
        printf("%d\n", w[1]);
        return;
    }
    for(int i = 1; i <= n - 1; ++i) {
        int u, v;
        scanf("%d%d", &u, &v);
        G[u].push_back(v);
        G[v].push_back(u);
    }
    dfs(1, -1);
    int ans = 0;
    for(int j = 0; j <= k; ++j)
        ans = max(ans, dp[1][j]);
    printf("%d\n", ans);
}

/*---*/

int main() {
#ifdef KisekiPurin
    freopen("KisekiPurin.in", "r", stdin);
#endif // KisekiPurin
    int t = 1;
    //scanf("%d", &t);
    for(int ti = 1; ti <= t; ++ti) {
        //printf("Case #%d: ", ti);
        test_case();
    }
}

/*
    1. С���������˻���
        ����Ϊ0��1�᲻������������������ı�����С��Ҫ�������㣬ǿ��ͨ�����������һ����ȵȡ�
    2. �ڴ棺
        �ڴ�Ŀռ���û�п�������ʱ�з���ߣ���ʱ�ж����¼ӵıߡ��߶�������4������
        �ɳ־û����ݽṹ�᲻���ڴ��������������ʱvector�᲻�ᷭ�����ַ���Сд�п�����
        ���������н��г�ʼ����memset�᲻�ᷭ����
    3. ���������
        �˷�������ˣ�����ȡģ�ˣ������������%d�����޷�����Խ�������ˣ�
    4. ϰ�ߣ�
        ��������ָ������ֵ�𣿷���ֵ��void������ÿһ����֧������ʽ�ķ���ֵ��ȷ���������ķ�֧����assertһ�ѡ�
        Yes��No��û�з�����Сд��û�д���û�и��n��m����ɢ��֮���cn��û�д����кͿո�Ҫ��ô����priorityҪ�ѷ��ŷ�������
        id��aid�ǲ���û�п��ǣ��ǲ���Ū���ˣ��������ǻ���֮��ģ����ʱӦ���ǻ���֮ǰ�ġ�
    5. ����bug��
        �����������ӻ����������ӻ���
*/


/*|In The Name Of Allah|*/

#include <bits/stdc++.h>

using namespace std;

const int N = 2e2+5;

int n;
int k;
int cost[N];
int dp[N][N];
/*
let dp[ i ] [ j ]
be the maximum cost at the subtree i
such that the least depth taken node
is j.
*/
vector < int > adj[N];

void dfs(int u , int p){
    for(int v : adj[u]){
        if(v == p)
            continue;
        dfs(v , u);
    }
    dp[u][0] = cost[u];
    for(int v : adj[u]){
        if(v == p)
            continue;
        dp[u][0] += dp[v][k - 1];
    }
    /*
        here we link some paths
        assume we have taken a node
        at depth A , to take another node
        in other path it must be far from that node
        with distance at least K
    */
    for(int dep = 1; dep <= k; dep++){
        for(int v : adj[u]){
            if(v == p)
                continue;
            int cur = dp[v][dep - 1];
            for(int y : adj[u]){
                if(y == p || y == v)
                    continue;
                cur += dp[y][k - dep - 1];
            }
            dp[u][dep] = max(dp[u][dep] , cur);
        }
    }
    for(int dep = k-1; dep >= 0; dep--){
        dp[u][dep] = max(dp[u][dep] , dp[u][dep + 1]);
    }
}

int main(){
    scanf("%d%d" , &n , &k);
    k++;
    for(int i = 1; i <= n; i++){
        scanf("%d" , &cost[i]);
    }
    for(int i = 1; i < n; i++){
        int u , v;
        scanf("%d%d" , &u , &v);
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    dfs(1 , -1);
    printf("%d\n" , dp[1][0]);
}


/*|In The Name Of Allah|*/

#include <bits/stdc++.h>

using namespace std;

const int N = 2e2+5;

int n;
int k;
int cost[N];
int dp[N][N];
/*
let dp[ i ] [ j ]
be the maximum cost at the subtree i
such that the least depth taken node
is j.
*/
vector < int > adj[N];

int op;

void dfs(int u , int p){
    for(int v : adj[u]){
        if(v == p)
            continue;
        dfs(v , u);
    }
    dp[u][0] = cost[u];
    for(int v : adj[u]){
        if(v == p)
            continue;
        dp[u][0] += dp[v][k - 1];
    }
    /*
        here we link some paths
        assume we have taken a node
        at depth A , to take another node
        in other path it must be far from that node
        with distance at least K
    */
    for(int dep = 1; dep <= k; dep++){
        for(int v : adj[u]){
            if(v == p)
                continue;
            int cur = dp[v][dep - 1];
            for(int y : adj[u]){
                if(y == p || y == v)
                    continue;
                op++;
                cur += dp[y][max(k - dep - 1 , dep - 1)];
            }
            dp[u][dep] = max(dp[u][dep] , cur);
        }
    }
    for(int dep = k-1; dep >= 0; dep--){
        dp[u][dep] = max(dp[u][dep] , dp[u][dep + 1]);
    }
}

int main(){
    scanf("%d%d" , &n , &k);
    k++;
    for(int i = 1; i <= n; i++){
        scanf("%d" , &cost[i]);
    }
    for(int i = 1; i < n; i++){
        int u , v;
        scanf("%d%d" , &u , &v);
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    dfs(1 , -1);
    printf("%d\n" , dp[1][0]);
    assert(op < n*n*n*n);
}


/*|In The Name Of Allah|*/

#include <bits/stdc++.h>

using namespace std;

const int N = 2e2+5;

int n;
int k;
int cost[N];
int dp[N][N];
/*
let dp[ i ] [ j ]
be the maximum cost at the subtree i
such that the least depth taken node
is j.
*/
vector < int > adj[N];

void dfs(int u , int p){
    for(int v : adj[u]){
        if(v == p)
            continue;
        dfs(v , u);
    }
    dp[u][0] = cost[u];
    for(int v : adj[u]){
        if(v == p)
            continue;
        dp[u][0] += dp[v][k - 1];
    }
    /*
        here we link some paths
        assume we have taken a node
        at depth A , to take another node
        in other path it must be far from that node
        with distance at least K
    */
    for(int dep = 1; dep <= k; dep++){
        for(int v : adj[u]){
            if(v == p)
                continue;
            int cur = dp[v][dep - 1];
            for(int y : adj[u]){
                if(y == p || y == v)
                    continue;
                cur += dp[y][max(k - dep - 1 , 0)];
            }
            dp[u][dep] = max(dp[u][dep] , cur);
        }
    }
    for(int dep = k-1; dep >= 0; dep--){
        dp[u][dep] = max(dp[u][dep] , dp[u][dep + 1]);
    }
}

int main(){
    scanf("%d%d" , &n , &k);
    k++;
    for(int i = 1; i <= n; i++){
        scanf("%d" , &cost[i]);
    }
    for(int i = 1; i < n; i++){
        int u , v;
        scanf("%d%d" , &u , &v);
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    dfs(1 , -1);
    printf("%d\n" , dp[1][0]);
}


/*|In The Name Of Allah|*/

#include <bits/stdc++.h>

using namespace std;

const int N = 2e2+5;

int n;
int k;
int cost[N];
int dp[N][N];
/*
let dp[ i ] [ j ]
be the maximum cost at the subtree i
such that the least depth taken node
is j.
*/
vector < int > adj[N];

void dfs(int u , int p){
    for(int v : adj[u]){
        if(v == p)
            continue;
        dfs(v , u);
    }
    dp[u][0] = cost[u];
    for(int v : adj[u]){
        if(v == p)
            continue;
        dp[u][0] += dp[v][k - 1];
    }
    /*
        here we link some paths
        assume we have taken a node
        at depth A , to take another node
        in other path it must be far from that node
        with distance at least K
    */
    for(int dep = 1; dep <= k; dep++){
        for(int v : adj[u]){
            if(v == p)
                continue;
            int cur = dp[v][dep - 1];
            for(int y : adj[u]){
                if(y == p || y == v)
                    continue;
                cur += dp[y][max(k - dep - 1 , dep - 1)];
            }
            dp[u][dep] = max(dp[u][dep] , cur);
        }
    }
    for(int dep = k-1; dep >= 0; dep--){
        dp[u][dep] = max(dp[u][dep] , dp[u][dep + 1]);
    }
}

int main(){
    scanf("%d%d" , &n , &k);
    k++;
    for(int i = 1; i <= n; i++){
        scanf("%d" , &cost[i]);
    }
    for(int i = 1; i < n; i++){
        int u , v;
        scanf("%d%d" , &u , &v);
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    dfs(1 , -1);
    printf("%d\n" , dp[1][0]);
}


#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define ff first
#define ss second
#define mp make_pair
#define pb push_back

const int N = 300 + 11;

int dp[N][N],a[N],k,dp1[N],dp2[N];
vector<int> v[N];

void dfs(int l, int pr)
{
    vector<int> vv;
    for (int j=0; j<v[l].size(); j++)
        if (v[l][j]!=pr) {dfs(v[l][j],l); vv.pb(v[l][j]);}
    for (int j=0; j<=k+1; j++)
        dp1[j]=0;
    if (vv.size()>0)
    {
        int to=vv[0];
        for (int d=0; d<=k; d++)
            dp1[d+1]=dp[to][d];
    }
    for (int j=1; j<vv.size(); j++)
    {
        for (int last=0; last<=k+1; last++)
    {
        for (int c=k; c>=0; c--)
        if (c+1+last>k)
            dp2[min(last,c+1)]=max(dp2[min(last,c+1)],dp1[last]+dp[vv[j]][c]);
    }
        for (int last=0; last<=k+1; last++)
        {
            dp1[last]=dp2[last];
            dp2[last]=0;
        }
    }
    for (int t=k; t>=0; t--)
    {
        if (t==0)
        {
            dp[l][t]=a[l];
            for (int j=0; j<vv.size(); j++)
                dp[l][t]+=dp[vv[j]][k];
        }
        dp[l][t]=max(dp[l][t],dp[l][t+1]);
        dp[l][t]=max(dp[l][t],dp1[t]);
    }
}

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int n;
    cin>>n>>k;
    for (int i=1; i<=n; i++)
        cin>>a[i];
    for (int i=1; i<n; i++)
    {
        int l,r;
        cin>>l>>r;
        v[l].pb(r);
        v[r].pb(l);
    }
    dfs(1,0);
    int ans=0;
    for (int j=0; j<=k; j++)
        ans=max(ans,dp[1][j]);
    cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define ff first
#define ss second
#define mp make_pair
#define pb push_back

const int N = 300 + 11;

int dp[N][N],a[N],k,dpp[N];
vector<int> v[N];

void dfs(int l, int pr)
{
    vector<int> vv;
    for (int j=0; j<v[l].size(); j++)
        if (v[l][j]!=pr) {dfs(v[l][j],l); vv.pb(v[l][j]);}
    for (int j=1; j<=k; j++)
        dpp[j]=0;
    if (vv.size()>0)
    {
        int to=vv[0];
        for (int d=0; d<=k; d++)
            dpp[d+1]=dp[to][d];
    }
    for (int j=1; j<vv.size(); j++)
    for (int last=0; last<=k; last++)
    for (int c=0; c<=k; c++)
    if (c+1+last>k)
        dpp[min(last,c+1)]=max(dpp[min(last,c+1)],dpp[last]+dp[vv[j]][c]);
    for (int t=k; t>=0; t--)
    {
        if (t==0)
        {
            dp[l][t]=a[l];
            for (int j=0; j<vv.size(); j++)
                dp[l][t]+=dp[vv[j]][k];
        }
        dp[l][t]=max(dp[l][t],dp[l][t+1]);
        dp[l][t]=max(dp[l][t],dpp[t]);
    }
}

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int n;
    cin>>n>>k;
    for (int i=1; i<=n; i++)
        cin>>a[i];
    for (int i=1; i<n; i++)
    {
        int l,r;
        cin>>l>>r;
        v[l].pb(r);
        v[r].pb(l);
    }
    dfs(1,0);
    int ans=0;
    for (int j=0; j<=k; j++)
        ans=max(ans,dp[1][j]);
    cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define ff first
#define ss second
#define mp make_pair
#define pb push_back

const int N = 300 + 11;

int dp[N][N],a[N],k,dpp[N];
vector<int> v[N];

void dfs(int l, int pr)
{
    vector<int> vv;
    for (int j=0; j<v[l].size(); j++)
        if (v[l][j]!=pr) {dfs(v[l][j],l); vv.pb(v[l][j]);}
    for (int j=1; j<=k; j++)
        dpp[j]=0;
    if (vv.size()>0)
    {
        int to=vv[0];
        for (int d=0; d<=k; d++)
            dpp[d+1]=dp[to][d];
    }
    for (int j=1; j<vv.size(); j++)
    for (int last=0; last<=k; last++)
    {
        int x=dpp[last];
        for (int c=k; c>=0; c--)
        if (c+1+last>k)
            dpp[min(last,c+1)]=max(dpp[min(last,c+1)],x+dp[vv[j]][c]);
    }
    for (int t=k; t>=0; t--)
    {
        if (t==0)
        {
            dp[l][t]=a[l];
            for (int j=0; j<vv.size(); j++)
                dp[l][t]+=dp[vv[j]][k];
        }
        dp[l][t]=max(dp[l][t],dp[l][t+1]);
        dp[l][t]=max(dp[l][t],dpp[t]);
    }
}

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int n;
    cin>>n>>k;
    for (int i=1; i<=n; i++)
        cin>>a[i];
    for (int i=1; i<n; i++)
    {
        int l,r;
        cin>>l>>r;
        v[l].pb(r);
        v[r].pb(l);
    }
    dfs(1,0);
    int ans=0;
    for (int j=0; j<=k; j++)
        ans=max(ans,dp[1][j]);
    cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define ff first
#define ss second
#define mp make_pair
#define pb push_back

const int N = 300 + 11;

int dp[N][N],a[N],k,dpp[N];
vector<int> v[N];

void dfs(int l, int pr)
{
    vector<int> vv;
    for (int j=0; j<v[l].size(); j++)
        if (v[l][j]!=pr) {dfs(v[l][j],l); vv.pb(v[l][j]);}
    for (int j=1; j<=k; j++)
        dpp[j]=0;
    if (vv.size()>0)
    {
        int to=vv[0];
        for (int d=0; d<=k; d++)
            dpp[d+1]=dp[to][d];
    }
    for (int j=1; j<vv.size(); j++)
    for (int last=k; last>=1; last--)
    for (int c=0; c<=k; c++)
    if (c+1+last>k)
        dpp[min(last,c+1)]=max(dpp[min(last,c+1)],dpp[last]+dp[vv[j]][c]);
    for (int t=k; t>=0; t--)
    {
        if (t==0)
        {
            dp[l][t]=a[l];
            for (int j=0; j<vv.size(); j++)
                dp[l][t]+=dp[vv[j]][k];
        }
        dp[l][t]=max(dp[l][t],dp[l][t+1]);
        dp[l][t]=max(dp[l][t],dpp[t]);
    }
}

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int n;
    cin>>n>>k;
    for (int i=1; i<=n; i++)
        cin>>a[i];
    for (int i=1; i<n; i++)
    {
        int l,r;
        cin>>l>>r;
        v[l].pb(r);
        v[r].pb(l);
    }
    dfs(1,0);
    int ans=0;
    for (int j=0; j<=k; j++)
        ans=max(ans,dp[1][j]);
    cout<<ans;
}


#include <bits/stdc++.h>
using namespace std;

#define ff first
#define ss second
#define mp make_pair
#define pb push_back

#define add(A,B) ((A)+(B))%MOD
#define mul(A,B) int((ll(A)*ll(B))%ll(MOD))

typedef long long ll;
typedef double lf;
typedef pair<int,int> pii;

const int N = 205;
const int MOD = int(1e9) + 7;

int n, k;
int val[N];
int son[N], bro[N];
int memo[N][N];
vector <int> graph[N];

void binarizeTree ( int node, int par ) {
  int nodeAnt = -1;
  for ( int it: graph[node] ) {
    if ( it == par ) continue;
    if ( nodeAnt == -1 )
      son[node] = it;
    else
      bro[nodeAnt] = it;
    nodeAnt = it;
    binarizeTree ( it, node );
  }
}

int dp ( int node, int cantL ) {
  if ( node == -1 ) return 0;
  int &r = memo[node][cantL];
  r = 0;
  if ( cantL == 0 )
    r = val[node] + dp ( son[node], k ) + dp ( bro[node], k-1 );
  else
    r = dp ( son[node], cantL-1 ) + dp ( bro[node], max ( cantL, k-cantL-1 ) );
  for ( int i = cantL+1; i <= k; ++i )
    r = max ( r, dp ( node, i ) );
  return r;
}

int main () {

  #ifdef LOCAL
    freopen("input.txt", "r", stdin);
  #else
    #define endl '\n'
  #endif

  memset ( son, -1, sizeof son );
  memset ( bro, -1, sizeof bro );

  scanf ( "%d%d", &n, &k );
  for ( int i = 0; i < n; ++i )
    scanf ( "%d", val+i );
  for ( int i = 1; i < n; ++i ) {
    int nA, nB;
    scanf ( "%d%d", &nA, &nB );
    nA--, nB--;
    graph[nA].pb ( nB );
    graph[nB].pb ( nA );
  }

  binarizeTree ( 0, -1 );
  printf ( "%d\n", dp ( 0, 0 ) );
  return 0;

}


#include <bits/stdc++.h>
using namespace std;

#define ff first
#define ss second
#define mp make_pair
#define pb push_back

#define add(A,B) ((A)+(B))%MOD
#define mul(A,B) int((ll(A)*ll(B))%ll(MOD))

typedef long long ll;
typedef double lf;
typedef pair<int,int> pii;

const int N = 205;
const int MOD = int(1e9) + 7;

int n, k;
int val[N];
int son[N], bro[N];
int memo[N][N];
vector <int> graph[N];

void binarizeTree ( int node, int par ) {
  int nodeAnt = -1;
  for ( int it: graph[node] ) {
    if ( it == par ) continue;
    if ( nodeAnt == -1 )
      son[node] = it;
    else
      bro[nodeAnt] = it;
    nodeAnt = it;
    binarizeTree ( it, node );
  }
}

int dp ( int node, int cantL ) {
  if ( node == -1 ) return 0;
  int &r = memo[node][cantL];
  if ( r != -1 ) return r;
  int cSon = son[node], cBro = bro[node];
  r = 0;
  if ( cantL == 0 )
    r = val[node] + dp ( cSon, k ) + dp ( cBro, k-1 );
  else
    r = dp ( cSon, cantL-1 ) + dp ( cBro, max ( cantL, k-cantL-1 ) );
  for ( int i = cantL+1; i <= k; ++i )
    r = max ( r, dp( cSon, i-1 ) + dp ( cBro, max ( cantL, k-i-1 ) ) );
  return r;
}

int main () {

  #ifdef LOCAL
    freopen("input.txt", "r", stdin);
  #else
    #define endl '\n'
  #endif

  memset ( son, -1, sizeof son );
  memset ( bro, -1, sizeof bro );
  memset ( memo, -1, sizeof memo );

  scanf ( "%d%d", &n, &k );
  for ( int i = 0; i < n; ++i )
    scanf ( "%d", val+i );
  for ( int i = 1; i < n; ++i ) {
    int nA, nB;
    scanf ( "%d%d", &nA, &nB );
    nA--, nB--;
    graph[nA].pb ( nB );
    graph[nB].pb ( nA );
  }

  binarizeTree ( 0, -1 );
  printf ( "%d\n", dp ( 0, 0 ) );
  return 0;

}


#include <bits/stdc++.h>
using namespace std;

#define ff first
#define ss second
#define mp make_pair
#define pb push_back

#define add(A,B) ((A)+(B))%MOD
#define mul(A,B) int((ll(A)*ll(B))%ll(MOD))

typedef long long ll;
typedef double lf;
typedef pair<int,int> pii;

const int N = 205;
const int MOD = int(1e9) + 7;

int n, k;
int val[N];
int son[N], bro[N];
int memo[N][N];
vector <int> graph[N];

void binarizeTree ( int node, int par ) {
  int nodeAnt = -1;
  for ( int it: graph[node] ) {
    if ( it == par ) continue;
    if ( nodeAnt == -1 )
      son[node] = it;
    else
      bro[nodeAnt] = it;
    nodeAnt = it;
    binarizeTree ( it, node );
  }
}

int dp ( int node, int cantL ) {
  if ( node == -1 ) return 0;
  int &r = memo[node][cantL];
  r = 0;
  if ( cantL == 0 )
    r = val[node] + dp ( son[node], k ) + dp ( bro[node], k-1 );
  else
    r = dp ( son[node], cantL-1 ) + dp ( bro[node], max ( cantL, k-cantL-1 ) );
  for ( int i = cantL+1; i <= k; ++i )
    r = max ( r, dp( son[node], i-1 ) + dp ( bro[node], max ( cantL, k-i-1 ) ) );
  return r;
}

int main () {

  #ifdef LOCAL
    freopen("input.txt", "r", stdin);
  #else
    #define endl '\n'
  #endif

  memset ( son, -1, sizeof son );
  memset ( bro, -1, sizeof bro );

  scanf ( "%d%d", &n, &k );
  for ( int i = 0; i < n; ++i )
    scanf ( "%d", val+i );
  for ( int i = 1; i < n; ++i ) {
    int nA, nB;
    scanf ( "%d%d", &nA, &nB );
    nA--, nB--;
    graph[nA].pb ( nB );
    graph[nB].pb ( nA );
  }

  binarizeTree ( 0, -1 );
  printf ( "%d\n", dp ( 0, 0 ) );
  return 0;

}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define endl "\n"
#define sz(x) ((int)x.size())

const int nax = 205;
vector<int> g[nax], w(nax, 0);
vector<vector<int>> dp(nax, vector<int>(nax, 0));
int n, k;

void dfs(int u, int p)
{
    dp[u][0] = w[u];
    int sm = 0;
    vector<int> temp(nax, 0);
    for(auto &f: g[u])
        if(f != p)
        {
            dfs(f, u);
            dp[u][0] += dp[f][k];
            for(int i = 0; i <  nax; i++)
                temp[i] += dp[f][i];
        }
    for(int i = k/2+1; i < nax; i++)
        dp[u][i] = temp[i-1];
    for(int i = 1; i < k/2+1; i++)
    {
        for(auto &f: g[u])
        {
            if(f == p)
                continue;
            int tmax = dp[f][i-1] + temp[k-i] - dp[f][k-i];
            dp[u][i] = max(dp[u][i], tmax);
        }
    }
    for(int i = nax-2; i >= 0; i--)
        dp[u][i] = max(dp[u][i], dp[u][i+1]);
}

int32_t main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> k;
    for(int i = 1; i <= n; i++)
        cin >> w[i];
    for(int i = 0; i < n-1; i++)
    {
        int u, v;
        cin >> u >> v;
        g[u].pb(v);
        g[v].pb(u);
    }
    dfs(1, 1);
    // for(int i = 1; i <= n; i++)
    //     for(int j = 0; j <= n; j++)
    //         cout << dp[i][j] << " \n"[j==n];
    int ans = -1;
    for(int i = 1; i <= n; i++)
        for(int j = 0; j < nax; j++)
            ans = max(ans, ((j > k) ? w[i] : 0) + dp[i][j]);
    cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define endl "\n"
#define sz(x) ((int)x.size())

const int nax = 205;
vector<int> g[nax], w(nax, 0);
vector<vector<int>> dp(nax, vector<int>(nax, 0));
int n, k;

void dfs(int u, int p)
{
    dp[u][0] = w[u];
    vector<int> temp(nax, 0);
    for(auto &f: g[u])
        if(f != p)
        {
            dfs(f, u);
            dp[u][0] += dp[f][k];
            for(int i = 0; i <  nax; i++)
                temp[i] += dp[f][i];
        }
    for(int i = k/2+1; i < nax; i++)
        dp[u][i] = temp[i-1];
    for(int i = 1; i < k/2+1; i++)
    {
        for(auto &f: g[u])
        {
            if(f == p)
                continue;
            int tmax = dp[f][i-1] + temp[k-i] - dp[f][k-i];
            dp[u][i] = max(dp[u][i], tmax);
        }
    }
}

int32_t main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> k;
    for(int i = 1; i <= n; i++)
        cin >> w[i];
    for(int i = 0; i < n-1; i++)
    {
        int u, v;
        cin >> u >> v;
        g[u].pb(v);
        g[v].pb(u);
    }
    dfs(1, 1);
    // for(int i = 1; i <= n; i++)
    //     for(int j = 0; j <= n; j++)
    //         cout << dp[i][j] << " \n"[j==n];
    int ans = -1;
    for(int i = 0; i < nax; i++)
        ans = max(ans, dp[1][i]);
    cout << ans << endl;
}


#include<bits/stdc++.h>
#define int long long int
#define ull unsigned long long int
#define getFaster ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
#define rep(i,init,n) for(int i=init;i<n;i++)
#define rev(i,n,init) for(int i=n;i>=init;i--)
#define MAXN 1200005
#define MOD 1000000007
#define f first
#define s second
#define endl '\n'
#define pii pair<int,int>
#define precise cout<<fixed<<setprecision(10)
using namespace std;
const double PI = atan(1.0)*4;
const int64_t INF=100000000000000003;
const int32_t LOG=21;
const int BIG=pow(2,20);
int weight[205];
vector<int> v1[205];
vector<int> v[205];
int dp[205][205];
int n,k;
int parent[205];
int sum[205][205];
void root_the_tree(int i)
{
       rep(j,0,v1[i].size())
       {
           int ver=v1[i][j];
           if(parent[i]==ver)continue;
           v[i].push_back(ver);
           parent[ver]=i;
           root_the_tree(ver);
       }
}
int solve(int i,int j)
{
    if(dp[i][j]!=-1)return dp[i][j];
    //BASE CASE 1
    if(v[i].size()==0)
    {
         if(j==0)dp[i][j]=weight[i];
         else dp[i][j]=0;
         return dp[i][j];
    }
    //BASE CASE 2
    if(j==n)
    {
        dp[i][j]=0;
        return dp[i][j];
    }
    //FIRST ALL MAXIMUMS GREATER THAN j
    dp[i][j]=max(dp[i][j],solve(i,j+1));
    //SUB-CASE 1
    if(j!=0)
    {
        //FIRST SOLVE ALL THE CHILDREN
        int a=0,b=0;
        bool g=true;

        rep(i1,0,v[i].size())
        {
             int ver=v[i][i1];
             a+=solve(ver,j-1);
             int x=max(k-j-1,j-1);
             b+=solve(ver,x);
        }
        sum[i][j-1]=a;
        sum[i][max(k-j-1,0LL)]=b;
        //THEN GO FOR THE DP
        rep(i1,0,v[i].size())
        {
            int ver=v[i][i1];
            int x=max(k-j-1,j-1);
            dp[i][j]=max(dp[i][j],dp[ver][j-1]+sum[i][x]-dp[ver][x]);
        }
    }
    //SUB-CASE 2
    else
    {
         //FIRST SOLVE ALL THE CHILDREN
         int a=0;
         rep(i1,0,v[i].size())
         {
           int ver=v[i][i1];
           a+=solve(ver,k-1);
         }
         sum[i][k-1]=a;
         //THEN GO FOR THE DP
         dp[i][j]=max(dp[i][j],sum[i][k-1]+weight[i]);
    }
    return dp[i][j];
}
int32_t main(){
        getFaster;
         cin>>n>>k;
         k++;
         rep(i,1,n+1)cin>>weight[i];
         rep(i,0,205)rep(j,0,205)dp[i][j]=-1;
         rep(i,1,n)
         {
           int x,y;
           cin>>x>>y;
           v1[x].push_back(y);
           v1[y].push_back(x);
         }
        root_the_tree(1);
        cout<<solve(1,0)<<endl;
        return 0;
}


#include<bits/stdc++.h>
#define int long long int
#define ull unsigned long long int
#define getFaster ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
#define rep(i,init,n) for(int i=init;i<n;i++)
#define rev(i,n,init) for(int i=n;i>=init;i--)
#define MAXN 1200005
#define MOD 1000000007
#define f first
#define s second
#define endl '\n'
#define pii pair<int,int>
#define precise cout<<fixed<<setprecision(10)
using namespace std;
const double PI = atan(1.0)*4;
const int64_t INF=100000000000000003;
const int32_t LOG=21;
const int BIG=pow(2,20);
int weight[205];
vector<int> v1[205];
vector<int> v[205];
int dp[205][205];
int n,k;
int parent[205];
int sum[205][205];
void root_the_tree(int i)
{
       rep(j,0,v1[i].size())
       {
           int ver=v1[i][j];
           if(parent[i]==ver)continue;
           v[i].push_back(ver);
           parent[ver]=i;
           root_the_tree(ver);
       }
}
int solve(int i,int j)
{
    if(dp[i][j]!=-1)return dp[i][j];
    //BASE CASE 1
    if(v[i].size()==0)
    {
         if(j==0)dp[i][j]=weight[i];
         else dp[i][j]=0;
         return dp[i][j];
    }
    //BASE CASE 2
    if(j==n)
    {
        dp[i][j]=0;
        return dp[i][j];
    }
    //FIRST ALL MAXIMUMS GREATER THAN j
    rep(temp,j+1,n+1)
    {
         dp[i][j]=max(dp[i][j],solve(i,temp));
    }
    //SUB-CASE 1
    if(j!=0)
    {
        //FIRST SOLVE ALL THE CHILDREN
        int a=0,b=0;
        bool g=true;

        rep(i1,0,v[i].size())
        {
             int ver=v[i][i1];
             a+=solve(ver,j-1);
             int x=max(k-j-1,j-1);
             b+=solve(ver,x);
        }
        sum[i][j-1]=a;
        sum[i][max(k-j-1,0LL)]=b;
        //THEN GO FOR THE DP
        rep(i1,0,v[i].size())
        {
            int ver=v[i][i1];
            int x=max(k-j-1,j-1);
            dp[i][j]=max(dp[i][j],dp[ver][j-1]+sum[i][x]-dp[ver][x]);
        }
    }
    //SUB-CASE 2
    else
    {
         //FIRST SOLVE ALL THE CHILDREN
         int a=0;
         rep(i1,0,v[i].size())
         {
           int ver=v[i][i1];
           a+=solve(ver,k-1);
         }
         sum[i][k-1]=a;
         //THEN GO FOR THE DP
         dp[i][j]=max(dp[i][j],sum[i][k-1]+weight[i]);
    }
    return dp[i][j];
}
int32_t main(){
        getFaster;
         cin>>n>>k;
         k++;
         rep(i,1,n+1)cin>>weight[i];
         rep(i,0,205)rep(j,0,205)dp[i][j]=-1;
         rep(i,1,n)
         {
           int x,y;
           cin>>x>>y;
           v1[x].push_back(y);
           v1[y].push_back(x);
         }
        int max1=0;
        root_the_tree(1);
        cout<<solve(1,0)<<endl;
        return 0;
}


#include<bits/stdc++.h>
#define int long long int
#define ull unsigned long long int
#define getFaster ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
#define rep(i,init,n) for(int i=init;i<n;i++)
#define rev(i,n,init) for(int i=n;i>=init;i--)
#define MAXN 1200005
#define MOD 1000000007
#define f first
#define s second
#define endl '\n'
#define pii pair<int,int>
#define precise cout<<fixed<<setprecision(10)
using namespace std;
const double PI = atan(1.0)*4;
const int64_t INF=100000000000000003;
const int32_t LOG=21;
const int BIG=pow(2,20);
int weight[205];
vector<int> v1[205];
vector<int> v[205];
int dp[205][205];
int n,k;
int parent[205];
int sum[205][205];
void root_the_tree(int i)
{
       rep(j,0,v1[i].size())
       {
           int ver=v1[i][j];
           if(parent[i]==ver)continue;
           v[i].push_back(ver);
           parent[ver]=i;
           root_the_tree(ver);
       }
}
int solve(int i,int j)
{
    if(dp[i][j]!=-1)return dp[i][j];
    //BASE CASE 1
    if(v[i].size()==0)
    {
         if(j==0)dp[i][j]=weight[i];
         else dp[i][j]=0;
         return dp[i][j];
    }
    //BASE CASE 2
    if(j==n)
    {
        dp[i][j]=0;
        return dp[i][j];
    }
    //FIRST ALL MAXIMUMS GREATER THAN j
    rep(temp,j+1,n+1)
    {
         dp[i][j]=max(dp[i][j],solve(i,temp));
    }
    //SUB-CASE 1
    if(j!=0)
    {
        //FIRST SOLVE ALL THE CHILDREN
        int a=0,b=0;
        bool g=true;

        rep(i1,0,v[i].size())
        {
             int ver=v[i][i1];
             a+=solve(ver,j-1);
             int x=max(k-j-1,j-1);
             b+=solve(ver,x);
        }
        sum[i][j-1]=a;
        sum[i][max(k-j-1,0LL)]=b;
        //THEN GO FOR THE DP
        rep(i1,0,v[i].size())
        {
            int ver=v[i][i1];
            int x=max(k-j-1,j-1);
            dp[i][j]=max(dp[i][j],dp[ver][j-1]+sum[i][x]-dp[ver][x]);
        }
    }
    //SUB-CASE 2
    else
    {
         //FIRST SOLVE ALL THE CHILDREN
         int a=0;
         rep(i1,0,v[i].size())
         {
           int ver=v[i][i1];
           a+=solve(ver,k-1);
         }
         sum[i][k-1]=a;
         //THEN GO FOR THE DP
         dp[i][j]=max(dp[i][j],sum[i][k-1]+weight[i]);
    }
    return dp[i][j];
}
int32_t main(){
        getFaster;
         cin>>n>>k;
         k++;
         rep(i,1,n+1)cin>>weight[i];
         rep(i,0,205)rep(j,0,205)dp[i][j]=-1;
         rep(i,1,n)
         {
           int x,y;
           cin>>x>>y;
           v1[x].push_back(y);
           v1[y].push_back(x);
         }
        root_the_tree(1);
        cout<<solve(1,0)<<endl;
        return 0;
}


#include<bits/stdc++.h>
#define int long long int
#define ull unsigned long long int
#define getFaster ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
#define rep(i,init,n) for(int i=init;i<n;i++)
#define rev(i,n,init) for(int i=n;i>=init;i--)
#define MAXN 1200005
#define MOD 1000000007
#define f first
#define s second
#define endl '\n'
#define pii pair<int,int>
#define precise cout<<fixed<<setprecision(10)
using namespace std;
const double PI = atan(1.0)*4;
const int64_t INF=100000000000000003;
const int32_t LOG=21;
const int BIG=pow(2,20);
int weight[205];
vector<int> v[205];
int calc[205][205];
  int n,k1;
int solve(int i,int j,int prev)
{
      //cout<<i<<" "<<j<<" yo "<<endl;
      if(calc[i][j]!=-1)return calc[i][j];
      if(j==0)
      {
           calc[i][j]=weight[i];
           rep(k,0,v[i].size())
           {
              int ver=v[i][k];
              if(ver==prev)continue;
              calc[i][j]+=solve(ver,max(0ll,k1-j-1),i);
           }
      }
      else
      {
          rep(k,0,v[i].size())
          {
              int ver=v[i][k];
              if(ver==prev)continue;
              int tot=solve(ver,j-1,i);
              rep(l,0,v[i].size())
              {
                 int ver1=v[i][l];
                 if(ver1==ver || ver1==prev)continue;
                 tot+=solve(ver1,max(j-1,k1-j-1),i);
              }
              calc[i][j]=max(calc[i][j],tot);
          }
      }
      if(calc[i][j]==-1)calc[i][j]=0;
      return calc[i][j];
}
int32_t main(){
        getFaster;
         cin>>n>>k1;
         k1++;
         rep(i,1,n+1)cin>>weight[i];
         rep(i,0,205)rep(j,0,205)calc[i][j]=-1;
         rep(i,1,n)
         {
           int x,y;
           cin>>x>>y;
           v[x].push_back(y);
           v[y].push_back(x);
         }
        int max1=0;
        rep(j,0,n+1)
        {
          max1=max(max1,solve(1,j,-1));
        }
         cout<<max1<<endl;
        return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define fto(i, s, e) for(int i = (s); i <= (e); ++i)
#define fto1(i, s, e) for(int i = (s); i < (e); ++i)
#define fdto(i, s, e) for(int i = (s); i >= (e); --i)
#define fit(it, var) for(auto it = (var).begin(); it != (var).end(); ++it)
#define frit(it, var) for(auto it = (var).rbegin(); it != (var).rend(); ++it)
#define fat(it, var) for (auto it : var)

#define bc __builtin_popcountll
#define isbit(x, bit) (((x >> bit) & 1) != 0)
#define onbit(x, bit) (x | (1LL << bit))
#define offbit(x, bit) (x & ~(1LL << bit))
#define y1 ansdj

#define ll long long
#define ii pair<int, int>
#define x first
#define y second
#define pb push_back
#define sz(v) int((v).size())
#define all(v) (v).begin(), (v).end()

template<class T1, class T2> ostream& operator<<(ostream &os, pair<T1, T2> const &v) {
	return os << '(' << v.x << ", " << v.y << ')';
}

template<class T> ostream& operator<<(ostream &os, vector<T> const &v) {
	fto1 (i, 0, sz(v)) { os << v[i]; if (i < sz(v)-1) os << ' '; }
	return os;
}

template<class T> void buga(const T &v, int l, int r) { fto (i, l, r) { cout << v[i] << " \n"[i == r]; } }

template<class T> void bug(T const &v) { cout << v << endl; }
template<class T, class... Args> void bug(T const &v, Args const&... args) { cout << v << ' '; bug(args...); }

double const pi = acos(-1);
#define oo 1000000007
#define OO 1000000000000000003LL
int const maxn = 2e2+3;

int n, k;
int dp[maxn][maxn];
vector<int> ke[maxn];
int a[maxn];

int solve(int u, int j, int p) {
	if (dp[u][j] != -1) return dp[u][j];
	dp[u][0] = a[u];
	fat (v, ke[u]) {
		if (v == p) continue;
		dp[u][0] += solve(v, k, u);
	}

	fto (tmp, 1, k) {
		dp[u][tmp] = 0;
		int sumdp = 0;
		fat (v, ke[u]) {
			if (v == p) continue;
			sumdp += dp[v][k-tmp];
		}
		fat (v, ke[u]) {
			if (v == p) continue;
			dp[u][tmp] = max(dp[u][tmp], sumdp-dp[v][k-tmp]+dp[v][tmp-1]);
		}
		dp[u][0] = max(dp[u][0], dp[u][tmp]);
	}

	return dp[u][j];
}

int main() {
	#ifdef KITTENS
		freopen("main.inp", "r", stdin);
		freopen("main.out", "w", stdout);
	#endif
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

	cin >> n >> k;
	fto (i, 1, n) cin >> a[i];
	fto (i, 1, n-1) {
		int u, v;
		cin >> u >> v;
		ke[u].pb(v);
		ke[v].pb(u);
	}

	// dp[i][j]: max khi chon tai dinh i, lay tai v cach i khoang cach la j
	// dp[i][0] = a[i]+dp[v][k];
	// dp[i][j] = max(dp[v][j-1]+d[v'][k-j]) j <= k
	fto (u, 1, n) fto (j, 0, k) dp[u][j] = -1;
	fto (u, 2, n) if (sz(ke[u]) == 1) {
		dp[u][0] = a[u];
		fto(j, 1, k) dp[u][j] = 0;
	}

	bug(solve(1, 0, 0));

	#ifdef KITTENS
		cerr << 0.001*clock() << endl;
	#endif
	return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define fto(i, s, e) for(int i = (s); i <= (e); ++i)
#define fto1(i, s, e) for(int i = (s); i < (e); ++i)
#define fdto(i, s, e) for(int i = (s); i >= (e); --i)
#define fit(it, var) for(auto it = (var).begin(); it != (var).end(); ++it)
#define frit(it, var) for(auto it = (var).rbegin(); it != (var).rend(); ++it)
#define fat(it, var) for (auto it : var)

#define bc __builtin_popcountll
#define isbit(x, bit) (((x >> bit) & 1) != 0)
#define onbit(x, bit) (x | (1LL << bit))
#define offbit(x, bit) (x & ~(1LL << bit))
#define y1 ansdj

#define ll long long
#define ii pair<int, int>
#define x first
#define y second
#define pb push_back
#define sz(v) int((v).size())
#define all(v) (v).begin(), (v).end()

template<class T1, class T2> ostream& operator<<(ostream &os, pair<T1, T2> const &v) {
	return os << '(' << v.x << ", " << v.y << ')';
}

template<class T> ostream& operator<<(ostream &os, vector<T> const &v) {
	fto1 (i, 0, sz(v)) { os << v[i]; if (i < sz(v)-1) os << ' '; }
	return os;
}

template<class T> void buga(const T &v, int l, int r) { fto (i, l, r) { cout << v[i] << " \n"[i == r]; } }

template<class T> void bug(T const &v) { cout << v << endl; }
template<class T, class... Args> void bug(T const &v, Args const&... args) { cout << v << ' '; bug(args...); }

double const pi = acos(-1);
#define oo 1000000007
#define OO 1000000000000000003LL
int const maxn = 2e2+3;

int n, k;
int dp[maxn][maxn];
vector<int> ke[maxn];
int a[maxn];

int solve(int u, int j, int p) {
	if (dp[u][j] != -1) return dp[u][j];
	dp[u][0] = a[u];
	fat (v, ke[u]) {
		if (v == p) continue;
		dp[u][0] += solve(v, k, u);
	}

	fdto (tmp, k, 1) {
		dp[u][tmp] = 0;
		int sumdp = 0;
		fat (v, ke[u]) {
			if (v == p) continue;
			sumdp += dp[v][max(k-tmp, tmp-1)];
		}
		fat (v, ke[u]) {
			if (v == p) continue;
			dp[u][tmp] = max(dp[u][tmp], sumdp-dp[v][max(k-tmp, tmp-1)]+dp[v][tmp-1]);
		}
		dp[u][tmp] = max(dp[u][tmp], dp[u][tmp+1]);
	}
	dp[u][0] = max(dp[u][0], dp[u][1]);

	//fdto (tmp, k-1, 0) dp[u][tmp] = max(dp[u][tmp], dp[u][tmp+1]);

	return dp[u][j];
}

int main() {
	#ifdef KITTENS
		freopen("main.inp", "r", stdin);
		freopen("main.out", "w", stdout);
	#endif
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

	cin >> n >> k;
	fto (i, 1, n) cin >> a[i];
	fto (i, 1, n-1) {
		int u, v;
		cin >> u >> v;
		ke[u].pb(v);
		ke[v].pb(u);
	}

	// dp[i][j]: max khi chon tai dinh i, lay tai v cach i khoang cach la j
	// dp[i][0] = a[i]+dp[v][k];
	// dp[i][j] = max(dp[v][j-1]+d[v'][k-j]) j <= k
	fto (u, 1, n) fto (j, 0, k) dp[u][j] = -1;
	fto (u, 2, n) if (sz(ke[u]) == 1) {
		dp[u][0] = a[u];
		fto(j, 1, k) dp[u][j] = 0;
	}

	bug(solve(1, 0, 0));

	#ifdef KITTENS
		cerr << 0.001*clock() << endl;
	#endif
	return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define fto(i, s, e) for(int i = (s); i <= (e); ++i)
#define fto1(i, s, e) for(int i = (s); i < (e); ++i)
#define fdto(i, s, e) for(int i = (s); i >= (e); --i)
#define fit(it, var) for(auto it = (var).begin(); it != (var).end(); ++it)
#define frit(it, var) for(auto it = (var).rbegin(); it != (var).rend(); ++it)
#define fat(it, var) for (auto it : var)

#define bc __builtin_popcountll
#define isbit(x, bit) (((x >> bit) & 1) != 0)
#define onbit(x, bit) (x | (1LL << bit))
#define offbit(x, bit) (x & ~(1LL << bit))
#define y1 ansdj

#define ll long long
#define ii pair<int, int>
#define x first
#define y second
#define pb push_back
#define sz(v) int((v).size())
#define all(v) (v).begin(), (v).end()

template<class T1, class T2> ostream& operator<<(ostream &os, pair<T1, T2> const &v) {
	return os << '(' << v.x << ", " << v.y << ')';
}

template<class T> ostream& operator<<(ostream &os, vector<T> const &v) {
	fto1 (i, 0, sz(v)) { os << v[i]; if (i < sz(v)-1) os << ' '; }
	return os;
}

template<class T> void buga(const T &v, int l, int r) { fto (i, l, r) { cout << v[i] << " \n"[i == r]; } }

template<class T> void bug(T const &v) { cout << v << endl; }
template<class T, class... Args> void bug(T const &v, Args const&... args) { cout << v << ' '; bug(args...); }

double const pi = acos(-1);
#define oo 1000000007
#define OO 1000000000000000003LL
int const maxn = 2e2+3;

int n, k;
int dp[maxn][maxn];
vector<int> ke[maxn];
int a[maxn];

int solve(int u, int j, int p) {
	if (dp[u][j] != -1) return dp[u][j];
	dp[u][0] = a[u];
	fat (v, ke[u]) {
		if (v == p) continue;
		dp[u][0] += solve(v, k, u);
	}

	fto (tmp, 1, k) {
		dp[u][tmp] = 0;
		int sumdp = 0;
		fat (v, ke[u]) {
			if (v == p) continue;
			sumdp += dp[v][k-tmp];
		}
		fat (v, ke[u]) {
			if (v == p) continue;
			dp[u][tmp] = max(dp[u][tmp], sumdp-dp[v][k-tmp]+dp[v][tmp-1]);
		}
	}

	fdto (tmp, k-1, 0) dp[u][tmp] = max(dp[u][tmp], dp[u][tmp+1]);

	return dp[u][j];
}

int main() {
	#ifdef KITTENS
		freopen("main.inp", "r", stdin);
		freopen("main.out", "w", stdout);
	#endif
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

	cin >> n >> k;
	fto (i, 1, n) cin >> a[i];
	fto (i, 1, n-1) {
		int u, v;
		cin >> u >> v;
		ke[u].pb(v);
		ke[v].pb(u);
	}

	// dp[i][j]: max khi chon tai dinh i, lay tai v cach i khoang cach la j
	// dp[i][0] = a[i]+dp[v][k];
	// dp[i][j] = max(dp[v][j-1]+d[v'][k-j]) j <= k
	fto (u, 1, n) fto (j, 0, k) dp[u][j] = -1;
	fto (u, 2, n) if (sz(ke[u]) == 1) {
		dp[u][0] = a[u];
		fto(j, 1, k) dp[u][j] = 0;
	}

	bug(solve(1, 0, 0));

	#ifdef KITTENS
		cerr << 0.001*clock() << endl;
	#endif
	return 0;
}