#include <vector>
#include <list>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <queue>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <fstream>
#include <cstdio>
#include <complex>
#include <cstdint>
#include <tuple>

#define M_PI       3.14159265358979323846

using namespace std;

//conversion
//------------------------------------------
inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; }
template<class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); }
inline int readInt() { int x; scanf("%d", &x); return x; }

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, PII> TIII;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;

//container util

//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SQ(a) ((a)*(a))
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())

//repetition
//------------------------------------------
#define FOR(i,s,n) for(int i=s;i<(int)n;++i)
#define REP(i,n) FOR(i,0,n)
#define MOD 1000000007

const int N = 5005;
vector<LL> dp1[N], dp2[N];
int a[N];
LL INF = 0x3f3f3f3f3f3f3f3f;


void dfs(VVI& g, int u, int p = -1) {
	dp1[u].push_back(INF);
	dp1[u].push_back((a[u] > 0) ? a[u] : INF);
	dp2[u].PB(INF);
	dp2[u].PB(a[u]);
	for (auto v : g[u]) {
		if (v == p)continue;
		dfs(g, v, u);
		VLL dp1x = VLL(dp1[u].size() + dp1[v].size() - 1, INF);
		VLL dp2x = VLL(dp1[u].size() + dp1[v].size() - 1, INF);
		REP(i, dp1[u].size()) {
			if (dp2[u][i]==INF)continue;
			REP(j, dp1[v].size()) {
				if (i + j - 1 >= 0) {//統合
					dp1x[i + j - 1] = min(dp1x[i + j - 1], dp1[u][i] + dp1[v][j]);
					dp2x[i + j - 1] = min(dp2x[i + j - 1], dp2[u][i] + dp1[v][j]);
					dp2x[i + j - 1] = min(dp2x[i + j - 1], dp2[u][i] + dp2[v][j]);
				}
				//分離
				if (dp1[v][j] != INF) {
					dp1x[i + j] = min(dp1x[i + j], dp1[u][i]);
					dp2x[i + j] = min(dp2x[i + j], dp2[u][i]);
				}
				if (dp2[v][j] < 0) {
					dp1x[i + j] = min(dp1x[i + j], dp1[u][i]);
					dp2x[i + j] = min(dp2x[i + j], dp2[u][i]);
				}
			}
		}
		dp1[u] = move(dp1x);
		dp2[u] = move(dp2x);
	}
}

int main() {
	int n;
	scanf("%d", &n);
	REP(i, n)scanf("%d", &a[i]);
	VVI g(n);
	REP(i, n - 1) {
		int u, v;
		scanf("%d%d", &u, &v);
		u--, v--;
		g[u].PB(v);
		g[v].PB(u);
	}
	dfs(g, 0);
	for (int i = 0; dp1[0].size(); i++) {
		if (dp1[0][i] > 0 && dp1[0][i] < INF) {
			printf("%d\n", i-1);
			return 0;
		}
		if (dp2[0][i] < 0) {
			printf("%d\n", i-1);
			return 0;
		}
	}
	return 0;
}

Measured memory_bytes: 164480