#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <limits.h>
#include <math.h>
#include <functional>
#include <bitset>
#include <iomanip>
#include <cassert>

#define repeat(i,n) for (long long i = 0; (i) < (n); ++ (i))
#define debug(x) cerr << #x << ": " << x << '\n'
#define debugArray(x,n) for(long long i = 0; (i) < (n); ++ (i)) cerr << #x << "[" << i << "]: " << x[i] << '\n'
#define debugArrayP(x,n) for(long long i = 0; (i) < (n); ++ (i)) cerr << #x << "[" << i << "]: " << x[i].first<< " " << x[i].second << '\n'

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> Pii;
typedef vector<int> vint;
typedef vector<ll> vll;
const ll INF = LLONG_MAX;
const ll MOD = 1e9+7;


int N;
ll A[5010];
vector<vint> G;
ll dp[5010][2][5010];
ll tmp[2][5010];
ll size[5010];

void dfs(int v,int par){
  size[v]=1;
  dp[v][0][0] = A[v]>0;
  dp[v][1][0] = A[v];
  for(int chi:G[v])if(chi!=par){
    dfs(chi,v);
    fill(tmp[0],tmp[0]+N,0);
    fill(tmp[1],tmp[1]+N,INF);
    for(int k=size[v]-1;k>=0;k--){
      for(int l=size[chi]-1;l>=0;l--){
        if(dp[v][0][k]&&dp[chi][1][l]<0){
          tmp[0][k+l+1]=1;
        }
        if(dp[v][0][k]&&dp[chi][0][l]){
          tmp[0][k+l]=1;
        }
        if(dp[v][1][k]<INF&&dp[chi][0][l]){
          tmp[1][k+l+1]=min(tmp[1][k+l+1],dp[v][1][k]);
        }
        if(dp[v][1][k]<INF&&dp[chi][1][l]<INF){
          tmp[1][k+l]=min(tmp[1][k+l],dp[v][1][k]+dp[chi][1][l]);
        }
      }
    }
    swap(dp[v],tmp);
    size[v]+=size[chi];
  }
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin>>N;
  repeat(i,N){
    cin>>A[i];
  }
  G.resize(N);
  repeat(i,N-1){
    int u,v;cin>>u>>v;
    u--;v--;
    G[u].push_back(v);
    G[v].push_back(u);
  }
  dfs(0,-1);
  ll ans;
  for(ans=0;ans<N-1;ans++){
    if(dp[0][0][ans]||dp[0][1][ans]<0){
      break;
    }
  }
  cout << ans << endl;
  return 0;
}
