#include <bits/stdc++.h>
using namespace std;
const long long mod = (long long)1e9 + 9;
const int inf = (int)2e9 + 10;
const long long INF = (long long)2e18;
const int base = 1000 * 1000 * 1000;
const int maxn = 200005;
const long double pi = acosl(-1.0);
const long double eps = 1e-9;
vector<vector<int>> g;
vector<pair<int, int>> t;
vector<int> d;
int ans = inf;
multiset<int> st;
void dfs(int v, int p = -1) {
  int can = d[v];
  st.erase(st.find(d[v]));
  for (auto to : g[v]) {
    st.erase(st.find(d[to]));
  }
  if (!st.empty()) {
    can = max(can, *st.rbegin() + 2);
  }
  for (auto to : g[v]) {
    st.insert(d[to]);
  }
  st.insert(d[v]);
  for (auto to : g[v]) {
    can = max(can, d[to] + 1);
    if (to == p) continue;
    dfs(to, v);
  }
  ans = min(ans, can);
}
void dfs2(int v, int p = -1) {
  int can = -inf;
  for (auto to : g[v]) {
    if (t[to].first == d[v]) {
      can = max(can, t[to].second);
    } else {
      can = max(can, t[to].first);
    }
    if (to == p) continue;
    dfs2(to, v);
  }
  ans = min(ans, max(d[v], max(t[v].first + 1, can + 2)));
}
void solve() {
  int n;
  scanf("%d", &n);
  d.resize(n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &d[i]);
    st.insert(d[i]);
  }
  g.resize(n);
  t.resize(n, make_pair(-inf, -inf));
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    scanf("%d %d", &a, &b);
    a--, b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  dfs(0);
  printf("%d", ans);
}
int main() {
  solve();
  return 0;
}
