#include <bits/stdc++.h>
using namespace std;
int viz[300010];
long x, y, i, n, in, j;
long long vl[300010], mx;
vector<long> a[300010];
void dfs(long x) {
  viz[x] = 1;
  mx = max(mx, vl[x]);
  for (long i = 0; i < a[x].size(); i++)
    if (!viz[a[x][i]]) {
      if (x != in)
        vl[a[x][i]] += 2;
      else
        vl[a[x][i]]++;
      dfs(a[x][i]);
    }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cerr.tie(0);
  cout.tie(0);
  cin >> n >> vl[0];
  mx = vl[0];
  for (i = 1; i < n; i++) {
    cin >> vl[i];
    if (mx < vl[i]) mx = vl[i], in = i;
  }
  for (i = 1; i < n; i++) {
    cin >> x >> y;
    x--, y--;
    a[x].push_back(y);
    a[y].push_back(x);
  }
  for (i = 0; i < n; i++) {
    int c = 0;
    for (j = 0; j < a[i].size(); j++)
      if (mx == vl[a[i][j]]) c++;
    if (c > 1) {
      in = i;
      break;
    }
  }
  dfs(in);
  cout << mx << endl;
  return 0;
}
