#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
#define ll long long
#define N 5010
int read()
{
    int x=0,f=1;char c=getchar();
    while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
    while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar();
    return x*f;
}
int n,a[N],p[N],g[N],size[N],t;
ll sum[N],f[N][N];
struct data{int to,nxt;
}edge[N<<1];
void addedge(int x,int y){t++;edge[t].to=y,edge[t].nxt=p[x],p[x]=t;}
void dfs(int k,int from)
{
    sum[k]=a[k];
    for (int i=p[k];i;i=edge[i].nxt)
    if (edge[i].to!=from)
    {
        dfs(edge[i].to,k);
        sum[k]+=sum[edge[i].to];
    }
    if (a[k]>0)
    {
        g[k]=0;
        for (int i=p[k];i;i=edge[i].nxt)
        if (edge[i].to!=from)
        {
            int x=g[edge[i].to];
            for (int j=0;j<=n;j++)
            if (f[edge[i].to][j]<0) {x=min(x,j+1);break;}
            g[k]+=x;
        }
    }
    f[k][0]=a[k];
    size[k]=1;
    for (int i=p[k];i;i=edge[i].nxt)
    if (edge[i].to!=from)
    {
        for (int j=size[k]+size[edge[i].to];j>=0;j--)
        {
            f[k][j]+=sum[edge[i].to];if (j-g[edge[i].to]-1>=0) f[k][j]=min(f[k][j],f[k][j-g[edge[i].to]-1]);
            for (int x=max(0,j-size[k]);x<=min(j,size[edge[i].to]);x++)
            {
                if (x) f[k][j]=min(f[k][j],f[k][j-x]+f[edge[i].to][x]);
                if (x<j&&f[edge[i].to][x]<0) f[k][j]=min(f[k][j],f[k][j-x-1]);
            }
        }
        size[k]+=size[edge[i].to];
    }
}
int main()
{
    /*freopen("e.in","r",stdin);
    freopen("e.out","w",stdout);*/
    n=read();
    for (int i=1;i<=n;i++) a[i]=read();
    for (int i=1;i<n;i++)
    {
        int x=read(),y=read();
        addedge(x,y),addedge(y,x);
    }
    memset(g,42,sizeof(g));
    memset(f,42,sizeof(f));
    dfs(1,1);
    /*for (int i=1;i<=n;i++) cout<<a[i]<<' ';cout<<endl;
    for (int i=1;i<=n;i++) cout<<g[i]<<' ';cout<<endl;
    cout<<endl;
    for (int i=1;i<=n;i++)
    {
        for (int j=0;j<n;j++)
        cout<<f[i][j]<<' ';
        cout<<endl;;
    }
    for (int i=1;i<=n;i++) cout<<sum[i]<<' ';cout<<endl;*/
    int ans=g[1];
    for (int i=0;i<=n;i++) 
    if (f[1][i]<0) {ans=min(ans,i);break;}
    cout<<ans;
    return 0;
}