#include <stdio.h>
#include <vector>
using namespace std;
int a[300005];
vector<int> way[300005];
int main(){
int n;
scanf("%d", &n);
int maxval=-1e9;
for(int i=1; i<=n; i++) scanf("%d", &a[i]), maxval = max(maxval, a[i]);
for(int i=0; i<n-1; i++){
int u, v;
scanf("%d%d", &u, &v);
way[u].push_back(v);
way[v].push_back(u);
}
int x=0, y=0;
for(int i=1; i<=n; i++){
if(a[i] == maxval) x++;
else if(a[i] == maxval-1) y++;
}
int res = maxval+2;
for(int i=1; i<=n; i++){
// minus
if(a[i] == maxval) x--;
else if(a[i] == maxval-1) y--;
for(int j=0; j<way[i].size(); j++){
int pos = way[i][j];
if(a[pos] == maxval) x--, y++;
else if(a[pos] == maxval-1) y--;
}
// check the needed strength and update the answer
if(x == 0){
res = min(res, maxval+1);
if(y == 0) res = min(res, maxval);
}
// plus
if(a[i] == maxval) x++;
else if(a[i] == maxval-1) y++;
for(int j=0; j<way[i].size(); j++){
int pos = way[i][j];
if(a[pos] == maxval) x++, y--;
else if(a[pos] == maxval-1) y++;
}
}
printf("%d", res);
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDx2ZWN0b3I+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgppbnQgYVszMDAwMDVdOwp2ZWN0b3I8aW50PiB3YXlbMzAwMDA1XTsKCmludCBtYWluKCl7CiAgICBpbnQgbjsKICAgIHNjYW5mKCIlZCIsICZuKTsKICAgIGludCBtYXh2YWw9LTFlOTsKICAgIGZvcihpbnQgaT0xOyBpPD1uOyBpKyspIHNjYW5mKCIlZCIsICZhW2ldKSwgbWF4dmFsID0gbWF4KG1heHZhbCwgYVtpXSk7CiAgICBmb3IoaW50IGk9MDsgaTxuLTE7IGkrKyl7CiAgICAgICAgaW50IHUsIHY7CiAgICAgICAgc2NhbmYoIiVkJWQiLCAmdSwgJnYpOwogICAgICAgIHdheVt1XS5wdXNoX2JhY2sodik7CiAgICAgICAgd2F5W3ZdLnB1c2hfYmFjayh1KTsKICAgIH0KICAgIGludCB4PTAsIHk9MDsKICAgIGZvcihpbnQgaT0xOyBpPD1uOyBpKyspewogICAgICAgIGlmKGFbaV0gPT0gbWF4dmFsKSB4Kys7CiAgICAgICAgZWxzZSBpZihhW2ldID09IG1heHZhbC0xKSB5Kys7CiAgICB9CiAgICAKICAgIGludCByZXMgPSBtYXh2YWwrMjsKICAgIAogICAgZm9yKGludCBpPTE7IGk8PW47IGkrKyl7CiAgICAgICAgCiAgICAgICAgLy8gbWludXMKICAgICAgICBpZihhW2ldID09IG1heHZhbCkgeC0tOwogICAgICAgIGVsc2UgaWYoYVtpXSA9PSBtYXh2YWwtMSkgeS0tOwogICAgICAgIGZvcihpbnQgaj0wOyBqPHdheVtpXS5zaXplKCk7IGorKyl7CiAgICAgICAgICAgIGludCBwb3MgPSB3YXlbaV1bal07CiAgICAgICAgICAgIGlmKGFbcG9zXSA9PSBtYXh2YWwpIHgtLSwgeSsrOwogICAgICAgICAgICBlbHNlIGlmKGFbcG9zXSA9PSBtYXh2YWwtMSkgeS0tOwogICAgICAgIH0KICAgICAgICAKICAgICAgICAvLyBjaGVjayB0aGUgbmVlZGVkIHN0cmVuZ3RoIGFuZCB1cGRhdGUgdGhlIGFuc3dlcgogICAgICAgIGlmKHggPT0gMCl7CiAgICAgICAgICAgIHJlcyA9IG1pbihyZXMsIG1heHZhbCsxKTsKICAgICAgICAgICAgaWYoeSA9PSAwKSByZXMgPSBtaW4ocmVzLCBtYXh2YWwpOwogICAgICAgIH0KICAgICAgICAKICAgICAgICAvLyBwbHVzCiAgICAgICAgaWYoYVtpXSA9PSBtYXh2YWwpIHgrKzsKICAgICAgICBlbHNlIGlmKGFbaV0gPT0gbWF4dmFsLTEpIHkrKzsKICAgICAgICBmb3IoaW50IGo9MDsgajx3YXlbaV0uc2l6ZSgpOyBqKyspewogICAgICAgICAgICBpbnQgcG9zID0gd2F5W2ldW2pdOwogICAgICAgICAgICBpZihhW3Bvc10gPT0gbWF4dmFsKSB4KyssIHktLTsKICAgICAgICAgICAgZWxzZSBpZihhW3Bvc10gPT0gbWF4dmFsLTEpIHkrKzsKICAgICAgICB9CiAgICAgICAgCiAgICB9CiAgICAKICAgIHByaW50ZigiJWQiLCByZXMpOwogICAgCiAgICByZXR1cm4gMDsKfQ==