#include using namespace std; #define rep(i,a,b) for(int i=int(a);i>N; vector A(N); rep(i,0,N) cin>>A[i]; vector order(N); rep(i,0,N) order[i]=i; sort(all(order),[&](int l,int r){ return A[l]>A[r]; }); vector> G(N); rep(i,0,N-1){ int a,b; cin>>a>>b; a--,b--; G[a].push_back(b); G[b].push_back(a); } int B=0; while((1< ans(N); vector depth(N,-1); vector E(N*2); vector table(B,vector(N*2)); int ind=0; auto dfs=[&](auto self,int var) -> void { E[var]=ind++; for(auto x:G[var]){ if(depth[x]==-1){ depth[x]=depth[var]+1; self(self,x); } } E[var+N]=ind++; }; depth[0]=0; dfs(dfs,0); rep(i,0,N){ table[0][E[i]]=depth[i]; table[0][E[i+N]]=depth[i]-1; } rep(i,0,B-1) rep(j,0,N*2){ table[i+1][j]=table[i][j]; if(j+(1< int { if(E[a]>E[b]) swap(a,b); int l=E[a]; int r=E[b]+1; int z=31-__builtin_clz(r-l); return depth[a]+depth[b]-2*min(table[z][l],table[z][r-(1< X(3); auto f=[&](int l1,int r1,int l2,int r2) -> void { X[0]=order[l1]; rep(i,0,2){ int tmp=-1,tmp2=0; rep(j,l1,r1){ tmp2=dist(order[j],X[i]); if(tmp