Buggy python submission (verdict: Time Limit Exceeded):
import queue
inf = 10**18
n = int(input())
a = list(map(int,input().split()))
way = [[] for i in range(n)]
for i in range(n-1):
    u,v = map(int,input().split())
    u,v = u-1,v-1
    way[u].append(v)
    way[v].append(u)

def Mnsr(ni,mns):
    mnsi = mns + [inf]
    l = 0
    r = len(mns)
    while l < r:
        mid = (r-l)//2+l
        if ni < mnsi[mid]:
            r = mid
        else:
            l = mid+1

    if not mnsi[l-1] == ni:
        mnsi[l] = ni
    return mnsi

def ansfind(arr):
    l = 0
    r = len(arr)-1
    while l < r:
        mid = l + (r-l)//2
        if arr[mid] == inf:
            r = mid
        else:
            l = mid+1
    return l-1


ans = [1 for i in range(n)]
q = queue.Queue()
for i in way[0]:
    q.put((i,0,Mnsr(a[i],[0,a[0],inf])))

while not q.empty():
    i,pi,mns = q.get()
    ans[i] = ansfind(mns)
    for j in way[i]:
        if pi == j:
            continue
        q.put((j,i,Mnsr(a[j],mns)))

for i in ans:
    print(i)


Fixed submission:
'''
自宅用PCでの解答
'''
import math
#import numpy as np
import itertools
import queue
import bisect
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit
#from scipy.sparse.csgraph import dijkstra
#from scipy.sparse import csr_matrix
ipt = stdin.readline
setrecursionlimit(10**7)



def main():
    n = int(ipt())
#    n = 2*10**5
    a = [int(i) for i in ipt().split()]
#    a = [i+1 for i in range(n)]
    way = [[] for i in range(n)]
    for i in range(n-1):
        u,v = map(int,ipt().split())
        u -= 1;v -= 1
        way[u].append(v)
        way[v].append(u)
#    way = [[i-1,i+1] for i in range(n)]
#    way[0] = [1]
#    way[n-1] = [n-2]
    ans = [0]*n
    dp = [10**18]*n
    stk = []

    def solve(ni,pi):
        ai = a[ni]
        pt = bisect.bisect_left(dp,ai)
        stk.append((pt,dp[pt]))
        dp[pt] = ai
        ans[ni] = bisect.bisect_left(dp,10**18-1)
        for i in way[ni]:
            if i == pi:
                continue
            else:
                solve(i,ni)
        pl,val = stk.pop()
        dp[pl] = val
        return None

    solve(0,-1)

    for i in ans:
        print(i)

    return None

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import queue
n = int(input())
a = list(map(int,input().split()))
way = [[] for i in range(n)]
for i in range(n-1):
    u,v = map(int,input().split())
    u,v = u-1,v-1
    way[u].append(v)
    way[v].append(u)

stst = [[] for i in range(n)]
stst[0] = [a[0]]
q = queue.Queue()
for i in way[0]:
    q.put((i,0))

while not q.empty():
    i,pi = q.get()
    for j in way[i]:
        if pi == j:
            continue
        q.put((j,i))
    stst[i] = stst[pi]+[a[i]]

def lenlis(arr):
    mns = [10**18 for i in range(len(arr)+2)]
    mns[0] = 0
    mai = 0
    for i in arr:
        for j in range(len(arr)):
            if i > mns[j]:
                if i < mns[j+1]:
                    mns[j+1] = i
                    break
            else:
                break
    return mns.index(10**18)-1

for i in stst:
    print(lenlis(i))


Fixed submission:
'''
自宅用PCでの解答
'''
import math
#import numpy as np
import itertools
import queue
import bisect
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit
#from scipy.sparse.csgraph import dijkstra
#from scipy.sparse import csr_matrix
ipt = stdin.readline
setrecursionlimit(10**7)



def main():
    n = int(ipt())
#    n = 2*10**5
    a = [int(i) for i in ipt().split()]
#    a = [i+1 for i in range(n)]
    way = [[] for i in range(n)]
    for i in range(n-1):
        u,v = map(int,ipt().split())
        u -= 1;v -= 1
        way[u].append(v)
        way[v].append(u)
#    way = [[i-1,i+1] for i in range(n)]
#    way[0] = [1]
#    way[n-1] = [n-2]
    ans = [0]*n
    dp = [10**18]*n
    stk = []

    def solve(ni,pi):
        ai = a[ni]
        pt = bisect.bisect_left(dp,ai)
        stk.append((pt,dp[pt]))
        dp[pt] = ai
        ans[ni] = bisect.bisect_left(dp,10**18-1)
        for i in way[ni]:
            if i == pi:
                continue
            else:
                solve(i,ni)
        pl,val = stk.pop()
        dp[pl] = val
        return None

    solve(0,-1)

    for i in ans:
        print(i)

    return None

if __name__ == '__main__':
    main()