#include <bits/stdc++.h>
using namespace std;
const int maxN = 3e5 + 10;
vector<int> edge[maxN];
int a[maxN];
int solve(int u, int maxv, int cntv, int cntv_) {
  if (a[u] == maxv + 2)
    cntv--;
  else if (a[u] == maxv + 1)
    cntv_--;
  int ans = a[u] - 2;
  for (int i = 0; i < edge[u].size(); i++) {
    int v = edge[u][i];
    ans = max(ans, a[v] - 1);
    if (a[v] == maxv + 2)
      cntv--;
    else if (a[v] == maxv + 1)
      cntv_--;
  }
  if (cntv) return maxv + 2;
  if (cntv_) return max(ans, maxv + 1);
  return ans;
}
int main() {
  int n;
  scanf("%d", &n);
  int maxv = -1e9 - 10, cntv = 0, cntv_ = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d", a + i);
    maxv = max(maxv, a[i]);
  }
  for (int i = 1; i <= n; i++) {
    if (a[i] == maxv)
      cntv++;
    else if (a[i] == maxv - 1)
      cntv_++;
    a[i] += 2;
  }
  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    edge[u].push_back(v);
    edge[v].push_back(u);
  }
  int ans = maxv + 2;
  for (int u = 1; u <= n; u++) {
    ans = min(ans, solve(u, maxv, cntv, cntv_));
  }
  printf("%d", ans);
  return 0;
}
