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

enum inf3 = 1_001_001_001;
enum inf6 = 1_001_001_001_001_001_001L;
enum mod = 1_000_000_007L;


void main() {
    int n;
    scan(n);
    auto a = readln.split.to!(long[]);
    auto adj = new int[][](n, 0);
    foreach (i ; 0 .. n - 1) {
        int u, v;
        scan(u, v);
        u--, v--;
        adj[u] ~= v;
        adj[v] ~= u;
    }

    auto dp = new long[][][](2, n, 0);
    auto size = new int[](n);

    void dfs(int v, int p) {
        size[v] = 1;
        dp[0][v] = new long[](2);
        dp[0][v][] = inf6;
        dp[0][v][1] = a[v];
        dp[1][v] = new long[](2);
        dp[1][v][] = inf6;
        if (a[v] > 0) dp[1][v][1] = a[v];

        foreach (u ; adj[v]) {
            if (u == p) continue;
            dfs(u, v);

            auto next0 = new long[](size[v] + size[u] + 1);
            next0[] = inf6;
            //next0[0 .. dp[0][v].length] = dp[0][v][];

            auto next1 = new long[](size[v] + size[u] + 1);
            next1[] = inf6;
            //next1[0 .. dp[1][v].length] = dp[1][v][];

            foreach (i ; 1 .. size[v] + 1) {
                foreach (j ; 1 .. size[u] + 1) {
                    next0[i + j - 1] = min(next0[i + j - 1], dp[0][v][i] + dp[0][u][j]);
                    if (dp[0][u][j] < 0 || dp[1][u][j] < inf6) next0[i + j] = min(next0[i + j], dp[0][v][i]);

                    next1[i + j - 1] = min(next1[i + j - 1], dp[1][v][i] + dp[1][u][j]);
                    if (dp[0][u][j] < 0 || dp[1][u][j] < inf6) next1[i + j] = min(next1[i + j], dp[1][v][i]);
                }
            }

            dp[0][v] = next0;
            //dp[0][v][0] = 0;
            dp[1][v] = next1;
            //dp[1][v][0] = 0;
            size[v] += size[u];
        }

    }

    dfs(0, -1);

    debug {
        foreach (i ; 0 .. n) {
            writefln("%d:", i);
            writeln(dp[0][i]);
            writeln(dp[1][i]);
        }
    }

    int ans = -1;

    foreach (i ; 1 .. n + 1) {
        if (dp[0][0][i] < 0 || dp[1][0][i] < inf6) {
            ans = i - 1;
            break;
        }
    }

    writeln(ans);
}






void scan(T...)(ref T args) {
    import std.stdio : readln;
    import std.algorithm : splitter;
    import std.conv : to;
    import std.range.primitives;

    auto line = readln().splitter();
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront();
    }
    assert(line.empty);
}

void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}
