#include <bits/stdc++.h>
using namespace std;
bool desc_sort(const int& lhs, const int& rhs) { return lhs > rhs; }
bool desc_pii_sort(const pair<int, int>& lhs, const pair<int, int>& rhs) {
  return lhs > rhs;
}
bool second_sort(const pair<int, int>& lhs, const pair<int, int>& rhs) {
  pair<int, int> a, b;
  a.first = lhs.second;
  a.second = lhs.first;
  b.first = rhs.second;
  b.second = rhs.first;
  return a < b;
}
template <class T>
ostream& operator<<(ostream& os, vector<T> V) {
  os << "[ ";
  for (auto v : V) os << v << " ";
  return os << "]";
}
template <class T>
ostream& operator<<(ostream& os, set<T> second) {
  os << "{ ";
  for (auto s : second) os << s << " ";
  return os << "}";
}
template <class L, class R>
ostream& operator<<(ostream& os, pair<L, R> P) {
  return os << "(" << P.first << "," << P.second << ")";
}
template <class L, class R>
ostream& operator<<(ostream& os, map<L, R> M) {
  os << "{ ";
  for (auto m : M) os << "(" << m.first << ":" << m.second << ") ";
  return os << "}";
}
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cerr << name << " : " << arg1 << "\n";
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
inline long long fast_expo(long long base, long long power,
                           long long modulo = 1e0) {
  base %= modulo;
  if (base < 0LL) base += modulo;
  long long x = base, cnt = power, ans = 1LL;
  while (cnt) {
    if (cnt & 1LL) ans = (ans * x) % modulo;
    x = (x * x) % modulo;
    cnt >>= 1LL;
  }
  return ans;
}
inline long long inv(long long base, long long modulo = 1e0) {
  return fast_expo(base, modulo - 2LL, modulo);
}
int a[300001];
vector<int> adj[300001];
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  int m = -1000000001;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    m = max(m, a[i]);
  }
  int cc_m = 0, cc_m1 = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i] == m)
      cc_m++;
    else if (a[i] == m - 1)
      cc_m1++;
  }
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  int ans = m + 2;
  for (int i = 1; i <= n; i++) {
    int ccm, ccm1;
    ccm = cc_m;
    ccm1 = cc_m1;
    int r = a[i];
    if (a[i] == m)
      ccm--;
    else if (a[i] == m - 1)
      ccm1--;
    for (auto x : adj[i]) {
      r = max(r, a[x] + 1);
      if (a[x] == m)
        ccm--;
      else if (a[x] == m - 1)
        ccm1--;
    }
    if (ccm != 0)
      r = max(r, m + 2);
    else if (ccm1 != 0)
      r = max(r, m + 1);
    ans = min(ans, r);
  }
  cout << ans << "\n";
  return 0;
}
