#include <bits/stdc++.h>
using namespace std;
int n, ap[300000], x, y;
vector<int> adjlist[300000];
long long int maximum(long long int a, long long int b) {
  if (a > b) return a;
  return b;
}
class segtree {
  int sz;
  long long int *t;

 public:
  segtree(void) {
    sz = 0;
    t = 0;
  }
  segtree(int n) {
    int i = 1;
    while (i < n) i *= 2;
    sz = 2 * i - 1;
    t = new long long int[sz];
    for (i = 0; i < sz; i++) t[i] = -1000000000000000LL;
  }
  void build(int *p, int low, int high, int pos) {
    if (low == high) {
      t[pos] = p[low];
      return;
    } else {
      int mid = (low + high) / 2;
      build(p, low, mid, 2 * pos + 1);
      build(p, mid + 1, high, 2 * pos + 2);
      t[pos] = maximum(t[2 * pos + 1], t[2 * pos + 2]);
    }
  }
  void display(void) {
    for (int i = 0; i < sz; i++) cout << t[i] << " ";
    cout << endl;
  }
  void update(long long int v, int left, int right, int low, int high,
              int pos) {
    if (low > high) return;
    if (left > high || right < low) return;
    if (low == high)
      t[pos] += v;
    else {
      int mid = (low + high) / 2;
      update(v, left, right, low, mid, 2 * pos + 1);
      update(v, left, right, mid + 1, high, 2 * pos + 2);
      t[pos] = maximum(t[2 * pos + 1], t[2 * pos + 2]);
    }
  }
  long long int rmq(int left, int right, int low, int high, int pos) {
    if (low > high) return -1000000000000000LL;
    if (left > high || right < low) return -1000000000000000LL;
    if (left <= low && right >= high) {
      return t[pos];
    }
    int mid = (low + high) / 2;
    return maximum(rmq(left, right, low, mid, 2 * pos + 1),
                   rmq(left, right, mid + 1, high, 2 * pos + 2));
  }
};
segtree t(1000000);
int main() {
  cin >> n;
  long long int mx = 1000000003;
  for (int i = 0; i < n; i++) {
    scanf("%d", &ap[i]);
    ap[i] += 2;
  }
  for (int i = 0; i < n - 1; i++) {
    scanf("%d %d", &x, &y);
    x--;
    y--;
    adjlist[x].push_back(y);
    adjlist[y].push_back(x);
  }
  t.build(ap, 0, n - 1, 0);
  for (int i = 0; i < n; i++) {
    t.update(-2, i, i, 0, n - 1, 0);
    for (int j = 0; j < adjlist[i].size(); j++)
      t.update(-1, adjlist[i][j], adjlist[i][j], 0, n - 1, 0);
    mx = min(mx, t.rmq(0, n - 1, 0, n - 1, 0));
    t.update(2, i, i, 0, n - 1, 0);
    for (int j = 0; j < adjlist[i].size(); j++)
      t.update(1, adjlist[i][j], adjlist[i][j], 0, n - 1, 0);
  }
  cout << mx << endl;
  return 0;
}
