#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); vector seg(N*4); rep(i,0,N){ seg[2*N+E[i]]=depth[i]; seg[2*N+E[i+N]]=depth[i]-1; } for(int i=2*N-1;i>0;i--) seg[i]=min(seg[2*i],seg[2*i+1]); auto dist=[&](int a,int b) -> int { int res=N; int l=E[a],r=E[b]; if(l>r) swap(l,r); r++; l+=2*N; r+=2*N; while(l>=1;r>>=1; } return depth[a]+depth[b]-2*res; }; vector 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