#include <bits/stdc++.h>
using namespace std;
long long t = 1;
void solve() {
  int n, x, y;
  scanf("%d", &n);
  vector<int> a(n + 1);
  multiset<int> s;
  vector<vector<int> > v(n + 1);
  for (long long i = 1; i < n + 1; i += 1) {
    scanf("%d", &a[i]);
    s.insert(a[i]);
  }
  for (long long i = 1; i < n; i += 1) {
    scanf("%d%d", &x, &y);
    v[x].emplace_back(y);
    v[y].emplace_back(x);
  }
  int ans = 2 * 1e9;
  for (long long i = 1; i < n + 1; i += 1) {
    int cnt = a[i];
    s.erase(s.find(a[i]));
    for (auto const& it : v[i]) {
      cnt = max(cnt, a[it] + 1);
      s.erase(s.find(a[it]));
    }
    if (int((s).size()) > 0) {
      cnt = max(cnt, *(--s.end()) + 2);
    }
    ans = min(cnt, ans);
    for (auto const& it : v[i]) {
      s.insert(a[it]);
    }
    s.insert(a[i]);
  }
  printf("%d", ans);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  while (t--) solve();
  return 0;
}
