#include <bits/stdc++.h>
using namespace std;
#define DEBUG_MODE
#define endl '\n'
#ifdef DEBUG_MODE
#define DEBUG(X) debug_func(X, #X)
#define DEBUG_ENDL endl
#define DEBUG_SEPARATOR_LINE cout<<"=================\n"
#else
#define DEBUG(X) 0
#define DEBUG_ENDL 0
#define DEBUG_SEPARATOR_LINE 0
#endif
#define ALL(V) (V).begin(), (V).end()
#define ALLR(V) (V).rbegin(), (V).rend()
#define DEBUG_ENDL_S(S) ((S).size() ? "\n" : "")
template <typename T> using V = vector<T>;
template <typename T> using VV = V<V<T>>;
template <typename T, typename U> using P = pair<T, U>;
using ll = int64_t;
using PLL = P<ll, ll>;
template <typename T> const T& var_min(const T &t) { return t; }
template <typename T> const T& var_max(const T &t) { return t; }
template <typename Head, typename... Tail> const Head& var_min(const Head &head, const Tail&... tail) { return min(head, var_min(tail...)); }
template <typename Head, typename... Tail> const Head& var_max(const Head &head, const Tail&... tail) { return max(head, var_max(tail...)); }
template <typename T, typename... Tail> void chmin(T &t, const Tail&... tail) { t = var_min(t, tail...); }
template <typename T, typename... Tail> void chmax(T &t, const Tail&... tail) { t = var_max(t, tail...); }
void debug_func_preffix(const string &s) { if(s.size()) cout << s << " = "; }
template <typename T>
void debug_func(const T &t, const string &s = "") {
    debug_func_preffix(s);
    cout << t << DEBUG_ENDL_S(s);
}
template <typename T, typename U>
void debug_func(const P<T, U> &p, const string &s = "") {
    debug_func_preffix(s);
    cout << "(";
    debug_func(p.first);
    cout << ", ";
    debug_func(p.second);
    cout << ")" << DEBUG_ENDL_S(s);
}
template <typename T>
void debug_func(const V<T> &v, const string &s = "") {
    for(ll i = 0; i < 10; i++) {
        string t = s + "[" + to_string(i) + "]";
        debug_func(v[i], t);
    }
}

const ll INF = 5e15;

V<ll> dp1(5010, INF);
VV<ll> dp2(5010, V<ll>(5010, INF));
V<ll> A;
VV<ll> edges(5010);

ll min_cut(ll node) {
    ll ret = INF;
    for(ll i = 0; dp2[node][i] != INF; i++) if(dp2[node][i] < 0) chmin(ret, i);
    return ret;
}

ll dfs(ll now, ll pre) {
    ll ret = 0;
    dp2[now][0] = 0;
    if(A[now] > 0) dp1[now] = 0;
    for(ll nxt : edges[now]) {
        if(nxt == pre) continue;
        ret += dfs(nxt, now) + 1;
        if(A[now] > 0) {
            dp1[now] += min<ll>(dp1[nxt], min_cut(nxt) + 1);
            chmin(dp1[now], INF);
        }
        V<ll> dp2sub(ret + 10, INF);
        dp2sub[ret] = 0;
        for(ll i = 0; dp2[now][i] != INF; i++) {
            ll mcut = min_cut(nxt);
            chmin(mcut, dp1[nxt]);
            if(mcut < INF) chmin(dp2sub[i + mcut + 1], dp2[now][i]);
            for(ll j = 0; dp2[nxt][j] != INF; j++) chmin(dp2sub[i + j], dp2[now][i] + dp2[nxt][j]);
        }
        dp2[now] = dp2sub;
    }
    if(ret == 0) dp2[now] = { A[now], INF };
    else for(ll &e : dp2[now]) e += A[now];
    return ret;
}

int main() {
    ll N;
    cin >> N;
    A = V<ll>(N);
    for(ll &e : A) cin >> e;
    for(ll i = 1; i < N; i++) {
        ll u, v;
        cin >> u >> v;
        u--;
        v--;
        edges[u].push_back(v);
        edges[v].push_back(u);
    }
    dfs(0, -1);
    ll ans = dp1[0];
    for(ll i = 0; i < 5010; i++) if(dp2[0][i] < 0) chmin(ans, i);
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 197888