#include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define NUM 100005

enum Type{
	Zero,
	One,
	Two,
};

struct Info{
	Info(int arg_node_id,int arg_diff){
		node_id = arg_node_id;
		diff = arg_diff;
	}
	bool operator<(const struct Info & arg) const{
		return diff < arg.diff;
	}
	int node_id,diff;
};

int N;
int point[NUM];
int value[NUM][3];
vector<int> G[NUM];


void dfs(int node_id,int parent){

	int tmp_value = point[node_id];
	int child,tmp;

	for(int i = 0; i < G[node_id].size(); i++){

		child = G[node_id][i];
		if(child == parent)continue;

		dfs(child,node_id);

		tmp = value[child][Zero]-2;

		if(tmp >= 0){
			tmp_value += tmp;
		}
	}

	value[node_id][Zero] = tmp_value;

	priority_queue<Info> Q;

	for(int i = 0; i < G[node_id].size(); i++){

		child = G[node_id][i];
		if(child == parent)continue;

		Q.push(Info(child,(value[child][One]-1)-max(0,value[child][Zero]-2)));
	}

	value[node_id][One] = value[node_id][Zero];

	if(Q.empty() == false && Q.top().diff > 0){

		value[node_id][One] += Q.top().diff;
		Q.pop();

		if(Q.empty() == false && Q.top().diff > 0){
			value[node_id][Two] = value[node_id][One]+Q.top().diff;
		}
	}

	for(int i = 0; i < G[node_id].size(); i++){

		child = G[node_id][i];
		if(child == parent)continue;

		if(value[node_id][Two] < value[node_id][Zero]+(value[child][Two]-2)-max(0,value[child][Zero]-2)){
			value[node_id][Two] = value[node_id][Zero]+(value[child][Two]-2)-max(0,value[child][Zero]-2);
		}
	}
}

int main(){

	scanf("%d",&N);

	for(int i = 0; i < NUM; i++){
		value[i][Zero] = 0;
		value[i][One] = -BIG_NUM;
		value[i][Two] = -BIG_NUM;
	}

	for(int i = 0; i < N; i++){
		scanf("%d",&point[i]);
	}

	int from,to;

	for(int loop = 0; loop < N-1; loop++){

		scanf("%d %d",&from,&to);
		from--;
		to--;
		G[from].push_back(to);
		G[to].push_back(from);
	}

	queue<int> Q;
	for(int i = 0; i < N; i++){
		if(G[i].size() == 0){
			Q.push(i);
		}
	}

	dfs(0,-1);
	int ans = -BIG_NUM;

	for(int i = 0; i < N; i++){
		ans = max(ans,max(value[i][Zero],max(value[i][One],value[i][Two])));
	}

	printf("%d\n",ans);

	return 0;
}

