#include <bits/stdc++.h>

using namespace std;

const int MaxN = 5005;
const long long oo = 1e18;

int n;
vector<int> Adj[MaxN];
int a[MaxN];
int s[MaxN];
long long dp0[MaxN][MaxN];
bool dp1[MaxN][MaxN];

void DFS(int u, int Pd)
{
    if (a[u] >= 0)
    {
        dp1[u][1] = true;
    }
    dp0[u][1] = a[u];
    s[u] = 1;
    for (auto v : Adj[u])
    {
        if (v != Pd)
        {
            DFS(v, u);
            s[u] += s[v];
            for (int i = s[u]; i > 0; i--)
            {
                bool flag = false;
                long long Min = oo;
                for (int j = max(1, i - s[u] + s[v]); j <= min(i, s[v]); j++)
                {
                    flag |= (dp1[u][i - j + 1] & dp1[v][j]);
                    Min = min(Min, dp0[u][i - j + 1] + dp0[v][j]);
                    if (dp0[v][j] < 0 || dp1[v][j])
                    {
                        flag |= dp1[u][i - j];
                        Min = min(Min, dp0[u][i - j]);
                    }
                }
                dp1[u][i] = flag;
                dp0[u][i] = Min;
            }
        }
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
    }
    int u, v;
    for (int i = 1; i < n; i++)
    {
        cin >> u >> v;
        Adj[u].push_back(v);
        Adj[v].push_back(u);
    }
    for (int i = 1; i <= n; i++)
    {
        for (int j = 0; j <= n; j++)
        {
            dp0[i][j] = oo;
            dp1[i][j] = false;
        }
    }
    DFS(1, -1);
    for (int i = 1; i <= n; i++)
    {
        if (dp0[1][i] < 0 || dp1[1][i])
        {
            cout << i - 1;
            break;
        }
    }
    
    return 0;
}
