#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > g(300005);
int a[300005], s1[300005], s2[300005];
int main() {
  ios_base::sync_with_stdio(0);
  int n;
  cin >> n;
  for (int i(1); i <= n; i++) {
    cin >> a[i];
  }
  for (int i(0); i < n - 1; i++) {
    int x, y;
    cin >> x >> y;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  long long mx = -1e15, kol = 0, kkol = 0, mxx = -1e15;
  for (int i(1); i <= n; i++) {
    if (a[i] > mx) {
      if (mx + 1 == a[i]) {
        mxx = mx;
        kkol = kol;
      } else {
        mxx = a[i] - 1;
        kkol = 0;
      }
      kol = 1;
      mx = a[i];
      continue;
    }
    if (a[i] == mx) kol++;
    if (a[i] == mxx) kkol++;
  }
  for (int i(1); i <= n; i++) {
    for (int j(0); j < g[i].size(); j++) {
      if (a[g[i][j]] == mx)
        s1[i]++;
      else if (a[g[i][j]] == mxx)
        s2[i]++;
    }
  }
  long long ans = mx;
  if (kol > 1) ans = mx + 1;
  int ces = 0;
  for (int i(1); i <= n; i++) {
    if (s1[i] == kol - 1 && a[i] == mx && kol > 1) {
      cout << mx + 1 << endl;
      exit(0);
    }
    if (s1[i] == kol && kol > 1) {
      cout << mx + 1 << endl;
      return 0;
    }
    if (a[i] == mx && s1[i] > 1 && ces == 0) {
      ans = max(ans, mx + 1);
      ces = 1;
      continue;
    }
    if (a[i] == mx && s1[i] > 1 && ces == 1) {
      ans = max(ans, mx + 2);
      continue;
    }
    if (s1[i] > 1 && ces == 0) {
      ces = 1;
      ans = max(mx + 1, ans);
      continue;
    }
    if (s1[i] > 1 && ces == 1) {
      ans = max(ans, mx + 2);
      continue;
    }
    if (s2[i] > 0 && a[i] == mxx) {
      ans = max(ans, mx + 1);
      continue;
    }
    if (s1[i] == 1 && s2[i] > 0 && ces == 0) {
      ans = max(ans, mx + 1);
      ces = 1;
      continue;
    }
    if (s1[i] == 1 && s2[i] > 0 && ces == 1) {
      ans = max(ans, mx + 2);
      continue;
    }
    if (s1[i] == 0 && kol > 1 && a[i] == mx) {
      ans = max(ans, mx + 2);
      ces = 1;
      continue;
    }
    if (s1[i] == kol && s2[i] == 0 && kkol > 0 && a[i] != mx) {
      ans = max(ans, mx + 1);
      continue;
    }
    if (a[i] == mx && kol == 1 && s2[i] == kkol) {
      cout << mx << endl;
      exit(0);
    }
  }
  cout << ans << endl;
}
