#include <bits/stdc++.h>
using namespace std;
/* Template Begins */
#define FOR(i,N) FORR(i, 0, N)
#define FORR(i,a,b) FOTR(i, a, b, 1)
#define FOTR(i,a,b,c) for(int i=(a);i<(b);i+=(c))
#define FOREACH(it, x) for(__typeof__((x).begin()) it=(x).begin();it!=(x).end();it++)
#define MEM(a,x) memset(a,x,sizeof(a))
#define BCHK(a,x) (((a)>>(x))&1)
#define BSET(a,x) ((a)|(1<<(x)))
#define BCLR(a,x) ((a)&(~(1<<(x))))
#define BTGL(a,x) ((a)^(1<<(x)))
#define FMT(...) (sprintf(CRTBUFF, __VA_ARGS__)?CRTBUFF:0)
#define read() freopen("input.txt","r",stdin)
#define write() freopen("output.txt","w",stdout)
#define cpp_io() {ios_base::sync_with_stdio(false);cin.tie(NULL);}
#define BUFFSIZE 30000
#define INF 1000000000
#define MOD 1000000007
#define MAX 301000
#define pb push_back
#define mkpr make_pair
#define pii pair<int, int>
typedef long long ll;
/* Template Ends */
int lvl[MAX], C[MAX];
vector<int> G[MAX];
int bfs(int u){
queue<int> q;
MEM(lvl, -1);
q.push(u); lvl[u]=0;
int mx = INT_MIN;
while(!q.empty()){
int u = q.front();
int lx = lvl[u];
if(lx>2) lx = 2;
mx=max(mx,lx+C[u]);
q.pop();
for(int i=0; i<G[u].size(); i++){
int v = G[u][i];
if(lvl[v]==-1){
q.push(v);
lvl[v] = lvl[u]+1;
}
}
}
return mx;
}
int main(){
//read();
cpp_io();
int n, x, y, maxi, maxc=INT_MIN, mcount=0;
cin>>n;
for(int i=1; i<=n; i++){
cin>>C[i];
if(C[i]>maxc)maxc=C[i];
}
for(int i=0; i<n-1; i++){
cin>>x>>y;
G[y].pb(x);
G[x].pb(y);
}
for(int i=1; i<=n; i++){
if(C[i]==maxc){
mcount++;
maxi = i;
}
}
if(mcount==1){
cout<<bfs(maxi)<<"\n";
return 0;
}
for(int i=1; i<=n; i++){
int cn = 0;
if(C[i]==maxc)cn++;
for(int j=0; j<G[i].size(); j++){
int u = G[i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mUgTUFYIDMwMTAwMAojZGVmaW5lIHBiIHB1c2hfYmFjawojZGVmaW5lIG1rcHIgbWFrZV9wYWlyCiNkZWZpbmUgcGlpIHBhaXI8aW50LCBpbnQ+CnR5cGVkZWYgbG9uZyBsb25nIGxsOwoKLyogVGVtcGxhdGUgRW5kcyAqLwoKaW50IGx2bFtNQVhdLCBDW01BWF07CnZlY3RvcjxpbnQ+IEdbTUFYXTsKCmludCBiZnMoaW50IHUpewogICAgcXVldWU8aW50PiBxOwogICAgTUVNKGx2bCwgLTEpOwogICAgcS5wdXNoKHUpOyBsdmxbdV09MDsKICAgIGludCBteCA9IElOVF9NSU47CiAgICB3aGlsZSghcS5lbXB0eSgpKXsKICAgICAgICBpbnQgdSA9IHEuZnJvbnQoKTsKICAgICAgICBpbnQgbHggPSBsdmxbdV07CiAgICAgICAgaWYobHg+MikgbHggPSAyOwogICAgICAgIG14PW1heChteCxseCtDW3VdKTsKICAgICAgICBxLnBvcCgpOwogICAgICAgIGZvcihpbnQgaT0wOyBpPEdbdV0uc2l6ZSgpOyBpKyspewogICAgICAgICAgICBpbnQgdiA9IEdbdV1baV07CiAgICAgICAgICAgIGlmKGx2bFt2XT09LTEpewogICAgICAgICAgICAgICAgcS5wdXNoKHYpOwogICAgICAgICAgICAgICAgbHZsW3ZdID0gbHZsW3VdKzE7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9CiAgICByZXR1cm4gbXg7Cn0KCmludCBtYWluKCl7CgkvL3JlYWQoKTsKCWNwcF9pbygpOwoJaW50IG4sIHgsIHksIG1heGksIG1heGM9SU5UX01JTiwgbWNvdW50PTA7CgljaW4+Pm47Cglmb3IoaW50IGk9MTsgaTw9bjsgaSsrKXsKICAgICAgICBjaW4+PkNbaV07CiAgICAgICAgaWYoQ1tpXT5tYXhjKW1heGM9Q1tpXTsKICAgIH0KICAgIGZvcihpbnQgaT0wOyBpPG4tMTsgaSsrKXsKICAgICAgICBjaW4+Png+Pnk7CiAgICAgICAgR1t5XS5wYih4KTsKICAgICAgICBHW3hdLnBiKHkpOwogICAgfQoKICAgIGZvcihpbnQgaT0xOyBpPD1uOyBpKyspewogICAgICAgIGlmKENbaV09PW1heGMpewogICAgICAgICAgICBtY291bnQrKzsKICAgICAgICAgICAgbWF4aSA9IGk7CiAgICAgICAgfQogICAgfQogICAgaWYobWNvdW50PT0xKXsKICAgICAgICBjb3V0PDxiZnMobWF4aSk8PCJcbiI7CiAgICAgICAgcmV0dXJuIDA7CiAgICB9CgogICAgZm9yKGludCBpPTE7IGk8PW47IGkrKyl7CiAgICAgICAgaW50IGNuID0gMDsKICAgICAgICBpZihDW2ldPT1tYXhjKWNuKys7CiAgICAgICAgZm9yKGludCBqPTA7IGo8R1tpXS5zaXplKCk7IGorKyl7CiAgICAgICAgICAgIGludCB1ID0gR1tpXVtqXTsKICAgICAgICAgICAgaWYoQ1t1XT09bWF4YyljbisrOwogICAgICAgIH0KICAgICAgICBpZihjbj09bWNvdW50KXsKICAgICAgICAgICAgY291dDw8bWF4YysxPDwiXG4iOwogICAgICAgICAgICByZXR1cm4gMDsKICAgICAgICB9CiAgICB9CiAgICBjb3V0PDxtYXhjKzI8PCJcbiI7Cn0=