#include <bits/stdc++.h>

using namespace std;

#define _for(i,j,N) for(int i = (j);i < (N);i++)
#define _rep(i,j,N) for(int i = (j);i <= (N);i++)
#define ALL(x) x.begin(),x.end()
#define pb push_back
#define LL long long
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

template<typename T>
ostream& operator<<(ostream& os,const vector<T>& v) {
    _for(i,0,(int)(v.size()-1)) os << v[i] << " ";
    os << v[v.size()-1];
    return os;
}

template<typename T>
ostream& operator<<(ostream& os,const set<T>& v){
    for(typename set<T>::iterator it = v.begin();it != v.end();it++)
    os << *it <<" ";
    return os;
}
const int maxn = 5005;
int N;
LL A[maxn];
vector<int> G[maxn];
LL res = 0;

struct Cmp{
    bool operator()(const int &a,const int &b)const{
        return A[a] > A[b];
    }
};

const LL INF = 1e16;
LL dp[maxn][maxn][2];
LL aux[maxn][2];
int child[maxn];

void dfs(int u,int p){
    for(int j : G[u]){
        if(j == p) continue;
        dfs(j,u);
    }
    if(A[u] > 0){
        dp[u][0][0] = A[u];
    }else dp[u][0][1] = A[u];

    _for(i,0,maxn) _for(j,0,2) aux[i][j] = INF;
    int sum = 1;
    child[u] = 1;

    for(int v: G[u]){
        if(v == p) continue;
        _for(i,0,sum){
            _for(j,0,child[v]){
                _for(k1,0,2) _for(k2,0,2){
                    //if(v == 5) cout <<"j:"<<j << "val:"<<dp[5][j][0] <<endl;
                    if(dp[v][j][k2] < 0 || (!k2 && dp[v][j][k2] < INF)){
                        chmin(aux[j+i+1][k1],dp[u][i][k1]);
                    }
                    chmin(aux[i+j][k1|k2],dp[u][i][k1] + dp[v][j][k2]);

                }
            }
        }
        child[u] += child[v];
        sum += child[v];

        _for(i,0,maxn) _for(j,0,2){
            dp[u][i][j] =aux[i][j];
            aux[i][j] = INF;
        }

    }
}

int main()
{
    cin >> N;
    _rep(i,1,N){
        cin >> A[i];
    }

    _for(i,0,N-1){
        int u,v;
        cin >> u >> v;
        G[u].push_back(v);
        G[v].push_back(u);
    }

    _for(i,0,maxn) _for(j,0,maxn) _for(k,0,2) dp[i][j][k] = INF;
    _for(i,0,maxn) _for(k,0,2) aux[i][k] = INF;

    dfs(1,-1);
    int ans = maxn;
    _for(i,0,maxn){
        if(dp[1][i][1] < 0) chmin(ans,i);
        //cout << dp[1][i][1] << endl;
    }
    _for(i,0,maxn){
        if(dp[1][i][0] < INF) chmin(ans,i);
        //cout << child[i] <<endl;
    }
    //cout << dp[4][2][1] << endl;

    cout << ans << endl;

    return 0;
}
