#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 100;
const int N = 3e5;
vector<int> g[N];
int a[N];
int f[N];
int res = INF;
void dfsf(int v, int p) {
  f[v] = -INF;
  for (auto u : g[v]) {
    if (u == p) continue;
    dfsf(u, v);
    f[v] = max(f[v], max(a[u], f[u]));
  }
}
void dfs(int v, int p, int mu2) {
  int mc = -INF;
  pair<int, int> m21 = make_pair(-INF, -1);
  pair<int, int> m22 = make_pair(-INF, -1);
  for (auto u : g[v]) {
    if (u == p) continue;
    mc = max(mc, a[u]);
    int x = max(a[u], f[u]);
    if (x > m21.first) {
      m22 = m21;
      m21 = make_pair(x, u);
    } else if (x > m22.first) {
      m22 = make_pair(x, u);
    }
  }
  int m2 = mu2;
  for (auto u : g[v]) {
    if (u == p) continue;
    int newMu2 = mu2;
    if (p != -1) newMu2 = max(newMu2, a[p]);
    if (m21.second == u) {
      newMu2 = max(newMu2, m22.first);
    } else {
      newMu2 = max(newMu2, m21.first);
    }
    dfs(u, v, newMu2);
    m2 = max(m2, f[u]);
  }
  int m1 = max(mc, (p != -1 ? a[p] : -INF));
  res = min(res, max(a[v], max(m1 + 1, m2 + 2)));
  for (auto u : g[v]) {
    if (u == p) continue;
    f[v] = max(f[v], max(a[u], f[u]));
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  for (int i = 0; i < n - 1; ++i) {
    int v, u;
    cin >> v >> u;
    --v, --u;
    g[v].push_back(u);
    g[u].push_back(v);
  }
  dfsf(0, -1);
  dfs(0, -1, -INF);
  cout << res << endl;
  return 0;
}
