from collections import deque
# import heapq


N = int(input())
G = [[] for i in range(N)]
for i in range(N-1):
    u,v = map(int,input().split())
    # グラフに頂点を追加
    G[u-1].append((v-1))
    G[v-1].append((u-1))
cs = sorted(list(map(int,input().split())),reverse=True)
gs = [len(g) for g in G]
K = gs.index(min(gs))

ds = [-1]*N
used = [False] * N
ans = 0

# BFS
q = deque([K])
ds[K] = cs[0]
i = 1
while len(q) > 0:
    a = q.popleft()
    Vs = G[a]
    for u in Vs:
        if ds[u] == -1:
            q.append(u)
            ds[u] = cs[i]
            used[u] = True
            i += 1
ansd = ""
for d in ds:
    ansd += str(d)+" "
print(sum(cs)-max(cs))
print(ansd) from collections import deque N = int(input()) N int(input()) int input() input [] 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 i in range(N-1):
    u,v = map(int,input().split())
    # グラフに頂点を追加
    G[u-1].append((v-1))
    G[v-1].append((u-1)) i range(N-1) range N-1 N 1 u,v = map(int,input().split()) u map(int,input().split()) map int input().split() ().split () input split v G[u-1].append((v-1)) [u-1].append [u-1] G u-1 u 1 append v-1 v 1 G[v-1].append((u-1)) [v-1].append [v-1] G v-1 v 1 append u-1 u 1 cs = sorted(list(map(int,input().split())),reverse=True) cs sorted(list(map(int,input().split())),reverse=True) sorted list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split reverse=True reverse True len(g) for g in G for g in G g G for g in G len(g) len g gs = [len(g) for g in G] gs [len(g) for g in G] K = gs.index(min(gs)) K gs.index(min(gs)) gs.index gs index min(gs) min gs ds = [-1]*N ds [-1]*N [-1] -1 N used = [False] * N used [False] * N [False] False N ans = 0 ans 0 q = deque([K]) q deque([K]) deque [K] K ds[K] = cs[0] ds[K] ds K cs[0] cs 0 i = 1 i 1 while len(q) > 0:
    a = q.popleft()
    Vs = G[a]
    for u in Vs:
        if ds[u] == -1:
            q.append(u)
            ds[u] = cs[i]
            used[u] = True
            i += 1 len(q) > 0 len(q) len q 0 a = q.popleft() a q.popleft() q.popleft q popleft Vs = G[a] Vs G[a] G a for u in Vs:
        if ds[u] == -1:
            q.append(u)
            ds[u] = cs[i]
            used[u] = True
            i += 1 u Vs if ds[u] == -1:
            q.append(u)
            ds[u] = cs[i]
            used[u] = True
            i += 1 ds[u] == -1 ds[u] ds u -1 q.append(u) q.append q append u ds[u] = cs[i] ds[u] ds u cs[i] cs i used[u] = True used[u] used u True i += 1 i 1 ansd = "" ansd "" for d in ds:
    ansd += str(d)+" " d ds ansd += str(d)+" " ansd str(d)+" " str(d) str d " " print(sum(cs)-max(cs)) print sum(cs)-max(cs) sum(cs) sum cs max(cs) max cs print(ansd) print ansd ansd = "" "" ansd v = map(int,input().split()) map(int,input().split()) v ansd += str(d)+" " str(d)+" " ansd i = 1 1 i K = gs.index(min(gs)) gs.index(min(gs)) K N = int(input()) int(input()) N u,v = map(int,input().split()) map(int,input().split()) u ans = 0 0 ans i += 1 1 i used = [False] * N [False] * N used gs = [len(g) for g in G] [len(g) for g in G] gs ds = [-1]*N [-1]*N ds q = deque([K]) deque([K]) q G = [[] for i in range(N)] [[] for i in range(N)] G a = q.popleft() q.popleft() a Vs = G[a] G[a] Vs cs = sorted(list(map(int,input().split())),reverse=True) sorted(list(map(int,input().split())),reverse=True) cs
from collections import deque
# import heapq


N = int(input())
G = [[] for i in range(N)]
for i in range(N-1):
    u,v = map(int,input().split())
    # グラフに頂点を追加
    G[u-1].append((v-1))
    G[v-1].append((u-1))
cs = sorted(list(map(int,input().split())),reverse=True)
gs = [len(g) for g in G]
K = gs.index(min(gs))

ds = [-1]*N
used = [False] * N
ans = 0

# BFS
q = deque([K])
ds[K] = cs[0]
i = 1
while len(q) > 0:
    a = q.popleft()
    Vs = G[a]
    for u in Vs:
        if ds[u] == -1:
            q.append(u)
            ds[u] = cs[i]
            used[u] = True
            i += 1
ansd = ""
for d in ds:
    ansd += str(d)+" "
print(sum(cs)-max(cs))
print(ansd)