#include using namespace std; #define int long long #define app push_back #define all(x) (x).begin(),(x).end() #ifdef LOCAL #define debug(...) [](auto...a){ ((cout << a << ' '), ...) << endl;}(#__VA_ARGS__, ":", __VA_ARGS__) #else #define debug(...) #endif #ifdef LOCAL #define __int128 long long #endif // LOCAL const int maxn=1e6+5; vector a[maxn]; int c[maxn]; bool used[maxn]; int corn[maxn]; int u[maxn][20]; int res[maxn]; pair cur={-1,-1}; void dfs(int x) { used[x]=true; for(int i=1;i<20;++i) {u[x][i]=u[u[x][i-1]][i-1];} for(int v:a[x]) { if(!used[v]) { u[v][0]=x; corn[v]=corn[x]+1; dfs(v); } } used[x]=false; } int lca(int x,int y) { for(int i=19;i>=0;--i) {if(corn[x]>=corn[y]+(1<=0;--i) {if(corn[y]>=corn[x]+(1<=0;--i) {if(u[x][i]!=u[y][i]) {x=u[x][i];y=u[y][i];}} if(x!=y) {x=u[x][0];y=u[y][0];} return x; } int dist(int x,int y) { if(x==(-1) || y==(-1)) return 0; return corn[x]+corn[y]-2*corn[lca(x,y)]; } void add(int pos) { if(cur.first==(-1)) {cur.first=pos;return;} if(cur.second==(-1)) {cur.second=pos;return;} int was=dist(cur.first,cur.second);int x1=dist(cur.first,pos);int x2=dist(cur.second,pos); int ma=max({was,x1,x2}); if(was==ma) return; if(x1==ma) {cur={cur.first,pos};return;} cur={pos,cur.second}; } int get(int pos) { return max(dist(pos,cur.first),dist(pos,cur.second)); } int32_t main() { ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); int n;cin>>n; for(int i=0;i>c[i]; for(int i=0;i>x>>y;--x;--y;a[x].app(y);a[y].app(x);} vector v(n);iota(all(v),0LL);sort(all(v),[&](int i,int j) {return make_pair(c[i],i)=0;--i) { int pos=v[i]; if(i==n-1) {add(pos);continue;} res[pos]=max(res[pos],get(pos)); add(pos); } cur={-1,-1}; sort(all(v),[&](int i,int j) {return make_pair(c[i],-i)=0;--i) { int pos=v[i]; if(i==n-1) {add(pos);continue;} res[pos]=max(res[pos],get(pos)); add(pos); } for(int i=0;i