#include <bits/stdc++.h>
using namespace std;
int n, a[300000 + 10], ans = 2e9;
vector<int> lj[300000 + 10];
multiset<int> s;
int main() {
  int i;
  scanf("%d", &n);
  for (i = 1; i <= n; ++i) scanf("%d", &a[i]), s.insert(a[i]);
  for (i = 1; i < n; ++i) {
    int u, v;
    scanf("%d%d", &u, &v);
    lj[u].push_back(v);
    lj[v].push_back(u);
  }
  for (i = 1; i <= n; ++i) {
    int da = a[i];
    s.erase(s.find(a[i]));
    for (int j : lj[i]) da = max(da, a[j] + 1), s.erase(s.find(a[j]));
    if (!s.empty()) da = max(da, *s.rbegin() + 2);
    s.insert(a[i]);
    for (int j : lj[i]) s.insert(a[j]);
    ans = min(da, ans);
  }
  printf("%d", ans);
}
