#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, x, n) for(int i = x; i <= n; i++)
#define rep3(i, x, n) for(int i = x; i >= n; i--)
#define elif else if
#define sp(x) fixed << setprecision(x)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const ll MOD = 1e9+7;
//const ll MOD = 998244353;
const int inf = (1<<30)-1;
const ll INF = (1LL<<60)-1;
const ld EPS = 1e-10;
template<typename T> bool chmax(T &x, const T &y) {return (x < y)? (x = y, true) : false;};
template<typename T> bool chmin(T &x, const T &y) {return (x > y)? (x = y, true) : false;};

const int MAX_V = 5e3;
vector<int> es[MAX_V];
ll A[MAX_V];
ll dp[MAX_V][MAX_V][2];
ll memo[MAX_V][2];

int dfs(int now, int pre){
    int sum = 1;
    if(A[now] > 0) dp[now][0][0] = A[now];
    else dp[now][0][1] = A[now];
    for(auto &e: es[now]){
        if(e == pre) continue;
        int tmp = dfs(e, now);
        rep(i, MAX_V) rep(j, 2) memo[i][j] = INF;
        rep(i, sum){
            rep(j, tmp){
                if(dp[e][j][0] < INF || dp[e][j][1] < 0){
                    chmin(memo[i+j+1][0], dp[now][i][0]);
                    chmin(memo[i+j+1][1], dp[now][i][1]);
                }
                rep(p, 2) rep(q, 2){
                    int r = (p != 0 || q != 0);
                    chmin(memo[i+j][r], dp[now][i][p]+dp[e][j][q]);
                }
            }
        }
        rep(i, MAX_V) rep(j, 2) dp[now][i][j] = memo[i][j];
        sum += tmp;
    }
    return sum;
}

int main(){
    int N;
    cin >> N;
    rep(i, N) cin >> A[i];
    rep(i, N-1){
        int u, v; cin >> u >> v; u--, v--;
        es[u].pb(v), es[v].pb(u);
    }
    rep(i, N) rep(j, N) rep(k, 2) dp[i][j][k] = INF;
    dfs(0, -1);
    rep(i, N){
        if(dp[0][i][0] < INF || dp[0][i][1] < 0){
            cout << i << endl;
            break;
        }
    }
}