#include<bits/stdc++.h>
#define pb push_back
#define x first
#define y second
using namespace std;
const int N = 5005;
int n, A[N], ok[N], F[N], sz[N];
long long dp[N][N], dpt[N];
vector < int > Adj[N];
void DFS(int v, int p)
{
    sz[v] = 1;
    for (int i = 0; i < Adj[v].size(); i++)
        if (Adj[v][i] == p)
            swap(Adj[v][i], Adj[v].back()), Adj[v].pop_back();

    if (!Adj[v].size())
    {
        ok[v] = 0;
        if (A[v] > 0)
            F[v] = 0;
        for (int i = 0; i < 1; i++)
            dp[v][i] = A[v];
        return ;
    }

    for (int &u : Adj[v])
        DFS(u, v);

    dp[v][0] = A[v];
    for (int &u : Adj[v])
    {
        memset(dpt, 63, sizeof(dpt));
        for (int i = 0; i < sz[v]; i++)
            for (int j = 0; j < sz[u]; j++)
                dpt[i + j] = min(dpt[i + j], dp[v][i] + dp[u][j]);
        for (int i = 0; i < sz[v]; i++)
            dpt[i + ok[u] + 1] = min(dpt[i + ok[u] + 1], dp[v][i]);
        sz[v] += sz[u];
        for (int i = 0; i < sz[v]; i++)
            dp[v][i] = dpt[i];
    }
    for (int i = 0; i < N; i++)
        if (dp[v][i] < 0)
            ok[v] = min(ok[v], i);

    if (A[v] > 0)
    {
        F[v] = 0;
        for (int &u : Adj[v])
            F[v] += min(ok[u] + 1, F[u]);
        ok[v] = min(ok[v], F[v]);
    }
}
int main()
{
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
        scanf("%d", &A[i]);
    for (int i = 1, v, u; i < n; i++)
        scanf("%d%d", &v, &u), Adj[v].pb(u), Adj[u].pb(v);
    memset(ok, 63, sizeof(ok));
    memset(F, 63, sizeof(F));
    memset(dp, 63, sizeof(dp));
    DFS(1, 0);
    return !printf("%d", ok[1]);
}