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.Linq;
using System.Collections.Generic;

namespace AtCoder.Contests.ABC165
{
    /// <summary>
    /// 
    /// </summary>
    static class F
    {
        static void Main(string[] args)
        {
            var n = NextInt();
            var aas = ReadIntArray();
            var g = new Graph(n+1);
            for (int i = 0; i < n-1; i++)
            {
                var u = NextInt();
                var v = NextInt();
                g[u].Add(v);
                g[v].Add(u);
            }
            var lists = new List<Lis>[n + 1];
            var s = new Stack<int>();
            var result = new int[n + 1];
            s.Push(1);
            lists[1] = new List<Lis> { new Lis { Length = 1, Max = aas[0] } };
            result[1] = 1;
            while (s.Any())
            {
                var node = s.Pop();
                var list = lists[node];
                foreach (var child in g[node])
                {
                    if (result[child] > 0) continue;
                    var childList= NewList(list, aas[child - 1]);
                    lists[child] = childList;
                    result[child] = childList.Max(l => l.Length);
                    s.Push(child);
                }
            }

            for (int i = 1; i <= n; i++)
            {
                Console.WriteLine(result[i]);
            }
        }
        static IComparer<Lis> comp = Comparer<Lis>.Create((x, y) => x.Max.CompareTo(y.Max));
        static List<Lis> NewList(List<Lis> prev, int value)
        {
            var temp = new Lis() { Max = value };
            var index = prev.BinarySearch(temp, comp);

            var result = prev.ToList();

            if (index < 0)
            {
                index = ~index;
                if (index == 0)
                {
                    result.Insert(index, new Lis { Length = 1, Max = value });
                }
                else
                {
                    result.Insert(index, new Lis { Length = result[index - 1].Length + 1, Max = value });
                }
            }
            else
            {
                if(index > 0)
                {
                    result[index] = new Lis { Length = Math.Max(result[index - 1].Length + 1, result[index].Length), Max = value };
                }
            }

            return result;
        }

        #region Console
        static int _index = 0;
        static string[] _buffer;

        static string Next()
        {
            if (_buffer == null || _index >= _buffer.Length)
            {
                _buffer = Console.ReadLine().Split();
                _index = 0;
            }

            return _buffer[_index++];
        }
        static int NextInt()
        {
            return int.Parse(Next());
        }
        static long NextLong()
        {
            return long.Parse(Next());
        }
        static int ReadInt()
        {
            return int.Parse(Console.ReadLine());
        }
        static long ReadLong()
        {
            return long.Parse(Console.ReadLine());
        }
        static int[] ReadIntArray()
        {
            return Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
        }
        static int[] ReadIntArray(int n)
        {
            var a = new int[n];
            for (int i = 0; i < n; i++)
            {
                a[i] = ReadInt();
            }
            return a;
        }
        static long[] ReadLongArray()
        {
            return Console.ReadLine().Split(' ').Select(long.Parse).ToArray();
        }
        static long[] ReadLongArray(int n)
        {
            var a = new long[n];
            for (int i = 0; i < n; i++)
            {
                a[i] = ReadLong();
            }
            return a;
        }
        static List<T> ReadObjects<T>(int n, Func<int[], T> creator)
        {
            var l = new List<T>(n);

            for (int i = 0; i < n; i++)
            {
                var a = ReadIntArray();
                l.Add(creator(a));
            }

            return l;
        }

        static int[,] ReadMatrix(int n, int m)
        {
            var r = new int[n, m];
            for (int i = 0; i < n; i++)
            {
                var a = ReadIntArray();
                for (int j = 0; j < m; j++)
                {
                    r[i, j] = a[j];
                }
            }
            return r;
        }
        #endregion
    }

    struct Lis
    {
        public int Length;
        public int Max;
    }


    class Graph
    {
        private int _size;
        private List<int>[] _edges;

        public Graph(int size)
        {
            _size = size;
            _edges = new List<int>[size];
        }

        public List<int> this[int i]
        {
            get
            {
                if (_edges[i] == null) _edges[i] = new List<int>();
                return _edges[i];
            }
        }
    }
}