import sys
sys.setrecursionlimit(20000)

def add_node_2_tree_dic(tree_dic, x, y):
    if x not in tree_dic:
        tree_dic[x] = [y]
    else:
        tree_dic[x].append(y)

#   深さ優先探索で行けそう

N = int(input())
tree_dic = {}
for _ in range(N-1):
    a, b = map(int, input().split())
    add_node_2_tree_dic(tree_dic, a, b)
    add_node_2_tree_dic(tree_dic, b, a)

c_lst = list(map(int, input().split()))
c_lst.sort()
ans_lst = [0]*N

def search_next_node_and_add_side_wate(tree_dic, ans, current_node, c_lst, ans_lst):
    next_node_lst = tree_dic[current_node][:]

    for next_node in next_node_lst:
        c = c_lst.pop()
        ans += c
        ans_lst[next_node - 1] = c

        tree_dic[current_node].remove(next_node)    #   今回通った枝を取り払う
        tree_dic[next_node].remove(current_node)

        if len(tree_dic[current_node]) == 0:
            del tree_dic[current_node]#   探索し終えたノードの削除
        if len(tree_dic[next_node]) == 0:
            del tree_dic[next_node]#   探索し終えたノードの削除
        else:   #   次のノードにつながっている辺があるとき
            ans = search_next_node_and_add_side_wate(tree_dic, ans, next_node, c_lst, ans_lst)

    return ans


ans = 0
current_node = 1
ans_lst[0] = c_lst.pop()
ans = search_next_node_and_add_side_wate(tree_dic, ans, current_node, c_lst, ans_lst)


ans_str = ""
for i in range(N):
    ans_str += str(ans_lst[i])
    if i != N - 1:
        ans_str += " "


print(ans)
print(ans_str) import sys sys sys.setrecursionlimit(20000) sys.setrecursionlimit sys setrecursionlimit 20000 def add_node_2_tree_dic(tree_dic, x, y):
    if x not in tree_dic:
        tree_dic[x] = [y]
    else:
        tree_dic[x].append(y)

#   深さ優先探索で行けそう add_node_2_tree_dic if x not in tree_dic:
        tree_dic[x] = [y]
    else:
        tree_dic[x].append(y)

