def main():
    n=int(input())
    ab=[list(map(int,input().split())) for _ in [0]*(n-1)]
    c=sorted(list(map(int,input().split())),reverse=True)
    g=[set() for _ in [0]*n]
    [g[a-1].add(b-1) for a,b in ab]
    [g[b-1].add(a-1) for a,b in ab]
    l=[]
    print(sum(c)-max(c))
    for i in range(n):
        if len(g[i])==1:
            l.append(i)
    ans_l=[0]*n
    while l:
        i=l.pop()
        ans_l[i]=c.pop()
        for j in g[i]:
            g[j].remove(i)
            if len(g[j])==1:
                l.append(j)
        g[i]=set()
    print(*ans_l)
main() def main():
    n=int(input())
    ab=[list(map(int,input().split())) for _ in [0]*(n-1)]
    c=sorted(list(map(int,input().split())),reverse=True)
    g=[set() for _ in [0]*n]
    [g[a-1].add(b-1) for a,b in ab]
    [g[b-1].add(a-1) for a,b in ab]
    l=[]
    print(sum(c)-max(c))
    for i in range(n):
        if len(g[i])==1:
            l.append(i)
    ans_l=[0]*n
    while l:
        i=l.pop()
        ans_l[i]=c.pop()
        for j in g[i]:
            g[j].remove(i)
            if len(g[j])==1:
                l.append(j)
        g[i]=set()
    print(*ans_l) main n=int(input()) n int(input()) int input() input list(map(int,input().split())) for _ in [0]*(n-1) for _ in [0]*(n-1) _ [0]*(n-1) [0] 0 n-1 n 1 for _ in [0]*(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 _ in [0]*(n-1)] ab [list(map(int,input().split())) for _ in [0]*(n-1)] c=sorted(list(map(int,input().split())),reverse=True) c sorted(list(map(int,input().split())),reverse=True) sorted list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split reverse=True reverse True set() for _ in [0]*n for _ in [0]*n _ [0]*n [0] 0 n for _ in [0]*n set() set g=[set() for _ in [0]*n] g [set() for _ in [0]*n] g[a-1].add(b-1) for a,b in ab for a,b in ab a b ab for a,b in ab g[a-1].add(b-1) [a-1].add [a-1] g a-1 a 1 add b-1 b 1 [g[a-1].add(b-1) for a,b in ab] g[b-1].add(a-1) for a,b in ab for a,b in ab a b ab for a,b in ab g[b-1].add(a-1) [b-1].add [b-1] g b-1 b 1 add a-1 a 1 [g[b-1].add(a-1) for a,b in ab] l=[] l [] print(sum(c)-max(c)) print sum(c)-max(c) sum(c) sum c max(c) max c for i in range(n):
        if len(g[i])==1:
            l.append(i)
     i range(n) range n if len(g[i])==1:
            l.append(i)
     len(g[i])==1 len(g[i]) len g[i] g i 1 l.append(i) l.append l append i ans_l=[0]*n ans_l [0]*n [0] 0 n while l:
        i=l.pop()
        ans_l[i]=c.pop()
        for j in g[i]:
            g[j].remove(i)
            if len(g[j])==1:
                l.append(j)
        g[i]=set()
     l i=l.pop() i l.pop() l.pop l pop ans_l[i]=c.pop() ans_l[i] ans_l i c.pop() c.pop c pop for j in g[i]:
            g[j].remove(i)
            if len(g[j])==1:
                l.append(j)
         j g[i] g i g[j].remove(i) [j].remove [j] g j remove i if len(g[j])==1:
                l.append(j)
         len(g[j])==1 len(g[j]) len g[j] g j 1 l.append(j) l.append l append j g[i]=set() g[i] g i set() set print(*ans_l) print *ans_l ans_l main() main def main():
    n=int(input())
    ab=[list(map(int,input().split())) for _ in [0]*(n-1)]
    c=sorted(list(map(int,input().split())),reverse=True)
    g=[set() for _ in [0]*n]
    [g[a-1].add(b-1) for a,b in ab]
    [g[b-1].add(a-1) for a,b in ab]
    l=[]
    print(sum(c)-max(c))
    for i in range(n):
        if len(g[i])==1:
            l.append(i)
    ans_l=[0]*n
    while l:
        i=l.pop()
        ans_l[i]=c.pop()
        for j in g[i]:
            g[j].remove(i)
            if len(g[j])==1:
                l.append(j)
        g[i]=set()
    print(*ans_l) def main():
    n=int(input())
    ab=[list(map(int,input().split())) for _ in [0]*(n-1)]
    c=sorted(list(map(int,input().split())),reverse=True)
    g=[set() for _ in [0]*n]
    [g[a-1].add(b-1) for a,b in ab]
    [g[b-1].add(a-1) for a,b in ab]
    l=[]
    print(sum(c)-max(c))
    for i in range(n):
        if len(g[i])==1:
            l.append(i)
    ans_l=[0]*n
    while l:
        i=l.pop()
        ans_l[i]=c.pop()
        for j in g[i]:
            g[j].remove(i)
            if len(g[j])==1:
                l.append(j)
        g[i]=set()
    print(*ans_l) main
def main():
    n=int(input())
    ab=[list(map(int,input().split())) for _ in [0]*(n-1)]
    c=sorted(list(map(int,input().split())),reverse=True)
    g=[set() for _ in [0]*n]
    [g[a-1].add(b-1) for a,b in ab]
    [g[b-1].add(a-1) for a,b in ab]
    l=[]
    print(sum(c)-max(c))
    for i in range(n):
        if len(g[i])==1:
            l.append(i)
    ans_l=[0]*n
    while l:
        i=l.pop()
        ans_l[i]=c.pop()
        for j in g[i]:
            g[j].remove(i)
            if len(g[j])==1:
                l.append(j)
        g[i]=set()
    print(*ans_l)
main()