import sys
def input():
    return sys.stdin.readline()[:-1]
N=int(input())
s=[[]for i in range(N)]
for i in range(N-1):
    a,b=map(lambda x: int(x)-1,input().split())
    s[a].append(b)
    s[b].append(a)
sys.setrecursionlimit(20000)
l=[None]*N
def hu(n):
    for i in s[n]:
        if l[i]==None:
            l[i]=c.pop()
            hu(i)
c=list(map(int,input().split()))
c.sort()
print(sum(c[:-1]))
l[0]=c.pop()
hu(0)
print(*l) import sys sys def input():
    return sys.stdin.readline()[:-1] input return sys.stdin.readline()[:-1] sys.stdin.readline()[:-1] sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline :-1 -1 N=int(input()) N int(input()) int input() input []for i in range(N) for i in range(N) i range(N) range N for i in range(N) [] s=[[]for i in range(N)] s [[]for i in range(N)] for i in range(N-1):
    a,b=map(lambda x: int(x)-1,input().split())
    s[a].append(b)
    s[b].append(a) i range(N-1) range N-1 N 1 a,b=map(lambda x: int(x)-1,input().split()) a map(lambda x: int(x)-1,input().split()) map lambda x: int(x)-1 int(x)-1 int(x) int x 1 x input().split() ().split () input split b s[a].append(b) [a].append [a] s a append b s[b].append(a) [b].append [b] s b append a sys.setrecursionlimit(20000) sys.setrecursionlimit sys setrecursionlimit 20000 l=[None]*N l [None]*N [None] None N def hu(n):
    for i in s[n]:
        if l[i]==None:
            l[i]=c.pop()
            hu(i) hu for i in s[n]:
        if l[i]==None:
            l[i]=c.pop()
            hu(i) i s[n] s n if l[i]==None:
            l[i]=c.pop()
            hu(i) l[i]==None l[i] l i None l[i]=c.pop() l[i] l i c.pop() c.pop c pop hu(i) hu i n n 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 print(sum(c[:-1])) print sum(c[:-1]) sum c[:-1] c :-1 -1 l[0]=c.pop() l[0] l 0 c.pop() c.pop c pop hu(0) hu 0 print(*l) print *l l c=list(map(int,input().split())) list(map(int,input().split())) c def input():
    return sys.stdin.readline()[:-1] def input():
    return sys.stdin.readline()[:-1] input l=[None]*N [None]*N l N=int(input()) int(input()) N b=map(lambda x: int(x)-1,input().split()) map(lambda x: int(x)-1,input().split()) b a,b=map(lambda x: int(x)-1,input().split()) map(lambda x: int(x)-1,input().split()) a def hu(n):
    for i in s[n]:
        if l[i]==None:
            l[i]=c.pop()
            hu(i) def hu(n):
    for i in s[n]:
        if l[i]==None:
            l[i]=c.pop()
            hu(i) hu s=[[]for i in range(N)] [[]for i in range(N)] s
import sys
def input():
    return sys.stdin.readline()[:-1]
N=int(input())
s=[[]for i in range(N)]
for i in range(N-1):
    a,b=map(lambda x: int(x)-1,input().split())
    s[a].append(b)
    s[b].append(a)
sys.setrecursionlimit(20000)
l=[None]*N
def hu(n):
    for i in s[n]:
        if l[i]==None:
            l[i]=c.pop()
            hu(i)
c=list(map(int,input().split()))
c.sort()
print(sum(c[:-1]))
l[0]=c.pop()
hu(0)
print(*l)