#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>

#include <iostream>
#include <complex>
#include <string>
#include <algorithm>
#include <numeric>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>

#include <functional>
#include <cassert>

typedef long long ll;
using namespace std;

#ifndef LOCAL
#define debug(x) ;
#else
#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;

template <typename T1, typename T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &p) {
  out << "{" << p.first << ", " << p.second << "}";
  return out;
}

template <typename T>
ostream &operator<<(ostream &out, const vector<T> &v) {
  out << '{';
  for (const T &item : v) out << item << ", ";
  out << "\b\b}";
  return out;
}
#endif

#define mod 1000000007 //1e9+7(prime number)
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define SIZE 5010

int n;
int a[SIZE];
vector<int> tree[SIZE];

ll dp[SIZE][SIZE][2];
bool dp2[SIZE][SIZE][2];

int dfs(int now, int prev = -1) {
  int sum_child = 1;

  dp[now][0][0] = a[now];
  dp2[now][0][0] = a[now] > 0;

  for(int to : tree[now]) {
    if (prev == to) continue;
    int child = dfs(to, now);

    for(int i=0;i<=sum_child;i++){
      for(int j=0;j<=child;j++){
        dp[now][i+j][1] = min(dp[now][i+j][1], dp[now][i][0] + dp[to][j][0]);
        dp2[now][i+j][1] = dp2[now][i+j][1] || (dp2[now][i][0] && dp2[to][j][0]);
      }
    }
    sum_child += child;

    for(int i=0;i<=sum_child;i++){
      dp[now][i][0] = dp[now][i][1];
      dp2[now][i][0] = dp2[now][i][1];
      dp[now][i][1] = LLINF;
      dp2[now][i][1] = false;
    }
  }

  for(int i=0;i<sum_child;i++){
    if(dp2[now][i][0] || dp[now][i][0] < 0) {
      dp2[now][i+1][0] = true;
      dp[now][i+1][0] = min(dp[now][i+1][0], 0LL);
    }
  }

  debug(sum_child);
  return sum_child;
}

int main(){
  scanf("%d", &n);

  for(int i=0;i<n;i++){
    scanf("%d", a+i);
  }

  for(int i=0;i<n-1;i++){
    int u, v;
    scanf("%d%d", &u, &v);
    u--; v--;
    tree[u].push_back(v);
    tree[v].push_back(u);
  }

  for(int i=0;i<=n;i++){
    for(int j=0;j<=n;j++){
      dp[i][j][0] = dp[i][j][1] = LLINF;
      dp2[i][j][0] = dp2[i][j][1] = false;
    }
  }

  dfs(0);

  int ans = INF;

  for(int i=0;i<n;i++){
    if(dp2[0][i][0]) ans = min(ans, i);
    if(dp[0][i][0] < 0) ans = min(ans, i);
  }

  assert(ans != INF);

  cout << ans << endl;

  return 0;
}
