#グラフの連結成分を調べる
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(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 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(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)) bfs q = deque() q deque() deque q.append((v, p)) q.append q append (v, p) v p rec[v - 1] = c.pop() rec[v - 1] rec v - 1 v 1 c.pop() c.pop 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)) c V, P = q.popleft() V q.popleft() q.popleft q popleft P for nv in G[V - 1]:
            if nv == P:continue
            rec[nv - 1] = c.pop()
            q.append((nv, V)) nv G[V - 1] G V - 1 V 1 if nv == P:continue
             nv == P nv P rec[nv - 1] = c.pop() rec[nv - 1] rec nv - 1 nv 1 c.pop() c.pop c pop q.append((nv, V)) q.append q append (nv, V) nv V 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.sort() c.sort c sort G = Graph(ab) G Graph(ab) Graph ab print(sum(c[:-1])) print sum(c[:-1]) sum c[:-1] c :-1 -1 rec = [0] * n rec [0] * n [0] 0 n bfs(1, -1) bfs 1 -1 print(*rec) print *rec 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 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 Graph c = list(map(int, input().split())) list(map(int, input().split())) c G = Graph(ab) Graph(ab) G 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)) 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)) bfs rec = [0] * n [0] * n rec ab = [list(map(int, input().split())) for i in range(n - 1)] [list(map(int, input().split())) for i in range(n - 1)] ab n = int(input()) int(input()) n
#グラフの連結成分を調べる
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(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)