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

template <typename T> inline void read (T &x) {bool b = 0; char c; while (!isdigit (c = getchar()) && c != '-');
if (c == '-') c = getchar(), b = 1; x = c - 48; while (isdigit(c = getchar())) x = (x<<3) + (x<<1) + c - 48; if (b)x=-x;}
template <typename T> inline void wrip(T x) {if (x > 9) wrip(x / 10); putchar(x%10 + 48); }

template <typename T> inline void write(T x) {if (x < 0) putchar('-'), x = -x; wrip(x); putchar(' ');}
template <typename T> inline void writeln(T x) {if (x < 0) putchar('-'), x = -x; wrip(x); putchar('\n');}
inline char readch() {char c; do c = getchar(); while (c == '\n' || c == ' '); return c;}
inline void reads(string &s) {char c; while((c=getchar())==' '||c =='\n'); s=c; while((c=getchar())!=' '&&c!='\n') s+=c;}
inline void getlines(string &s) {char c; while((c=getchar())=='\n'); s=c; while((c=getchar())!='\n') s+=c;}
typedef const int csint;

#define fi first
#define se second
#define mp make_pair
typedef long long ll;
typedef long double ld;
typedef pair <int, int> ii;
typedef vector <int> vi;

const ld pi = acos(-1);
const ll inf = 1e16;
const int mod = 1e9 + 7,
N = 5011;

int n, a[N], num[N];
ll f[N][N][2];
vi ke[N];
void Dfs(int u, int p) {
    num[u] = 1;
    f[u][0][0] = f[u][0][1] = a[u];
    if (a[u] < 0)
        f[u][0][0] = inf;
    for (auto v: ke[u]) {
        if (v == p)
            continue;
        Dfs(v, u);
        for (int i = num[u] - 1; i >= 0; i--) {
            for (int j = num[v] - 1, k = i + num[v] - 1, t = i + num[v]; j >= 0; j--, k--, t--) {
                if (j)
                    f[u][k][1] = min(f[u][k][1], f[u][i][1] + f[v][j][1]),
                    f[u][k][0] = min(f[u][k][0], f[u][i][0] + f[v][j][0]);
                if (f[v][j][1] < 0)
                    f[u][t][1] = min(f[u][t][1], f[u][i][1]),
                    f[u][t][0] = min(f[u][t][0], f[u][i][0]);
                if (f[v][j][0] < inf)
                    f[u][t][1] = min(f[u][t][1], f[u][i][1]),
                    f[u][t][0] = min(f[u][t][0], f[u][i][0]);
            }
            f[u][i][1] += f[v][0][1];
            f[u][i][0] += f[v][0][0];
        }
        num[u] += num[v];
        for (int i = 0; i < num[u]; i++) {
            f[u][i][0] = min(f[u][i][0], inf);
            f[u][i][1] = min(f[u][i][1], f[u][i][0]);
        }
    }
}
long long Rand(long long l, long long h)
{
    return l + ((long long)rand() * (RAND_MAX + 1) * (RAND_MAX + 1) * (RAND_MAX + 1) +
                (long long)rand() * (RAND_MAX + 1) * (RAND_MAX + 1) +
                (long long)rand() * (RAND_MAX + 1) +
                rand()) % (h - l + 1);
}
int main() {
    #ifdef makacha
        srand(time(NULL));
        freopen("m.inp", "w", stdout);
        //Test();
        n = 5000;
        cout << n << '\n';
        for (int i = 1; i <= n; i++)
            cout << Rand(-1e8, 1e9) << ' ';
        cout << '\n';
        for (int i = 2; i <= n; i++)
            cout << i / 2 << ' ' << i << '\n';
        freopen("m.inp", "r", stdin);
        freopen("m.out", "w", stdout);
    #endif // makacha
    read(n);
    for (int i = 1; i <= n; i++)
        read(a[i]);
    for (int i = 1, u, v; i < n; i++) {
        read(u);
        read(v);
        ke[u].push_back(v);
        ke[v].push_back(u);
    }
    for (int i = 1; i <= n; i++)
        for (int j = 0; j <= n; j++)
            f[i][j][0] = f[i][j][1] = inf;
    Dfs(1, 1);
    for (int i = 0; i < n; i++)
        if (f[1][i][1] < 0 || f[1][i][0] < inf)
            return cout << i, 0;
    cout << 0;
}


Measured memory_bytes: 392704