#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e15 + 7;
const long long MAX = 3e5 + 7;
const long long MOD = 1e9 + 7;
typedef vector<long long> vi;
typedef vector<vi> vvi;
typedef vector<pair<long long, long long> > vii;
typedef vector<vii> vvii;
typedef vector<bool> vb;
long long n, k, parent[MAX], st[MAX * 4];
void update(long long val, long long x) {
  st[x + k] = val, x += k;
  while (x > 0) {
    x /= 2;
    st[x] = max(st[x * 2], st[x * 2 + 1]);
  }
}
long long check(long long l, long long r, long long ans = -INF) {
  l += k, r += k;
  while (l < r) {
    if (l % 2 != 0) ans = max(ans, st[l++]);
    if (r % 2 == 0) ans = max(ans, st[r--]);
    l /= 2, r /= 2;
  }
  if (l == r) ans = max(ans, st[l]);
  return ans;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long i, j, x, y, ans;
  cin >> n;
  k = 1;
  while (k <= n) k *= 2;
  for (i = 1; i <= n; i++) cin >> parent[i], update(parent[i], i);
  vvi node(MAX);
  for (i = 0; i < n - 1; i++)
    cin >> x >> y, node[x].push_back(y), node[y].push_back(x);
  ans = INF;
  for (i = 1; i <= n; i++) {
    update(parent[i] - 2, i);
    for (j = 0; j < node[i].size(); j++)
      update(parent[node[i][j]] - 1, node[i][j]);
    ans = min(ans, check(1, n) + 2);
    update(parent[i], i);
    for (j = 0; j < node[i].size(); j++) update(parent[node[i][j]], node[i][j]);
  }
  cout << ans << "\n";
  return 0;
}
