# include "bits/stdc++.h"
using namespace std;
using LL = long long;
using ULL = unsigned long long;
const double PI = acos(-1);
template<class T>constexpr T INF() { return ::std::numeric_limits<T>::max(); }
template<class T>constexpr T HINF() { return INF<T>() / 2; }
template <typename T_char>T_char TL(T_char cX) { return tolower(cX); };
template <typename T_char>T_char TU(T_char cX) { return toupper(cX); };
typedef pair<LL, LL> pii;
const int vy[] = { -1, -1, -1, 0, 1, 1, 1, 0 }, vx[] = { -1, 0, 1, 1, 1, 0, -1, -1 };
const int dx[4] = { 0,1,0,-1 }, dy[4] = { 1,0,-1,0 };
int popcnt(unsigned long long n) { int cnt = 0; for (int i = 0; i < 64; i++)if ((n >> i) & 1)cnt++; return cnt; }
int d_sum(LL n) { int ret = 0; while (n > 0) { ret += n % 10; n /= 10; }return ret; }
int d_cnt(LL n) { int ret = 0; while (n > 0) { ret++; n /= 10; }return ret; }
LL gcd(LL a, LL b) { if (b == 0)return a; return gcd(b, a%b); };
LL lcm(LL a, LL b) { LL g = gcd(a, b); return a / g*b; };
# define ALL(qpqpq)           (qpqpq).begin(),(qpqpq).end()
# define UNIQUE(wpwpw)        sort(ALL((wpwpw)));(wpwpw).erase(unique(ALL((wpwpw))),(wpwpw).end())
# define LOWER(epepe)         transform(ALL((epepe)),(epepe).begin(),TL<char>)
# define UPPER(rprpr)         transform(ALL((rprpr)),(rprpr).begin(),TU<char>)
# define FOR(i,tptpt,ypypy)   for(LL i=(tptpt);i<(ypypy);i++)
# define REP(i,upupu)         FOR(i,0,upupu)
# define INIT                 std::ios::sync_with_stdio(false);std::cin.tie(0)

int n;
int a[5050];
vector<int> g[5050];
LL dp[5050][5050][2];
int visited[5050];
LL dp2[5050][2];
int u, v;

void dfs(int cur, int pre){
    visited[cur] = 1;
    REP(i, (int)g[cur].size()){
        if(g[cur][i] == pre)continue;
        dfs(g[cur][i], cur);
        visited[cur] += visited[g[cur][i]];
    }

    REP(j, 5050)dp2[j][0] = dp2[j][1] = HINF<LL>();

    dp2[0][0] = a[cur];
    if(a[cur] > 0)dp2[0][1] = a[cur];

    int chcnt = 0;
    REP(i, (int)g[cur].size()){
        if(g[cur][i] == pre)continue;
        for(int j = chcnt;j >= 0;j--){
            LL tmp0 = dp2[j][0], tmp1 = dp2[j][1];
            dp2[j][0] = dp2[j][1] = HINF<LL>();
            for(int k = 0;k < visited[g[cur][i]];k++){
                dp2[j + k][0] = min(dp2[j + k][0], tmp0 + dp[g[cur][i]][k][0]);
                if(dp[g[cur][i]][k][0] < 0 || dp[g[cur][i]][k][1] < HINF<LL>())dp2[j + k + 1][0] = min(dp2[j + k + 1][0], tmp0);
                if(a[cur] > 0){
                    dp2[j + k][1] = min(dp2[j + k][1], tmp1 + dp[g[cur][i]][k][1]);
                    if(dp[g[cur][i]][k][0] < 0 || dp[g[cur][i]][k][1] < HINF<LL>())dp2[j + k + 1][1] = min(dp2[j + k + 1][1], tmp1);
                }
            }
        }
        chcnt += visited[g[cur][i]];
    }
    for(int j = 0;j < visited[cur];j++){
        dp[cur][j][0] = dp2[j][0];
        dp[cur][j][1] = dp2[j][1];
    }
}

int main(){
    cin >> n;
    REP(i, n)cin >> a[i];
    REP(i, n - 1){
        cin >> u >> v;
        u--, v--;
        g[u].emplace_back(v);
        g[v].emplace_back(u);
    }
    REP(i, 5050)REP(j, 5050)REP(k, 2)dp[i][j][k] = HINF<LL>();
    dfs(0, -1);

    LL ans = n - 1;
    REP(i, n){
        if(dp[0][i][0] < 0)ans = min(ans, i);
        if(dp[0][i][1] < HINF<LL>())ans = min(ans, i);
    }
    cout << ans << endl;
}

Measured memory_bytes: 399616