Problem:
Score : 600 points

Problem Statement

We have a tree with N vertices, whose i-th edge connects Vertex u_i and Vertex v_i.
Vertex i has an integer a_i written on it.
For every integer k from 1 through N, solve the following problem:

We will make a sequence by lining up the integers written on the vertices along the shortest path from Vertex 1 to Vertex k, in the order they appear. Find the length of the longest increasing subsequence of this sequence.

Here, the longest increasing subsequence of a sequence A of length L is the subsequence A_{i_1} , A_{i_2} , ... , A_{i_M} with the greatest possible value of M such that 1 \leq i_1 < i_2 < ... < i_M \leq L and A_{i_1} < A_{i_2} < ... < A_{i_M}.

Constraints

2 \leq N \leq 2 \times 10^5

1 \leq a_i \leq 10^9

1 \leq u_i , v_i \leq N

u_i \neq v_i

The given graph is a tree.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N
u_1 v_1
u_2 v_2
:
u_{N-1} v_{N-1}

Output

Print N lines. The k-th line, print the length of the longest increasing subsequence of the sequence obtained from the shortest path from Vertex 1 to Vertex k.

Sample Input 1

10
1 2 5 3 4 6 7 3 2 4
1 2
2 3
3 4
4 5
3 6
6 7
1 8
8 9
9 10

Sample Output 1

1
2
3
3
4
4
5
2
2
3

For example, the sequence A obtained from the shortest path from Vertex 1 to Vertex 5 is 1,2,5,3,4. Its longest increasing subsequence is A_1, A_2, A_4, A_5, with the length of 4.

Solution in C# (.NET Core 3.1.201) (judge verdict: Wrong Answer)

