#include<vector>
#include<cmath>
#include<map>
#include<cstdlib>
#include<iostream>
#include<sstream>
#include<fstream>
#include<string>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<set>
#include<stack>
#include<bitset>
#include<functional>
#include<ctime>
#include<queue>
#include<deque>
#include<complex>
#include<cassert>
using namespace std;
#define pb push_back
#define pf push_front
typedef long long lint;
typedef complex<double> P;
#define mp make_pair
#define fi first
#define se second
typedef pair<int,int> pint;
#define All(s) s.begin(),s.end()
#define rAll(s) s.rbegin(),s.rend()
#define REP(i,a,b) for(int i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)
lint inf=1e15;
lint dp[2][5010][5010][2];
vector<int> gr[5010];
lint a[5010];
int n;
int dfs(int v,int u){
	int ch=0,now=0,ne=1;
	if(a[v]<0) dp[0][v][0][1]=a[v];else dp[0][v][0][0]=a[v];
	//cout<<v<<' '<<dp[0][v][0][0]<<' '<<dp[0][v][0][0]<<endl;
	rep(i,gr[v].size()){
		int w=gr[v][i];
		if(w==u) continue;
		rep(j,ch+3) dp[ne][v][j][0]=dp[ne][v][j][1]=inf;
		int c2=dfs(w,v),x=1-gr[w].size()%2;
		rep(j,ch+1) rep(k,c2+1){
			//切る
			if(dp[x][w][k][0]<inf/2 || dp[x][w][k][1]<0){
				//cout<<"hoge"<<endl;
				dp[ne][v][j+k+1][0]=min(dp[ne][v][j+k+1][0],dp[now][v][j][0]);
				dp[ne][v][j+k+1][1]=min(dp[ne][v][j+k+1][1],dp[now][v][j][1]);
			}
			//切らない
			rep(l,2) rep(m,2){
				//cout<<"huga"<<' '<<dp[now][v][j][l]<<' '<<dp[x][w][k][m]<<' '<<x<<w<<k<<m<<endl;
				dp[ne][v][j+k][(l|m)]=min(dp[ne][v][j+k][(l|m)], dp[now][v][j][l]+dp[x][w][k][m]);
			}
		}
		ch+=c2+1;
		rep(j,ch+1){
			//cout<<v<<' '<<j<<' '<<dp[ne][v][j][0]<<' '<<dp[ne][v][j][1]<<endl;
		}
		swap(now,ne);
	}
	return ch;
}
int main()
{
	int x,y,out=5010;
	cin>>n;
	rep(i,n) cin>>a[i];
	rep(i,n-1){
		cin>>x>>y;x--;y--;
		gr[x].pb(y);gr[y].pb(x);
	}
	rep(k,2) rep(i,5005) rep(j,5005) dp[k][i][j][0]=dp[k][i][j][1]=inf;
	dfs(0,-1);
	rep(i,n+1){
		if(dp[gr[0].size()%2][0][i][0]<inf/2 || dp[gr[0].size()%2][0][i][1]<0L) out=min(out,i);
	}
	cout<<out<<endl;
}
