import sys

from bisect import bisect_left

sys.setrecursionlimit(10**7)

def input():return sys.stdin.readline().strip()



def main():

    N = int(input())

    A = tuple(map(int, input().split()))



    to = [[] for _ in range(N)]

    for _ in range(N-1):

        u, v = map(int, input().split())

        u -= 1

        v -= 1

        to[u].append(v)

        to[v].append(u)



    INF = 10 ** 18    

    ans = [0] * N

    dp = [INF] * N

    def dfs(now, pre):

        a = A[now]

        idx = bisect_left(dp, a)



        old = dp[idx]

        dp[idx] = a



        ans_idx = bisect_left(dp, INF)

        ans[now] = ans_idx



        

        for nv in to[now]:

            if nv != pre:

                dfs(nv, now)

        



        dp[idx] = old

    

    dfs(0, -1)



    print(*ans, sep="\n")

        



if __name__ == "__main__":

    main()

from collections import deque

from bisect import bisect_left

import sys

def input():return sys.stdin.readline().strip()



def main():

    N = int(input())

    A = tuple(map(int, input().split()))



    to = [[] for _ in range(N)]

    for _ in range(N-1):

        a, b = map(int, input().split())

        a -= 1

        b -= 1

        to[a].append(b)

        to[b].append(a)

    

    # bfs

    INF = 10 ** 18

    dp = [INF] * N

    ans = [0] * N



    def dfs(s):

        stack = deque()

        pop = stack.pop

        push = stack.append



        push((s, -1, None))



        while stack:

            now, pre, old = pop()



            # 復元

            if now == -1:

                dp[pre] = old

                continue

            

            a = A[now]

            idx = bisect_left(dp, a)

            old = dp[idx]

            dp[idx] = a



            ans_idx = bisect_left(dp, INF)

            ans[now] = ans_idx



            # 復元用 頂点

            # (フラグ, 変更したidx, 前の値)

            push((-1, idx, old))

            

            for nv in to[now]:

                if nv != pre:

                    push((nv, now, None))

    

    dfs(0)

    print(*ans, sep="\n")



if __name__ == "__main__":

    main()

import sys
from bisect import bisect_left

sys.setrecursionlimit(10**7)


def input():
    return sys.stdin.readline().strip()


def main():
    N = int(input())
    A = tuple(map(int, input().split()))
    to = [[] for _ in range(N)]
    for _ in range(N - 1):
        u, v = map(int, input().split())
        u -= 1
        v -= 1
        to[u].append(v)
        to[v].append(u)
    INF = 10**18
    ans = [0] * N
    dp = [INF] * N

    def dfs(now, pre):
        a = A[now]
        idx = bisect_left(dp, a)
        old = dp[idx]
        dp[idx] = a
        ans_idx = bisect_left(dp, INF)
        ans[now] = ans_idx
        for nv in to[now]:
            if nv != pre:
                dfs(nv, now)
        dp[idx] = old

    dfs(0, -1)
    print(*ans, sep="\n")


if __name__ == "__main__":
    main()

from collections import deque
from bisect import bisect_left
import sys


def input():
    return sys.stdin.readline().strip()


def main():
    N = int(input())
    A = tuple(map(int, input().split()))
    to = [[] for _ in range(N)]
    for _ in range(N - 1):
        a, b = map(int, input().split())
        a -= 1
        b -= 1
        to[a].append(b)
        to[b].append(a)
    # bfs
    INF = 10**18
    dp = [INF] * N
    ans = [0] * N

    def dfs(s):
        stack = deque()
        pop = stack.pop
        push = stack.append
        push((s, -1, None))
        while stack:
            now, pre, old = pop()
            # 復元
            if now == -1:
                dp[pre] = old
                continue
            a = A[now]
            idx = bisect_left(dp, a)
            old = dp[idx]
            dp[idx] = a
            ans_idx = bisect_left(dp, INF)
            ans[now] = ans_idx
            # 復元用 頂点
            # (フラグ, 変更したidx, 前の値)
            push((-1, idx, old))
            for nv in to[now]:
                if nv != pre:
                    push((nv, now, None))

    dfs(0)
    print(*ans, sep="\n")


if __name__ == "__main__":
    main()

+from collections import deque +from bisect import bisect_left -from bisect import bisect_left - -sys.setrecursionlimit(10**7) -        u, v = map(int, input().split()) -        u -= 1 -        v -= 1 -        to[u].append(v) -        to[v].append(u) +        a, b = map(int, input().split()) +        a -= 1 +        b -= 1 +        to[a].append(b) +        to[b].append(a) +    # bfs +    dp = [INF] * N -    dp = [INF] * N -    def dfs(now, pre): -        a = A[now] -        idx = bisect_left(dp, a) -        old = dp[idx] -        dp[idx] = a -        ans_idx = bisect_left(dp, INF) -        ans[now] = ans_idx -        for nv in to[now]: -            if nv != pre: -                dfs(nv, now) -        dp[idx] = old +    def dfs(s): +        stack = deque() +        pop = stack.pop +        push = stack.append +        push((s, -1, None)) +        while stack: +            now, pre, old = pop() +            # 復元 +            if now == -1: +                dp[pre] = old +                continue +            a = A[now] +            idx = bisect_left(dp, a) +            old = dp[idx] +            dp[idx] = a +            ans_idx = bisect_left(dp, INF) +            ans[now] = ans_idx +            # 復元用 頂点 +            # (フラグ, 変更したidx, 前の値) +            push((-1, idx, old)) +            for nv in to[now]: +                if nv != pre: +                    push((nv, now, None)) -    dfs(0, -1) +    dfs(0)