#include <bits/stdc++.h>
typedef long long int ll;
using namespace std;
ll dfs(int s, int p, vector<ll>& w, vector<unordered_set<int>>& tree, unordered_map<int, int>& parent, map<pair<ll, ll>, int>& arr) {
parent[s] = p;
ll sum = 0;
for (auto v : tree[s]) {
if (v == p)
continue;
sum += dfs(v, s, w, tree, parent, arr);
}
sum += w[s];
arr[{sum, w[s]}] = s;
return sum;
}
ll dfs_sum(int s, int p, vector<ll>& w, vector<unordered_set<int>>& tree) {
ll sum = 0;
for (auto v : tree[s]) {
if (v == p)
continue;
sum += dfs_sum(v, s, w, tree);
}
sum += w[s];
return sum;
}
int main() {
int n, k;
cin >> n >> k;
vector<ll> w(n+1);
for (int i = 1; i <= n; i++)
cin >> w[i];
vector<unordered_set<int>> tree(n+1);
int u, v;
for (int i = 1; i <= n-1; i++) {
cin >> u >> v;
tree[u].insert(v);
tree[v].insert(u);
}
ll last_sum = LLONG_MIN;
for (int i = 1; i <= k; i++) {
map<pair<ll, ll>, int> arr;
unordered_map<int, int> parent;
ll sum = dfs(1, -1, w, tree, parent, arr);
last_sum = max(last_sum, sum);
if (arr.begin()->first.first >= 0)
break;
int s = arr.begin()->second;
if (s == 1) {
cout << 0 << "\n";
return 0;
}
tree[parent[s]].erase(s);
tree[s].erase(parent[s]);
}
cout << max(last_sum, dfs_sum(1, -1, w, tree)) << "\n";
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+Cgp0eXBlZGVmIGxvbmcgbG9uZyBpbnQgbGw7CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgpsbCBkZnMoaW50IHMsIGludCBwLCB2ZWN0b3I8bGw+JiB3LCB2ZWN0b3I8dW5vcmRlcmVkX3NldDxpbnQ+PiYgdHJlZSwgdW5vcmRlcmVkX21hcDxpbnQsIGludD4mIHBhcmVudCwgbWFwPHBhaXI8bGwsIGxsPiwgaW50PiYgYXJyKSB7CiAgICBwYXJlbnRbc10gPSBwOwogICAgbGwgc3VtID0gMDsKICAgIGZvciAoYXV0byB2IDogdHJlZVtzXSkgewogICAgICAgIGlmICh2ID09IHApCiAgICAgICAgICAgIGNvbnRpbnVlOwogICAgICAgIHN1bSArPSBkZnModiwgcywgdywgdHJlZSwgcGFyZW50LCBhcnIpOwogICAgfQogICAgc3VtICs9IHdbc107CiAgICBhcnJbe3N1bSwgd1tzXX1dID0gczsKICAgIHJldHVybiBzdW07Cn0KCmxsIGRmc19zdW0oaW50IHMsIGludCBwLCB2ZWN0b3I8bGw+JiB3LCB2ZWN0b3I8dW5vcmRlcmVkX3NldDxpbnQ+PiYgdHJlZSkgewogICAgbGwgc3VtID0gMDsKICAgIGZvciAoYXV0byB2IDogdHJlZVtzXSkgewogICAgICAgIGlmICh2ID09IHApCiAgICAgICAgICAgIGNvbnRpbnVlOwogICAgICAgIHN1bSArPSBkZnNfc3VtKHYsIHMsIHcsIHRyZWUpOwogICAgfQogICAgc3VtICs9IHdbc107CiAgICByZXR1cm4gc3VtOwp9CgppbnQgbWFpbigpIHsKICAgIGludCBuLCBrOwogICAgY2luID4+IG4gPj4gazsKCiAgICB2ZWN0b3I8bGw+IHcobisxKTsKICAgIGZvciAoaW50IGkgPSAxOyBpIDw9IG47IGkrKykKICAgICAgICBjaW4gPj4gd1tpXTsKCiAgICB2ZWN0b3I8dW5vcmRlcmVkX3NldDxpbnQ+PiB0cmVlKG4rMSk7CiAgICBpbnQgdSwgdjsKICAgIGZvciAoaW50IGkgPSAxOyBpIDw9IG4tMTsgaSsrKSB7CiAgICAgICAgY2luID4+IHUgPj4gdjsKICAgICAgICB0cmVlW3VdLmluc2VydCh2KTsKICAgICAgICB0cmVlW3ZdLmluc2VydCh1KTsKICAgIH0KCiAgICBsbCBsYXN0X3N1bSA9IExMT05HX01JTjsKCiAgICBmb3IgKGludCBpID0gMTsgaSA8PSBrOyBpKyspIHsKICAgICAgICBtYXA8cGFpcjxsbCwgbGw+LCBpbnQ+IGFycjsKICAgICAgICB1bm9yZGVyZWRfbWFwPGludCwgaW50PiBwYXJlbnQ7CgogICAgICAgIGxsIHN1bSA9IGRmcygxLCAtMSwgdywgdHJlZSwgcGFyZW50LCBhcnIpOwogICAgICAgIGxhc3Rfc3VtID0gbWF4KGxhc3Rfc3VtLCBzdW0pOwogICAgICAgIGlmIChhcnIuYmVnaW4oKS0+Zmlyc3QuZmlyc3QgPj0gMCkKICAgICAgICAgICAgYnJlYWs7CgogICAgICAgIGludCBzID0gYXJyLmJlZ2luKCktPnNlY29uZDsKICAgICAgICBpZiAocyA9PSAxKSB7CiAgICAgICAgICAgIGNvdXQgPDwgMCA8PCAiXG4iOwogICAgICAgICAgICByZXR1cm4gMDsKICAgICAgICB9CiAgICAgICAgdHJlZVtwYXJlbnRbc11dLmVyYXNlKHMpOwogICAgICAgIHRyZWVbc10uZXJhc2UocGFyZW50W3NdKTsKICAgIH0KCiAgICBjb3V0IDw8IG1heChsYXN0X3N1bSwgZGZzX3N1bSgxLCAtMSwgdywgdHJlZSkpIDw8ICJcbiI7Cn0=