#include<bits/stdc++.h>

using namespace std;

#define all(x) (x).begin(), (x).end()
#define sz(x) ( (int)(x).size() )
using LL = long long;

template<class T>
inline bool asMn(T &a, const T &b) { return a > b ? a = b, true : false; }
template<class T>
inline bool asMx(T &a, const T &b) { return a < b ? a = b, true : false; }

const LL infLL = 1e18;
const int inf = 1e9;
mt19937 rng( (int)chrono::steady_clock::now().time_since_epoch().count() );

vector<int> a;
vector<vector<int> > gr;
vector<vector<array<LL, 2> > > f;
void dfs(int u, int pr) {
  f[u].assign(1, array<LL, 2>{ infLL, infLL } );
  f[u][0][ a[u] < 0 ] = a[u];

  for (int v : gr[u]) if (v ^ pr) {
    dfs(v, u);

    vector<array<LL, 2> > nF(sz(f[u]) + sz(f[v]), array<LL, 2>{ infLL, infLL });
    for (int i = 0; i < sz(f[u]); ++i) {
      for (int flagI = 0; flagI < 2; ++flagI) if (f[u][i][flagI] ^ infLL) {
        for (int j = 0; j < sz(f[v]); ++j) {
          for (int flagJ = 0; flagJ < 2; ++flagJ) if (f[v][j][flagJ] ^ infLL) {
            asMn(nF[i + j][flagI | flagJ], f[u][i][flagI] + f[v][j][flagJ]);
            if (!flagJ || f[v][j][flagJ] < 0) asMn(nF[i + j + 1][flagI], f[u][i][flagI]);
          }
        }
      }
    }

    f[u].swap(nF);
  }

//  cerr << "u = " << u + 1 << '\n';
//  for (int i = 0; i < sz(f[u]); ++i) {
//    for (int j = 0; j < 2; ++j) if (f[u][i][j] ^ infLL) cerr << i << ' ' << j << ' ' << f[u][i][j] << '\n';
//  }
}

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

  #ifdef FourLeafClover
  freopen("input", "r", stdin);
  #endif // FourLeafClover

  int n; cin >> n;
  a.assign(n, 0);
  for (auto &i : a) cin >> i;
  gr.assign(n, {} );
  for (int i = 1; i < n; ++i) {
    int u, v; cin >> u >> v; --u; --v;
    gr[u].emplace_back(v);
    gr[v].emplace_back(u);
  }

  f.assign(n, {} );
  dfs(0, -1);
  int ans = inf;
  for (int i = 0; i < sz(f[0]); ++i) {
    for (int j = 0; j < 2; ++j) if (f[0][i][j] ^ infLL && (!j || f[0][i][j] < 0) ) asMn(ans, i);
  }

  cout << ans << '\n';

  return 0;
}
