#include <bits/stdc++.h>
using namespace std;
const int MAX = 500009;
int n, m, k, x, y;
int a[MAX];
int ji[MAX];
int ji2[MAX];
int flag, max1, sum1, bj, max2, sum2;
vector<int> g[MAX];
int main() {
  memset(ji2, 0, sizeof(ji2));
  memset(ji, 0, sizeof(ji));
  memset(a, 0, sizeof(a));
  scanf("%d", &n);
  max1 = -2000000000;
  max2 = -2000000000;
  sum1 = 1;
  sum2 = 2;
  bj = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    if (a[i] == max1) {
      sum1++;
    }
    if (a[i] > max1) {
      max2 = max1;
      max1 = a[i];
      sum2 = sum1;
      sum1 = 1;
    } else {
      if (a[i] > max2 && a[i] < max1) {
        max2 = a[i];
        sum2 = 1;
      } else if (a[i] == max2)
        sum2++;
    }
  }
  for (int i = 1; i <= n - 1; i++) {
    scanf("%d%d", &x, &y);
    if (a[x] == max1) ji[y]++;
    if (a[y] == max1) ji[x]++;
    if (a[x] == max1 && a[y] == max2 && max2 == max1 - 1) {
      ji2[x]++;
    }
    if (a[x] == max2 && a[y] == max1 && max2 == max1 - 1) {
      ji2[y]++;
    }
  }
  if (sum1 == 1) {
    if (max2 != max1 - 1)
      flag = 0;
    else {
      flag = 1;
      for (int i = 1; i <= n; i++) {
        if (a[i] == max1 && ji2[i] == sum2) {
          flag = 0;
          break;
        }
      }
    }
  } else {
    flag = 2;
    for (int i = 1; i <= n; i++) {
      if (a[i] != max1 && ji[i] == sum1) {
        flag = 1;
        break;
      }
      if (a[i] == max1 && ji[i] + 1 == sum1) {
        flag = 1;
        break;
      }
    }
  }
  printf("%d\n", max1 + flag);
  return 0;
}
