from collections import deque
N = int(input())
tree = [[] for _ in range(N)]
for _ in range(N-1):
    a, b = map(lambda x: int(x)-1, input().split())
    tree[a].append(b)
    tree[b].append(a)
C = list(map(int, input().split()))
C.sort(reverse=True)
q = deque([0])
visited = [False] * N
ans = {}
ans[0] = C[0]
visited[0] = True
pointa = 1
while len(q):
    v = q.pop()
    for e in tree[v]:
        if visited[e]:
            continue
        visited[e] = True
        ans[e] = C[pointa]
        pointa += 1
        q.append(e)
num = []
for i in range(N):
    num.append(str(ans[i]))
print(sum(C[1:]))
print(" ".join(num)) 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) [] tree = [[] for _ in range(N)] tree [[] for _ in range(N)] for _ in range(N-1):
    a, b = map(lambda x: int(x)-1, input().split())
    tree[a].append(b)
    tree[b].append(a) _ range(N-1) range N-1 N 1 a, b = map(lambda x: int(x)-1, input().split()) a map(lambda x: int(x)-1, input().split()) map lambda x: int(x)-1 int(x)-1 int(x) int x 1 x input().split() ().split () input split b tree[a].append(b) [a].append [a] tree a append b tree[b].append(a) [b].append [b] tree b append a 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([0]) q deque([0]) deque [0] 0 visited = [False] * N visited [False] * N [False] False N ans = {} ans {} ans[0] = C[0] ans[0] ans 0 C[0] C 0 visited[0] = True visited[0] visited 0 True pointa = 1 pointa 1 while len(q):
    v = q.pop()
    for e in tree[v]:
        if visited[e]:
            continue
        visited[e] = True
        ans[e] = C[pointa]
        pointa += 1
        q.append(e) len(q) len q v = q.pop() v q.pop() q.pop q pop for e in tree[v]:
        if visited[e]:
            continue
        visited[e] = True
        ans[e] = C[pointa]
        pointa += 1
        q.append(e) e tree[v] tree v if visited[e]:
            continue
         visited[e] visited e continue visited[e] = True visited[e] visited e True ans[e] = C[pointa] ans[e] ans e C[pointa] C pointa pointa += 1 pointa 1 q.append(e) q.append q append e num = [] num [] for i in range(N):
    num.append(str(ans[i])) i range(N) range N num.append(str(ans[i])) num.append num append str(ans[i]) str ans[i] ans i print(sum(C[1:])) print sum(C[1:]) sum C[1:] C 1: 1 print(" ".join(num)) print " ".join(num) " ".join " " join num pointa += 1 1 pointa num = [] [] num a, b = map(lambda x: int(x)-1, input().split()) map(lambda x: int(x)-1, input().split()) a pointa = 1 1 pointa v = q.pop() q.pop() v b = map(lambda x: int(x)-1, input().split()) map(lambda x: int(x)-1, input().split()) b visited = [False] * N [False] * N visited N = int(input()) int(input()) N C = list(map(int, input().split())) list(map(int, input().split())) C ans = {} {} ans tree = [[] for _ in range(N)] [[] for _ in range(N)] tree q = deque([0]) deque([0]) q
from collections import deque
N = int(input())
tree = [[] for _ in range(N)]
for _ in range(N-1):
    a, b = map(lambda x: int(x)-1, input().split())
    tree[a].append(b)
    tree[b].append(a)
C = list(map(int, input().split()))
C.sort(reverse=True)
q = deque([0])
visited = [False] * N
ans = {}
ans[0] = C[0]
visited[0] = True
pointa = 1
while len(q):
    v = q.pop()
    for e in tree[v]:
        if visited[e]:
            continue
        visited[e] = True
        ans[e] = C[pointa]
        pointa += 1
        q.append(e)
num = []
for i in range(N):
    num.append(str(ans[i]))
print(sum(C[1:]))
print(" ".join(num))
