#include <iostream>
#include <vector>
using namespace std;

const int64_t inf = 1LL << 40;

int64_t a[5000], dp1[5000][5001], dp2[5000][5001], size[5000], merged1[5001], merged2[5001];
vector<int> graph[5000];

template <typename T>
void ChangeMin(T& var, T t) { var = min(var, t); }

void DFS(int root, int parent) {
  for (int num = 0; num <= 5000; num++) dp1[root][num] = dp2[root][num] = inf;

  size[root] = 1;

  dp1[root][1] = a[root] > 0 ? a[root] : inf;
  dp2[root][1] = a[root];

  for (int to : graph[root]) {
    if (to == parent) continue;

    DFS(to, root);

    for (int num = 1; num <= size[root] + size[to]; num++) merged1[num] = merged2[num] = inf;

    for (int num1 = 1; num1 <= size[root]; num1++) {
      for (int num2 = 1; num2 <= size[to]; num2++) {
        // 繋げない
        if (dp1[to][num2] < inf) {
          ChangeMin(merged1[num1 + num2], dp1[root][num1]);
          ChangeMin(merged2[num1 + num2], dp2[root][num1]);
        }
        if (dp2[to][num2] < 0) {
          ChangeMin(merged1[num1 + num2], dp1[root][num1]);
          ChangeMin(merged2[num1 + num2], dp2[root][num1]);
        }

        // 繋げる
        ChangeMin(merged1[num1 + num2 - 1], dp1[root][num1] + dp1[to][num2]);
        ChangeMin(merged2[num1 + num2 - 1], dp2[root][num1] + dp2[to][num2]);
      }
    }
    size[root] += size[to];
    for (int num = 1; num <= size[root]; num++) {
      dp1[root][num] = merged1[num];
      dp2[root][num] = merged2[num];
    }
  }
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    u--; v--;
    graph[u].push_back(v);
    graph[v].push_back(u);
  }

  DFS(0, -1);

  for (int num = 1; num <= n; num++) {
    if (dp1[0][num] < inf or dp2[0][num] < 0) {
      cout << num - 1 << endl;
      break;
    }
  }

  return 0;
}