#include <bits/stdc++.h>
using namespace std;
using lint     = long long;
const lint inf = 1LL << 60;
const lint mod = 1000000007;

template <class T>
bool chmax(T &a, const T &b) {
    return (a < b) ? (a = b, 1) : 0;
}
template <class T>
bool chmin(T &a, const T &b) {
    return (b < a) ? (a = b, 1) : 0;
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    vector<lint> a(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    vector<vector<int>> edges(n);
    for (int i = 0; i < n - 1; ++i) {
        int u, v;
        cin >> u >> v;
        u--, v--;
        edges[u].emplace_back(v);
        edges[v].emplace_back(u);
    }
    vector<int> size(n, 0);
    vector<vector<lint>> dp1(n, vector<lint>(n, inf));
    vector<vector<lint>> dp2(n, vector<lint>(n, inf));
    vector<lint> to1(n, inf);
    vector<lint> to2(n, inf);
    function<void(int, int)> dfs = [&](int c, int p) {
        size[c] = 1;
        if (a[c] > 0)
            dp1[c][0] = a[c];
        else
            dp2[c][0] = a[c];

        for (auto &v : edges[c]) {
            if (v == p)
                continue;
            dfs(v, c);
            to1.assign(n, inf);
            to2.assign(n, inf);
            for (int i = 0; i < size[c]; ++i) {
                for (int j = 0; j < size[v]; ++j) {
                    if (dp1[v][j] < inf || dp2[v][j] < 0) {
                        chmin(to1[i + j + 1], dp1[c][i]);
                        chmin(to2[i + j + 1], dp2[c][i]);
                    }
                    chmin(to1[i + j], dp1[c][i] + dp1[v][j]);
                    chmin(to2[i + j], dp1[c][i] + dp2[v][j]);
                    chmin(to2[i + j], dp2[c][i] + dp1[v][j]);
                    chmin(to2[i + j], dp2[c][i] + dp2[v][j]);
                }
            }
            size[c] += size[v];
            swap(to1, dp1[c]);
            swap(to2, dp2[c]);
        }
    };
    dfs(0, -1);
    for (int i = 0; i < n; ++i) {
        if (dp1[0][i] < inf || dp2[0][i] < 0) {
            cout << i << "\n";
            return 0;
        }
    }
    return 0;
}