n = int(input())

deg = [0 for i in range(n)]

from collections import defaultdict
G = defaultdict(list)


abl = []
for _ in range(n-1):
    a,b = map(int,input().split())
    deg[a-1] += 1
    deg[b-1] += 1
    abl.append((a-1, b-1))
    G[a-1].append(b-1)
    G[b-1].append(a-1)

cl = list(map(int,input().split()))
cl = sorted(cl)
ansl = [0 for _ in range(n)]
for c in cl:
    for idx, d in enumerate(deg):
        if d == 1:
            ansl[idx] = c
            deg[idx] -= 1
            for j in G[idx]:
                deg[j] -= 1
            break
for idx, a in enumerate(ansl):
    if a == 0:
        ansl[idx] = cl[-1]

print(sum(cl[:-1]))
print(*ansl)

 n = int(input()) n int(input()) int input() input 0 for i in range(n) for i in range(n) i range(n) range n for i in range(n) 0 deg = [0 for i in range(n)] deg [0 for i in range(n)] from collections import defaultdict G = defaultdict(list) G defaultdict(list) defaultdict list abl = [] abl [] for _ in range(n-1):
    a,b = map(int,input().split())
    deg[a-1] += 1
    deg[b-1] += 1
    abl.append((a-1, b-1))
    G[a-1].append(b-1)
    G[b-1].append(a-1) _ 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 deg[a-1] += 1 deg[a-1] deg a-1 a 1 1 deg[b-1] += 1 deg[b-1] deg b-1 b 1 1 abl.append((a-1, b-1)) abl.append abl append (a-1, b-1) a-1 a 1 b-1 b 1 G[a-1].append(b-1) [a-1].append [a-1] G a-1 a 1 append b-1 b 1 G[b-1].append(a-1) [b-1].append [b-1] G b-1 b 1 append a-1 a 1 cl = list(map(int,input().split())) cl list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split cl = sorted(cl) cl sorted(cl) sorted cl 0 for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) 0 ansl = [0 for _ in range(n)] ansl [0 for _ in range(n)] for c in cl:
    for idx, d in enumerate(deg):
        if d == 1:
            ansl[idx] = c
            deg[idx] -= 1
            for j in G[idx]:
                deg[j] -= 1
            break c cl for idx, d in enumerate(deg):
        if d == 1:
            ansl[idx] = c
            deg[idx] -= 1
            for j in G[idx]:
                deg[j] -= 1
            break idx d enumerate(deg) enumerate deg if d == 1:
            ansl[idx] = c
            deg[idx] -= 1
            for j in G[idx]:
                deg[j] -= 1
            break d == 1 d 1 ansl[idx] = c ansl[idx] ansl idx c deg[idx] -= 1 deg[idx] deg idx 1 for j in G[idx]:
                deg[j] -= 1
             j G[idx] G idx deg[j] -= 1 deg[j] deg j 1 break for idx, a in enumerate(ansl):
    if a == 0:
        ansl[idx] = cl[-1] idx a enumerate(ansl) enumerate ansl if a == 0:
        ansl[idx] = cl[-1] a == 0 a 0 ansl[idx] = cl[-1] ansl[idx] ansl idx cl[-1] cl -1 print(sum(cl[:-1])) print sum(cl[:-1]) sum cl[:-1] cl :-1 -1 print(*ansl) print *ansl ansl cl = list(map(int,input().split())) list(map(int,input().split())) cl abl = [] [] abl G = defaultdict(list) defaultdict(list) G b = map(int,input().split()) map(int,input().split()) b cl = sorted(cl) sorted(cl) cl ansl = [0 for _ in range(n)] [0 for _ in range(n)] ansl deg = [0 for i in range(n)] [0 for i in range(n)] deg n = int(input()) int(input()) n a,b = map(int,input().split()) map(int,input().split()) a
n = int(input())

deg = [0 for i in range(n)]

from collections import defaultdict
G = defaultdict(list)


abl = []
for _ in range(n-1):
    a,b = map(int,input().split())
    deg[a-1] += 1
    deg[b-1] += 1
    abl.append((a-1, b-1))
    G[a-1].append(b-1)
    G[b-1].append(a-1)

cl = list(map(int,input().split()))
cl = sorted(cl)
ansl = [0 for _ in range(n)]
for c in cl:
    for idx, d in enumerate(deg):
        if d == 1:
            ansl[idx] = c
            deg[idx] -= 1
            for j in G[idx]:
                deg[j] -= 1
            break
for idx, a in enumerate(ansl):
    if a == 0:
        ansl[idx] = cl[-1]

print(sum(cl[:-1]))
print(*ansl)


