import sys

N=int(input())
ab=[list(map(int,input().split())) for i in range(N-1)]
c=list(map(int,input().split()))
c.sort(reverse=True)
data=[[] for i in range(N+1)]
for a,b in ab:
    data[a].append(b)
    data[b].append(a)
    
flag=[0]*(N+1)
ans=[0]*(N+1)
flag[1]=1
k=0
que=[1]
while que:
    for u in que:
        ans[u]=c[k]
        k+=1
    if k>N:
        break
    h=[]
    for u in que:
        for v in data[u]:
            if flag[v]==0:
                flag[v]=1
                h.append(v)
    que=h
print(sum(ans[2:]))
print(*ans[1:]) import sys sys N=int(input()) N int(input()) int input() input list(map(int,input().split())) for i in range(N-1) for i in range(N-1) i range(N-1) range N-1 N 1 for i in range(N-1) list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split ab=[list(map(int,input().split())) for i in range(N-1)] ab [list(map(int,input().split())) for i in range(N-1)] 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(reverse=True) c.sort c sort reverse=True reverse True [] for i in range(N+1) for i in range(N+1) i range(N+1) range N+1 N 1 for i in range(N+1) [] data=[[] for i in range(N+1)] data [[] for i in range(N+1)] for a,b in ab:
    data[a].append(b)
    data[b].append(a)
     a b ab data[a].append(b) [a].append [a] data a append b data[b].append(a) [b].append [b] data b append a flag=[0]*(N+1) flag [0]*(N+1) [0] 0 N+1 N 1 ans=[0]*(N+1) ans [0]*(N+1) [0] 0 N+1 N 1 flag[1]=1 flag[1] flag 1 1 k=0 k 0 que=[1] que [1] 1 while que:
    for u in que:
        ans[u]=c[k]
        k+=1
    if k>N:
        break
    h=[]
    for u in que:
        for v in data[u]:
            if flag[v]==0:
                flag[v]=1
                h.append(v)
    que=h que for u in que:
        ans[u]=c[k]
        k+=1
     u que ans[u]=c[k] ans[u] ans u c[k] c k k+=1 k 1 if k>N:
        break
     k>N k N break h=[] h [] for u in que:
        for v in data[u]:
            if flag[v]==0:
                flag[v]=1
                h.append(v)
     u que for v in data[u]:
            if flag[v]==0:
                flag[v]=1
                h.append(v)
     v data[u] data u if flag[v]==0:
                flag[v]=1
                h.append(v)
     flag[v]==0 flag[v] flag v 0 flag[v]=1 flag[v] flag v 1 h.append(v) h.append h append v que=h que h print(sum(ans[2:])) print sum(ans[2:]) sum ans[2:] ans 2: 2 print(*ans[1:]) print *ans[1:] ans[1:] ans 1: 1 ans=[0]*(N+1) [0]*(N+1) ans ab=[list(map(int,input().split())) for i in range(N-1)] [list(map(int,input().split())) for i in range(N-1)] ab h=[] [] h k+=1 1 k k=0 0 k N=int(input()) int(input()) N c=list(map(int,input().split())) list(map(int,input().split())) c que=h h que flag=[0]*(N+1) [0]*(N+1) flag data=[[] for i in range(N+1)] [[] for i in range(N+1)] data que=[1] [1] que
import sys

N=int(input())
ab=[list(map(int,input().split())) for i in range(N-1)]
c=list(map(int,input().split()))
c.sort(reverse=True)
data=[[] for i in range(N+1)]
for a,b in ab:
    data[a].append(b)
    data[b].append(a)
    
flag=[0]*(N+1)
ans=[0]*(N+1)
flag[1]=1
k=0
que=[1]
while que:
    for u in que:
        ans[u]=c[k]
        k+=1
    if k>N:
        break
    h=[]
    for u in que:
        for v in data[u]:
            if flag[v]==0:
                flag[v]=1
                h.append(v)
    que=h
print(sum(ans[2:]))
print(*ans[1:])