#include<bits/stdc++.h>
using namespace std;


#define li          long long int
#define rep(i,to)   for(li i=0;i<((li)(to));i++)
#define repp(i,start,to)    for(li i=(li)(start);i<((li)(to));i++)
#define pb          push_back
#define sz(v)       ((li)(v).size())
#define bgn(v)      ((v).begin())
#define eend(v)     ((v).end())
#define allof(v)    (v).begin(), (v).end()
#define dodp(v,n)       memset(v,(li)n,sizeof(v))
#define bit(n)      (1ll<<(li)(n))
#define mp(a,b)     make_pair(a,b)
#define rin rep(i,n)
#define EPS 1e-12
#define ETOL 1e-8
#define MOD 1000000007
typedef pair<li, li> PI;

#define INF bit(60)

#define DBGP 1


#define idp if(DBGP)
#define F first
#define S second
#define p2(a,b)     idp cout<<a<<"\t"<<b<<endl
#define p3(a,b,c)       idp cout<<a<<"\t"<<b<<"\t"<<c<<endl
#define p4(a,b,c,d)     idp cout<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<endl
#define p5(a,b,c,d,e)       idp cout<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<"\t"<<e<<endl
#define p6(a,b,c,d,e,f)     idp cout<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<"\t"<<e<<"\t"<<f<<endl
#define p7(a,b,c,d,e,f,g)       idp cout<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<"\t"<<e<<"\t"<<f<<"\t"<<g<<endl
#define p8(a,b,c,d,e,f,g,h)     idp cout<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<"\t"<<e<<"\t"<<f<<"\t"<<g<<"\t"<<h<<endl
#define p9(a,b,c,d,e,f,g,h,i)       idp cout<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<"\t"<<e<<"\t"<<f<<"\t"<<g<<"\t"<<h<<"\t"<<i<<endl
#define p10(a,b,c,d,e,f,g,h,i,j)        idp cout<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<"\t"<<e<<"\t"<<f<<"\t"<<g<<"\t"<<h<<"\t"<<i<<"\t"<<j<<endl
#define foreach(it,v)   for(__typeof((v).begin()) it=(v).begin(); it!=(v).end(); ++it)
#define p2p(x)      idp p2((x).F, (x).S)
#define dump(x,n)   idp{rep(i,n){cout<<x[i]<<" ";}puts("");}
#define dump2(x,n)  idp{rep(i,n){cout<<"["<<x[i].F<<" , "<<x[i].S<<"] ";}puts("");}
#define dumpi(x)    idp{foreach(it, x){cout<<(*it)<<" ";}puts("");}
#define dumpi2(x)   idp{foreach(it, x){cout<<"["<<(it)->F<<" , "<<(it)->S<<"] ";}puts("");}

#define read2d(a,w,h)   rep(i,h)rep(j,w)cin>>a[i][j]
#define dump2d(a,w,h)   rep(i,h){rep(j,w)cout<<a[i][j]<<" ";puts("");}

typedef pair<li, li> PI;


// dp1[i][j] : i以下の部分木をj個に分け、iが入るパーツが全部正な場合の最小値
vector<li> dp1[5050];
// dp2[i][j] : i以下の部分木をj個に分け、iが入るパーツの最小値
vector<li> dp2[5050];

li n;
vector<li> edge[5050];
li a[5050];

inline vector<li> merge1(const vector<li>& parent,
                         const vector<li>& child,
                         const vector<li>& child2) {
    li num1 = sz(parent) - 1;
    li num2 = sz(child) - 1;
    vector<li> res(num1 + num2 + 1, INF);
    repp(i, 1, sz(parent)) {
        repp(j, 1, sz(child)) {
            if (parent[i] == INF) {
                continue;
            }
            // merge
            res[i + j - 1] = min(res[i + j - 1], parent[i] + child[j]);
            // not merge
            if (child2[j] < 0 || child[j] != INF) {
                res[i + j] = min(res[i + j], parent[i]);
            }
        }
    }
    return res;
}

inline vector<li> merge2(const vector<li>& parent,
                         const vector<li>& child1,
                         const vector<li>& child) {
    li num1 = sz(parent) - 1;
    li num2 = sz(child) - 1;
    vector<li> res(num1 + num2 + 1, INF);
    repp(i, 1, sz(parent)) {
        repp(j, 1, sz(child)) {
            // merge
            res[i + j - 1] = min(res[i + j - 1], parent[i] + child[j]);
            // not merge
            if (child[j] < 0 || child1[j] != INF) {
                res[i + j] = min(res[i + j], parent[i]);
            }
        }
    }
    return res;
}

inline void calc(li now, li from) {
    li cn = sz(edge[now]);
    if (from >= 0) {
        cn--;
    }
    li i = 0;
    vector<vector<li>> dpp1(1);
    vector<vector<li>> dpp2(1);
    dpp1[0].pb(INF);
    dpp1[0].pb(a[now] > 0 ? a[now] : INF);
    dpp2[0].pb(INF);
    dpp2[0].pb(a[now]);
    for (auto && child : edge[now]) {
        if (child == from) {
            continue;
        }
        calc(child, now);
        dpp1.pb(merge1(dpp1.back(), dp1[child], dp2[child]));
        dpp2.pb(merge2(dpp2.back(), dp1[child], dp2[child]));

        i++;
    }
    dp1[now] = dpp1.back();
    dp2[now] = dpp2.back();

    //cout << "-------------------- " << now << endl;
    //dumpi(dp1[now]);
    //dumpi(dp2[now]);
}

int main() {
    cin >> n;
    rin{
        cin >> a[i];
    }
    rep(i, n - 1) {
        li u, v;
        cin >> u >> v;
        u--;
        v--;
        edge[u].pb(v);
        edge[v].pb(u);
    }

    calc(0, -1);
    li res = INF;
    rep(i, sz(dp1[0])) {
        if (dp1[0][i] != INF) {
            res = min(res, i - 1);
        }
        if (dp2[0][i] < 0) {
            res = min(res, i - 1);
        }
    }
    cout << res << endl;

    return 0;
}

Measured memory_bytes: 196992