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

const LL INF = 123456789012345;

int N;
LL p[123456];
vector<int>G[123456];
int par[123456];
vector<int>child[123456];
void dfs(int x,int las){
	par[x] = las;
	for(int nx : G[x]){
		if(nx != las){
			dfs(nx,x);
			child[x].push_back(nx);
		}
	}
}

bool visit[123456][2];
LL memo[123456][2];
vector<LL>diff[123456];
LL dp(int x,int f){
	if(visit[x][f])return memo[x][f];
	visit[x][f]=true;
	LL res=p[x];
	//dp(x,0) dp(x,1) 共通
	for(int ch:child[x]){
		res+=max(dp(ch,0)-2,0LL);
		if(f){
			//dp(x,1)のみ
			diff[x].push_back(max(dp(ch,1)-1,0LL) - max(dp(ch,0)-2,0LL));
		}
	}
	if(f){
		sort(diff[x].rbegin(),diff[x].rend());
		if(!diff[x].empty()){
			res += diff[x][0];
		}
	}
	return memo[x][f] = res;
}

bool visit2[123456][2];
LL memo2[123456][2];
LL ep(int x,int f){
	if(visit2[x][f])return memo2[x][f];
	visit2[x][f] = true;
	//dp(親,f) without x を取得
	if(x == 0){
		return memo2[x][f] = 0;
	}
	LL rawsum = dp(par[x],0);
	LL ep0=ep(par[x],0);
	LL ep1=ep(par[x],1);
	LL pdif = max(ep1-1,0LL)-max(ep0-2,0LL);
	//diff[par[x]].push_back(max(ep1,0)-max(ep0,0));
	//sort(diff[par[x]].rbegin(),diff[par[x]].rend());
	rawsum += max(ep0 - 2,0LL);
	if(f){
		if(pdif >= diff[par[x]][0]){
			//親へのdifが一番の時、それを足す。この時、これが削除されることはあり得ない
			rawsum += pdif;
			rawsum -= max(dp(x,0) - 2,0LL);
		}else if(max(dp(x,1)-1,0LL)-max(dp(x,0)-2,0LL) == diff[par[x]][0]){
			//自分が不幸にして一番になってしまった場合、難しいことをする
			if(diff[par[x]].size()==1){
				//自分しかいないので、max(ep(par[x],1)-2,0LL) + p[par[x]]が答え
				rawsum = max(ep1-1,0LL) + p[par[x]];
			}else{
				rawsum -= max(dp(x,0)-2,0LL);
				rawsum += diff[par[x]][1];
			}
		}else{
			//一番ではないときは難しくないのでうれしい
			rawsum -= max(dp(x,0)-2,0LL);
			rawsum += diff[par[x]][0];
		}
	}else{
		rawsum -= max(dp(x,0)-2,0LL);
	}
	return memo2[x][f] = rawsum;
}

int main(){
	cin>>N;
	for(int i=0;i<N;++i){
		cin>>p[i];
	}
	for(int i=0;i<N-1;++i){
		int u,v;
		cin>>u>>v;
		--u;--v;
		G[u].push_back(v);
		G[v].push_back(u);
	}
	dfs(0,-1);
	for(int i=0;i<N;++i){
		for(int f=0;f<2;++f){
			dp(i,f);
		}
	}
	vector<LL>zero,diffs;
	for(int i=0;i<N;++i){
		LL ep0=ep(i,0);
		LL ep1=ep(i,1);
		zero.push_back(max(ep0-2,0LL));
		diffs.push_back(max(ep1-1,0LL)-max(ep0-2,0LL));
	}
	LL ans = -INF;
	for(int i=0;i<N;++i){
		//diff[i].push_back(diffs[i]);
		//sort(diff[i].rbegin(),diff[i].rend());
		LL rawsum = dp(i,0);
		rawsum += zero[i];
		LL DPi0 = rawsum;//この時のrawsumがDP(i,0)
		LL chmax = (diff[i].size() > 0) ? diff[i][0] : 0;
		LL almax = max(chmax,diffs[i]);
		rawsum += almax;
		LL DPi1 = rawsum;//この時のrawsumがDP(i,1)
		ans = max(ans,DPi0);
		ans = max(ans,DPi1);
		//cerr <<dp(i,0)<<" " <<dp(i,1) <<" "<<ep(i,0) << " " << ep(i,1) << endl;
		
	}
	cout << ans << endl;
	return 0;
}
