#include <bits/stdc++.h>
using namespace std;
const int MX = 5e5;
vector<long long> adj[MX];
long long a[MX];
multiset<long long> s;
long long getMax(multiset<long long> &s) {
  auto it = s.end();
  it--;
  return *it;
}
void add(long long u) {
  s.erase(s.find(a[u] + 2));
  s.insert(a[u]);
  for (long long v : adj[u]) {
    s.erase(s.find(a[v] + 2));
    s.insert(a[v] + 1);
  }
}
void erase(long long u) {
  s.erase(s.find(a[u]));
  s.insert(a[u] + 2);
  for (long long v : adj[u]) {
    s.erase(s.find(a[v] + 1));
    s.insert(a[v] + 2);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long n;
  cin >> n;
  for (long long i = 0; i < (long long)n; i++) {
    cin >> a[i];
    s.insert(a[i] + 2);
  }
  for (long long i = 0; i < (long long)n - 1; i++) {
    long long u, v;
    cin >> u >> v;
    u--;
    v--;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  long long ans = getMax(s);
  for (int u = 0; u < n; u++) {
    add(u);
    ans = min(ans, getMax(s));
    erase(u);
  }
  cout << ans << "\n";
  return 0;
}