#   深さ優先探索で行けそう x not in tree_dic x tree_dic tree_dic[x] = [y] tree_dic[x] tree_dic x [y] y tree_dic[x].append(y) [x].append [x] tree_dic x append y tree_dic tree_dic x x y y N = int(input()) N int(input()) int input() input tree_dic = {} tree_dic {} for _ in range(N-1):
    a, b = map(int, input().split())
    add_node_2_tree_dic(tree_dic, a, b)
    add_node_2_tree_dic(tree_dic, b, a) _ range(N-1) range N-1 N 1 a, b = map(int, input().split()) a map(int, input().split()) map int input().split() ().split () input split b add_node_2_tree_dic(tree_dic, a, b) add_node_2_tree_dic tree_dic a b add_node_2_tree_dic(tree_dic, b, a) add_node_2_tree_dic tree_dic b a c_lst = list(map(int, input().split())) c_lst list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split c_lst.sort() c_lst.sort c_lst sort ans_lst = [0]*N ans_lst [0]*N [0] 0 N def search_next_node_and_add_side_wate(tree_dic, ans, current_node, c_lst, ans_lst):
    next_node_lst = tree_dic[current_node][:]

    for next_node in next_node_lst:
        c = c_lst.pop()
        ans += c
        ans_lst[next_node - 1] = c

        tree_dic[current_node].remove(next_node)    #   今回通った枝を取り払う
        tree_dic[next_node].remove(current_node)

        if len(tree_dic[current_node]) == 0:
            del tree_dic[current_node]#   探索し終えたノードの削除
        if len(tree_dic[next_node]) == 0:
            del tree_dic[next_node]#   探索し終えたノードの削除
        else:   #   次のノードにつながっている辺があるとき
            ans = search_next_node_and_add_side_wate(tree_dic, ans, next_node, c_lst, ans_lst)

    return ans search_next_node_and_add_side_wate next_node_lst = tree_dic[current_node][:] next_node_lst tree_dic[current_node][:] [current_node] tree_dic current_node : for next_node in next_node_lst:
        c = c_lst.pop()
        ans += c
        ans_lst[next_node - 1] = c

        tree_dic[current_node].remove(next_node)    #   今回通った枝を取り払う
        tree_dic[next_node].remove(current_node)

        if len(tree_dic[current_node]) == 0:
            del tree_dic[current_node]#   探索し終えたノードの削除
        if len(tree_dic[next_node]) == 0:
            del tree_dic[next_node]#   探索し終えたノードの削除
        else:   #   次のノードにつながっている辺があるとき
            ans = search_next_node_and_add_side_wate(tree_dic, ans, next_node, c_lst, ans_lst)

     next_node next_node_lst c = c_lst.pop() c c_lst.pop() c_lst.pop c_lst pop ans += c ans c ans_lst[next_node - 1] = c ans_lst[next_node - 1] ans_lst next_node - 1 next_node 1 c tree_dic[current_node].remove(next_node) [current_node].remove [current_node] tree_dic current_node remove next_node tree_dic[next_node].remove(current_node) [next_node].remove [next_node] tree_dic next_node remove current_node if len(tree_dic[current_node]) == 0:
            del tree_dic[current_node]#   探索し終えたノードの削除
         len(tree_dic[current_node]) == 0 len(tree_dic[current_node]) len tree_dic[current_node] tree_dic current_node 0 del tree_dic[current_node] tree_dic[current_node] tree_dic current_node if len(tree_dic[next_node]) == 0:
            del tree_dic[next_node]#   探索し終えたノードの削除
        else:   #   次のノードにつながっている辺があるとき
            ans = search_next_node_and_add_side_wate(tree_dic, ans, next_node, c_lst, ans_lst)

     len(tree_dic[next_node]) == 0 len(tree_dic[next_node]) len tree_dic[next_node] tree_dic next_node 0 del tree_dic[next_node] tree_dic[next_node] tree_dic next_node ans = search_next_node_and_add_side_wate(tree_dic, ans, next_node, c_lst, ans_lst) ans search_next_node_and_add_side_wate(tree_dic, ans, next_node, c_lst, ans_lst) search_next_node_and_add_side_wate tree_dic ans next_node c_lst ans_lst return ans ans tree_dic tree_dic ans ans current_node current_node c_lst c_lst ans_lst ans_lst ans = 0 ans 0 current_node = 1 current_node 1 ans_lst[0] = c_lst.pop() ans_lst[0] ans_lst 0 c_lst.pop() c_lst.pop c_lst pop ans = search_next_node_and_add_side_wate(tree_dic, ans, current_node, c_lst, ans_lst) ans search_next_node_and_add_side_wate(tree_dic, ans, current_node, c_lst, ans_lst) search_next_node_and_add_side_wate tree_dic ans current_node c_lst ans_lst ans_str = "" ans_str "" for i in range(N):
    ans_str += str(ans_lst[i])
    if i != N - 1:
        ans_str += " " i range(N) range N ans_str += str(ans_lst[i]) ans_str str(ans_lst[i]) str ans_lst[i] ans_lst i if i != N - 1:
        ans_str += " " i != N - 1 i N - 1 N 1 ans_str += " " ans_str " " print(ans) print ans print(ans_str) print ans_str tree_dic = {} {} tree_dic ans_lst = [0]*N [0]*N ans_lst b = map(int, input().split()) map(int, input().split()) b c_lst = list(map(int, input().split())) list(map(int, input().split())) c_lst ans_str = "" "" ans_str ans_str += " " " " ans_str a, b = map(int, input().split()) map(int, input().split()) a ans = search_next_node_and_add_side_wate(tree_dic, ans, current_node, c_lst, ans_lst) search_next_node_and_add_side_wate(tree_dic, ans, current_node, c_lst, ans_lst) ans current_node = 1 1 current_node ans_str += str(ans_lst[i]) str(ans_lst[i]) ans_str N = int(input()) int(input()) N def add_node_2_tree_dic(tree_dic, x, y):
    if x not in tree_dic:
        tree_dic[x] = [y]
    else:
        tree_dic[x].append(y)

