#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(false); cin.tie(0);
int n, k;
cin >> n >> k;
vector<int> a(n + 1);
for(int i = 1; i <= n; i++) cin >> a[i];
vector<vector<int>> g(n + 1);
for(int i = 0; i + 1 < n; i++) {
int u, v; cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
vector<vector<vector<ll>>> dp(n + 1, vector<vector<ll>> (2, vector<ll> (k + 1, 0))); // [vertex][left / right][length]
auto dfs = [&](auto&& self, int u, int par) -> ll {
dp[u][0][1] = dp[u][1][1] = a[u];
short dir = 0;
ll mx = 0;
for(auto &v: g[u]) {
if(v == par) continue;
mx = max(mx, self(self, v, u));
for(int i = 2; i <= k; i++) {
if(dp[v][1][i - 1] == 0 && dp[v][0][i - 1] == 0) break;
dp[u][dir][i] = a[u] + max(dp[v][0][i - 1], dp[v][1][i - 1]);
}
++dir;
}
for(int i = 1; i <= k; i++) {
if(dp[u][0][i] && dp[u][1][k - i + 1]) {
mx = max(mx, dp[u][0][i] + dp[u][1][k - i + 1] - a[u]);
}
if(dp[u][1][i] && dp[u][0][k - i + 1]) {
mx = max(mx, dp[u][1][i] + dp[u][0][k - i + 1] - a[u]);
}
}
return mx;
};
cout << dfs(dfs, 1, 0) << endl;
return 0;
}