#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 5;
int N, K;
vector<int> adj[MAXN]; // Adjacency list
vector<int> values; // Node values
int findMaxWeightPath() {
int maxWeight = 0;
// Perform BFS from each node
for (int start = 1; start <= N; ++start) {
queue<pair<int, pair<int, int>>> q; // {current node, {current depth, weight sum}}
q.push({start, {1, values[start - 1]}});
vector<bool> visited(N + 1, false);
visited[start] = true;
while (!q.empty()) {
auto [node, data] = q.front();
int depth = data.first;
int sumWeight = data.second;
q.pop();
// If path of exact length K is reached, update maxWeight
if (depth == K) {
maxWeight = max(maxWeight, sumWeight);
continue;
}
// Traverse adjacent nodes
for (int neighbor : adj[node]) {
if (!visited[neighbor]) {
visited[neighbor] = true;
q.push({neighbor, {depth + 1, sumWeight + values[neighbor - 1]}});
}
}
}
}
return maxWeight;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> N >> K;
values.resize(N);
for (int i = 0; i < N; ++i) cin >> values[i];
for (int i = 0; i < N - 1; ++i) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
cout << findMaxWeightPath() << "\n";
return 0;
}