#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9 + 10;
int n, mxindex, cnt, mx = -inf, gbmax = -inf, start;
vector<int> a, adj[1000000];
void dfs1(int v, int par, int d) {
  int val = a[v];
  if (d == 2 && val == mx) start = par;
  for (auto u : adj[v])
    if (u != par) dfs1(u, v, d + 1);
}
void dfs2(int v, int par, int d) {
  int val = a[v];
  if (d == 0)
    gbmax = max(gbmax, val);
  else if (d == 1)
    gbmax = max(gbmax, val + 1);
  else
    gbmax = max(gbmax, val + 2);
  for (auto u : adj[v])
    if (u != par) dfs2(u, v, d + 1);
}
int main() {
  cin >> n;
  a.push_back(-inf);
  for (int i = 1; i <= n; i++) {
    int temp;
    cin >> temp;
    a.push_back(temp);
    if (temp > a[mxindex]) {
      mxindex = i;
      mx = temp;
    }
  }
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  start = mxindex;
  dfs1(mxindex, -1, 0);
  dfs2(start, -1, 0);
  cout << gbmax;
  return 0;
}
