#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
#define N 100010
ll n,p[N];
vector<ll> e[N];

ll ans,f[N],g[N],h[N];
void solve(ll x,ll from){
    ll sigg=0;
    for(auto y:e[x])if(y!=from){
	solve(y,x);
	if(g[y]-2>0)sigg+=g[y]-2;
    }
    g[x]=sigg+p[x];
    vector<ll> v;
    for(auto y:e[x])if(y!=from){
	ll ff=f[y]-1;
	ll gg=g[y]-2; if(gg<0)gg=0;
	v.push_back(ff-gg);
    }
    f[x]=p[x],h[x]=-1e17;
    if(v.size()>=1){
	sort(v.begin(),v.end(),greater<ll>());
	f[x]=max(f[x],sigg+p[x]+v[0]);
    }
    if(v.size()>=2){
	h[x]=sigg+p[x]+v[0]+v[1];
    }
    for(auto y:e[x])if(y!=from){
	ll hh=h[y]-2;
	ll gg=g[y]-2; if(gg<0)gg=0;
	h[x]=max(h[x],sigg+p[x]+hh-gg);
    }
}
int main(){
    cin>>n;
    for(int i=1;i<=n;i++){
	cin>>p[i];
    }
    for(int i=0;i<n-1;i++){
	ll a,b;cin>>a>>b;
	e[a].push_back(b);
	e[b].push_back(a);
    }
    ans=-1e17;
    solve(1,0);
    for(int i=1;i<=n;i++){
	ans=max(ans,f[i]);
	ans=max(ans,g[i]);
	ans=max(ans,h[i]);
    }
    cout<<ans<<endl;
    return 0;
}

