#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, int> PII;
const int N = 200010;
struct edge {
int nxt, to;
} e[N << 1];
int n, k, cnt = 1, tot;
ll ans;
int head[N], dfn[N], low[N], fa[N], mp[N];
ll v[N], w[N], tag[N << 2];
PII tree[N << 2];
void link(int u, int v)
{
e[++cnt].nxt = head[u];
head[u] = cnt;
e[cnt].to = v;
}
namespace seg
{
void pushdown(int x)
{
if(!tag[x]) return;
tree[x << 1].first += tag[x]; tree[x << 1 | 1].first += tag[x];
tag[x << 1] += tag[x]; tag[x << 1 | 1] += tag[x];
tag[x] = 0;
}
void build(int l, int r, int x)
{
if(l == r) { tree[x].first = w[mp[l]]; tree[x].second = mp[l]; return; }
int mid = (l + r) >> 1;
build(l, mid, x << 1); build(mid + 1, r, x << 1 | 1);
if(tree[x << 1].first > tree[x << 1 | 1].first) tree[x] = tree[x << 1];
else tree[x] = tree[x << 1 | 1];
}
void update(int l, int r, int x, int a, int b, int num)
{
if(l > b || r < a) return;
if(l >= a && r <= b)
{
tree[x].first += num; tag[x] += num; return;
}
pushdown(x);
int mid = (l + r) >> 1;
update(l, mid, x << 1, a, b, num);
update(mid + 1, r, x << 1 | 1, a, b, num);
if(tree[x << 1].first > tree[x << 1 | 1].first) tree[x] = tree[x << 1];
else tree[x] = tree[x << 1 | 1];
}
} using namespace seg;
void dfs(int u, int last, ll sum)
{
dfn[u] = ++tot; mp[tot] = u; w[u] = sum;
for(int i = head[u]; i; i = e[i].nxt) if(e[i].to != last)
{
fa[e[i].to] = u;
dfs(e[i].to, u, sum + v[e[i].to]);
}
low[u] = tot;
}
void change(int pos)
{
while(v[pos])
{
update(1, n, 1, dfn[pos], low[pos], -v[pos]);
v[pos] = 0; pos = fa[pos];
}
}
int main()
{
scanf("%d%d", &n, &k);
for(int i = 1; i <= n; ++i) scanf("%lld", &v[i]);
for(int i = 1; i < n; ++i)
{
int u, v; scanf("%d%d", &u, &v);
link(u, v); link(v, u);
}
ans = v[1];
v[1] = 0;
dfs(1, 0, 0);
build(1, n, 1);
while(k--)
{
PII x = tree[1];
ans += x.first;
change(x.second);
}
printf("%lld\n", ans);
return 0;
}