# include <iostream>
# include <algorithm>
#include <array>
# include <cassert>
#include <cctype>
#include <climits>
#include <numeric>
# include <vector>
# include <string>
# include <set>
# include <map>
# include <cmath>
# include <iomanip>
# include <functional>
# include <tuple>
# include <utility>
# include <stack>
# include <queue>
# include <list>
# include <bitset>
# include <complex>
# include <chrono>
# include <random>
# include <limits.h>
# include <unordered_map>
# include <unordered_set>
# include <deque>
# include <cstdio>
# include <cstring>
#include <stdio.h>
#include<time.h>
#include <stdlib.h>
#include <cstdint>
#include <cfenv>
#include<fstream>
//#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
long long MOD = 1000000000 + 7;//1000000000 + 7 998244353 924844033 1000000000 + 9;
constexpr long long INF = 1LL << 60;//numeric_limits<LL>::max();
const double PI = acos(-1);
#define fir first
#define sec second
#define thi third
#define debug(x) cerr<<#x<<": "<<x<<'\n'
typedef pair<LL, LL> Pll;
typedef pair<double, double> Dll;
typedef pair<LL, pair<LL, LL>> Ppll;
typedef pair<LL, pair<LL, bitset<100001>>> Pbll;
typedef pair<LL, pair<LL, vector<LL>>> Pvll;
typedef pair<LL, LL> Vec2;
struct Tll { LL first, second, third; };
struct Fll { LL first, second, third, fourth; };
typedef pair<LL, Tll> Ptll;
#define rep(i,rept) for(LL i=0;i<rept;i++)
#define Rrep(i,mf) for(LL i=mf-1;i>=0;i--)
void YN(bool f) {
	if (f)
		cout << "YES" << endl;
	else
		cout << "NO" << endl;
}
void yn(bool f) {
	if (f)
		cout << "Yes" << endl;
	else
		cout << "No" << endl;
}
struct Edge { LL to, cost; };
struct edge {
	LL from, to, cost;
};
vector<vector<LL>>g;
vector<edge>ed;
vector<Pll>pv;
set<LL>st;
map<Pll, LL>ma;
int di[4][2] = { { 0,1 },{ 1,0 },{ 0,-1 },{ -1,0 } };
string str, ss;
bool f;
LL n, m, s, t, h, w, a[6010],sub[6010];
LL dp1[6010][6010], dp2[6010][6010], to1[6010], to2[6010];
//dp1[i][j]:頂点i,j回切ったときのiに含まれる木の合計の最小(正のみ)
//dp2[i][j]:頂点i,j回切ったときのiに含まれる木の合計の最小(正負ともに)
//二乗の木dp
void dfs(int cur, int par) {
	sub[cur] = 1;
	rep(i, 6000)dp1[cur][i] = INF, dp2[cur][i] = INF;
	if (a[cur] > 0)
		dp1[cur][0] = a[cur];
	else
		dp2[cur][0] = a[cur];
	rep(i, g[cur].size()) {
		LL v = g[cur][i];
		if (v == par)continue;
		dfs(v, cur);
		
		rep(i, 6000)to1[i] = INF, to2[i] = INF;
		for (int i = 0; i < sub[cur]; i++) {
			for (int j = 0; j < sub[v]; j++) {
				//子が切れるとき
				if (dp2[v][j] < 0 || dp1[v][j] < INF) {
					to1[i + j + 1] = min(to1[i + j + 1], dp1[cur][i]);
					to2[i + j + 1] = min(to2[i + j + 1], dp2[cur][i]);
				}

				to1[i + j] = min(to1[i + j], dp1[cur][i] + dp1[v][j]);
				to2[i + j] = min({ to2[i + j], dp1[cur][i] + dp1[v][j] ,
					dp2[cur][i] + dp1[v][j] ,dp1[cur][i] + dp2[v][j],dp2[cur][i] + dp2[v][j] });
			}
		}
		rep(i, 6000)dp1[cur][i] = to1[i], dp2[cur][i] = to2[i];
		sub[cur] += sub[v];
	}
}
int main() {
	cin >> n;
	rep(i, n) {
		cin >> a[i];
	}
	g.resize(n);
	rep(i, n - 1) {
		LL x, y;
		cin >> x >> y;
		x--, y--;
		g[x].push_back(y);
		g[y].push_back(x);
	}
	dfs(0, -1);
	rep(i, n+1) {
		if (dp1[0][i] < INF) {
			cout << i << endl;
			return 0;
		}
		if (dp2[0][i] < 0) {
			cout << i << endl;
			return 0;
		}
	}
	return 0;
}


Measured memory_bytes: 474496