#include <bits/stdc++.h>
using namespace std;
double PI = 3.14159265358979323846;
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
long long get(long long x, long long n) {
  long long l, r;
  if (x > n) return 0;
  long long acc = 0;
  if (x % 2 == 0) {
    l = x;
    r = x + 1;
  } else {
    l = 2 * x;
    r = 2 * x + 1;
    acc += 1;
  }
  while (r <= n) {
    acc += r - l + 1;
    l *= 2;
    r = 2 * r + 1;
  }
  if (l <= n) acc += (n - l + 1);
  return acc;
}
int dfs(int x, vector<bool> &chk, vector<int> &val, int step,
        vector<map<int, int> > &e) {
  chk[x] = true;
  int maxval = -1e9 - 1;
  for (auto &k : e[x]) {
    if (chk[k.first]) continue;
    maxval = max(maxval, dfs(k.first, chk, val, step + 1, e));
  }
  if (step == 0)
    maxval = max(maxval, val[x]);
  else if (step == 1)
    maxval = max(maxval, val[x] + 1);
  else
    maxval = max(maxval, val[x] + 2);
  return maxval;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  vector<int> a(n + 1);
  int mmm = -1e9 - 1;
  for (int i = 0; i < n; i++) {
    cin >> a[i + 1];
    mmm = max(mmm, a[i + 1]);
  }
  set<int> s;
  for (int i = 1; i <= n; i++) {
    if (a[i] == mmm) s.insert(i);
  }
  vector<map<int, int> > v(n + 1);
  for (int i = 0; i < n - 1; i++) {
    int x, y;
    cin >> x >> y;
    v[x][y] = 1;
    v[y][x] = 1;
  }
  vector<bool> chk(n + 1);
  if (s.size() == 1) {
    int ans = dfs(*(s.begin()), chk, a, 0, v);
    cout << ans << endl;
  } else {
    bool isok = false;
    for (int i = 1; i <= n; i++) {
      if (v[i].size() + 1 < s.size()) continue;
      bool isok2 = true;
      for (auto k : s) {
        if (v[i][k] != 1 && k != i) {
          isok2 = false;
          break;
        }
      }
      if (isok2) {
        isok = true;
        break;
      }
    }
    if (isok) {
      cout << mmm + 1 << endl;
    } else {
      cout << mmm + 2 << endl;
    }
  }
  return 0;
}
