#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
const int N = 3 * 1e5 + 1;
int n, dp[N][3], a[N], maxm[N][2];
vector<vector<int>> reb;
bool used[N];
void dfs(int u) {
  used[u] = 1;
  maxm[u][1] = a[u] - 2;
  maxm[u][0] = a[u] - 1;
  dp[u][0] = a[u];
  dp[u][1] = MOD;
  dp[u][2] = MOD;
  for (vector<int>::iterator it = reb[u].begin(); it != reb[u].end(); it++) {
    if (used[*it]) continue;
    dfs(*it);
    dp[u][1] =
        max(max(max(dp[u][1], a[*it] + 2), maxm[*it][0] + 2), maxm[*it][1] + 2);
    dp[u][2] =
        max(max(max(dp[u][2], a[*it] + 2), maxm[*it][0] + 2), maxm[*it][1] + 2);
    dp[u][1] = min(dp[u][1], max(max(max(dp[*it][0], a[u] + 1), maxm[u][1] + 2),
                                 maxm[u][0] + 2));
    dp[u][2] =
        min(dp[u][2], max(max(max(min(dp[*it][1], dp[*it][2]), maxm[u][1] + 2),
                              maxm[u][0] + 2),
                          a[u] + 2));
    maxm[u][0] = max(maxm[u][0], a[*it]);
    maxm[u][1] = max(max(maxm[u][1], maxm[*it][0]), maxm[*it][1]);
  }
  dp[u][0] = max(max(a[u], maxm[u][0] + 1), maxm[u][1] + 2);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(20);
  cin >> n;
  reb.resize(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v;
    reb[u].push_back(v);
    reb[v].push_back(u);
  }
  dfs(1);
  cout << min(min(dp[1][0], dp[1][1]), dp[1][2]);
  return 0;
}
