#include <bits/stdc++.h>
using namespace std;
vector<int> edg[300005];
int a[300005];
map<int, int> cnt;
int MAX = INT_MIN, dis[300005];
vector<int> temp1, temp2;
bool vis[300005];
map<int, int> st;
void dfs(int i) {
  vis[i] = true;
  for (int j = 0; j < edg[i].size(); j++) {
    if (!vis[edg[i][j]]) {
      dis[edg[i][j]] = dis[i] + 1;
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  ;
  int n;
  cin >> n;
  int q = -1;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (cnt.count(a[i]) != 0)
      cnt[a[i]]++;
    else
      cnt[a[i]] = 1;
    if (a[i] > MAX) {
      q = i;
      MAX = a[i];
    }
  }
  for (int i = 1; i <= n; i++) {
    if (a[i] == MAX)
      temp1.push_back(i);
    else if (a[i] == MAX - 1)
      temp2.push_back(i);
  }
  for (int i = 1; i <= n - 1; i++) {
    int u, v;
    cin >> u >> v;
    edg[u].push_back(v);
    edg[v].push_back(u);
  }
  if (cnt[MAX] == 1) {
    if (cnt.count(MAX - 1) == 0)
      cout << MAX << endl;
    else {
      memset(dis, 0, sizeof(dis));
      for (int i = 0; i < 300005; i++) vis[i] = false;
      dfs(q);
      bool pos = true;
      for (int i = 0; i < temp2.size(); i++) {
        if (dis[temp2[i]] != 1) {
          pos = false;
          break;
        }
      }
      if (pos)
        cout << MAX << endl;
      else
        cout << MAX + 1 << endl;
    }
  } else {
    for (int i = 0; i < temp1.size(); i++) {
      for (int j = 0; j < edg[temp1[i]].size(); j++) {
        st[edg[temp1[i]][j]] += 1;
      }
    }
    map<int, int>::iterator it;
    for (it = st.begin(); it != st.end(); it++) {
      if (a[it->first] != MAX and it->second == temp1.size()) {
        cout << MAX + 1 << endl;
        return 0;
      }
    }
    for (it = st.begin(); it != st.end(); it++) {
      if (a[it->first] == MAX and it->second == temp1.size() - 1) {
        cout << MAX + 1 << endl;
        return 0;
      }
    }
    cout << MAX + 2 << endl;
  }
}
