#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 3e5 + 3;
int maxx = -2 * INF;
int a[maxn];
vector<int> q[maxn];
int sum1, sum2;
int num1[maxn], num2[maxn];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    if (a[i] > maxx) maxx = a[i];
  }
  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);
  }
  for (int i = 1; i <= n; i++) {
    if (a[i] == maxx) sum1++;
    if (a[i] == maxx - 1) sum2++;
  }
  for (int i = 1; i <= n; i++) {
    int c1, c2;
    c1 = 0;
    c2 = 0;
    for (int j = 0; j < q[i].size(); j++) {
      if (a[q[i][j]] == maxx - 1) c2++;
      if (a[q[i][j]] == maxx) c1++;
    }
    if (a[i] == maxx) c1++;
    num1[i] = c1;
    num2[i] = c2;
  }
  int root;
  for (int i = 1; i <= n; i++) {
    if (maxx == a[i]) {
      root = i;
      break;
    }
  }
  if (sum1 == 1) {
    if (num2[root] == sum2) {
      cout << maxx << endl;
      return 0;
    }
  }
  for (int i = 1; i <= n; i++) {
    if (sum1 == num1[i]) {
      cout << maxx + 1 << endl;
      return 0;
    }
  }
  cout << maxx + 2 << endl;
  return 0;
}
