#include <bits/stdc++.h>
const long long maxn = 1e6 + 5;
const long long mod = 1e9 + 7;
const long long base = 26;
using namespace std;
vector<long long> g[maxn];
long long a[maxn];
long long n;
bool mark[maxn];
vector<long long> pos;
multiset<long long> ms;
void read(void) {
  long long mx = -1e10;
  long long j;
  cin >> n;
  for (long long i = 1; i <= (long long)(n); ++i) {
    cin >> a[i];
    mx = max(mx, a[i]);
    ms.insert(a[i]);
  }
  for (long long i = 1; i <= (long long)(n - 1); ++i) {
    long long u, v;
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  long long ans = 1e10 + 1;
  for (long long i = 1; i <= (long long)(n); ++i) {
    long long p = a[i];
    ms.erase(ms.find(p));
    for (auto &x : g[i]) {
      ms.erase(ms.find(a[x]));
      p = max(p, a[x] + 1);
    }
    auto tmp = ms.end();
    if (!ms.empty()) {
      tmp--;
      p = max(p, *tmp + 2);
    }
    for (auto &x : g[i]) ms.insert(a[x]);
    ms.insert(a[i]);
    ans = min(ans, p);
  }
  cout << ans;
}
signed main(void) {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  read();
}
