import sys

import collections

input = sys.stdin.readline

N = int(eval(input()))

edge = [list(map(int,input().split())) for _ in range(N-1)]

c = sorted(list(map(int,input().split())),reverse=True)

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

ans = [-1 for _ in range(N)]

queue = collections.deque()



for e in edge:

    graph[e[0]-1].append(e[1]-1)

    graph[e[1]-1].append(e[0]-1)



for i in range(N):

    if len(graph[i])==1:

        queue.append(i)

        ans[i] = c[0]

        break

k=1

while queue:

    node = queue.popleft()

    for adj in graph[node]:

        if ans[adj]==-1:

            ans[adj] = c[k]

            k += 1

            queue.append(adj)

            

print((sum(c)-max(c)))

print((*ans))
class Tree():

    def __init__(self, n, edge, indexed=1):

        self.n = n

        self.tree = [[] for _ in range(n)]

        for e in edge:

            self.tree[e[0] - indexed].append(e[1] - indexed)

            self.tree[e[1] - indexed].append(e[0] - indexed)



    def setroot(self, root):

        self.root = root

        self.parent = [None for _ in range(self.n)]

        self.parent[root] = -1

        self.depth = [None for _ in range(self.n)]

        self.depth[root] = 0

        self.order = []

        self.order.append(root)

        self.size = [1 for _ in range(self.n)]

        stack = [root]

        while stack:

            node = stack.pop()

            for adj in self.tree[node]:

                if self.parent[adj] is None:

                    self.parent[adj] = node

                    self.depth[adj] = self.depth[node] + 1

                    self.order.append(adj)

                    stack.append(adj)

        for node in self.order[::-1]:

            for adj in self.tree[node]:

                if self.parent[node] == adj:

                    continue

                self.size[node] += self.size[adj]



import sys

input = sys.stdin.readline



N = int(eval(input()))

E = [tuple(map(int, input().split())) for _ in range(N - 1)]



C = list(map(int, input().split()))

C.sort(reverse=True)



res = [None for _ in range(N)]

t = Tree(N, E)

t.setroot(0)



for i in range(N):

    res[t.order[i]] = C[i]



print((sum(C) - max(C)))

print((*res))
import sys
import collections

input = sys.stdin.readline
N = int(eval(input()))
edge = [list(map(int, input().split())) for _ in range(N - 1)]
c = sorted(list(map(int, input().split())), reverse=True)
graph = [[] for _ in range(N)]
ans = [-1 for _ in range(N)]
queue = collections.deque()
for e in edge:
    graph[e[0] - 1].append(e[1] - 1)
    graph[e[1] - 1].append(e[0] - 1)
for i in range(N):
    if len(graph[i]) == 1:
        queue.append(i)
        ans[i] = c[0]
        break
k = 1
while queue:
    node = queue.popleft()
    for adj in graph[node]:
        if ans[adj] == -1:
            ans[adj] = c[k]
            k += 1
            queue.append(adj)
print((sum(c) - max(c)))
print((*ans))

class Tree:
    def __init__(self, n, edge, indexed=1):
        self.n = n
        self.tree = [[] for _ in range(n)]
        for e in edge:
            self.tree[e[0] - indexed].append(e[1] - indexed)
            self.tree[e[1] - indexed].append(e[0] - indexed)

    def setroot(self, root):
        self.root = root
        self.parent = [None for _ in range(self.n)]
        self.parent[root] = -1
        self.depth = [None for _ in range(self.n)]
        self.depth[root] = 0
        self.order = []
        self.order.append(root)
        self.size = [1 for _ in range(self.n)]
        stack = [root]
        while stack:
            node = stack.pop()
            for adj in self.tree[node]:
                if self.parent[adj] is None:
                    self.parent[adj] = node
                    self.depth[adj] = self.depth[node] + 1
                    self.order.append(adj)
                    stack.append(adj)
        for node in self.order[::-1]:
            for adj in self.tree[node]:
                if self.parent[node] == adj:
                    continue
                self.size[node] += self.size[adj]


import sys

input = sys.stdin.readline
N = int(eval(input()))
E = [tuple(map(int, input().split())) for _ in range(N - 1)]
C = list(map(int, input().split()))
C.sort(reverse=True)
res = [None for _ in range(N)]
t = Tree(N, E)
t.setroot(0)
for i in range(N):
    res[t.order[i]] = C[i]
print((sum(C) - max(C)))
print((*res))

+class Tree: +    def __init__(self, n, edge, indexed=1): +        self.n = n +        self.tree = [[] for _ in range(n)] +        for e in edge: +            self.tree[e[0] - indexed].append(e[1] - indexed) +            self.tree[e[1] - indexed].append(e[0] - indexed) + +    def setroot(self, root): +        self.root = root +        self.parent = [None for _ in range(self.n)] +        self.parent[root] = -1 +        self.depth = [None for _ in range(self.n)] +        self.depth[root] = 0 +        self.order = [] +        self.order.append(root) +        self.size = [1 for _ in range(self.n)] +        stack = [root] +        while stack: +            node = stack.pop() +            for adj in self.tree[node]: +                if self.parent[adj] is None: +                    self.parent[adj] = node +                    self.depth[adj] = self.depth[node] + 1 +                    self.order.append(adj) +                    stack.append(adj) +        for node in self.order[::-1]: +            for adj in self.tree[node]: +                if self.parent[node] == adj: +                    continue +                self.size[node] += self.size[adj] + + -import collections -edge = [list(map(int, input().split())) for _ in range(N - 1)] -c = sorted(list(map(int, input().split())), reverse=True) -graph = [[] for _ in range(N)] -ans = [-1 for _ in range(N)] -queue = collections.deque() -for e in edge: -    graph[e[0] - 1].append(e[1] - 1) -    graph[e[1] - 1].append(e[0] - 1) +E = [tuple(map(int, input().split())) for _ in range(N - 1)] +C = list(map(int, input().split())) +C.sort(reverse=True) +res = [None for _ in range(N)] +t = Tree(N, E) +t.setroot(0) -    if len(graph[i]) == 1: -        queue.append(i) -        ans[i] = c[0] -        break -k = 1 -while queue: -    node = queue.popleft() -    for adj in graph[node]: -        if ans[adj] == -1: -            ans[adj] = c[k] -            k += 1 -            queue.append(adj) -print((sum(c) - max(c))) -print((*ans)) +    res[t.order[i]] = C[i] +print((sum(C) - max(C))) +print((*res))