#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5;
const long long inf = 1e15;
long long a[N];
multiset<long long, greater<long long>> s;
vector<int> g[N];
int main() {
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    s.insert(a[i]);
  }
  for (int i = 0; i < n - 1; ++i) {
    int u, v;
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  long long ans = inf;
  for (int i = 1; i <= n; ++i) {
    long long temp = a[i];
    s.erase(s.find(a[i]));
    for (auto &j : g[i]) {
      s.erase(s.find(a[j]));
      temp = max(temp, a[j] + 1);
    }
    if (!s.empty()) temp = max(temp, (*s.begin()) + 2);
    ans = min(ans, temp);
    s.insert(a[i]);
    for (auto &j : g[i]) s.insert(a[j]);
  }
  cout << ans;
}
