N = int(input())
e_list = [[] for i in range(N)]
for i in range(N-1):
        a,b = list(map(int,input().split()))
        a,b = a-1,b-1
        e_list[a].append(b)
        e_list[b].append(a)
c = list(map(int,input().split()))

vi = 0 #時と場合によってここを変える
from collections import deque
Q = deque([vi])
checked_list = [False for i in range(N)]
checked_list[vi]=True
min_path_list = [10**27 for i in range(N)] #問題によりここを変える
min_path_list[vi] = 0
while len(Q)>0:
    v = Q.pop()
    for v1 in e_list[v]:
        if not checked_list[v1]:
            checked_list[v1]=True
            Q.appendleft(v1)
            min_path_list[v1]=min(min_path_list[v1],min_path_list[v]+1) #問題によりここを変える


min_i_list = [[i,min_path_list[i]] for i in range(N)]
min_i_list.sort(key=lambda x:x[1],reverse=True)

c.sort()

ans = [[min_i_list[i][0]+1, c[i]] for i in range(N)]

ans.sort(key=lambda x:x[0])
ans = [ans[i][1] for i in range(N)]
print(sum(c[:-1]))
print(*ans) N = int(input()) N int(input()) int input() input [] for i in range(N) for i in range(N) i range(N) range N for i in range(N) [] e_list = [[] for i in range(N)] e_list [[] for i in range(N)] for i in range(N-1):
        a,b = list(map(int,input().split()))
        a,b = a-1,b-1
        e_list[a].append(b)
        e_list[b].append(a) i range(N-1) range N-1 N 1 a,b = list(map(int,input().split())) a list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split b a,b = a-1,b-1 a a-1 a 1 b b-1 b 1 e_list[a].append(b) [a].append [a] e_list a append b e_list[b].append(a) [b].append [b] e_list b append a c = list(map(int,input().split())) c list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split vi = 0 vi 0 from collections import deque Q = deque([vi]) Q deque([vi]) deque [vi] vi False for i in range(N) for i in range(N) i range(N) range N for i in range(N) False checked_list = [False for i in range(N)] checked_list [False for i in range(N)] checked_list[vi]=True checked_list[vi] checked_list vi True 10**27 for i in range(N) for i in range(N) i range(N) range N for i in range(N) 10**27 10 27 min_path_list = [10**27 for i in range(N)] min_path_list [10**27 for i in range(N)] min_path_list[vi] = 0 min_path_list[vi] min_path_list vi 0 while len(Q)>0:
    v = Q.pop()
    for v1 in e_list[v]:
        if not checked_list[v1]:
            checked_list[v1]=True
            Q.appendleft(v1)
            min_path_list[v1]=min(min_path_list[v1],min_path_list[v]+1) #問題によりここを変える len(Q)>0 len(Q) len Q 0 v = Q.pop() v Q.pop() Q.pop Q pop for v1 in e_list[v]:
        if not checked_list[v1]:
            checked_list[v1]=True
            Q.appendleft(v1)
            min_path_list[v1]=min(min_path_list[v1],min_path_list[v]+1) #問題によりここを変える v1 e_list[v] e_list v if not checked_list[v1]:
            checked_list[v1]=True
            Q.appendleft(v1)
            min_path_list[v1]=min(min_path_list[v1],min_path_list[v]+1) #問題によりここを変える not checked_list[v1] checked_list[v1] checked_list v1 checked_list[v1]=True checked_list[v1] checked_list v1 True Q.appendleft(v1) Q.appendleft Q appendleft v1 min_path_list[v1]=min(min_path_list[v1],min_path_list[v]+1) min_path_list[v1] min_path_list v1 min(min_path_list[v1],min_path_list[v]+1) min min_path_list[v1] min_path_list v1 min_path_list[v]+1 min_path_list[v] min_path_list v 1 [i,min_path_list[i]] for i in range(N) for i in range(N) i range(N) range N for i in range(N) [i,min_path_list[i]] i min_path_list[i] min_path_list i min_i_list = [[i,min_path_list[i]] for i in range(N)] min_i_list [[i,min_path_list[i]] for i in range(N)] min_i_list.sort(key=lambda x:x[1],reverse=True) min_i_list.sort min_i_list sort key=lambda x:x[1] key lambda x:x[1] x[1] x 1 x reverse=True reverse True c.sort() c.sort c sort [min_i_list[i][0]+1, c[i]] for i in range(N) for i in range(N) i range(N) range N for i in range(N) [min_i_list[i][0]+1, c[i]] min_i_list[i][0]+1 min_i_list[i][0] [i] min_i_list i 0 1 c[i] c i ans = [[min_i_list[i][0]+1, c[i]] for i in range(N)] ans [[min_i_list[i][0]+1, c[i]] for i in range(N)] ans.sort(key=lambda x:x[0]) ans.sort ans sort key=lambda x:x[0] key lambda x:x[0] x[0] x 0 x ans[i][1] for i in range(N) for i in range(N) i range(N) range N for i in range(N) ans[i][1] [i] ans i 1 ans = [ans[i][1] for i in range(N)] ans [ans[i][1] for i in range(N)] print(sum(c[:-1])) print sum(c[:-1]) sum c[:-1] c :-1 -1 print(*ans) print *ans ans vi = 0 0 vi min_path_list = [10**27 for i in range(N)] [10**27 for i in range(N)] min_path_list a,b = a-1,b-1 a-1 a b = list(map(int,input().split())) list(map(int,input().split())) b ans = [ans[i][1] for i in range(N)] [ans[i][1] for i in range(N)] ans b = a-1,b-1 b-1 b ans = [[min_i_list[i][0]+1, c[i]] for i in range(N)] [[min_i_list[i][0]+1, c[i]] for i in range(N)] ans c = list(map(int,input().split())) list(map(int,input().split())) c min_i_list = [[i,min_path_list[i]] for i in range(N)] [[i,min_path_list[i]] for i in range(N)] min_i_list Q = deque([vi]) deque([vi]) Q a,b = list(map(int,input().split())) list(map(int,input().split())) a v = Q.pop() Q.pop() v checked_list = [False for i in range(N)] [False for i in range(N)] checked_list N = int(input()) int(input()) N e_list = [[] for i in range(N)] [[] for i in range(N)] e_list
N = int(input())
e_list = [[] for i in range(N)]
for i in range(N-1):
        a,b = list(map(int,input().split()))
        a,b = a-1,b-1
        e_list[a].append(b)
        e_list[b].append(a)
c = list(map(int,input().split()))

vi = 0 #時と場合によってここを変える
from collections import deque
Q = deque([vi])
checked_list = [False for i in range(N)]
checked_list[vi]=True
min_path_list = [10**27 for i in range(N)] #問題によりここを変える
min_path_list[vi] = 0
while len(Q)>0:
    v = Q.pop()
    for v1 in e_list[v]:
        if not checked_list[v1]:
            checked_list[v1]=True
            Q.appendleft(v1)
            min_path_list[v1]=min(min_path_list[v1],min_path_list[v]+1) #問題によりここを変える


min_i_list = [[i,min_path_list[i]] for i in range(N)]
min_i_list.sort(key=lambda x:x[1],reverse=True)

c.sort()

ans = [[min_i_list[i][0]+1, c[i]] for i in range(N)]

ans.sort(key=lambda x:x[0])
ans = [ans[i][1] for i in range(N)]
print(sum(c[:-1]))
print(*ans)