#include <bits/stdc++.h>
using namespace std;
long long add(long long a, long long b, long long c) {
  long long res = a + b;
  return (res >= c ? res - c : res);
}
long long mod_neg(long long a, long long b, long long c) {
  long long res;
  if (abs(a - b) < c)
    res = a - b;
  else
    res = (a - b) % c;
  return (res < 0 ? res + c : res);
}
long long mul(long long a, long long b, long long c) {
  long long res = (long long)a * b;
  return (res >= c ? res % c : res);
}
template <typename T>
T power(T e, T n) {
  T x = 1, p = e;
  while (n) {
    if (n & 1) x = x * p;
    p = p * p;
    n >>= 1;
  }
  return x;
}
template <typename T>
T mdpower(T e, T n, T m) {
  T x = 1, p = e;
  while (n) {
    if (n & 1) x = mul(x, p, m);
    p = mul(p, p, m);
    n >>= 1;
  }
  return x;
}
template <typename T>
T extended_euclid(T a, T b, T &x, T &y) {
  T xx = 0, yy = 1;
  y = 0;
  x = 1;
  while (b) {
    T q = a / b, t = b;
    b = a % b;
    a = t;
    t = xx;
    xx = x - q * xx;
    x = t;
    t = yy;
    yy = y - q * yy;
    y = t;
  }
  return a;
}
template <typename T>
T mod_inverse(T a, T n) {
  T x, y, z = 0;
  T d = extended_euclid(a, n, x, y);
  return (d > 1 ? -1 : mod_neg(x, z, n));
}
const long long N = 300005;
long long arr[N];
long long dp[N];
long long dp1[N];
long long subtree[N];
vector<long long> v[N];
void dfs(long long s, long long par) {
  for (auto it : v[s]) {
    if (it == par) continue;
    dfs(it, s);
  }
  subtree[s] = v[s].size() - 1;
}
void dfs1(long long s, long long par) {
  dp[s] = arr[s];
  for (auto it : v[s]) {
    if (it == par) continue;
    dfs1(it, s);
    dp[s] += dp[it] + subtree[it];
  }
  dp[s] += subtree[s];
}
void dfs2(long long s, long long par) {
  dp1[s] = dp1[par] + dp[par] - dp[s] - 1;
  for (auto it : v[s]) {
    if (it == par) continue;
    dfs2(it, s);
  }
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long n;
  cin >> n;
  map<long long, long long> mp;
  long long ans = INT_MAX;
  for (long long i = 1; i <= n; i++) {
    cin >> arr[i];
    mp[arr[i]]++;
  }
  for (long long i = 1; i <= n - 1; i++) {
    long long a, b;
    cin >> a >> b;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  for (long long i = 1; i <= n; i++) {
    long long node = i;
    long long ans1 = arr[node];
    mp[arr[node]]--;
    if (mp[arr[node]] == 0) {
      mp.erase(mp.find(arr[node]));
    }
    for (auto it : v[node]) {
      ans1 = max(ans1, arr[it] + 1);
      mp[arr[it]]--;
      if (mp[arr[it]] == 0) mp.erase(mp.find(arr[it]));
    }
    if (mp.size() > 0) {
      auto jt = mp.end();
      jt--;
      ans1 = max(ans1, jt->first + 2);
    }
    ans = min(ans, ans1);
    for (auto it : v[node]) {
      mp[arr[it]]++;
    }
    mp[arr[node]]++;
  }
  cout << ans << "\n";
  cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
}
