from collections import deque
import sys
sys.setrecursionlimit(10**9)

n = int(input())
tree = [[] for _i in range(n)]
for _i in range(n-1):
    a, b = map(int, input().split())
    tree[a-1].append(b-1)
    tree[b-1].append(a-1)

c = deque(sorted(list(map(int, input().split())), reverse=True))
visit = [-1 for _i in range(n)]
visit[0] = c.popleft()

def solve(p):
    r = 0
    for i in tree[p]:
        if visit[i] < 0:
            visit[i] = c.popleft()
            r += visit[i]
            r += solve(i)
    return r
x = solve(0)
print(x)
print(*visit) from collections import deque import sys sys sys.setrecursionlimit(10**9) sys.setrecursionlimit sys setrecursionlimit 10**9 10 9 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) [] tree = [[] for _i in range(n)] tree [[] for _i in range(n)] for _i in range(n-1):
    a, b = map(int, input().split())
    tree[a-1].append(b-1)
    tree[b-1].append(a-1) _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 tree[a-1].append(b-1) [a-1].append [a-1] tree a-1 a 1 append b-1 b 1 tree[b-1].append(a-1) [b-1].append [b-1] tree b-1 b 1 append a-1 a 1 c = deque(sorted(list(map(int, input().split())), reverse=True)) c deque(sorted(list(map(int, input().split())), reverse=True)) deque 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 -1 for _i in range(n) for _i in range(n) _i range(n) range n for _i in range(n) -1 visit = [-1 for _i in range(n)] visit [-1 for _i in range(n)] visit[0] = c.popleft() visit[0] visit 0 c.popleft() c.popleft c popleft def solve(p):
    r = 0
    for i in tree[p]:
        if visit[i] < 0:
            visit[i] = c.popleft()
            r += visit[i]
            r += solve(i)
    return r solve r = 0 r 0 for i in tree[p]:
        if visit[i] < 0:
            visit[i] = c.popleft()
            r += visit[i]
            r += solve(i)
     i tree[p] tree p if visit[i] < 0:
            visit[i] = c.popleft()
            r += visit[i]
            r += solve(i)
     visit[i] < 0 visit[i] visit i 0 visit[i] = c.popleft() visit[i] visit i c.popleft() c.popleft c popleft r += visit[i] r visit[i] visit i r += solve(i) r solve(i) solve i return r r p p x = solve(0) x solve(0) solve 0 print(x) print x print(*visit) print *visit visit c = deque(sorted(list(map(int, input().split())), reverse=True)) deque(sorted(list(map(int, input().split())), reverse=True)) c visit = [-1 for _i in range(n)] [-1 for _i in range(n)] visit x = solve(0) solve(0) x b = map(int, input().split()) map(int, input().split()) b a, b = map(int, input().split()) map(int, input().split()) a n = int(input()) int(input()) n tree = [[] for _i in range(n)] [[] for _i in range(n)] tree def solve(p):
    r = 0
    for i in tree[p]:
        if visit[i] < 0:
            visit[i] = c.popleft()
            r += visit[i]
            r += solve(i)
    return r def solve(p):
    r = 0
    for i in tree[p]:
        if visit[i] < 0:
            visit[i] = c.popleft()
            r += visit[i]
            r += solve(i)
    return r solve
from collections import deque
import sys
sys.setrecursionlimit(10**9)

n = int(input())
tree = [[] for _i in range(n)]
for _i in range(n-1):
    a, b = map(int, input().split())
    tree[a-1].append(b-1)
    tree[b-1].append(a-1)

c = deque(sorted(list(map(int, input().split())), reverse=True))
visit = [-1 for _i in range(n)]
visit[0] = c.popleft()

def solve(p):
    r = 0
    for i in tree[p]:
        if visit[i] < 0:
            visit[i] = c.popleft()
            r += visit[i]
            r += solve(i)
    return r
x = solve(0)
print(x)
print(*visit)