import sys;sys.setrecursionlimit(10**6)
from collections import*
n=int(input())
d=defaultdict(list)

start=float("inf")
for i in range(n-1):
    a,b=map(int,input().split())
    d[a].append(b)
    d[b].append(a)
    start=min(a,start)
p=sorted(map(int,input().split()))

def DFS(point,TFlist):
    global ans
    global score
    if all(TFlist):
        return 1
    for i in d[point]:
        if TFlist[i-1]:
            continue
        TFlist[i-1]=True
        tmp=p.pop()
        ans[i-1]=tmp
        score.append(tmp)
        #print(score)
        DFS(i,TFlist)
        TFlist[i-1]=False
    return score,ans

TFlist=[False]*(n)
TFlist[start-1]=True
ans=[0 for i in range(n)]
score=[]
ans[start-1]=p.pop()
s,t=DFS(start,TFlist)
#print(d)
print(sum(s))
print(*t) import sys sys sys.setrecursionlimit(10**6) sys.setrecursionlimit sys setrecursionlimit 10**6 10 6 from collections import* n=int(input()) n int(input()) int input() input d=defaultdict(list) d defaultdict(list) defaultdict list start=float("inf") start float("inf") float "inf" for i in range(n-1):
    a,b=map(int,input().split())
    d[a].append(b)
    d[b].append(a)
    start=min(a,start) i 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 d[a].append(b) [a].append [a] d a append b d[b].append(a) [b].append [b] d b append a start=min(a,start) start min(a,start) min a start p=sorted(map(int,input().split())) p sorted(map(int,input().split())) sorted map(int,input().split()) map int input().split() ().split () input split def DFS(point,TFlist):
    global ans
    global score
    if all(TFlist):
        return 1
    for i in d[point]:
        if TFlist[i-1]:
            continue
        TFlist[i-1]=True
        tmp=p.pop()
        ans[i-1]=tmp
        score.append(tmp)
        #print(score)
        DFS(i,TFlist)
        TFlist[i-1]=False
    return score,ans DFS global ans global score if all(TFlist):
        return 1
     all(TFlist) all TFlist return 1 1 for i in d[point]:
        if TFlist[i-1]:
            continue
        TFlist[i-1]=True
        tmp=p.pop()
        ans[i-1]=tmp
        score.append(tmp)
        #print(score)
        DFS(i,TFlist)
        TFlist[i-1]=False
     i d[point] d point if TFlist[i-1]:
            continue
         TFlist[i-1] TFlist i-1 i 1 continue TFlist[i-1]=True TFlist[i-1] TFlist i-1 i 1 True tmp=p.pop() tmp p.pop() p.pop p pop ans[i-1]=tmp ans[i-1] ans i-1 i 1 tmp score.append(tmp) score.append score append tmp DFS(i,TFlist) DFS i TFlist TFlist[i-1]=False TFlist[i-1] TFlist i-1 i 1 False return score,ans return score,ans score ans point point TFlist TFlist TFlist=[False]*(n) TFlist [False]*(n) [False] False n TFlist[start-1]=True TFlist[start-1] TFlist start-1 start 1 True 0 for i in range(n) for i in range(n) i range(n) range n for i in range(n) 0 ans=[0 for i in range(n)] ans [0 for i in range(n)] score=[] score [] ans[start-1]=p.pop() ans[start-1] ans start-1 start 1 p.pop() p.pop p pop s,t=DFS(start,TFlist) s DFS(start,TFlist) DFS start TFlist t print(sum(s)) print sum(s) sum s print(*t) print *t t n=int(input()) int(input()) n def DFS(point,TFlist):
    global ans
    global score
    if all(TFlist):
        return 1
    for i in d[point]:
        if TFlist[i-1]:
            continue
        TFlist[i-1]=True
        tmp=p.pop()
        ans[i-1]=tmp
        score.append(tmp)
        #print(score)
        DFS(i,TFlist)
        TFlist[i-1]=False
    return score,ans def DFS(point,TFlist):
    global ans
    global score
    if all(TFlist):
        return 1
    for i in d[point]:
        if TFlist[i-1]:
            continue
        TFlist[i-1]=True
        tmp=p.pop()
        ans[i-1]=tmp
        score.append(tmp)
        #print(score)
        DFS(i,TFlist)
        TFlist[i-1]=False
    return score,ans DFS start=min(a,start) min(a,start) start t=DFS(start,TFlist) DFS(start,TFlist) t a,b=map(int,input().split()) map(int,input().split()) a d=defaultdict(list) defaultdict(list) d ans=[0 for i in range(n)] [0 for i in range(n)] ans s,t=DFS(start,TFlist) DFS(start,TFlist) s TFlist=[False]*(n) [False]*(n) TFlist start=float("inf") float("inf") start b=map(int,input().split()) map(int,input().split()) b score=[] [] score p=sorted(map(int,input().split())) sorted(map(int,input().split())) p
import sys;sys.setrecursionlimit(10**6)
from collections import*
n=int(input())
d=defaultdict(list)

start=float("inf")
for i in range(n-1):
    a,b=map(int,input().split())
    d[a].append(b)
    d[b].append(a)
    start=min(a,start)
p=sorted(map(int,input().split()))

def DFS(point,TFlist):
    global ans
    global score
    if all(TFlist):
        return 1
    for i in d[point]:
        if TFlist[i-1]:
            continue
        TFlist[i-1]=True
        tmp=p.pop()
        ans[i-1]=tmp
        score.append(tmp)
        #print(score)
        DFS(i,TFlist)
        TFlist[i-1]=False
    return score,ans

TFlist=[False]*(n)
TFlist[start-1]=True
ans=[0 for i in range(n)]
score=[]
ans[start-1]=p.pop()
s,t=DFS(start,TFlist)
#print(d)
print(sum(s))
print(*t)