#include <bits/stdc++.h>
using namespace std;

// #define int long long
#define rep(i, n) for (long long i = (long long)(0); i < (long long)(n); ++i)
#define reps(i, n) for (long long i = (long long)(1); i <= (long long)(n); ++i)
#define rrep(i, n) for (long long i = ((long long)(n)-1); i >= 0; i--)
#define rreps(i, n) for (long long i = ((long long)(n)); i > 0; i--)
#define irep(i, m, n) for (long long i = (long long)(m); i < (long long)(n); ++i)
#define ireps(i, m, n) for (long long i = (long long)(m); i <= (long long)(n); ++i)
#define SORT(v, n) sort(v, v + n);
#define REVERSE(v, n) reverse(v, v+n);
#define vsort(v) sort(v.begin(), v.end());
#define all(v) v.begin(), v.end()
#define mp(n, m) make_pair(n, m);
#define cout(d) cout<<d<<endl;
#define coutd(d) cout<<std::setprecision(10)<<d<<endl;
#define cinline(n) getline(cin,n);
#define replace_all(s, b, a) replace(s.begin(),s.end(), b, a);
#define PI (acos(-1))
#define FILL(v, n, x) fill(v, v + n, x);
#define sz(x) long long(x.size())

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vs = vector<string>;
using vpll = vector<pair<ll, ll>>;
using vtp = vector<tuple<ll,ll,ll>>;
using vb = vector<bool>;

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

const ll INF = 1e9+10;
const ll MOD = 1e9+7;
const ll LINF = 1e18;

ll n;
vll a;
vi G[5005];

int num[5005];
ll dp[5005][5005][2]; // vで条件を満たすようにi辺取り除いたときvの連結成分の和の最小値(j=1なら全ての成分が正である)
ll sub[5005][2];

void dfs(int v, int p){
  vi child;
  for(auto &nv: G[v]){
    if(nv==p) continue;
    dfs(nv,v);
    child.push_back(nv);
  }
  
  dp[v][0][(a[v]>0)]=a[v];
  
  for(auto &nv: child){
    for(int i=0; i<=num[v]; i++){
      for(int j=0; j<=num[nv]; j++){
        rep(k1,2) rep(k2,2){
          ll v1=dp[v][i][k1], v2=dp[nv][j][k2];
          if(max(v1,v2)==LINF) continue;
          chmin(sub[i+j][k1&k2], v1+v2);
          if(k2==1 || v2<0) chmin(sub[i+j+1][k1], v1);
        }
      }
    }
    num[v]+=num[nv]+1;
    for(int i=0; i<=num[v]; i++) for(int j=0; j<2; j++){
      dp[v][i][j]=sub[i][j];
      sub[i][j]=LINF;
    }
  }
}

signed main()
{
  cin.tie( 0 ); ios::sync_with_stdio( false );
  
  cin>>n;
  a.resize(n);
  rep(i,n) cin>>a[i];
  rep(i,n-1){
    ll a,b; cin>>a>>b; a--,b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  
  rep(i,n+1) rep(j,n+1) rep(k,2) dp[i][j][k]=LINF;
  rep(i,n+1) rep(j,2) sub[i][j]=LINF;
  dfs(0,-1);
  
  ll ans=LINF;
  // rep(i,n) cout<<dp[0][i][0]<<' ';
  // cout<<endl;
  // rep(i,n) cout<<dp[0][i][1]<<' ';
  // cout<<endl;
  
  rep(i,n){
    if(dp[0][i][0]<0 || dp[0][i][1]!=LINF){
      ans=i;
      break;
    }
  }
  
  cout<<ans<<endl;
  
}