#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define MAXN 5005
#define eps 1e-10
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
    res = 0;T f = 1;char c = getchar();
    while(c < '0' || c > '9') {
        if(c == '-') f = -1;
        c = getchar();
    }
    while(c >= '0' && c <= '9') {
        res = res * 10 + c - '0';
        c = getchar();
    }
    res *= f;
}
template<class T>
void out(T x) {
    if(x < 0) {x = -x;putchar('-');}
    if(x >= 10) {
        out(x / 10);
    }
    putchar('0' + x % 10);
}
struct node {
    int to,next;
}E[MAXN * 2];
int head[MAXN],sumE;
bool vis[MAXN][MAXN][2],used[MAXN][2];
int64 dp[MAXN][MAXN][2],A[MAXN],g[MAXN][2];
int N,siz[MAXN],ans;
void add(int u,int v) {
    E[++sumE].to = v;
    E[sumE].next = head[u];
    head[u] = sumE;
}
void dfs(int u,int fa) {
    if(A[u] < 0) {
        vis[u][0][1] = 1;dp[u][0][1] = A[u];
    }
    if(A[u] > 0) {
        vis[u][0][0] = 1;dp[u][0][0] = A[u];
    }
    for(int i = head[u] ; i ; i = E[i].next) {
        int v = E[i].to;
        if(v != fa) {
            dfs(v,u);
        }
    }
    memset(used,0,sizeof(used));
    siz[u] = 1;
    for(int i = head[u] ; i ; i = E[i].next) {
        int v = E[i].to;
        if(v != fa) {
            for(int j = 0 ; j <= siz[u] + siz[v] ; ++j) used[j][0] = used[j][1] = 0;
            for(int j = 0 ; j <= siz[u] ; ++j) {
                for(int a = 0 ; a <= 1 ; ++a) {
                    if(!vis[u][j][a]) continue;
                    for(int h = 0 ; h <= siz[v] ; ++h) {
                        for(int b = 0 ; b <= 1 ; ++b) {
                            if(!vis[v][h][b]) continue;
                            if(!used[j + h][a | b]) {
                                g[j + h][a | b] = dp[u][j][a] + dp[v][h][b];
                                used[j + h][a | b] = 1;
                            }
                            else g[j + h][a | b] = min(g[j + h][a | b],dp[u][j][a] + dp[v][h][b]);
                        }
                    }
                }
            }
            for(int j = 0 ; j <= siz[u] + siz[v] ; ++j) {
                for(int a = 0 ; a <= 1 ; ++a) {
                    vis[u][j][a] = used[j][a];
                    dp[u][j][a] = g[j][a];
                }
            }
            siz[u] += siz[v];
        }
    }
    if(fa) {
        for(int j = siz[u] ; j >= 0 ; --j) {
            if(vis[u][j][1] && dp[u][j][1] < 0) {dp[u][j + 1][0] = 0;vis[u][j + 1][0] = 1;}
            if(vis[u][j][0]) {dp[u][j + 1][0] = 0;vis[u][j + 1][0] = 1;}
        }
    }
    else {
        for(int j = 0 ; j <= siz[u] ; ++j) {
            if(vis[u][j][1] && dp[u][j][1] < 0) {ans = j;break;}
            if(vis[u][j][0]) {ans = j;break;}
        }
    }
}
void Solve() {
    read(N);
    for(int i = 1 ; i <= N ; ++i) read(A[i]);
    int u,v;
    for(int i = 1 ; i < N ; ++i) {
        read(u);read(v);
        add(u,v);add(v,u);
    }
    dfs(1,0);
    out(ans);enter;
}
int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    Solve();
}

Measured memory_bytes: 440448