#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef long double ld;
typedef complex<ld> Point;
const ld eps = 1e-11;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
typedef pair<ld, ld> LDP;
typedef unsigned long long ul;
int n;
ll a[5000];
vector<int> G[5000];
bool used[5000];
vector<ll> x[5000];
int mi[5000];

void dfs(int id) {
	used[id] = true;
	x[id].push_back({ a[id] });
	if (a[id] > 0)mi[id] = 0;
	else mi[id] = 10000;
	rep(j, G[id].size()) {
		int to = G[id][j];
		if (used[to])continue;
		dfs(to);
		int len = x[to].size();
		int add = mi[to];
		vector<ll> nex; nex.resize(len + x[id].size(),INF);
		rep(k, len) {
			if (x[to][k] < 0) {
				add = min(add, k + 1);
				rep(l, x[id].size()) {
					nex[k+l + 1] = min(nex[k+l + 1], x[id][l]);
				}
			}
			rep(l, x[id].size()) {
				nex[k + l] = min(nex[k + l], x[to][k] + x[id][l]);
			}
		}
		if (mi[to] < 10000) {
			rep(l, x[id].size()) {
				nex[l + mi[to]+1] = min(nex[l + mi[to]+1], x[id][l]);
			}
		}
		x[id] = nex;
		mi[id] += add;
	}
}

int main() {
	cin >> n;
	rep(i, n)cin >> a[i];
	rep(i, n - 1) {
		int u, v; cin >> u >> v; u--; v--;
		G[u].push_back(v);
		G[v].push_back(u);
	}
	dfs(0);
	int ans = mi[0];
	rep(i, x[0].size()) {
		if (x[0][i] < 0) {
			ans = min(ans, i);
		}
	}
	cout << ans << endl;
	//stop
		return 0;
}
