#グラフの連結成分を調べる

def Graph(ab):

    G=[[] for i in range(n)]

    for a,b in ab:

        G[a-1].append(b)

        G[b-1].append(a)

    return G



#BFS

from collections import deque

def bfs(G, v, p):

    point = [0] * n

    q=deque()

    q.append((v, p))

    point[0] = c.pop(0)



    score = 0

    while q:

        #qの先頭を取り出す

        V, P = q.popleft()

        for next_v in G[V - 1]:

            if next_v == P:continue

            q.append((next_v, V))

            temp = c.pop(0)

            score += temp

            point[next_v - 1] = temp



    return point, score



n = int(eval(input()))

ab = [list(map(int,input().split())) for i in range(n - 1)]

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

c = sorted(c, reverse=True)

G = Graph(ab)



point, score = bfs(G, 1, -1)

print(score)

print((*point))
#グラフの連結成分を調べる

def Graph(ab):

    G=[[] for i in range(n)]

    for a,b in ab:

        G[a-1].append(b)

        G[b-1].append(a)

    return G

from collections import deque

def bfs(v, p):

    q = deque()

    q.append((v, p))

    rec[v - 1] = c.pop()

    while c:

        V, P = q.popleft()

        for nv in G[V - 1]:

            if nv == P:continue

            rec[nv - 1] = c.pop()

            q.append((nv, V))



n = int(eval(input()))

ab = [list(map(int, input().split())) for i in range(n - 1)]

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

c.sort()

G = Graph(ab)

print((sum(c[:-1])))

rec = [0] * n

bfs(1, -1)

print((*rec))
# グラフの連結成分を調べる
def Graph(ab):
    G = [[] for i in range(n)]
    for a, b in ab:
        G[a - 1].append(b)
        G[b - 1].append(a)
    return G


# BFS
from collections import deque


def bfs(G, v, p):
    point = [0] * n
    q = deque()
    q.append((v, p))
    point[0] = c.pop(0)
    score = 0
    while q:
        # qの先頭を取り出す
        V, P = q.popleft()
        for next_v in G[V - 1]:
            if next_v == P:
                continue
            q.append((next_v, V))
            temp = c.pop(0)
            score += temp
            point[next_v - 1] = temp
    return point, score


n = int(eval(input()))
ab = [list(map(int, input().split())) for i in range(n - 1)]
c = list(map(int, input().split()))
c = sorted(c, reverse=True)
G = Graph(ab)
point, score = bfs(G, 1, -1)
print(score)
print((*point))

# グラフの連結成分を調べる
def Graph(ab):
    G = [[] for i in range(n)]
    for a, b in ab:
        G[a - 1].append(b)
        G[b - 1].append(a)
    return G


from collections import deque


def bfs(v, p):
    q = deque()
    q.append((v, p))
    rec[v - 1] = c.pop()
    while c:
        V, P = q.popleft()
        for nv in G[V - 1]:
            if nv == P:
                continue
            rec[nv - 1] = c.pop()
            q.append((nv, V))


n = int(eval(input()))
ab = [list(map(int, input().split())) for i in range(n - 1)]
c = list(map(int, input().split()))
c.sort()
G = Graph(ab)
print((sum(c[:-1])))
rec = [0] * n
bfs(1, -1)
print((*rec))

-# BFS -def bfs(G, v, p): -    point = [0] * n +def bfs(v, p): -    point[0] = c.pop(0) -    score = 0 -    while q: -        # qの先頭を取り出す +    rec[v - 1] = c.pop() +    while c: -        for next_v in G[V - 1]: -            if next_v == P: +        for nv in G[V - 1]: +            if nv == P: -            q.append((next_v, V)) -            temp = c.pop(0) -            score += temp -            point[next_v - 1] = temp -    return point, score +            rec[nv - 1] = c.pop() +            q.append((nv, V)) -c = sorted(c, reverse=True) +c.sort() -point, score = bfs(G, 1, -1) -print(score) -print((*point)) +print((sum(c[:-1]))) +rec = [0] * n +bfs(1, -1) +print((*rec))