#include <bits/stdc++.h>
using namespace std;
int n;
int a[300010];
vector<int> v[300010];
int main() {
  scanf(" %d", &n);
  int maxn = -0x3f3f3f3f;
  for (int i = 0; i < n; i++) {
    scanf(" %d", &a[i]);
    maxn = max(maxn, a[i]);
  }
  for (int i = 0; i < n - 1; i++) {
    int x, y;
    scanf(" %d %d", &x, &y);
    x--;
    y--;
    v[x].push_back(y);
    v[y].push_back(x);
  }
  int cnt_maxn = 0, cnt_maxn_min_1 = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] == maxn)
      cnt_maxn++;
    else if (a[i] == maxn - 1)
      cnt_maxn_min_1++;
  }
  if (cnt_maxn == 1) {
    for (int i = 0; i < n; i++) {
      if (a[i] == maxn) {
        int cnt = 0;
        for (int j = 0; j < v[i].size(); j++) {
          if (a[v[i][j]] == maxn - 1) cnt++;
        }
        if (cnt == cnt_maxn_min_1) {
          printf("%d\n", maxn);
          return 0;
        } else {
          printf("%d\n", maxn + 1);
          return 0;
        }
      }
    }
  } else {
    for (int i = 0; i < n; i++) {
      int cnt = 0;
      if (a[i] == maxn) cnt++;
      for (int j = 0; j < v[i].size(); j++) {
        if (a[v[i][j]] == maxn) cnt++;
      }
      if (cnt == cnt_maxn) {
        printf("%d\n", maxn + 1);
        return 0;
      }
    }
    printf("%d\n", maxn + 2);
  }
  return 0;
}
