#include <bits/stdc++.h>
using namespace std;
vector<int> g[300001];
int arr[300001];
int dp[300001];
int maxi = -(int)1e10, max_pos;
bool flag = false;
void dfs(int beta, int baap) {
  if (flag) return;
  if (arr[beta] == maxi)
    dp[beta] = 0;
  else
    dp[beta] = -(int)1e10;
  for (int i = 0; i < g[beta].size(); i++) {
    int pota = g[beta][i];
    if (pota == baap) continue;
    dfs(pota, beta);
    if (dp[pota] != -(int)1e10) {
      if (dp[beta] == -(int)1e10)
        dp[beta] = 1 + dp[pota];
      else {
        int dist = 1 + dp[beta] + dp[pota];
        if (dist > 2) flag = true;
        dp[beta] = max(dp[beta], 1 + dp[pota]);
      }
    }
  }
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &arr[i]);
    if (arr[i] > maxi) {
      maxi = arr[i];
      max_pos = i;
    }
  }
  for (int i = 1; i < n; i++) {
    int x, y;
    scanf("%d %d", &x, &y);
    g[x].push_back(y);
    g[y].push_back(x);
  }
  int max_count = 0, max_cnt = 0;
  for (int i = 1; i <= n; i++) {
    if (arr[i] == maxi) max_count++;
    if (arr[i] == maxi - 1) max_cnt++;
  }
  if (max_count == 1) {
    for (int i = 0; i < g[max_pos].size(); i++) {
      int j = g[max_pos][i];
      if (arr[j] == maxi - 1) max_cnt--;
    }
    if (max_cnt)
      printf("%d\n", maxi + 1);
    else
      printf("%d\n", maxi);
  } else {
    dfs(1, 0);
    if (flag)
      printf("%d\n", maxi + 2);
    else
      printf("%d\n", maxi + 1);
  }
  return 0;
}
