#include <bits/stdc++.h>
using namespace std;
//function to calculate the points collected in the path
int dfs(int x, vector<vector<int>> &arr, vector<bool> &vis, vector<int> &weight){
int points_max = 0 ;
int points = 0 ;
vis[x] = true ;
for(auto &i: arr[x]){
if(!vis[i]){
points = dfs(i, arr, vis, weight) ;
}
points_max = max(points_max, points) ;
}
return weight[x] + points_max ;
}
int main() {
// input starts
int N ;
cin >> N ;
vector<vector<int>> arr(N + 1) ;
vector<bool> vis(N + 1, false) ;
vector<int> weight(N + 1) ;
vector<int> ans ;
for(int i = 1; i <= N; ++i)cin >> weight[i] ;
int u, v;
for(int i = 0; i < N - 1; ++i){
cin >> u >> v ;
arr[u].push_back(v) ;
arr[v].push_back(u) ;
}
// input ends here
vis[1] = true ;
for(auto &x: arr[1]){
ans.push_back(dfs(x, arr, vis, weight)) ;
}
sort(ans.begin(), ans.end(), greater<int>()) ;
// if only 1 node & 0 edges
if(ans.size() == 0){
cout << weight[1] ;
}
// if only 2 node nodes & 1 edge
else if(ans.size() == 1){
cout << weight[1] + ans[0] ;
}
else{
cout << ans[0] + ans[1] + weight[1] ;
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgovL2Z1bmN0aW9uIHRvIGNhbGN1bGF0ZSB0aGUgcG9pbnRzIGNvbGxlY3RlZCBpbiB0aGUgcGF0aAppbnQgZGZzKGludCB4LCB2ZWN0b3I8dmVjdG9yPGludD4+ICZhcnIsIHZlY3Rvcjxib29sPiAmdmlzLCB2ZWN0b3I8aW50PiAmd2VpZ2h0KXsKCWludCBwb2ludHNfbWF4ID0gMCA7IAoJaW50IHBvaW50cyA9IDAgOyAKCXZpc1t4XSA9IHRydWUgOwoJCglmb3IoYXV0byAmaTogYXJyW3hdKXsKCQlpZighdmlzW2ldKXsKCQkJcG9pbnRzID0gZGZzKGksIGFyciwgdmlzLCB3ZWlnaHQpIDsKCQl9CgkJcG9pbnRzX21heCA9IG1heChwb2ludH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