#グラフの連結成分を調べる
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(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

#BFS Graph [] for i in range(n) for i in range(n) i range(n) range n for i in range(n) [] G=[[] for i in range(n)] G [[] for i in range(n)] for a,b in ab:
        G[a-1].append(b)
        G[b-1].append(a)
     a b ab G[a-1].append(b) [a-1].append [a-1] G a-1 a 1 append b G[b-1].append(a) [b-1].append [b-1] G b-1 b 1 append a return G G ab ab 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 bfs point = [0] * n point [0] * n [0] 0 n q=deque() q deque() deque q.append((v, p)) q.append q append (v, p) v p point[0] = c.pop(0) point[0] point 0 c.pop(0) c.pop c pop 0 score = 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

     q V, P = q.popleft() V q.popleft() q.popleft q popleft P 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

     next_v G[V - 1] G V - 1 V 1 if next_v == P:continue
             next_v == P next_v P q.append((next_v, V)) q.append q append (next_v, V) next_v V temp = c.pop(0) temp c.pop(0) c.pop c pop 0 score += temp score temp point[next_v - 1] = temp point[next_v - 1] point next_v - 1 next_v 1 temp return point, score return point, score point score G G v v p p n = int(input()) n int(input()) int input() input list(map(int,input().split())) for i in range(n - 1) for i in range(n - 1) i range(n - 1) range n - 1 n 1 for i in range(n - 1) list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split ab = [list(map(int,input().split())) for i in range(n - 1)] ab [list(map(int,input().split())) for i in range(n - 1)] c = list(map(int,input().split())) c list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split c = sorted(c, reverse=True) c sorted(c, reverse=True) sorted c reverse=True reverse True G = Graph(ab) G Graph(ab) Graph ab point, score = bfs(G, 1, -1) point bfs(G, 1, -1) bfs G 1 -1 score print(score) print score print(*point) print *point point score = bfs(G, 1, -1) bfs(G, 1, -1) score 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 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 bfs point, score = bfs(G, 1, -1) bfs(G, 1, -1) point n = int(input()) int(input()) n c = list(map(int,input().split())) list(map(int,input().split())) c c = sorted(c, reverse=True) sorted(c, reverse=True) c ab = [list(map(int,input().split())) for i in range(n - 1)] [list(map(int,input().split())) for i in range(n - 1)] ab G = Graph(ab) Graph(ab) G 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 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 Graph
#グラフの連結成分を調べる
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(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)