#include <bits/stdc++.h>
using namespace std;
vector<int> conn[300010];
int vis[300010];
int cost[300010];
int mx = -2e9;
int vv = 1;
void get_cost(int u, int depth = 0) {
  if (cost[u] + depth > mx) mx = cost[u] + depth;
  vis[u] = vv;
  if (depth == 1) {
    return;
  }
  int len = conn[u].size();
  for (int i = 0; i < len; i++) {
    if (vis[conn[u][i]] != vv) get_cost(conn[u][i], depth + 1);
  }
}
bool r_cmp(pair<int, int> a, pair<int, int> b) { return a.first > b.first; }
int main() {
  int n;
  cin >> n;
  int u, v;
  vector<pair<int, int> > vec;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &cost[i]);
    vec.push_back(make_pair((int)cost[i], i));
  }
  sort(vec.begin(), vec.end(), r_cmp);
  for (int i = 1; i < n; i++) {
    scanf("%d %d", &u, &v);
    conn[u].push_back(v);
    conn[v].push_back(u);
  }
  int ans = 2e9;
  int len = vec.size();
  for (int i = 1; i <= n; i++) {
    mx = -2e9;
    vis[i] = vv;
    get_cost(i);
    for (int i = 0; i < len; i++) {
      int cst = vec[i].first, u = vec[i].second;
      if (vis[u] != vv) {
        mx = max(mx, cst + 2);
        break;
      }
    }
    ans = min(ans, mx);
    vv++;
  }
  cout << ans;
  return 0;
}
