from collections import deque, _heapq
N = int(input())

g = [{"num":0, "neighber":[]} for _ in range(N+1)]

for _ in range(N-1):
    x, y = map(int, input().split())
    g[x]["neighber"].append(y)
    g[y]["neighber"].append(x)

c = sorted(map(int, input().split()))
print(sum(c[:-1]))

i = 0
s = [(0, 1)]
while i < len(s):
    pre, cur = s[i]
    i+=1
    g[cur]["num"] = c.pop()

    for x in g[cur]["neighber"]:
        if x == pre:
            continue
        s.append((cur, x))

print(" ".join((str(g[i]["num"]) for i in range(1, N+1)))) from collections import deque, _heapq N = int(input()) N int(input()) int input() input {"num":0, "neighber":[]} for _ in range(N+1) for _ in range(N+1) _ range(N+1) range N+1 N 1 for _ in range(N+1) {"num":0, "neighber":[]} "num" "num" 0 "neighber" "neighber" [] g = [{"num":0, "neighber":[]} for _ in range(N+1)] g [{"num":0, "neighber":[]} for _ in range(N+1)] for _ in range(N-1):
    x, y = map(int, input().split())
    g[x]["neighber"].append(y)
    g[y]["neighber"].append(x) _ range(N-1) range N-1 N 1 x, y = map(int, input().split()) x map(int, input().split()) map int input().split() ().split () input split y g[x]["neighber"].append(y) ["neighber"].append ["neighber"] [x] g x "neighber" append y g[y]["neighber"].append(x) ["neighber"].append ["neighber"] [y] g y "neighber" append x c = sorted(map(int, input().split())) c sorted(map(int, input().split())) sorted map(int, input().split()) map int input().split() ().split () input split print(sum(c[:-1])) print sum(c[:-1]) sum c[:-1] c :-1 -1 i = 0 i 0 s = [(0, 1)] s [(0, 1)] (0, 1) 0 1 while i < len(s):
    pre, cur = s[i]
    i+=1
    g[cur]["num"] = c.pop()

    for x in g[cur]["neighber"]:
        if x == pre:
            continue
        s.append((cur, x)) i < len(s) i len(s) len s pre, cur = s[i] pre s[i] s i cur i+=1 i 1 g[cur]["num"] = c.pop() g[cur]["num"] [cur] g cur "num" c.pop() c.pop c pop for x in g[cur]["neighber"]:
        if x == pre:
            continue
        s.append((cur, x)) x g[cur]["neighber"] [cur] g cur "neighber" if x == pre:
            continue
         x == pre x pre continue s.append((cur, x)) s.append s append (cur, x) cur x str(g[i]["num"]) for i in range(1, N+1) for i in range(1, N+1) i range(1, N+1) range 1 N+1 N 1 for i in range(1, N+1) str(g[i]["num"]) str g[i]["num"] [i] g i "num" print(" ".join((str(g[i]["num"]) for i in range(1, N+1)))) print " ".join((str(g[i]["num"]) for i in range(1, N+1))) " ".join " " join (str(g[i]["num"]) for i in range(1, N+1)) c = sorted(map(int, input().split())) sorted(map(int, input().split())) c i+=1 1 i cur = s[i] s[i] cur g = [{"num":0, "neighber":[]} for _ in range(N+1)] [{"num":0, "neighber":[]} for _ in range(N+1)] g x, y = map(int, input().split()) map(int, input().split()) x i = 0 0 i N = int(input()) int(input()) N pre, cur = s[i] s[i] pre y = map(int, input().split()) map(int, input().split()) y s = [(0, 1)] [(0, 1)] s
from collections import deque, _heapq
N = int(input())

g = [{"num":0, "neighber":[]} for _ in range(N+1)]

for _ in range(N-1):
    x, y = map(int, input().split())
    g[x]["neighber"].append(y)
    g[y]["neighber"].append(x)

c = sorted(map(int, input().split()))
print(sum(c[:-1]))

i = 0
s = [(0, 1)]
while i < len(s):
    pre, cur = s[i]
    i+=1
    g[cur]["num"] = c.pop()

    for x in g[cur]["neighber"]:
        if x == pre:
            continue
        s.append((cur, x))

print(" ".join((str(g[i]["num"]) for i in range(1, N+1))))
