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

using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;
using LL = long long;
using VL = vector<LL>;
using VVL = vector<VL>;
using PLL = pair<LL, LL>;
using VS = vector<string>;

#define ALL(a)  begin((a)),end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define MT make_tuple
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

#define FF first
#define SS second

template<class S, class T>
istream& operator>>(istream& is, pair<S,T>& p){
  return is >> p.FF >> p.SS;
}
template<class T>
istream& operator>>(istream& is, vector<T>& xs){
  for(auto& x: xs)
	is >> x;
  return is;
}
template<class S, class T>
ostream& operator<<(ostream& os, const pair<S,T>& p){
  return os << p.FF << " " << p.SS;
}
template<class T>
ostream& operator<<(ostream& os, const vector<T>& xs){
  for(unsigned int i=0;i<xs.size();++i)
	os << (i?" ":"") << xs[i];
  return os;
}
template<class T>
void maxi(T& x, T y){
  if(x < y) x = y;
}
template<class T>
void mini(T& x, T y){
  if(x > y) x = y;
}
void debug(istringstream&){}
template <char sep=',', class Head, class... Tail>
void debug(istringstream& iss, Head&& head, Tail&&... tail)
{
  string name;
  getline(iss, name, ',');
  cerr << sep << name << "=" << head;
  debug(iss, forward<Tail>(tail)...);
}
#define DEBUG(...) \
  do{ \
    istringstream ss(#__VA_ARGS__); \
    debug<' '>(ss, __VA_ARGS__); \
    cerr<<endl; \
  }while(0)

const double EPS = 1e-10;
const double PI  = acos(-1.0);
const LL MOD = 1e9+7;
const LL INF = 1e15;

LL dp[5010][5010][2];
LL nxts[5010][5010][2];
int sz[5010];

void dfs(const VVI& G, int u, int p, VL& xs){
  sz[u] = 1;

  auto crt = dp[u];
  auto nxt = nxts[u];

  fill((LL*)crt, (LL*)crt+5010*2, INF);
  fill((LL*)nxt, (LL*)nxt+5010*2, INF);
  int ub = 1;
  if(xs[u] > 0) crt[0][0] = xs[u];
  crt[0][1] = xs[u];

  for(int v: G[u]){
    if(v == p) continue;

    dfs(G, v, u, xs);
    sz[u] += sz[v];
    REP(i,ub+1) REP(j,2) nxt[i][j] = INF;
    for(int k=0;k<=ub;++k){
      for(int l=0;l<=sz[v];++l){
        if(xs[u] > 0 && crt[k][0] < INF){
          if(dp[v][l][1] < 0 || dp[v][l][0] < INF){
            mini(nxt[k+l+1][0], crt[k][0]);
            mini(nxt[k+l+1][1], crt[k][0]);
          }
          if(dp[v][l][0] < INF){
            mini(nxt[k+l][0], crt[k][0] + dp[v][l][0]);
            mini(nxt[k+l][1], crt[k][0] + dp[v][l][0]);
          }
        }

        if(crt[k][1] < INF){
          if(dp[v][l][1] < INF)
            mini(nxt[k+l][1], crt[k][1] + dp[v][l][1]);
          if(dp[v][l][1] < 0 || dp[v][l][0] < INF)
            mini(nxt[k+l+1][1], crt[k][1]);
        }
      }
    }
    ub += sz[v];
    swap(crt, nxt);
  }

  REP(k,ub+1) REP(j,2){
    dp[u][k][j] = crt[k][j];
    //DEBUG(u,k,j,dp[u][k][j]);
  }
}

int main(){
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int N;
  cin >> N;
  VL xs(N);
  cin >> xs;

  VVI G(N);

  REP(i,N-1){
    int u, v;
    cin >> u >> v;
    --u;
    --v;
    G[u].PB(v);
    G[v].PB(u);
  }

  fill((LL*)dp, (LL*)dp+5010*5010*2, INF);
  dfs(G, 0, -1, xs);

  int ans = N;
  for(int i=0;i<N;++i){
    if(dp[0][i][0] < INF || dp[0][i][1] < 0){
      ans = i;
      break;
    }
  }
  cout << ans << endl;

  return 0;
}


Measured memory_bytes: 785664