#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> 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); } vector ans(N); vector depth(N,-1); vector E(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); const int B=16,len=(1<<15); vector base(N*2,N); vector table(B,vector(len,N)); vector TL(N*2,N),TR(N*2+1,N); rep(i,0,N){ base[E[i]]=depth[i]; base[E[i+N]]=depth[i]-1; } rep(i,0,N*2){ chmin(table[0][i/B],base[i]); TL[i]=base[i]; if((i+1)%B==0){ for(int j=i;j>i+1-B;j--){ chmin(TL[j-1],TL[j]); } } else{ chmin(TR[i+1],min(TR[i],base[i])); } } rep(i,0,B-1) rep(j,0,len){ 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 res=N; if((l^r)/B==0){ rep(i,l,r) chmin(res,base[i]); } else{ chmin(res,TL[l]); chmin(res,TR[r]); l=(l+B-1)/B; r=r/B; int z=31-__builtin_clz(r-l); if(z>=0) chmin(res,min(table[z][l],table[z][r-(1< order(N); rep(i,0,N) order[i]=i; sort(all(order),[&](int l,int r){ return A[l]>A[r]; }); int L=0,R=0; int X=order[0],Y=order[0],D=0; while(L