import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

N = int(input())
edge = [[] for _ in range(N + 1)]
AB = []
for _ in range(N - 1):
    a, b = map(int, input().split())
    edge[a].append(b)
    edge[b].append(a)
    AB.append((a, b))
C = list(map(int, input().split()))
C.sort()

for i, e in enumerate(edge[1:], start=1):
    if len(e) == 1:
        s = i
        break

node = [s]
memo = [-1] * (N + 1)
while node:
    s = node.pop()
    memo[s] = C.pop()
    for t in edge[s]:
        if memo[t] != -1:
            continue
        node.append(t)

ans = 0
for a, b in AB:
    ans += min(memo[a], memo[b])

print(ans)
print(*memo[1:]) import sys sys read = sys.stdin.buffer.read read sys.stdin.buffer.read sys.stdin.buffer sys.stdin sys stdin buffer read readline = sys.stdin.buffer.readline readline sys.stdin.buffer.readline sys.stdin.buffer sys.stdin sys stdin buffer readline readlines = sys.stdin.buffer.readlines readlines sys.stdin.buffer.readlines sys.stdin.buffer sys.stdin sys stdin buffer readlines sys.setrecursionlimit(10 ** 7) sys.setrecursionlimit sys setrecursionlimit 10 ** 7 10 7 N = int(input()) N int(input()) int input() input [] for _ in range(N + 1) for _ in range(N + 1) _ range(N + 1) range N + 1 N 1 for _ in range(N + 1) [] edge = [[] for _ in range(N + 1)] edge [[] for _ in range(N + 1)] AB = [] AB [] for _ in range(N - 1):
    a, b = map(int, input().split())
    edge[a].append(b)
    edge[b].append(a)
    AB.append((a, b)) _ 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 edge[a].append(b) [a].append [a] edge a append b edge[b].append(a) [b].append [b] edge b append a AB.append((a, b)) AB.append AB append (a, b) a b 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 for i, e in enumerate(edge[1:], start=1):
    if len(e) == 1:
        s = i
        break i e enumerate(edge[1:], start=1) enumerate edge[1:] edge 1: 1 start=1 start 1 if len(e) == 1:
        s = i
        break len(e) == 1 len(e) len e 1 s = i s i break node = [s] node [s] s memo = [-1] * (N + 1) memo [-1] * (N + 1) [-1] -1 N + 1 N 1 while node:
    s = node.pop()
    memo[s] = C.pop()
    for t in edge[s]:
        if memo[t] != -1:
            continue
        node.append(t) node s = node.pop() s node.pop() node.pop node pop memo[s] = C.pop() memo[s] memo s C.pop() C.pop C pop for t in edge[s]:
        if memo[t] != -1:
            continue
        node.append(t) t edge[s] edge s if memo[t] != -1:
            continue
         memo[t] != -1 memo[t] memo t -1 continue node.append(t) node.append node append t ans = 0 ans 0 for a, b in AB:
    ans += min(memo[a], memo[b]) a b AB ans += min(memo[a], memo[b]) ans min(memo[a], memo[b]) min memo[a] memo a memo[b] memo b print(ans) print ans print(*memo[1:]) print *memo[1:] memo[1:] memo 1: 1 ans += min(memo[a], memo[b]) min(memo[a], memo[b]) ans read = sys.stdin.buffer.read sys.stdin.buffer.read read readlines = sys.stdin.buffer.readlines sys.stdin.buffer.readlines readlines a, b = map(int, input().split()) map(int, input().split()) a edge = [[] for _ in range(N + 1)] [[] for _ in range(N + 1)] edge b = map(int, input().split()) map(int, input().split()) b node = [s] [s] node s = i i s s = node.pop() node.pop() s N = int(input()) int(input()) N C = list(map(int, input().split())) list(map(int, input().split())) C readline = sys.stdin.buffer.readline sys.stdin.buffer.readline readline memo = [-1] * (N + 1) [-1] * (N + 1) memo ans = 0 0 ans AB = [] [] AB
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

N = int(input())
edge = [[] for _ in range(N + 1)]
AB = []
for _ in range(N - 1):
    a, b = map(int, input().split())
    edge[a].append(b)
    edge[b].append(a)
    AB.append((a, b))
C = list(map(int, input().split()))
C.sort()

for i, e in enumerate(edge[1:], start=1):
    if len(e) == 1:
        s = i
        break

node = [s]
memo = [-1] * (N + 1)
while node:
    s = node.pop()
    memo[s] = C.pop()
    for t in edge[s]:
        if memo[t] != -1:
            continue
        node.append(t)

ans = 0
for a, b in AB:
    ans += min(memo[a], memo[b])

print(ans)
print(*memo[1:])
