#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
typedef long long ll;

const ll INF = 10000000000000;

struct Graph
{
    int n;
    vector<vector<int>> g;
    
    Graph(int n) : n(n){
        g.resize(n);
    }
    
    void init(int n_){
        n = n_;
        g.resize(n_);
    }
    
    void add_edge(int from, int to){
        g[from].push_back(to);
    }
};

struct Tree	//create tree(directed) from graph(undirected)
{
    int n;
    int root;
    vector<vector<int>> t;
    vector<int> par;
    vector<int> dpt;
    
    void init(Graph &g, int root_){
        n = g.n;
        root = root_;
        t.resize(n);
        par.resize(n);
        dpt.resize(n);
        fill(dpt.begin(), dpt.end(), -1);
        queue<int> que;
        par[root] = -1;
        dpt[root] = 0;
        que.push(root);
        while(que.size()){
            int pa = que.front();
            que.pop();
            for(int ch : g.g[pa]){
                if(dpt[ch] == -1){
                    t[pa].push_back(ch);
                    par[ch] = pa;
                    dpt[ch] = dpt[pa] + 1;
                    que.push(ch);
                }
            }
        }
    }

    Tree(){}
    
    Tree(Graph &g, int root_){
        init(g, root_);
    }
    
    vector<int> toposort(){
        typedef pair<int, int> P;
        vector<P> p(n);
        for(int i = 0; i < n; i++) p[i] = P(dpt[i], i);
        sort(p.begin(), p.end(), greater<P>());
        vector<int> res(n);
        for(int i = 0; i < n; i++) res[i] = p[i].second;
        return res;
    }
};

ll dp[5005][2][5005];
int w[5005];

int main()
{
    int n;
    cin >> n;
    ll a[5005];
    for(int i = 0; i < n; i++) cin >> a[i];
    Graph g(n);
    for(int i = 0; i < n - 1; i++){
        int u, v;
        cin >> u >> v;
        u--; v--;
        g.add_edge(u, v);
        g.add_edge(v, u);
    }
    Tree t(g, 0);
    vector<int> d = t.toposort();
    int s[5002];
    for(int i = 0; i < n; i++){
        int u = d[i];
        s[u] = 1;
        for(int v : t.t[u]){
            s[u] += s[v];
        }
    }
    for(int i = 0; i < n; i++){
        for(int k = 0; k <= n; k++) dp[i][0][k] = dp[i][1][k] = INF;
        dp[i][0][0] = 0;
    }
    for(int i = 0; i < n; i++){
        int u = d[i];
        int r = 1;
        for(int j = 1; j <= (int)t.t[u].size(); j++){
            int v = t.t[u][j - 1];
            for(int k = 0; k < r + s[v]; k++){
                dp[u][j % 2][k] = INF;
            }
            for(int k = 0; k < r; k++){
                for(int l = 0; l <= s[v]; l++){
                    dp[u][j % 2][k + l] = min(dp[u][j % 2][k + l], dp[u][(j + 1) % 2][k] + dp[v][0][l]);
                }
            }
            r += s[v];
        }
        if((int)t.t[u].size() % 2){
            for(int k = 0; k < r; k++) dp[u][0][k] = dp[u][1][k];
        }
        for(int k = 0; k < r; k++) dp[u][0][k] += a[u];
        dp[u][0][r] = 0;
        if(a[u] > 0){
            for(int v : t.t[u]){
                int k;
                for(k = 0; k < s[v]; k++){
                    if(dp[v][0][k] < 0) break;
                }
                w[u] += min(w[v], k + 1);
            }
            for(int k = w[u] + 1; k <= s[u]; k++) dp[u][0][k] = min(dp[u][0][k], 0ll);
        }
        else{
            w[u] = n;
        }
    }
    int ans;
    for(ans = 0; ans <= s[0]; ans++){
        if(dp[0][0][ans] < 0) break;
    }
    ans = min(ans, w[0]);
    cout << ans << endl;
}


Measured memory_bytes: 392064