#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
const int N = 3e5 + 5;
const int INF = 1e9 + 7;
vector<int> graph[N];
multiset<int, greater<int>> st;
int n, a, b, dat[N], ans, cur;
void rem(int val) {
  auto it = st.find(val);
  st.erase(it);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  ans = INF;
  cin >> n;
  for (int i = (0); i < (n); i++) {
    cin >> dat[i];
    st.insert(dat[i]);
  }
  for (int i = (0); i < (n - 1); i++) {
    cin >> a >> b;
    --a;
    --b;
    graph[a].push_back(b);
    graph[b].push_back(a);
  }
  for (int i = (0); i < (n); i++) {
    rem(dat[i]);
    for (auto it : graph[i]) rem(dat[it]);
    cur = -INF;
    cur = max(cur, dat[i]);
    if (!st.empty()) cur = max(cur, *st.begin() + 2);
    st.insert(dat[i]);
    for (auto it : graph[i]) {
      st.insert(dat[it]);
      cur = max(cur, dat[it] + 1);
    }
    ans = min(ans, cur);
  }
  cout << ans << "\n";
  return 0;
}
