import sys
from collections import deque
input = sys.stdin.readline
n = int(input())
T = [[] for _ in range(n)]
for i in range(n-1):
    ai,bi = map(int, input().split( ))
    ai-=1;bi-=1
    T[ai].append(bi)
    T[bi].append(ai)

c = list(map(int, input().split( )))
c.sort()


#最小の数字は葉に書き込む
#次点は葉あるいは最小頂点除いたら葉な点
#r = 0
Q = deque()
Q.append(0)
P = [-1 for i in range(n)]
P[0] = 0
D = [0]*n
L=[]
while Q:
    v = Q.popleft()
    leaf = True
    for u in T[v]:
        if P[u]<0:
            D[v]+=1
            P[u]=v
            leaf = False
            Q.append(u)
    if leaf:
        L.append(v)

Q =deque(L)

j = 0
A = [0]*n
m = 0
while Q:
    v = Q.popleft()
    A[v] = c[j]
    if v:
        m+=c[j]
    j+=1
    D[P[v]]-=1
    if not D[P[v]] and v:
        Q.append(P[v])
print(m)
print(*A) import sys sys from collections import deque input = sys.stdin.readline input sys.stdin.readline sys.stdin sys stdin readline 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) [] T = [[] for _ in range(n)] T [[] for _ in range(n)] for i in range(n-1):
    ai,bi = map(int, input().split( ))
    ai-=1;bi-=1
    T[ai].append(bi)
    T[bi].append(ai) i range(n-1) range n-1 n 1 ai,bi = map(int, input().split( )) ai map(int, input().split( )) map int input().split( ) ().split () input split bi ai-=1 ai 1 bi-=1 bi 1 T[ai].append(bi) [ai].append [ai] T ai append bi T[bi].append(ai) [bi].append [bi] T bi append ai 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 Q = deque() Q deque() deque Q.append(0) Q.append Q append 0 -1 for i in range(n) for i in range(n) i range(n) range n for i in range(n) -1 P = [-1 for i in range(n)] P [-1 for i in range(n)] P[0] = 0 P[0] P 0 0 D = [0]*n D [0]*n [0] 0 n L=[] L [] while Q:
    v = Q.popleft()
    leaf = True
    for u in T[v]:
        if P[u]<0:
            D[v]+=1
            P[u]=v
            leaf = False
            Q.append(u)
    if leaf:
        L.append(v) Q v = Q.popleft() v Q.popleft() Q.popleft Q popleft leaf = True leaf True for u in T[v]:
        if P[u]<0:
            D[v]+=1
            P[u]=v
            leaf = False
            Q.append(u)
     u T[v] T v if P[u]<0:
            D[v]+=1
            P[u]=v
            leaf = False
            Q.append(u)
     P[u]<0 P[u] P u 0 D[v]+=1 D[v] D v 1 P[u]=v P[u] P u v leaf = False leaf False Q.append(u) Q.append Q append u if leaf:
        L.append(v) leaf L.append(v) L.append L append v Q =deque(L) Q deque(L) deque L j = 0 j 0 A = [0]*n A [0]*n [0] 0 n m = 0 m 0 while Q:
    v = Q.popleft()
    A[v] = c[j]
    if v:
        m+=c[j]
    j+=1
    D[P[v]]-=1
    if not D[P[v]] and v:
        Q.append(P[v]) Q v = Q.popleft() v Q.popleft() Q.popleft Q popleft A[v] = c[j] A[v] A v c[j] c j if v:
        m+=c[j]
     v m+=c[j] m c[j] c j j+=1 j 1 D[P[v]]-=1 D[P[v]] D P[v] P v 1 if not D[P[v]] and v:
        Q.append(P[v]) not D[P[v]] and v not D[P[v]] D[P[v]] D P[v] P v v Q.append(P[v]) Q.append Q append P[v] P v print(m) print m print(*A) print *A A A = [0]*n [0]*n A T = [[] for _ in range(n)] [[] for _ in range(n)] T bi = map(int, input().split( )) map(int, input().split( )) bi v = Q.popleft() Q.popleft() v leaf = False False leaf c = list(map(int, input().split( ))) list(map(int, input().split( ))) c Q = deque() deque() Q leaf = True True leaf j = 0 0 j bi-=1 1 bi ai-=1 1 ai L=[] [] L n = int(input()) int(input()) n ai,bi = map(int, input().split( )) map(int, input().split( )) ai Q =deque(L) deque(L) Q input = sys.stdin.readline sys.stdin.readline input v = Q.popleft() Q.popleft() v m+=c[j] c[j] m D = [0]*n [0]*n D m = 0 0 m P = [-1 for i in range(n)] [-1 for i in range(n)] P j+=1 1 j
import sys
from collections import deque
input = sys.stdin.readline
n = int(input())
T = [[] for _ in range(n)]
for i in range(n-1):
    ai,bi = map(int, input().split( ))
    ai-=1;bi-=1
    T[ai].append(bi)
    T[bi].append(ai)

c = list(map(int, input().split( )))
c.sort()


#最小の数字は葉に書き込む
#次点は葉あるいは最小頂点除いたら葉な点
#r = 0
Q = deque()
Q.append(0)
P = [-1 for i in range(n)]
P[0] = 0
D = [0]*n
L=[]
while Q:
    v = Q.popleft()
    leaf = True
    for u in T[v]:
        if P[u]<0:
            D[v]+=1
            P[u]=v
            leaf = False
            Q.append(u)
    if leaf:
        L.append(v)

Q =deque(L)

j = 0
A = [0]*n
m = 0
while Q:
    v = Q.popleft()
    A[v] = c[j]
    if v:
        m+=c[j]
    j+=1
    D[P[v]]-=1
    if not D[P[v]] and v:
        Q.append(P[v])
print(m)
print(*A)
