#include<bits/stdc++.h>

using namespace std;

#define ll  long long
#define ld  double

#define all(x)  x.begin(),x.end()
#define sz(x)   (int)x.size()

#define pb  emplace_back
#define X   first
#define Y   second

const int   N   = 5005;

typedef pair<int,int>   ii;

vector<int> g[N];

int nCh[N];
ll  f[N][N][2];
ll  h[N][2];
int a[N];

void dfs(int u,int p)   {
    nCh[u] = 1;

    f[u][0][0] = a[u] < 0 ? 1e18 : a[u];
    f[u][0][1] = a[u] > 0 ? 1e18 : a[u];

    for(int v : g[u])   if (v != p) {
        dfs(v,u);

        for(int i = 0 ; i < nCh[u] + nCh[v] ; ++i)
            h[i][0] = 1e18,
            h[i][1] = 1e18;

        for(int i = 0 ; i < nCh[u] ; ++i)
        for(int j = 0 ; j < nCh[v] ; ++j)   {
            int k = i + j;
            h[k][0] = min(h[k][0],f[u][i][0] + f[v][j][0]);
            h[k][1] = min(h[k][1],f[u][i][0] + f[v][j][1]);
            h[k][1] = min(h[k][1],f[u][i][1] + f[v][j][0]);
            h[k][1] = min(h[k][1],f[u][i][1] + f[v][j][1]);


            if (f[v][j][0] < 1e18 || f[v][j][1] < 0)    {
                h[k + 1][0] = min(h[k + 1][0],f[u][i][0]);
                h[k + 1][1] = min(h[k + 1][1],f[u][i][1]);
            }
        }
        nCh[u] += nCh[v];

        for(int i = 0 ; i < nCh[u] ; ++i)
            f[u][i][0] = h[i][0],
            f[u][i][1] = h[i][1];
    }
}

int main()  {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    int n;  cin >> n;

    for(int i = 1 ; i <= n ; ++i)
        cin >> a[i];

    for(int i = 1 ; i < n ; ++i)    {
        int x;  cin >> x;
        int y;  cin >> y;

        g[x].pb(y);
        g[y].pb(x);
    }

    dfs(1,0);

    for(int i = 0 ; i < n ; ++i)
        if (f[1][i][0] < 1e18 || f[1][i][1] < 0)    {
            cout << i;
            return  0;
        }
}