#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MINF = -1e18;
int N, K;
vector<ll> val;
vector<vector<int>> adj;
// For each node u, dp[u][d] = maximum sum of a downward path starting at u with exactly d nodes.
// The DFS returns a vector dp (size K+1, where index 1..K are used).
// 'ans' is updated globally with any valid path of exactly K nodes.
vector<ll> dfs(int u, int parent, ll &ans) {
vector<ll> dp(K+1, MINF);
dp[1] = val[u]; // The trivial path that is just the node u.
// Process every child v of u.
for (int v : adj[u]) {
if(v == parent) continue;
vector<ll> childDP = dfs(v, u, ans);
// --- Combination step ---
// Before merging the new child's information into dp[u], try to combine
// a branch already in dp[u] (coming from a different child or u itself)
// with a new branch from v.
// A branch from child v gives (when attached to u):
// new_branch_length = L+1, with sum = val[u] + childDP[L]
// Now, if dp[u] already has a branch of length i (which already includes u),
// combining the two branches (and subtracting one copy of u) gives:
// total length = i + (L+1) - 1 = i + L.
// To have exactly K nodes we require: i + L = K.
for (int i = 1; i < K; i++) {
int L = K - i; // L must be at least 1 and at most K-1.
if(L >= 1 && L <= K-1) {
if(dp[i] != MINF && childDP[L] != MINF) {
ans = max(ans, dp[i] + childDP[L]);
}
}
}
// --- Merging step ---
// Now update dp[u] with the new downward branches coming from v.
// For every possible branch from v of length L (1 <= L <= K-1),
// we can form a branch from u of length L+1 with sum = val[u] + childDP[L].
vector<ll> newDP = dp;
for (int L = 1; L <= K-1; L++) {
if(childDP[L] != MINF) {
newDP[L+1] = max(newDP[L+1], val[u] + childDP[L]);
}
}
dp = newDP;
}
// Also update the answer with any downward branch from u that has exactly K nodes.
ans = max(ans, dp[K]);
return dp;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> N >> K;
val.resize(N+1);
for (int i = 1; i <= N; i++){
cin >> val[i];
}
adj.resize(N+1);
for (int i = 1; i <= N-1; i++){
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
ll ans = MINF;
dfs(1, -1, ans);
cout << (ans == MINF ? 0 : ans) << "\n";
return 0;
}