#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 1e5 + 20;
const double eps = 1e-8;
int n, a[N * 3];
vector<int> edg[3 * N];
int main() {
  while (cin >> n) {
    int st = 0;
    map<int, int, greater<int> > q;
    for (int i = 1; i <= n; i++) {
      scanf("%d", a + i);
      edg[i].clear();
      q[a[i]]++;
    }
    for (int i = 0; i < n - 1; i++) {
      int x, y;
      scanf("%d%d", &x, &y);
      edg[x].push_back((y));
      edg[y].push_back((x));
    }
    int ma = 0;
    int ans = INF;
    for (int i = 1; i <= n; i++) {
      q[a[i]]--;
      ma = a[i];
      for (auto j : edg[i]) {
        q[a[j]]--;
        ma = max(ma, a[j] + 1);
      }
      for (auto j : q)
        if (j.second > 0) {
          ma = max(ma, j.first + 2);
          break;
        }
      for (auto j : edg[i]) q[a[j]]++;
      q[a[i]]++;
      ans = min(ma, ans);
    }
    printf("%d\n", ans);
  }
  return 0;
}
