#include <bits/stdc++.h>
using namespace std;
int n, num[300010], vis[300010], mx = -1e9 - 1, re, cnt1, cnt2, num1, num2;
vector<int> hu[300010];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &num[i]);
    mx = max(mx, num[i]);
  }
  re = mx + 2;
  for (int i = 1; i <= n - 1; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    hu[a].push_back(b);
    hu[b].push_back(a);
  }
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    if (mx == num[i]) {
      cnt1++;
    }
    if (mx - 1 == num[i]) cnt2++;
  }
  if (cnt1 == 1) re = mx + 1;
  if (cnt2 == 0 && cnt1 == 1) re = mx;
  for (int i = 1; i <= n; i++) {
    num1 = 0;
    num2 = 0;
    int bian = hu[i].size() - 1, f1 = 0, f2 = 0;
    if (num[i] == mx) f1++;
    if (num[i] == mx - 1) f2++;
    for (int j = 0; j <= bian; j++) {
      if (num[hu[i][j]] == mx) num1++;
      if (num[hu[i][j]] == mx - 1) num2++;
    }
    if ((cnt1 - f1)) {
      if (num1 == (cnt1 - f1)) re = min(re, mx + 1);
    }
    if ((cnt2 - f2)) {
      if (num2 != (cnt2 - f2) && num1 == (cnt1 - f1)) re = min(re, mx + 1);
      if ((!(cnt1 - f1) && num2 == (cnt2 - f2))) re = min(re, mx);
    }
  }
  cout << re << endl;
  return 0;
}
