CAPIMOVE - Editorial

no it can be solved using dp on trees !

practice and practice and get to know through experience… that will help

It’s giving TLE for later cases.
Where can I improve?

def weight(dmap , l , ind , d):
    l1 = l[:]
    l1.remove(ind)
    for i in dmap[ind]:
        l1.remove(i)
    m = 0
    ans = -1
    for i in l1:
        m1 = d[i]
        if(m<m1):
            m = m1
            ans = i
    return ans
        
t = int(input())
while(t>0):
    n = int(input())
    indexs = []
    for i in range(n):
        indexs.append(i+1)
    d =list(map(int , input().strip().split()))
    d1 = {}
    for i in range(1 , n+1):
        d1[i] = d[i-1]
    # print(d)
    lmap = {}
    for i in range(n-1):
        l = list(map(int , input().strip().split()))
        if l[0] in lmap:
            if(l[1] not in lmap[l[0]]):
                lmap[l[0]].append(l[1])
        else:
            lmap[l[0]] = [l[1]]
        if(l[1] in lmap):
            if(l[0] not in lmap[l[1]]):
                lmap[l[1]].append(l[0])
        else:
            lmap[l[1]] = [l[0]]
 
    
    for i in range(1 , n+1):
        m = weight(lmap , indexs , i , d1 )
        print(m , end = " ")
        
    
    
        
    
    
    
    
    
    t-=1