#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(),a.end()
using namespace std;
typedef long long int ll;
typedef pair<int,int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
#define dame { puts("-1"); return 0;}
#define yn {puts("Yes");}else{puts("No");}
#define MAX_N 6005

/*
    Attack to a Tree
*/


vector<int> pre_G[MAX_N];
vector<int> G[MAX_N];
int par_[MAX_N];
int flag_netsukigi[MAX_N];
void make_netsukigi(int r){
    rep(i,MAX_N){
        G[i].clear();
        par_[i] = 0;
        flag_netsukigi[i] = 0;
    }
    queue<int> que;
    que.push(r);
    flag_netsukigi[r] = 1;
    par_[r] = -1;

    while(que.size() > 0){
        int x = que.front();
        que.pop();
        flag_netsukigi[x] = 1;
        rep(i,pre_G[x].size()){
            if(flag_netsukigi[pre_G[x][i]] == 0){
                par_[pre_G[x][i]] = x;
                G[x].push_back(pre_G[x][i]);
                que.push(pre_G[x][i]);
            }
        }
    }
}

int n;
ll a[MAX_N];
ll u[MAX_N], v[MAX_N];
vector<int> son[MAX_N];
ll dp[MAX_N][MAX_N][2];
const ll inf = 1001001001001001001;
int cnt_son[MAX_N];

int main() {
    cin >> n;
    rep(i,n)cin >> a[i];
    rep(i,n-1){
        cin >> u[i] >> v[i];
        u[i]--; v[i]--;
        pre_G[u[i]].push_back(v[i]);
        pre_G[v[i]].push_back(u[i]);
    }

    make_netsukigi(0);

    rep(i,n){
        rep(j,n){
            rep(k,2){
                dp[i][j][k] = inf;
            }
        }
    }

    queue<int> que;
    rep(i,n){
        if(G[i].empty()){
            que.push(i);
            cnt_son[i] = 1;
            if(a[i] < 0){
                dp[i][0][1] = a[i];
            }else{
                dp[i][0][0] = a[i];
            }
        }
    }

    while(que.size() > 0){
        int x = que.front();
        que.pop();
        int y = par_[x];
        if(y == -1)break;
        son[y].push_back(x);
        if(son[y].size() == G[y].size()){
            que.push(y);
            cnt_son[y] = 1;
            // DP計算
            rep(i,son[y].size()){
                int z = son[y][i];
                ll dp2[n][2];
                rep(j,n)rep(k,2)dp2[j][k] = inf;
                if(i == 0){
                    if(a[y] < 0){
                        // 切らない
                        rep(j,cnt_son[z]+1){
                            dp2[j][1] = min(dp2[j][1], dp[z][j][0] + a[y]);
                            dp2[j][1] = min(dp2[j][1], dp[z][j][1] + a[y]);
                        }
                        // 切る
                        srep(j,1,cnt_son[z]+1){
                            if(dp[z][j-1][0] < inf || dp[z][j-1][1] < 0) dp2[j][1] = min(dp2[j][1], a[y]);
                        }
                    }else{
                        // 切らない
                        rep(j,cnt_son[z]+1){
                            dp2[j][0] = min(dp2[j][0], dp[z][j][0] + a[y]);
                            dp2[j][1] = min(dp2[j][1], dp[z][j][1] + a[y]);
                        }
                        // 切る
                        srep(j,1,cnt_son[z]+1){
                            if(dp[z][j-1][0] < inf || dp[z][j-1][1] < 0) dp2[j][0] = min(dp2[j][0], a[y]);
                        }
                    }
                }else{
                    // 切らない
                    rep(j,cnt_son[y]){
                        rep(k,cnt_son[z]){
                            if(j+k>=n)break;
                            dp2[j+k][0] = min(dp2[j+k][0], dp[y][j][0] + dp[z][k][0]);
                            dp2[j+k][1] = min(min(dp2[j+k][1], dp[y][j][1] + dp[z][k][1]), min(dp[y][j][1] + dp[z][k][0], dp[y][j][0] + dp[z][k][1]));
                        }
                    }
                    // 切る
                    rep(j,cnt_son[y]){
                        rep(k,cnt_son[z]){
                            if(j+k+1>=n)break;
                            if(dp[z][k][0] < inf || dp[z][k][1] < 0){
                                dp2[j+k+1][0] = min(dp2[j+k+1][0], dp[y][j][0]);
                                dp2[j+k+1][1] = min(dp2[j+k+1][1], dp[y][j][1]);
                            }
                        }
                    }
                }
                rep(j,n)rep(k,2)dp[y][j][k] = dp2[j][k];
                cnt_son[y] += cnt_son[z];
            }
        }
    }

    int ans = 1001001;
    rep(j,n){
        if(dp[0][j][1] < 0){
            ans = min(ans, j);
        }
        if(dp[0][j][0] < inf){
            ans = min(ans, j);
        }
    }

    cout << ans << endl;
    return 0;
}
 
 


Measured memory_bytes: 415328