#include <bits/stdc++.h>
using namespace std;
vector<int> v[300005];
int main() {
  int n, i, b, c, c2 = 0, c1 = 0, c0 = 0, j, maxi = -(1e9 + 5), ans = 1e9 + 5,
                  a[300005], tc0, tc1, tc2;
  scanf("%d", &n);
  ;
  for (i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    ;
    maxi = max(maxi, a[i]);
    a[i] = a[i] + 2;
  }
  for (i = 1; i < n; i++) {
    scanf("%d %d", &b, &c);
    ;
    v[b].push_back(c);
    v[c].push_back(b);
  }
  for (i = 1; i <= n; i++) {
    if (a[i] == maxi + 2) {
      c2++;
    } else if (a[i] == maxi + 1) {
      c1++;
    } else if (a[i] == maxi) {
      c0++;
    }
  }
  for (i = 1; i <= n; i++) {
    tc2 = c2;
    tc1 = c1;
    tc0 = c0;
    if (a[i] == maxi + 2) {
      tc2--;
      tc0++;
    }
    for (j = 0; j < v[i].size(); j++) {
      if (a[v[i][j]] == maxi + 2) {
        tc2--;
        tc1++;
      } else if (a[v[i][j]] == maxi + 1) {
        tc1--;
        tc0++;
      }
    }
    if (tc2 > 0) {
      ans = min(ans, maxi + 2);
    } else if (tc1 > 0) {
      ans = min(ans, maxi + 1);
    } else {
      ans = min(ans, maxi);
    };
  }
  printf("%d", ans);
  ;
}
