#include <bits/stdc++.h>
using namespace std;
const int maxn = 300005;
pair<int, int> s[maxn];
pair<int, int> s_copy[maxn];
vector<int> chart[maxn];
bool vis[maxn];
int ans[maxn];
bool cmp(pair<int, int> a, pair<int, int> b) { return a.second > b.second; }
int main() {
  memset(vis, false, sizeof vis);
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &s[i].second);
    s[i].first = i;
    s_copy[i] = s[i];
  }
  for (int i = 1; i < n; ++i) {
    int u, v;
    scanf("%d%d", &u, &v);
    chart[u].push_back(v);
    chart[v].push_back(u);
  }
  sort(s_copy + 1, s_copy + n + 1, cmp);
  for (int i = 1; i <= n; ++i) ans[i] = -0x3f3f3f3f;
  for (int i = 1; i <= n; ++i) {
    ans[i] = max(ans[i], s[i].second);
    vis[i] = true;
    int len = chart[i].size();
    for (int j = 0; j < len; ++j) {
      int u = chart[i][j];
      vis[u] = true;
      ans[i] = max(ans[i], s[u].second + 1);
    }
    bool flag = true;
    for (int j = 1; j <= n && flag; ++j) {
      if (!vis[s_copy[j].first])
        ans[i] = max(ans[i], s_copy[j].second + 2), flag = false;
    }
    vis[i] = false;
    for (int j = 0; j < len; ++j) vis[chart[i][j]] = false;
  }
  int res = 1;
  for (int i = 1; i <= n; ++i)
    if (ans[i] < ans[res]) res = i;
  cout << ans[res] << endl;
  return 0;
}
