#include <bits/stdc++.h>
using namespace std;
long long n;
vector<vector<pair<long long, long long>>> a;
vector<long long> b;
vector<bool> used;
long long DFS(long long n, long long delta, long long steps) {
  used[n] = true;
  long long answer = b[n] + delta;
  long long begdelta = delta;
  for (auto &i : a[n]) {
    if (!used[i.second]) {
      answer = max(answer, DFS(i.second, delta + 1 - steps, delta - steps));
    }
  }
  return answer;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n;
  a.resize(n);
  b.resize(n);
  used.resize(n);
  for (long long &i : b) {
    cin >> i;
  }
  for (long long i = 0; i < n - 1; i++) {
    long long a1, b1;
    cin >> a1 >> b1;
    a1--;
    b1--;
    a[a1].push_back(make_pair(b[b1], b1));
    a[b1].push_back(make_pair(b[a1], a1));
  }
  long long maxt = -10000000001;
  for (long long i = 0; i < n; i++) {
    sort(a[i].begin(), a[i].end());
    reverse(a[i].begin(), a[i].end());
  }
  vector<long long> carma(n);
  for (long long i = 0; i < n; i++) {
    if (b[i] > maxt) {
      maxt = b[i];
    }
  }
  for (long long i = 0; i < n; i++) {
    if (b[i] != maxt) {
      carma[i] = -1;
    }
    for (long long j = 0; j < a[i].size(); j++) {
      carma[i] += max(0LL, a[i][j].first + 1 - maxt);
    }
  }
  maxt = 0;
  for (long long i = 0; i < n; i++) {
    if (carma[i] > carma[maxt] || (carma[i] == carma[maxt] && b[i] > b[maxt])) {
      maxt = i;
    }
  }
  cout << DFS(maxt, 0, 0) << endl;
  return 0;
}
