#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

template<class T> bool chmin(T &target, const T &value){
  if( target > value ){
    target = value;
    return true;
  }
  return false;
}


const long long INF = 1e18;


vector<int> g[5050];

vector<ll> A;
vector<ll> dp0[5050];
vector<ll> dp1[5050];

vector<ll> merge(const vector<ll> &dp, const vector<ll> &op0, const vector<ll> &op1, bool source_only){
  vector<ll> res(dp.size() + op0.size(), INF);
  for(int i = 0 ; i < dp.size() ; i++){
    for(int j = 0 ; j < op0.size() ; j++){
      if( !source_only ) chmin(res[i+j], op0[j] + dp[i]);
      chmin(res[i+j], op1[j] + dp[i]);
      if( op0[j] < 0 ) chmin(res[i+j+1], dp[i]);
      if( op1[j] != INF) chmin(res[i+j+1], dp[i]);
    }
  }
  return move(res);
}

void dfs(int root, int parent){
  dp0[root] = vector<ll>(1, A[root]);
  dp1[root] = vector<ll>(1, A[root] > 0 ? A[root] : INF);

  for( auto i : g[root]){
    if( parent == i ) continue;
    dfs(i, root);
    dp0[root] = merge(dp0[root], dp0[i], dp1[i], false);
    dp1[root] = merge(dp1[root], dp0[i], dp1[i], true);
  }
//  cout << "==" << " " << root << " " << "==" << endl;
//  for(int i = 0 ; i < dp0[root].size() ; i++){
//    cout << i << ": " << dp0[root][i] << " " << dp1[root][i] << endl;
//  }

}

void add_edge(int a,int b){
  g[a].push_back(b);
  g[b].push_back(a);
}


void solve(long long N, vector<long long> A, vector<long long> U,
           vector<long long> V) {
  ::A = A;

  for(int i = 0 ; i < N - 1 ; i++){
    add_edge(U[i]-1, V[i]-1);
  }
  dfs(0, -1);

  for(int i = 0 ; i < dp0[0].size() ; i++){
    if( dp0[0][i] < 0 || dp1[0][i] != INF ){
      cout << i << endl;
      return;
    }
  }




}




int main() {

  long long N;
  scanf("%lld", &N);
  vector<long long> A(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &A[i]);
  }
  vector<long long> U(N - 1);
  vector<long long> V(N - 1);
  for (int i = 0; i < N - 1; i++) {
    scanf("%lld", &U[i]);
    scanf("%lld", &V[i]);
  }
  solve(N, A, U, V);
  return 0;
}
