#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 10;
int a[maxn];
vector<int> E[maxn];
multiset<int, greater<int> > s;
int main() {
  std::ios::sync_with_stdio(false);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    s.insert(a[i]);
  }
  for (int i = 1; i < n; i++) {
    int x, y;
    cin >> x >> y;
    E[x].push_back(y);
    E[y].push_back(x);
  }
  int ans = 2e9;
  for (int i = 1; i <= n; i++) {
    auto it = s.find(a[i]);
    s.erase(it);
    for (auto v : E[i]) {
      it = s.find(a[v]);
      s.erase(it);
    }
    int tmp = -2e9;
    if (!s.empty()) tmp = max(tmp, (*s.begin()) + 2);
    for (auto v : E[i]) {
      s.insert(a[v]);
      tmp = max(tmp, a[v] + 1);
    }
    s.insert(a[i]);
    tmp = max(tmp, a[i]);
    ans = min(ans, tmp);
  }
  cout << ans << "\n";
  return 0;
}
