#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 20;
int n, i, a[N], u, v, ans = 1e9 + 20;
vector<int> adj[N];
multiset<int> ms;
int main() {
  scanf("%d", &n);
  for (i = 1; i <= n; i++) scanf("%d", &a[i]), ms.insert(a[i]);
  for (i = 0; i < n - 1; i++) {
    scanf("%d%d", &u, &v);
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  for (i = 1; i <= n; i++) {
    int tans = a[i];
    ms.erase(ms.find(a[i]));
    for (auto j : adj[i]) {
      tans = max(tans, a[j] + 1);
      ms.erase(ms.find(a[j]));
    }
    if (!ms.empty()) tans = max(tans, *--ms.end() + 2);
    for (auto j : adj[i]) ms.insert(a[j]);
    ms.insert(a[i]);
    ans = min(ans, tans);
  }
  cout << ans;
}
