#include <bits/stdc++.h>
using namespace std;
const int N = 300010;
vector<int> q[N];
int w[N];
int main() {
  int n, m = -1e9;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &w[i]), m = max(m, w[i]);
  int need1 = 0, need2 = 0;
  for (int i = 1; i <= n; i++)
    if (w[i] == m)
      need2++;
    else if (w[i] == m - 1)
      need1++;
  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    q[u].push_back(v);
    q[v].push_back(u);
  }
  int res = m + 2;
  for (int i = 1; i <= n; i++) {
    if (w[i] == m)
      need2--;
    else if (w[i] == m - 1)
      need1--;
    for (int j = 0; j < q[i].size(); j++) {
      int temp = q[i][j];
      if (w[temp] == m)
        need2--, need1++;
      else if (w[temp] == m - 1)
        need1--;
      if (need2 == 0 && need1 == 0) break;
    }
    if (need2 == 0) {
      if (need1 == 0) {
        res = min(res, m);
        break;
      }
      res = min(res, m + 1);
    }
    if (w[i] == m)
      need2++;
    else if (w[i] == m - 1)
      need1++;
    for (int j = 0; j < q[i].size(); j++) {
      int temp = q[i][j];
      if (w[temp] == m)
        need2++, need1--;
      else if (w[temp] == m - 1)
        need1++;
    }
  }
  printf("%d\n", res);
  return 0;
}
