// ~~ icebear love attttt ~~
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
template<class T>
bool minimize(T &a, const T &b) {
if (a > b) return a = b, true;
return false;
}
template<class T>
bool maximize(T &a, const T &b) {
if (a < b) return a = b, true;
return false;
}
#define FOR(i,a,b) for(int i=(a); i<=(b); ++i)
#define FORR(i,a,b) for(int i=(a); i>=(b); --i)
#define REP(i, n) for(int i=0; i<(n); ++i)
#define RED(i, n) for(int i=(n)-1; i>=0; --i)
#define MASK(i) (1LL << (i))
#define BIT(S, i) (((S) >> (i)) & 1)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define task "icebearat"
const int MOD = 1e9 + 7;
const int inf = 1e9 + 27092008;
const ll INF = 1e18 + 27092008;
const int N = 3e5 + 5;
int n, k, cnt;
bool bom[N], have[N];
vector<int> G[N], adj[N];
/**
f[u] : min dist to a bom in subtree u
g[u] : max dist to a node which is parent of a bom in subtree u
*/
void dfs(int u, int par) {
for(int v : G[u]) if (v != par) {
dfs(v, u);
if (have[v]) {
adj[u].pb(v);
adj[v].pb(u);
}
have[u] |= have[v];
}
}
ii DFS(int u, int par, const int limit) {
int f = inf, g = -inf;
for(int v : adj[u]) if (v != par) {
ii tmp = DFS(v, u, limit);
minimize(f, tmp.fi + 1);
maximize(g, tmp.se + 1);
}
if (cnt > k) return mp(0, 0);
if (bom[u] && f > limit) // now calculate, dist = 2 * limit
maximize(g, 0);
if (f + g <= limit) // a node choose at f[u] is good enough
g = -inf;
if (g == limit) {
cnt++;
f = 0;
g = -inf;
}
return mp(f, g);
}
bool check(int limit) {
cnt = 0;
int g = DFS(1, -1, limit).se;
if (g >= 0) cnt++;
return (cnt <= k);
}
void init(void) {
cin >> n >> k;
FOR(i, 1, n) {
cin >> bom[i];
have[i] = bom[i];
}
FOR(i, 2, n) {
int u, v;
cin >> u >> v;
G[u].pb(v);
G[v].pb(u);
}
}
void process(void) {
dfs(1, -1);
int low = 0, high = n - 1, res = n;
while(low <= high) {
int mid = (low + high) >> 1;
if (check(mid)) res = mid, high = mid - 1;
else low = mid + 1;
}
cout << res;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
if (fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
int tc = 1;
// cin >> tc;
while(tc--) {
init();
process();
}
return 0;
}
Ly8gfn4gaWNlYmVhciBsb3ZlIGF0dHR0dCB+fgojaW5jbHVkZSA8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCnR5cGVkZWYgbG9uZyBsb25nIGxsOwp0eXBlZGVmIHBhaXI8aW50LCBpbnQ+IGlpOwp0eXBlZGVmIHBhaXI8aW50LCBpaT4gaWlpOwoKdGVtcGxhdGU8Y2xhc3MgVD4KICAgIGJvb2wgbWluaW1pemUoVCAmYSwgY29uc3QgVCAmYikgewogICAgICAgIGlmIChhID4gYikgcmV0dXJuIGEgPSBiLCB0cnVlOwogICAgICAgIHJldHVybiBmYWxzZTsKICAgIH0KCnRlbXBsYXRlPGNsYXNzIFQ+CiAgICBib29sIG1heGltaXplKFQgJmEsIGNvbnN0IFQgJmIpIHsKICAgICAgICBpZiAoYSA8IGIpIHJldHVybiBhID0gYiwgdHJ1ZTsKICAgICAgICByZXR1cm4gZmFsc2U7CiAgICB9CgojZGVmaW5lIEZPUihpLGEsYikgZm9yKGludCBpPShhKTsgaTw9KGIpOyArK2kpCiNkZWZpbmUgRk9SUihpLGEsYikgZm9yKGludCBpPShhKTsgaT49KGIpOyAtLWkpCiNkZWZpbmUgUkVQKGksIG4pIGZvcihpbnQgaT0wOyBpPChuKTsgKytpKQojZGVmaW5lIFJFRChpLCBuKSBmb3IoaW50IGk9KG4pLTE7IGk+PTA7IC0taSkKI2RlZmluZSBNQVNLKGkpICgxTEwgPDwgKGkpKQojZGVmaW5lIEJJVChTLCBpKSAoKChTKSA+PiAoaSkp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ywgdG1wLnNlICsgMSk7CiAgICB9CiAgICBpZiAoY250ID4gaykgcmV0dXJuIG1wKDAsIDApOwogICAgaWYgKGJvbVt1XSAmJiBmID4gbGltaXQpIC8vIG5vdyBjYWxjdWxhdGUsIGRpc3QgPSAyICogbGltaXQKICAgICAgICBtYXhpbWl6ZShnLCAwKTsKICAgIGlmIChmICsgZyA8PSBsaW1pdCkgLy8gYSBub2RlIGNob29zZSBhdCBmW3VdIGlzIGdvb2QgZW5vdWdoCiAgICAgICAgZyA9IC1pbmY7CiAgICBpZiAoZyA9PSBsaW1pdCkgewogICAgICAgIGNudCsrOwogICAgICAgIGYgPSAwOwogICAgICAgIGcgPSAtaW5mOwogICAgfQogICAgcmV0dXJuIG1wKGYsIGcpOwp9Cgpib29sIGNoZWNrKGludCBsaW1pdCkgewogICAgY250ID0gMDsKICAgIGludCBnID0gREZTKDEsIC0xLCBsaW1pdCkuc2U7CiAgICBpZiAoZyA+PSAwKSBjbnQrKzsKICAgIHJldHVybiAoY250IDw9IGspOwp9Cgp2b2lkIGluaXQodm9pZCkgewogICAgY2luID4+IG4gPj4gazsKICAgIEZPUihpLCAxLCBuKSB7CiAgICAgICAgY2luID4+IGJvbVtpXTsKICAgICAgICBoYXZlW2ldID0gYm9tW2ldOwogICAgfQogICAgRk9SKGksIDIsIG4pIHsKICAgICAgICBpbnQgdSwgdjsKICAgICAgICBjaW4gPj4gdSA+PiB2OwogICAgICAgIEdbdV0ucGIodik7CiAgICAgICAgR1t2XS5wYih1KTsKICAgIH0KfQoKdm9pZCBwcm9jZXNzKHZvaWQpIHsKICAgIGRmcygxLCAtMSk7CiAgICBpbnQgbG93ID0gMCwgaGlnaCA9IG4gLSAxLCByZXMgPSBuOwogICAgd2hpbGUobG93IDw9IGhpZ2gpIHsKICAgICAgICBpbnQgbWlkID0gKGxvdyArIGhpZ2gpID4+IDE7CiAgICAgICAgaWYgKGNoZWNrKG1pZCkpIHJlcyA9IG1pZCwgaGlnaCA9IG1pZCAtIDE7CiAgICAgICAgZWxzZSBsb3cgPSBtaWQgKyAxOwogICAgfQogICAgY291dCA8PCByZXM7Cn0KCmludCBtYWluKCkgewogICAgaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbygwKTsKICAgIGNpbi50aWUoMCk7IGNvdXQudGllKDApOwogICAgaWYgKGZvcGVuKHRhc2siLmlucCIsICJyIikpIHsKICAgICAgICBmcmVvcGVuKHRhc2siLmlucCIsICJyIiwgc3RkaW4pOwogICAgICAgIGZyZW9wZW4odGFzayIub3V0IiwgInciLCBzdGRvdXQpOwogICAgfQogICAgaW50IHRjID0gMTsKLy8gICAgY2luID4+IHRjOwogICAgd2hpbGUodGMtLSkgewogICAgICAgIGluaXQoKTsKICAgICAgICBwcm9jZXNzKCk7CiAgICB9CiAgICByZXR1cm4gMDsKfQoK