#!/usr/bin python3
# -*- coding: utf-8 -*-

# 双方向グラフで幅優先探索

from collections import deque

N = int(input())
graph = [[] for _ in range(N)]
#隣接リストの作成
for i in range(N-1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    graph[a].append(b)
    graph[b].append(a)
mx = 0
st = -1
for i, g in enumerate(graph):
    if len(g)>mx:
        st = i
        mx = len(g)
#    print(graph)
c = list(map(int,input().split()))
c.sort(reverse = True)

#幅優先探索
q = deque()
seen = [False]*N
q.append(st)
seen[st] = c[0]
idx = 1
while len(q)>0:
    cur = q.popleft()
    for i in graph[cur]:
        if seen[i]==False:
            seen[i] = c[idx]
            idx += 1
            q.append(i)
print(sum(c[1:]))
print(' '.join(map(str,seen))) from collections import deque N = int(input()) N int(input()) int input() input [] for _ in range(N) for _ in range(N) _ range(N) range N for _ in range(N) [] graph = [[] for _ in range(N)] graph [[] for _ in range(N)] for i in range(N-1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    graph[a].append(b)
    graph[b].append(a) i range(N-1) range N-1 N 1 a, b = map(int, input().split()) a map(int, input().split()) map int input().split() ().split () input split b a -= 1 a 1 b -= 1 b 1 graph[a].append(b) [a].append [a] graph a append b graph[b].append(a) [b].append [b] graph b append a mx = 0 mx 0 st = -1 st -1 for i, g in enumerate(graph):
    if len(g)>mx:
        st = i
        mx = len(g)
#    print(graph) i g enumerate(graph) enumerate graph if len(g)>mx:
        st = i
        mx = len(g)
#    print(graph) len(g)>mx len(g) len g mx st = i st i mx = len(g) mx len(g) len g 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(reverse = True) c.sort c sort reverse = True reverse True q = deque() q deque() deque seen = [False]*N seen [False]*N [False] False N q.append(st) q.append q append st seen[st] = c[0] seen[st] seen st c[0] c 0 idx = 1 idx 1 while len(q)>0:
    cur = q.popleft()
    for i in graph[cur]:
        if seen[i]==False:
            seen[i] = c[idx]
            idx += 1
            q.append(i) len(q)>0 len(q) len q 0 cur = q.popleft() cur q.popleft() q.popleft q popleft for i in graph[cur]:
        if seen[i]==False:
            seen[i] = c[idx]
            idx += 1
            q.append(i) i graph[cur] graph cur if seen[i]==False:
            seen[i] = c[idx]
            idx += 1
            q.append(i) seen[i]==False seen[i] seen i False seen[i] = c[idx] seen[i] seen i c[idx] c idx idx += 1 idx 1 q.append(i) q.append q append i print(sum(c[1:])) print sum(c[1:]) sum c[1:] c 1: 1 print(' '.join(map(str,seen))) print ' '.join(map(str,seen)) ' '.join ' ' join map(str,seen) map str seen b = map(int, input().split()) map(int, input().split()) b mx = 0 0 mx idx += 1 1 idx st = -1 -1 st a -= 1 1 a c = list(map(int,input().split())) list(map(int,input().split())) c idx = 1 1 idx N = int(input()) int(input()) N graph = [[] for _ in range(N)] [[] for _ in range(N)] graph mx = len(g) len(g) mx q = deque() deque() q st = i i st seen = [False]*N [False]*N seen b -= 1 1 b cur = q.popleft() q.popleft() cur a, b = map(int, input().split()) map(int, input().split()) a
#!/usr/bin python3
# -*- coding: utf-8 -*-

# 双方向グラフで幅優先探索

from collections import deque

N = int(input())
graph = [[] for _ in range(N)]
#隣接リストの作成
for i in range(N-1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    graph[a].append(b)
    graph[b].append(a)
mx = 0
st = -1
for i, g in enumerate(graph):
    if len(g)>mx:
        st = i
        mx = len(g)
#    print(graph)
c = list(map(int,input().split()))
c.sort(reverse = True)

#幅優先探索
q = deque()
seen = [False]*N
q.append(st)
seen[st] = c[0]
idx = 1
while len(q)>0:
    cur = q.popleft()
    for i in graph[cur]:
        if seen[i]==False:
            seen[i] = c[idx]
            idx += 1
            q.append(i)
print(sum(c[1:]))
print(' '.join(map(str,seen)))