#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
const ll INF = 1e18;

#define p_ary(ary,a,b,i) do { cout << "["; for (int i = a;i < b;++i) cout << ary[i] << (b-1 == i ? "]\n" : ", "); } while(0)
#define p_map(map,it) do {cout << "{";for (auto it = map.begin();;++it) {if (it == map.end()) {cout << "}\n";break;}else cout << "" << it->first << "=>" << it->second << ", ";}}while(0)

int n;
vector<int> sz(5010,1);
vector<ll> a(5010);
vector<vector<int>> edges(5010);
vector<vector<ll>> dp0(5010,vector<ll>(5010,INF)),dp1(5010,vector<ll>(5010,INF));

void dfs(int u,int par = -1) {
    (a[u] > 0 ? dp0 : dp1)[u][0] = a[u];
    for (int& v : edges[u]) if (v != par) {
        dfs(v,u);
        vector<ll> temp0(5010,INF),temp1(5010,INF);
        sz[u] += sz[v];
        for (int i = 0;i < sz[u];++i) {
            if (dp0[u][i] == INF) continue;
            for (int j = 0;j < sz[v];++j) {
                if (dp0[v][j] == INF) continue;
                temp0[i+j] = min(temp0[i+j],dp0[u][i]+dp0[v][j]);
                temp0[i+j+1] = min(temp0[i+j+1],dp0[u][i]);
            }
            for (int j = 0;j < sz[v];++j) {
                if (dp1[v][j] == INF) continue;
                temp1[i+j] = min(temp1[i+j],dp0[u][i]+dp1[v][j]);
                if (dp1[v][j] < 0) temp0[i+j+1] = min(temp0[i+j+1],dp0[u][i]);

            }
        }
        for (int i = 0;i < sz[u];++i) {
            if (dp1[u][i] == INF) continue;
            for (int j = 0;j < sz[v];++j) {
                if (dp0[v][j] == INF) continue;
                temp1[i+j] = min(temp1[i+j],dp1[u][i]+dp0[v][j]);
                temp1[i+j+1] = min(temp1[i+j+1],dp1[u][i]);
            }
            for (int j = 0;j < sz[v];++j) {
                if (dp1[v][j] == INF) continue;
                temp1[i+j] = min(temp1[i+j],dp1[u][i]+dp1[v][j]);
                if (dp1[v][j] < 0) temp1[i+j+1] = min(temp1[i+j+1],dp1[u][i]);
            }
        }
        dp0[u] = temp0;
        dp1[u] = temp1;
    }
}

int main() {
    cin >> n;
    for (int i = 0;i < n;++i) cin >> a[i];
    for (int i = 0;i < n-1;++i) {
        int u,v;
        cin >> u >> v;
        u--;v--;
        edges[u].push_back(v);
        edges[v].push_back(u);
    }
    dfs(0);
    int ans = n;
    for (int i = 0;i < n;++i) if (dp0[0][i] < INF) {
        ans = i;
        break;
    }
    for (int i = 0;i < n;++i) if (dp1[0][i] < INF && dp1[0][i] < 0) {
        ans = min(ans,i);
        break;
    }
    cout << ans << endl;
    // for (int i = 0;i < n;++i) p_ary(dp0[i],0,n,j);
    // cout << endl;
    // for (int i = 0;i < n;++i) p_ary(dp1[i],0,n,j);
    // cout << endl;
    // p_ary(sz,0,n,i);
    return 0;
}