#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000005;
int n, m, k;
long long a[MAXN];
vector<int> G[MAXN];
vector<int> M1, M2;
long long Max;
long long ans;
long long solve(int x) {
  long long ret = 0;
  int cnt1 = 0, cnt2 = 0;
  for (int i = 0; i < G[x].size(); i++) {
    if (a[G[x][i]] == Max) cnt1++;
    if (a[G[x][i]] == Max - 1) cnt2++;
  }
  if (a[x] == Max) cnt1++;
  if (a[x] == Max - 1) cnt2++;
  if (cnt1 < M1.size())
    ret = Max + 2;
  else if (M1.size() == 1 && a[x] == Max)
    ret = Max;
  else
    ret = Max + 1;
  if (cnt2 < M2.size())
    ret = max(ret, Max + 1);
  else if (M2.size() == 1 && a[x] == Max - 1)
    ret = max(ret, Max - 1);
  else
    ret = max(ret, Max);
  return ret;
}
int main() {
  scanf("%d", &n);
  Max = -1e10;
  for (int i = 1; i <= n; i++) scanf("%lld", &a[i]), Max = max(Max, a[i]);
  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    G[u].push_back(v);
    G[v].push_back(u);
  }
  for (int i = 1; i <= n; i++) {
    if (a[i] == Max) M1.push_back(i);
    if (a[i] == Max - 1) M2.push_back(i);
  }
  ans = Max + 10;
  for (int i = 1; i <= n; i++) {
    ans = min(ans, solve(i));
  }
  cout << ans << endl;
}
