#include <bits/stdc++.h>
using namespace std;using ll=long long;using uint=unsigned int;using pii=pair<int,int>;using pll=pair<ll,ll>;using ull = unsigned long long;using ld=long double;template<typename T>void _(const char*s,T h){cerr<<s<<" = "<<h<<"\n";}template<typename T,typename...Ts>void _(const char*s,T h,Ts...t){int b=0;while(((b+=*s=='(')-=*s==')')!=0||*s!=',')cerr<<*s++;cerr<<" = "<<h<<",";_(s+1,t...);}// break continue pop_back 998244353
#define int ll
#define pii pll
#define f first
#define s second
#define pb emplace_back
#define forn(i,n) for(int i=0;i<(n);++i)
#define sz(a)((int)(a).size())
#define sqr(x) ((x)*(x))
struct init{init(){cin.tie(0);iostream::sync_with_stdio(0);cout<<fixed<<setprecision(10);cerr<<fixed<<setprecision(5);}~init(){
#ifdef LOCAL
#define dbg(...) _(#__VA_ARGS__,__VA_ARGS__)
cerr<<"Time elapsed: "<<(double)clock()/CLOCKS_PER_SEC<<"s.\n";
#else
#define dbg(...) 
#endif
}}init;template<typename T,typename U>void upx(T&x,U y){if(x<y)x=y;}template<typename T,typename U>void upn(T&x,U y){if(x>y)x=y;}mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());const int D=4,dx[]={+1,0,-1,0},dy[]={0,+1,0,-1};
const int N=5001,OO=1e18;
int dp[N][N],ba[N][N];
int a[N];
vector<int> g[N];
int sz[N],dv[N],du[N],dt[N];
void dfs(int v,int p=0){
    sz[v]=1;
    for(int to:g[v]){
        if(to^p){
            dfs(to,v);
            sz[v]+=sz[to];
        }
    }
    {
        int maxv=0;
        forn(i,N)dv[i]=OO;
        dv[0]=0;
        for(int to:g[v]){
            if(to^p){
                int maxto=sz[to];
                forn(i,maxv+maxto+2)dt[i]=OO;
                forn(i,maxto+1){
                    du[i]=dp[to][i];
                    if(i&&du[i-1]<0)upn(du[i],0);
                    if(i&&a[to]>0&&ba[to][i-1])upn(du[i],0);
                }
                forn(i,maxv+1){
                    forn(j,maxto+1){
                        upn(dt[i+j],dv[i]+du[j]);
                    }
                }
                forn(i,maxv+maxto+2)dv[i]=dt[i];
                maxv+=maxto;
            }
        }
        forn(i,sz[v])dp[v][i]=dv[i]+a[v];
    }
    if(a[v]>0){
        int maxv=0;
        forn(i,N)dv[i]=0;
        dv[0]=1;
        for(int to:g[v]){
            if(to^p){
                int maxto=sz[to];
                forn(i,maxv+maxto+2)dt[i]=0;
                forn(i,maxto+1){
                    if(a[to]>0){
                        du[i]=ba[to][i];
                        if(i)du[i]|=ba[to][i-1];
                        if(i)du[i]|=dp[to][i-1]<0;
                    }else{
                        du[i]=0;
                        if(i)du[i]=dp[to][i-1]<0;
                    }
                }
                forn(i,maxv+1){
                    forn(j,maxto+1){
                        dt[i+j]|=dv[i]&du[j];
                    }
                }
                forn(i,maxv+maxto+2)dv[i]=dt[i];
                maxv+=maxto;
            }
        }
        forn(i,sz[v])ba[v][i]=dv[i];
    }
}
int32_t main(){
    int n;
    cin>>n;
    for(int i=1;i<=n;++i)cin>>a[i];
    forn(i,n-1){
        int x,y;
        cin>>x>>y;
        g[x].pb(y);
        g[y].pb(x);
    }
    dfs(1);
    int ans=N;
    forn(i,n){
        if(a[1]>0&&ba[1][i]){
            ans=i;
            break;
        }
        if(dp[1][i]<0){
            ans=i;
            break;
        }
    }
    cout<<ans<<'\n';
    return 0;
}


Measured memory_bytes: 156940