#   深さ優先探索で行けそう def add_node_2_tree_dic(tree_dic, x, y):
    if x not in tree_dic:
        tree_dic[x] = [y]
    else:
        tree_dic[x].append(y)

#   深さ優先探索で行けそう add_node_2_tree_dic ans = 0 0 ans def search_next_node_and_add_side_wate(tree_dic, ans, current_node, c_lst, ans_lst):
    next_node_lst = tree_dic[current_node][:]

    for next_node in next_node_lst:
        c = c_lst.pop()
        ans += c
        ans_lst[next_node - 1] = c

        tree_dic[current_node].remove(next_node)    #   今回通った枝を取り払う
        tree_dic[next_node].remove(current_node)

        if len(tree_dic[current_node]) == 0:
            del tree_dic[current_node]#   探索し終えたノードの削除
        if len(tree_dic[next_node]) == 0:
            del tree_dic[next_node]#   探索し終えたノードの削除
        else:   #   次のノードにつながっている辺があるとき
            ans = search_next_node_and_add_side_wate(tree_dic, ans, next_node, c_lst, ans_lst)

    return ans def search_next_node_and_add_side_wate(tree_dic, ans, current_node, c_lst, ans_lst):
    next_node_lst = tree_dic[current_node][:]

    for next_node in next_node_lst:
        c = c_lst.pop()
        ans += c
        ans_lst[next_node - 1] = c

        tree_dic[current_node].remove(next_node)    #   今回通った枝を取り払う
        tree_dic[next_node].remove(current_node)

        if len(tree_dic[current_node]) == 0:
            del tree_dic[current_node]#   探索し終えたノードの削除
        if len(tree_dic[next_node]) == 0:
            del tree_dic[next_node]#   探索し終えたノードの削除
        else:   #   次のノードにつながっている辺があるとき
            ans = search_next_node_and_add_side_wate(tree_dic, ans, next_node, c_lst, ans_lst)

    return ans search_next_node_and_add_side_wate
import sys
sys.setrecursionlimit(20000)

def add_node_2_tree_dic(tree_dic, x, y):
    if x not in tree_dic:
        tree_dic[x] = [y]
    else:
        tree_dic[x].append(y)

#   深さ優先探索で行けそう

N = int(input())
tree_dic = {}
for _ in range(N-1):
    a, b = map(int, input().split())
    add_node_2_tree_dic(tree_dic, a, b)
    add_node_2_tree_dic(tree_dic, b, a)

c_lst = list(map(int, input().split()))
c_lst.sort()
ans_lst = [0]*N

def search_next_node_and_add_side_wate(tree_dic, ans, current_node, c_lst, ans_lst):
    next_node_lst = tree_dic[current_node][:]

    for next_node in next_node_lst:
        c = c_lst.pop()
        ans += c
        ans_lst[next_node - 1] = c

        tree_dic[current_node].remove(next_node)    #   今回通った枝を取り払う
        tree_dic[next_node].remove(current_node)

        if len(tree_dic[current_node]) == 0:
            del tree_dic[current_node]#   探索し終えたノードの削除
        if len(tree_dic[next_node]) == 0:
            del tree_dic[next_node]#   探索し終えたノードの削除
        else:   #   次のノードにつながっている辺があるとき
            ans = search_next_node_and_add_side_wate(tree_dic, ans, next_node, c_lst, ans_lst)

    return ans


ans = 0
current_node = 1
ans_lst[0] = c_lst.pop()
ans = search_next_node_and_add_side_wate(tree_dic, ans, current_node, c_lst, ans_lst)


ans_str = ""
for i in range(N):
    ans_str += str(ans_lst[i])
    if i != N - 1:
        ans_str += " "


print(ans)
print(ans_str)
