import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.string;

immutable long INF = 1L<<59;

void main() {
    auto N = readln.chomp.to!int;
    auto A = readln.split.map!(to!long).array;
    auto G = new int[][](N);
    foreach (i; 0..N-1) {
        auto s = readln.split.map!(to!int);
        G[s[0]-1] ~= s[1]-1;
        G[s[1]-1] ~= s[0]-1;
    }

    auto sub = new int[](N);

    int dfs1(int n, int p) {
        sub[n] = 1;
        foreach (m; G[n]) if (m != p) sub[n] += dfs1(m, n);
        return sub[n];
    }

    dfs1(0, -1);

    /*
      dp1[i][j]: 頂点iを根とする部分木で、辺をj本切っており、iを含む連結成分にコンピュータがないときの最小コスト
      dp2[i][j]: 頂点iを根とする部分木で、辺をj本切っており、iを含む連結成分にコンピュータがあるときの最小コスト
     */
    auto dp1 = new long[][](N, N+1);
    auto dp2 = new long[][](N, N+1);
    foreach (i; 0..N) dp1[i][] = dp2[i][] = INF;

    void dfs(int n, int p) {
        auto dpdp1 = new long[][](2, sub[n]+1);
        auto dpdp2 = new long[][](2, sub[n]+1);
        foreach (i; 0..2) dpdp1[i][] = dpdp2[i][] = INF;

        if (A[n] > 0) {
            dpdp1[0][0] = A[n];
            dpdp2[0][0] = A[n];
        } else {
            dpdp2[0][0] = A[n];
        }

        int cur = 0;
        int tar = 1;

        foreach (m; G[n]) {
            if (m == p) continue;
            dfs(m, n);
            dpdp1[tar][] = INF;
            dpdp2[tar][] = INF;
            foreach_reverse (i; 0..sub[n]) {
                foreach (j; 0..sub[m]) {
                    if (i + j >= sub[n]) continue;
                    // 切る
                    if (dp1[m][j] != INF || dp2[m][j] < 0) {
                        dpdp1[tar][i+j+1] = min(dpdp1[tar][i+j+1], dpdp1[cur][i]);
                        dpdp2[tar][i+j+1] = min(dpdp2[tar][i+j+1], dpdp2[cur][i]);
                    }
                    // 切らない
                    dpdp1[tar][i+j] = min(dpdp1[tar][i+j], dpdp1[cur][i] + dp1[m][j]);
                    dpdp2[tar][i+j] = min(dpdp2[tar][i+j], dpdp2[cur][i] + dp1[m][j], dpdp2[cur][i] + dp2[m][j], dpdp1[cur][i] + dp2[m][j]);
                }
            }
            swap(cur, tar);
        }

        foreach (j; 0..sub[n]) {
            dp1[n][j] = dpdp1[cur][j];
            dp2[n][j] = dpdp2[cur][j];
        }
    }

    dfs(0, -1);

    int ans = 1 << 29;
    foreach (i; 0..N) if (dp1[0][i] != INF) ans = min(ans, i);
    foreach (i; 0..N) if (dp2[0][i] < 0) ans = min(ans, i);
    ans.writeln;
}
