#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 9;
long long arr[maxn];
int n, u, v;
int vis[maxn];
int power[maxn];
vector<int> aaa[maxn];
void dfs(int x, int dep) {
  vis[x] = 1;
  if (dep == 1) {
    arr[x]++;
  } else if (dep > 1) {
    arr[x] += 2;
  }
  for (int i = 0; i < aaa[x].size(); i++) {
    if (!vis[aaa[x][i]]) {
      dfs(aaa[x][i], dep + 1);
    }
  }
}
int c1[maxn];
int c2[maxn];
int main() {
  scanf("%d", &n);
  int root;
  long long ans = -0x3f3f3f3f;
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &arr[i]);
    if (arr[i] >= ans) {
      ans = arr[i];
      root = i;
    }
  }
  int x, y;
  for (int i = 1; i < n; i++) {
    scanf("%d %d", &x, &y);
    aaa[x].push_back(y);
    aaa[y].push_back(x);
  }
  int cnt1 = 0, cnt2 = 0;
  for (int i = 1; i <= n; i++) {
    if (arr[i] == ans) {
      c1[i]++;
      for (int j = 0; j < aaa[i].size(); j++) {
        c1[aaa[i][j]]++;
      }
      cnt1++;
    } else if (arr[i] + 1 == ans) {
      c2[i]++;
      for (int j = 0; j < aaa[i].size(); j++) {
        c2[aaa[i][j]]++;
      }
      cnt2++;
    }
  }
  int m1 = 0, m2 = 0;
  for (int i = 1; i <= n; i++) {
    m1 = max(m1, c1[i]);
    m2 = max(m2, c2[i]);
  }
  if (cnt1 == 1 && c2[root] == cnt2) {
    printf("%lld\n", ans);
  } else if (cnt1 == m1) {
    printf("%lld\n", ans + 1);
  } else {
    printf("%lld\n", ans + 2);
  }
}