using System;
using System.Collections.Generic;
using System.Collections;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
using static System.Math;
using System.Numerics;
using nint=System.Int64;
static class Program{
	const int mod=(int)1e9+7;
	const long inf=(long)1e18;
	static List<int>[] li;
	static bool[] b;
	static int[] h,ans;
	static Avl avl=new Avl(true,true);
	static void Main(){
		Sc sc=new Sc();
		var s=sc.Ia;
		int n=s[0],m=n-1;
		li=new List<int>[n+1];
		b=new bool[n+1];
		h=sc.Ia2;
		ans=new int[n];
		for(int i=1;i<=n;i++){li[i]=new List<int>();}
		for(int i=0;i<m;i++){
			var e=sc.Ia;
			li[e[0]].Add(e[1]);
			li[e[1]].Add(e[0]);
		}
		Fu(1,0);
		Console.WriteLine("{0}",String.Join("\n",ans));
	}
	static void Fu(int a,int g){
		b[a]=true;
		long d=(long)h[a]*1000000+a;
		var e=avl.Rd(0,(long)(h[a])*1000000);
		var p=e==null?1:(e.d+1);
		g=Max((int)p,g);
		ans[a-1]=g;
		avl.Ud(d,p);
		for(int i=0;i<li[a].Count;i++){
			if(!b[li[a][i]]){Fu(li[a][i],g);}
		}
		avl.Dl(d);
	}
}
public class Avl{
	public class Nd{
		public int h=1,c=1;
		public readonly nint n;
		public long d;
		public Nd l,r,nx;
		public Nd(nint n,long d,Nd du){this.n=n;this.d=d;nx=this;l=du;r=du;}
		public override string ToString()=>"n:"+n.ToString()+" d:"+d.ToString();
	}
	private static Func<long,long,bool> compare;
	private static Action<Nd> compare2;
	public Nd root,du;
	public int cnt=0;
	public bool bb;
	public Avl(bool bo,bool bb){
		if(bo){
			du=new Nd(nint.MinValue,long.MinValue,du);
			compare=CoMax;
			compare2=CoMax;
		}
		else{
			du=new Nd(nint.MinValue,long.MaxValue,du);
			compare=CoMin;
			compare2=CoMin;
		}
		this.bb=bb;
		du.h=0;du.c=0;
		root=du;
	}
	private bool CoMax(long a,long b){return a>=b;}
	private bool CoMin(long a,long b){return a<=b;}
	private void CoMax(Nd t){
		t.nx=t;
		if(t.nx.d<t.l.nx.d){t.nx=t.l.nx;}
		if(t.nx.d<t.r.nx.d){t.nx=t.r.nx;}
	}
	private void CoMin(Nd t){
		t.nx=t;
		if(t.nx.d>t.l.nx.d){t.nx=t.l.nx;}
		if(t.nx.d>t.r.nx.d){t.nx=t.r.nx;}
	}
	public void Ud(nint n,long d){
		if(root==du){root=new Nd(n,d,du);cnt++;}
		else if(root.n==n){
			if(bb){root.d+=d;}
			else{root.d=d;}
			compare2(root);
		}
		else if(Fu(root,n,d)){
			if(Abs(root.l.h-root.r.h)>1){root=Rotate(root);}
			cnt++;
		}
	}
	private bool Fu(Nd t,nint n,long d){
		bool bo=false;
		if(t.n>n){
			if(t.l!=du){
				if(t.l.n==n){
					if(bb){t.l.d+=d;}
					else{t.l.d=d;}
					compare2(t.l);
				}
				else if(bo=Fu(t.l,n,d)){
					t.c++;
					if(Abs(t.l.l.h-t.l.r.h)>1){t.l=Rotate(t.l);}
					t.h=Max(t.l.h,t.r.h)+1;
				}
				compare2(t);
			}
			else{
				t.l=new Nd(n,d,du);
				bo=true;
				t.c++;
				t.h=Max(t.l.h,t.r.h)+1;
				t.nx=compare(t.nx.d,t.l.d)?t.nx:t.l;
			}
		}
		else{
			if(t.r!=du){
				if(t.r.n==n){
					if(bb){t.r.d+=d;}
					else{t.r.d=d;}
					compare2(t.r);
				}
				else if(bo=Fu(t.r,n,d)){
					t.c++;
					if(Abs(t.r.l.h-t.r.r.h)>1){t.r=Rotate(t.r);}
					t.h=Max(t.l.h,t.r.h)+1;
				}
				compare2(t);
			}
			else{
				t.r=new Nd(n,d,du);
				bo=true;
				t.c++;
				t.h=Max(t.l.h,t.r.h)+1;
				t.nx=compare(t.nx.d,t.r.d)?t.nx:t.r;
			}
		}
		return bo;
	}
	private Nd Rotate(Nd t){
		Nd nd=du;
		if(t.l.h>t.r.h){
			if(t.l.l.h>t.l.r.h){
				nd=t.l;t.l=t.l.r;nd.r=t;
				Ft(t);
			}
			else{
				nd=t.l.r;
				t.l.r=nd.l;nd.l=t.l;
				Ft(t.l);
				t.l=nd.r;nd.r=t;
				Ft(t);
			}
		}
		else{
			if(t.r.l.h>t.r.r.h){
				nd=t.r.l;
				t.r.l=nd.r;nd.r=t.r;
				Ft(t.r);
				t.r=nd.l;nd.l=t;
				Ft(t);
			}
			else{
				nd=t.r;t.r=t.r.l;nd.l=t;
				Ft(t);
			}
		}
		Ft(nd);
		return nd;
	}
	private void Ft(Nd t){
		t.h=Max(t.l.h,t.r.h)+1;
		t.c=t.l.c+t.r.c+1;
		compare2(t);
	}
	public bool Dl(nint n){
		if(cnt==0){return false;}
		Nd t=root;
		if(t.n==n){
			root=Fd2(root);
			if(cnt==0){return true;}
			else if(Abs(root.l.h-root.r.h)>1){root=Rotate(root);}
			else{Ft(root);}
			return true;
		}
		bool bo=Fd1(root,n);
		if(Abs(root.l.h-root.r.h)>1){root=Rotate(root);}
		else{Ft(root);}
		return bo;
	}
	private bool Fd1(Nd t,nint n){
		if(t.n>n){
			if(t.l==du){return false;}
			else if(t.l.n!=n){
				bool bo=Fd1(t.l,n);
				if(Abs(t.l.l.h-t.l.r.h)>1){t.l=Rotate(t.l);}
				else{Ft(t.l);}
				return bo;
			}
			else{
				t.l=Fd2(t.l);
				Ft(t);
				return true;
			}
		}
		else{
			if(t.r==du){return false;}
			else if(t.r.n!=n){
				bool bo=Fd1(t.r,n);
				if(Abs(t.r.l.h-t.r.r.h)>1){t.r=Rotate(t.r);}
				else{Ft(t.r);}
				return bo;
			}
			else{
				t.r=Fd2(t.r);
				Ft(t);
				return true;
			}
		}
	}
	private Nd Fd2(Nd dn){
		cnt--;
		if(dn.l==du){return dn.r;}
		if(dn.r==du){return dn.l;}
		Nd u=dn.l;
		if(u.r==du){
			u.r=dn.r;
			Ft(u);
			return u;
		}
		u=Fd3(u,dn);
		if(Abs(dn.l.l.h-dn.l.r.h)>1){u.l=Rotate(dn.l);}
		else{Ft(dn.l);}
		Ft(u);
		return u;
	}
	private Nd Fd3(Nd u,Nd dn){
		if(u.r.r!=du){
			Nd v=Fd3(u.r,dn);
			if(Abs(u.r.l.h-u.r.r.h)>1){u.r=Rotate(u.r);}
			else{Ft(u.r);}
			return v;
		}
		else{
			Nd v=u.r;u.r=u.r.l;v.l=dn.l;v.r=dn.r;
			return v;
		}
	}
	public Nd Hs(nint n){
		Nd t=root;
		if(cnt==0){return null;}
		if(t.n==n){return t;}
		Nd a=null;
		while(true){
			if(t.n>n){
				if(t.l==du){break;}
				else if(t.l.n!=n){t=t.l;}
				else{a=t.l;break;}
			}
			else{
				if(t.r==du){break;}
				else if(t.r.n!=n){t=t.r;}
				else{a=t.r;break;}
			}
		}
		return a;
	}
	public Nd Ra(int n){return cnt>=n?Fr(root,n):null;}
	private Nd Fr(Nd t,int n){
		if(t.l.c>n-1){return Fr(t.l,n);}
		if(t.l.c<n-1){return Fr(t.r,n-t.l.c-1);}
		return t;
	}
	public Nd Lb(nint n){return cnt!=0?Flb(root,n):null;}
	private Nd Flb(Nd t,nint n){
		if(t.n<n){
			if(t.r==du){return null;}
			return Flb(t.r,n);
		}
		if(t.n>n){
			if(t.l==du){return t;}
			Nd u=Flb(t.l,n);
			return u==null?t:u;
		}
		return t;
	}
	public Nd Ub(nint n){return cnt!=0?Fub(root,n):null;}
	private Nd Fub(Nd t,nint n){
		if(t.n>n){
			if(t.l==du){return null;}
			return Fub(t.l,n);
		}
		if(t.n<n){
			if(t.r==du){return t;}
			Nd u=Fub(t.r,n);
			return u==null?t:u;
		}
		return t;
	}
	public int Rg(nint a,nint b){return cnt!=0?Frg(root,a,b,false,false):0;}
	private int Frg(Nd t,nint a,nint b,bool l,bool r){
		if(t.h==0){return 0;}
		if(l&&r){return t.c;}
		if(t.n<a){return Frg(t.r,a,b,t.n>=a,r);}
		if(t.n>b){return Frg(t.l,a,b,l,t.n<=b);}
		return Frg(t.l,a,b,l,t.n<=b)+Frg(t.r,a,b,t.n>=a,r)+1;
	}
	public Nd Rd(nint a,nint b){return cnt!=0?Frd(root,a,b,false,false):null;}
	private Nd Frd(Nd t,nint a,nint b,bool l,bool r){
		if(t.h==0){return du;}
		if(l&&r){return t.nx;}
		if(t.n<a){return Frd(t.r,a,b,t.n>=a,r);}
		if(t.n>b){return Frd(t.l,a,b,l,t.n<=b);}
		var e1=Frd(t.l,a,b,l,t.n<=b);
		var e2=Frd(t.r,a,b,t.n>=a,r);
		e1=compare(e1.d,e2.d)?e1:e2;
		return compare(t.d,e1.d)?t:e1;
	}
	public Nd Gl(nint a,nint b,long d){return cnt==0?null:Fgl(root,a,b,d);}
	private Nd Fgl(Nd t,nint a,nint b,long d){
		Nd p=null;
		if(a<=t.n&&t.n<=b&&compare(t.d,d)){p=t;}
		if(a<=t.n&&compare(t.l.nx.d,d)){
			var z=Fgl(t.l,a,b,d);
			if(z!=null){p=z;}
		}
		if(p==null&&t.n<=b&&compare(t.r.nx.d,d)){p=Fgl(t.r,a,b,d);}
		return p;
	}
	public Nd Gr(nint a,nint b,long d){return cnt==0?null:Fgr(root,a,b,d);}
	private Nd Fgr(Nd t,nint a,nint b,long d){
		Nd p=null;
		if(a<=t.n&&t.n<=b&&compare(t.d,d)){p=t;}
		if(t.n<=b&&compare(t.r.nx.d,d)){
			var z=Fgr(t.r,a,b,d);
			if(z!=null){p=z;}
		}
		if(p==null&&a<=t.n&&compare(t.l.nx.d,d)){p=Fgr(t.r,a,b,d);}
		return p;
	}
	public void En(Action<Nd> f){if(cnt>0){Fen(root,f);}}
	private void Fen(Nd t,Action<Nd> f){
		f(t);
		if(t.l!=du){Fen(t.l,f);}
		if(t.r!=du){Fen(t.r,f);}
	}
}
public class Sc{
	public int I{get{return int.Parse(Console.ReadLine());}}
	public long L{get{return long.Parse(Console.ReadLine());}}
	public double D{get{return double.Parse(Console.ReadLine());}}
	public string S{get{return Console.ReadLine();}}
	public int[] Ia{get{return Array.ConvertAll(Console.ReadLine().Split(),int.Parse);}}
	public long[] La{get{return Array.ConvertAll(Console.ReadLine().Split(),long.Parse);}}
	public double[] Da{get{return Array.ConvertAll(Console.ReadLine().Split(),double.Parse);}}
	public string[] Sa{get{return Console.ReadLine().Split();}}
	public object[] Oa{get{return Console.ReadLine().Split();}}
	public int[] Ia2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),int.Parse);}}
	public int[] Ia3(int a){return Array.ConvertAll((a.ToString()+" "+Console.ReadLine()).Split(),int.Parse);}
	public int[] Ia3(bool a,int b,bool c,int d){return Array.ConvertAll(((a?b.ToString()+" ":"")+Console.ReadLine()+(c?" "+d.ToString():"")).Split(),int.Parse);}
	public long[] La2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),long.Parse);}}
	public long[] La3(int a){return Array.ConvertAll((a.ToString()+" "+Console.ReadLine()).Split(),long.Parse);}
	public long[] La3(bool a,int b,bool c,int d){return Array.ConvertAll(((a?b.ToString()+" ":"")+Console.ReadLine()+(c?" "+d.ToString():"")).Split(),long.Parse);}
	public double[] Da2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),double.Parse);}}
	public double[] Da3(int a){return Array.ConvertAll((a.ToString()+" "+Console.ReadLine()).Split(),double.Parse);}
	public double[] Da3(bool a,int b,bool c,int d){return Array.ConvertAll(((a?b.ToString()+" ":"")+Console.ReadLine()+(c?" "+d.ToString():"")).Split(),double.Parse);}
	public T[] Arr<T>(int n,Func<T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f();}return a;}
	public T[] Arr<T>(int n,Func<int,T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(i);}return a;}
	public T[] Arr<T>(int n,Func<string[],T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(Console.ReadLine().Split());}return a;}
	public T[] Arr<T>(int n,Func<int,string[],T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(i,Console.ReadLine().Split());}return a;}
}