#include <iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<math.h>
using namespace std;
typedef long long ll;
#define int long long
typedef vector<int> VI;
typedef pair<int, int> pii;
#define fore(i,a) for(auto &i:a)
#define REP(i,n) for(int i=0;i<n;i++)
#define eREP(i,n) for(int i=0;i<=n;i++)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define eFOR(i,a,b) for(int i=(a);i<=(b);++i)
#define SORT(c) sort((c).begin(),(c).end())
#define rSORT(c) sort((c).rbegin(),(c).rend())
#define LB(x,a) lower_bound((x).begin(),(x).end(),(a))
#define UB(x,a) upper_bound((x).begin(),(x).end(),(a))
#define INF 1000000000
#define LLINF 9223372036854775807
#define mod 1000000007
//vector<vector<int> > dp;
//vector<vector<vector<int> > > vvvi;
//dp=vector<vector<int> >(N, vector<int>(M,0));
//vector<pair<int,int> > v;
//v.push_back(make_pair(x,y));
//priority_queue<int,vector<int>, greater<int> > q2;

struct t {
	int r, s1, s2;
	t(void) { r = 0, s1 = 0, s2 = 0; }
	void up(int x) {
		if (s1 < x)swap(s1, x);
		s2 = max(s2, x);
	}
	void mn(int x) {
		if (s1 == x) {
			s1 = s2;
			s2 = 0;
		}
	}
};
int N, cs[100005], ans;
VI G[100005];
map<int, t>dp[100005];
t dfs(int v, int p, int f) {
	if (dp[v].count(p))return dp[v][p];
	t res;
	if (p == -1 || f) {
		REP(i, G[v].size()) {
			int u = G[v][i];
			if (u == p)continue;
			t x = dfs(u, v, f);
			x.r -= 2;
			x.s1--;
			if (x.r>0) {
				res.r += x.r;
				res.up(-x.r + x.s1);
			}
			else res.up(x.s1);
		}
		res.r += cs[v];
		res.s1 += res.r;
		res.s2 += res.r;
	}
	else {
		res = dfs(v, -1, 0);
		t x = dfs(p, v, 0);
		x.r -= 2;
		x.s1--;
		if (x.r > 0) {
			res.r -= x.r;
			res.mn(res.r + x.s1);
			res.s1 -= x.r;
		}
		else res.mn(res.r + x.s1);
	}

	return dp[v][p] = res;
}

signed main(){
cin.tie(0);
ios::sync_with_stdio(false);

	cin >> N;
	int ma = -LLINF;
	REP(i, N) {
		cin >> cs[i];
		ma = max(ma, cs[i]);
	}
	if (ma <= 0) {
		cout << ma << endl;
		return 0;
	}
	REP(i, N - 1) {
		int a, b;
		cin >> a >> b;
		G[--a].push_back(--b);
		G[b].push_back(a);
	}
	dfs(0, -1, 1);
	REP(i, N)ans = max(ans, dfs(i, -1, 0).s1);
	cout << ans << endl;
	
	return 0;
}


