#include <algorithm>
#include <iostream>
#include <cstdio>
#include <map>
#include <numeric>
#include <cmath>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <complex>
#include <string.h>
#include <unordered_set>
#include <unordered_map>
#include <bitset>
#include <iomanip>
#include <sys/time.h>
#include <tuple>
#include <random>
using namespace std;

#define endl '\n'
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define UNIQ(v) (v).erase(unique((v).begin(), (v).end()), (v).end())

typedef long long ll;
typedef long double ld;
typedef pair<int, ll> P;
typedef tuple<int, ll, ll> T;
typedef complex<double> comp;
typedef vector< vector<ld> > matrix;
struct pairhash {
public:
    template<typename T, typename U>
    size_t operator()(const pair<T, U> &x) const {
	size_t seed = hash<T>()(x.first);
	return hash<U>()(x.second) + 0x9e3779b9 + (seed<<6) + (seed>>2);
    }
};
const ll inf = 1e18 + 9;
const ll mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = acos(-1);

int n;
ll a[5010];
int u[5010], v[5010];

vector<int> G[5010];
ll dp[5010][5010];
int all_b[5010];
int num[5010];

void calc(int v, int prev) {
    num[v] = 1;
    dp[v][0] = a[v];
    for (int i = 1; i < 5010; i++) dp[v][i] = inf;
    for (int u : G[v]) {
        if (u == prev) continue;
        calc(u, v);
        vector<P> temp;
        for (int i = 0; i < num[v]; i++) {
            for (int j = 0; j < num[u]; j++) {
                temp.push_back(make_pair(i+j, dp[v][i]+dp[u][j]));
                if (dp[u][j] < 0 || (a[u]>0 && all_b[u]<=j))
                    temp.push_back(make_pair(i+j+1, dp[v][i]));
            }
        }
        for (int i = 0; i <= num[v]+num[u]; i++) dp[v][i] = inf;
        for (P p : temp) {
            dp[v][p.first] = min(dp[v][p.first], p.second);
        }
        num[v] += num[u];
    }

    if (a[v] > 0) {
        all_b[v] = 0;
        for (int u : G[v]) {
            if (u == prev) continue;
            if (a[u] < 0) {
                for (int i = 0; i < num[u]; i++) {
                    if (dp[u][i] < 0) {
                        all_b[v] += i+1;
                        break;
                    }
                }
            } else {
                int m = 1e9;
                for (int i = 0; i < num[u]; i++) {
                    if (dp[u][i] < 0) {
                        m = i+1;
                        break;
                    }
                }
                all_b[v] += min(m, all_b[u]);
            }
        }
    }
}

int solve() {
    for (int i = 0; i < n-1; i++) {
        G[u[i]].push_back(v[i]);
        G[v[i]].push_back(u[i]);
    }
    int s = -1;
    for (int i = 0; i < n; i++) {
        if (a[i] < 0) {
            s = i;
            break;
        }
    }
    if (s < 0) return 0;

    calc(s, -1);

    for (int i = 0; i < n; i++) {
        if (dp[s][i] < 0)
            return i;
    }
    return -1;
}

void input() {
    cin >> n;
    for (int i = 0; i < n; i++) cin >> a[i];
    for (int i = 0; i < n-1; i++) {
        cin >> u[i] >> v[i];
        u[i]--;
        v[i]--;
    }
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(15);

    input();
    cout << solve() << endl;
}


Measured memory_bytes: 461876