#include <bits/stdc++.h>
using namespace std;
const int MAXN = 3e5 + 5;
const int MOD = 1e9 + 7;
long long powmod(long long a, long long b) {
  long long res = 1;
  a %= MOD;
  for (; b; b >>= 1) {
    if (b & 1) res = res * a % MOD;
    a = a * a % MOD;
  }
  return res;
}
struct node {
  int x, val;
} a[MAXN];
int head[MAXN << 1], tot, u, v;
map<int, map<int, int>> mp;
bool cmp(node a, node b) { return a.val > b.val; }
int main() {
  int n;
  while (~scanf("%d", &n)) {
    mp.clear();
    for (int i = 1; i <= n; i++) {
      scanf("%d", &a[i].val);
      a[i].x = i;
    }
    sort(a + 1, a + n + 1, cmp);
    for (int i = 1; i <= n - 1; i++) {
      scanf("%d%d", &u, &v);
      mp[u][v] = 1;
      mp[v][u] = 1;
    }
    int ans = 1e9 + 5;
    a[0].val = a[1].val;
    int temp;
    for (int i = 1; i <= n; i++) {
      if (a[i].val - a[i - 1].val > 2) break;
      temp = a[i].val;
      for (int j = 1; j <= n; j++) {
        if (i == j) continue;
        if (a[j].val + 2 <= temp) break;
        if (mp[a[i].x][a[j].x]) {
          temp = max(temp, a[j].val + 1);
        } else {
          temp = max(temp, a[j].val + 2);
        }
      }
      ans = min(ans, temp);
    }
    printf("%d\n", ans);
  }
  return 0;
}
