//****** @mdazmat9 **********
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define int long long
#define UB upper_bound
#define LB lower_bound
#define BS binary_search
#define EB emplace_back
#define PB push_back
#define endl "\n"
#define MOD 1000000007
#define MOD2 998244353
#define F first
#define S second
#define ALL(a) (a).begin(),(a).end()
typedef pair<int, int> pr;
typedef vector<int> VI;
typedef vector<pr> VP;
typedef vector<string> VS;
typedef vector<vector<int>> VV;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define trace1(x)                cout<<#x<<": "<<x<<endl
#define trace2(x, y)             cout<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define trace3(x, y, z)          cout<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
#define trace4(a, b, c, d)       cout<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl
#define trace5(a, b, c, d, e)    cout<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<endl
#define trace6(a, b, c, d, e, f) cout<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<" | "<<#f<<": "<<f<<endl
#define trace(v) for(auto it=v.begin();it!=v.end();it++)cout<<*it<<" ";cout<<endl;
const int inf = 1e17;
const int MAXN = 200001;
int fast_pow(int x, int y, int p);

int ans = 0;
VI g[MAXN];
VI a(MAXN);
VI dp(MAXN); // Stores sum of value of subtree including current node

void dfs1(int pos = 1,int pr = 0){
	dp[pos] = a[pos];
	for(auto x: g[pos]){
		if(x!=pr){
			dfs1(x,pos);
			dp[pos] += dp[x];
		}
	}
}

void dfs2(int pos = 1, int pr = 0 , int upper = 0){
	
	priority_queue<int,VI,greater<int>> pq;
	if(pr){
		upper = upper + dp[pr] - dp[pos];
		pq.emplace(upper);
	}
	
	for(auto x:g[pos]){
		if(x==pr)continue;
		pq.emplace(dp[x]);
		dfs2(x,pos,upper);
		if(pq.size()==3){
			pq.pop();
		}
	}
	if(pq.size()==2){
		int op1 = pq.top();pq.pop();
		int op2 = pq.top();
		ans = max(ans,op1+op2);
	}
}


void solve() {
	int n;cin>>n;
	for(int i=1;i<=n;i++)cin>>a[i];
	for(int i=0;i<n-1;i++){
		int x,y;cin>>x>>y;
		g[x].EB(y);
		g[y].EB(x);
	}
	dfs1();
	dfs2();
	cout<<ans<<endl;
}


int32_t main() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	IOS;
	int test = 1;
	// cin>>test;
	for (int i = 1; i <= test; i++) {
        // cout<<"Case #"<<i<<": ";
		solve();
	}
	return 0;
}
int fast_pow(int x, int y, int p) {
	int res = 1;
	x = x % p;
	while (y > 0) {
		if (y & 1)
			res = (res * x) % p;
		y = y >> 1;
		x = (x * x) % p;
	}
	return res;
}