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 children = new int[](N);
    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), children[n] += 1;
        return sub[n];
    }

    dfs1(0, -1);

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

    int dfs(int n, int p) {
        if (A[n] > 0) {
            dp1[n][0][0] = A[n];
            dp2[n][0][0] = A[n];
        } else {
            dp2[n][0][0] = A[n];
        }

        int s = 1;
        int c = 0;

        foreach (m; G[n]) {
            if (m == p) continue;
            int mc = children[m];
            int x = dfs(m, n);
            foreach (i; 0..s) {
                foreach (j; 0..sub[m]) {
                    // 切る
                    if (dp1[m][mc][j] != INF || dp2[m][mc][j] < 0) {
                        dp1[n][c+1][i+j+1] = min(dp1[n][c+1][i+j+1], dp1[n][c][i]);
                        dp2[n][c+1][i+j+1] = min(dp2[n][c+1][i+j+1], dp2[n][c][i]);
                    }
                    // 切らない
                    dp1[n][c+1][i+j] = min(dp1[n][c+1][i+j], dp1[n][c][i] + dp1[m][mc][j]);
                    dp2[n][c+1][i+j] = min(dp2[n][c+1][i+j], dp2[n][c][i] + dp1[m][mc][j], dp2[n][c][i] + dp2[m][mc][j], dp1[n][c][i] + dp2[m][mc][j]);
                }
            }
            s += x;
            c += 1;
        }

        return s;
    }

    dfs(0, -1);

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