#include <bits/stdc++.h>
using namespace std;
const int maxn = 300005;
const int INF = 1e9;
int n;
int maxbank[maxn];
int bank[maxn];
vector<int> mp[maxn];
int main() {
  while (scanf("%d", &n) != EOF) {
    for (int i = 0; i < maxn; i++) mp[i].clear();
    set<int> s;
    int maxv = -INF - 1;
    for (int i = 1; i <= n; i++) {
      scanf("%d", &bank[i]);
      maxv = max(maxv, bank[i]);
    }
    for (int i = 1; i < n; i++) {
      int u, v;
      scanf("%d%d", &u, &v);
      mp[u].push_back(v);
      mp[v].push_back(u);
    }
    int max1 = 0, max2 = 0;
    int pos;
    for (int i = 1; i <= n; i++) {
      if (bank[i] == maxv) {
        max1++;
        pos = i;
      } else if (bank[i] == maxv - 1)
        max2++;
    }
    if (max1 == 1) {
      int tot = 0;
      for (int i = 0; i < mp[pos].size(); i++) {
        if (bank[mp[pos][i]] == maxv - 1) tot++;
      }
      if (tot == max2) {
        cout << maxv << endl;
        continue;
      }
    }
    int flag = 0;
    for (int i = 1; i <= n && !flag; i++) {
      int tot = 0;
      if (bank[i] == maxv) tot++;
      for (int j = 0; j < mp[i].size(); j++) {
        if (bank[mp[i][j]] == maxv) tot++;
      }
      if (tot == max1) {
        flag = 1;
        break;
      }
    }
    if (flag)
      cout << maxv + 1 << endl;
    else
      cout << maxv + 2 << endl;
  }
  return 0;
}
