#include <bits/stdc++.h>

using namespace std;

#define REP(i,n) for(ll (i) = (0);(i) < (n);++i)
#define REV(i,n) for(ll (i) = (n) - 1;(i) >= 0;--i)
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v,n) {REP(WW,n)cerr << v[WW] << ' ';cerr << endl << endl;}
#define SHOW2d(v,WW,HH) {REP(W_,WW){REP(H_,HH)cerr << v[W_][H_] << ' ';cerr << endl;}cerr << endl;}
#define ALL(v) v.begin(),v.end()
#define Decimal fixed<<setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000LL
#define MOD 1000000007

typedef long long ll;
typedef pair<ll,ll> P;

ll n;
ll mp[5555];
vector<vector<int>> v(5555);
ll dp1[5555][5555];
ll dp2[5555][5555];
ll tmp1[5555];
ll tmp2[5555];
ll subNum[5555];

void dfs(int node, int pre){
	subNum[node] = 1;
	
	if(mp[node] > 0)dp1[node][0] = mp[node];
	dp2[node][0] = mp[node];
	
	REP(i,v[node].size())if(v[node][i] != pre){
		int now = v[node][i];
		dfs(now, node);
		
		REP(j,subNum[node] + subNum[now] + 1){
			tmp1[j] = LLINF;
			tmp2[j] = LLINF;
		}
		REP(j,subNum[node]){
			REP(k,subNum[now]){
				if(dp1[now][k] != LLINF || dp2[now][k] < 0){
					tmp1[j+k+1] = min(tmp1[j+k+1],dp1[node][j]);
					tmp2[j+k+1] = min(tmp2[j+k+1],dp2[node][j]);
				}
				
				tmp1[j+k] = min(tmp1[j+k],dp1[node][j] + dp1[now][k]);
				tmp2[j+k] = min(tmp2[j+k],dp1[node][j] + dp2[now][k]);
				tmp2[j+k] = min(tmp2[j+k],dp2[node][j] + dp1[now][k]);
				tmp2[j+k] = min(tmp2[j+k],dp2[node][j] + dp2[now][k]);
			}
		}
		
		subNum[node] += subNum[now];
		REP(j,subNum[node]){
			dp1[node][j] = tmp1[j];
			dp2[node][j] = tmp2[j];
		}
	}
}

int main(){
	
	REP(i,5555){
		REP(j,5555){
			dp1[i][j] = LLINF;
			dp2[i][j] = LLINF;
		}
	}	
	
	cin >> n;
	REP(i,n)cin >> mp[i];
	REP(i,n-1){
		int a, b;cin >> a >> b;
		a--;b--;
		v[a].PB(b);
		v[b].PB(a);
	}
	
	dfs(0, -1);
	
	for(int i = 0;i <= n;i++){
		if(dp1[0][i] != LLINF){
			cout << i << endl;
			return 0;
		}
		if(dp2[0][i] < 0){
			cout << i << endl;
			return 0;
		}
	}

	return 0;
}
