#include<bits/stdc++.h>
using namespace std;

#define int long long
typedef pair<int,int>pint;
typedef vector<int>vint;
typedef vector<pint>vpint;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define reps(i,f,n) for(int i=(f);i<(n);i++)
#define each(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)
template<class T,class U>inline void chmin(T &t,U f){if(t>f)t=f;}
template<class T,class U>inline void chmax(T &t,U f){if(t<f)t=f;}

const int SIZE=50000;
int N;
int A[SIZE];
vint G[SIZE];

int ans;
map<int,int>li_mi[SIZE],li_ma[SIZE];
int dp_mi[SIZE],dp_ma[SIZE];

void dfs(int v,int p,int t){
    if(t==0){
        for(auto to:G[v]){
            if(to==p)continue;
            dfs(to,v,1);
            if(A[v]>=A[to])continue;
            int tmp;
            if(li_ma[to].find(A[v])!=li_ma[to].end()){
                tmp=dp_ma[to]-li_ma[to][A[v]];
            }
            else{
                int hoge=li_ma[to].size();
                tmp=dp_ma[to]-hoge*(hoge-1)/2+(hoge+1)*hoge/2;
            }
            chmax(li_mi[v][A[to]],tmp);
        }
        each(it,li_mi[v])dp_mi[v]+=it->se;
        int hoge=li_mi[v].size();
        dp_mi[v]+=hoge*(hoge-1)/2;
        chmax(ans,dp_mi[v]);
    }
    else{
        for(auto to:G[v]){
            if(to==p)continue;
            dfs(to,v,0);
            if(A[v]<=A[to])continue;
            int tmp;
            if(li_mi[to].find(A[v])!=li_mi[to].end()){
                tmp=dp_mi[to]-li_mi[to][A[v]];
            }
            else{
                int hoge=li_mi[to].size();
                tmp=dp_mi[to]-hoge*(hoge-1)/2+(hoge+1)*hoge/2;
            }
            chmax(li_ma[v][A[to]],tmp);
        }
        each(it,li_ma[v])dp_ma[v]+=it->se;
        int hoge=li_ma[v].size();
        dp_ma[v]+=hoge*(hoge-1)/2;
        chmax(ans,dp_ma[v]);
    }
}

signed main(){
    scanf("%lld",&N);
    rep(i,N)scanf("%lld",&A[i]);
    rep(i,N-1){
        int a,b;
        scanf("%lld%lld",&a,&b);
        a--;b--;
        G[a].pb(b);
        G[b].pb(a);
    }

    dfs(0,-1,0);dfs(0,-1,1);
    cout<<ans<<endl;
    return 0;
}