#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MINF = -1e18;
int N, K;
vector<ll> A;
vector<vector<int>> g;
vector<vector<ll>> dp; // dp[i][j] = max weight path in subtree of i with exactly j nodes
ll ans = 0;
void dfs(int u, int p) {
dp[u][1] = A[u]; // base case
vector<vector<ll>> child_dp; // child_dp[c][j] = dp for each child
vector<int> children;
for (int v : g[u]) {
if (v == p) continue;
dfs(v, u);
children.push_back(v);
// Single chain path: extend chain by taking child
for (int j = 1; j < K; ++j) {
if (dp[v][j] != MINF)
dp[u][j + 1] = max(dp[u][j + 1], dp[v][j] + A[u]);
}
}
ans = max(ans, dp[u][K]);
// For each length j, collect dp for all children
vector<vector<ll>> all_dp(K + 1);
for (int v : children) {
for (int j = 1; j <= K; ++j) {
if (dp[v][j] != MINF) {
all_dp[j].push_back(dp[v][j]);
}
}
}
// For each length j, build prefix and suffix max
vector<vector<ll>> pre(K + 1), suf(K + 1);
for (int j = 1; j <= K; ++j) {
int sz = all_dp[j].size();
if (sz == 0) continue;
pre[j].resize(sz);
suf[j].resize(sz);
pre[j][0] = all_dp[j][0];
for (int i = 1; i < sz; ++i)
pre[j][i] = max(pre[j][i - 1], all_dp[j][i]);
suf[j][sz - 1] = all_dp[j][sz - 1];
for (int i = sz - 2; i >= 0; --i)
suf[j][i] = max(suf[j][i + 1], all_dp[j][i]);
}
// For each child, try combining with other children
for (int idx = 0; idx < (int)children.size(); ++idx) {
int v = children[idx];
for (int r = 1; r <= K - 2; ++r) {
ll left = dp[v][r];
ll rem = K - r - 1;
if (left == MINF || rem < 1) continue;
if (dp[v][rem] == MINF && all_dp[rem].size() == 0) continue;
int pos = -1; // find the position of dp[v][rem] in all_dp[rem]
for (int i = 0; i < (int)all_dp[rem].size(); ++i) {
if (all_dp[rem][i] == dp[v][rem]) {
pos = i;
break;
}
}
ll otherMax = MINF;
int sz = all_dp[rem].size();
if (sz == 1 && pos != -1) {
otherMax = MINF;
} else if (sz > 0) {
if (pos == -1) {
otherMax = suf[rem][0];
} else {
if (pos > 0) otherMax = max(otherMax, pre[rem][pos - 1]);
if (pos + 1 < sz) otherMax = max(otherMax, suf[rem][pos + 1]);
}
}
if (otherMax != MINF) {
ans = max(ans, left + A[u] + otherMax);
}
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> N >> K;
A.resize(N + 1);
g.resize(N + 1);
dp.assign(N + 1, vector<ll>(K + 1, MINF));
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);
}
dfs(1, 0);
cout << ans << '\n';
return 0;
}