

# https://atcoder.jp/contests/m-solutions2019/submissions/5738704


from collections import deque, _heapq

n = int(input())

g = [{"num":0,"neighber":[]} for _ in range(n+1) ]

for _ in range(n-1):

    a, b = map(int, input().split())

    g[a]["neighber"].append(b)
    g[b]["neighber"].append(a)


c = sorted( map(int, input().split()) )

# sum(c[:-1]) 
print( sum(c) - c[-1] )


i = 0
s = [(0,1)]

while i < len(s):
    
    pre, cur = s[i]
    i += 1

    g[cur]["num"] = c.pop()

    for e in g[cur]["neighber"]:
        if e == pre:
            continue

        s.append( (cur,e) )

ans = ''
for i in range(1,n+1):
    ans += str( g[i]["num"] ) + " "



print( ans[:-1] )

 from collections import deque, _heapq n = int(input()) n int(input()) int input() input {"num":0,"neighber":[]} for _ in range(n+1) for _ in range(n+1) _ range(n+1) range n+1 n 1 for _ in range(n+1) {"num":0,"neighber":[]} "num" "num" 0 "neighber" "neighber" [] g = [{"num":0,"neighber":[]} for _ in range(n+1) ] g [{"num":0,"neighber":[]} for _ in range(n+1) ] for _ in range(n-1):

    a, b = map(int, input().split())

    g[a]["neighber"].append(b)
    g[b]["neighber"].append(a) _ 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 g[a]["neighber"].append(b) ["neighber"].append ["neighber"] [a] g a "neighber" append b g[b]["neighber"].append(a) ["neighber"].append ["neighber"] [b] g b "neighber" append a c = sorted( map(int, input().split()) ) c sorted( map(int, input().split()) ) sorted map(int, input().split()) map int input().split() ().split () input split print( sum(c) - c[-1] ) print sum(c) - c[-1] sum(c) sum c c[-1] c -1 i = 0 i 0 s = [(0,1)] s [(0,1)] (0,1) 0 1 while i < len(s):
    
    pre, cur = s[i]
    i += 1

    g[cur]["num"] = c.pop()

    for e in g[cur]["neighber"]:
        if e == pre:
            continue

        s.append( (cur,e) ) i < len(s) i len(s) len s pre, cur = s[i] pre s[i] s i cur i += 1 i 1 g[cur]["num"] = c.pop() g[cur]["num"] [cur] g cur "num" c.pop() c.pop c pop for e in g[cur]["neighber"]:
        if e == pre:
            continue

        s.append( (cur,e) ) e g[cur]["neighber"] [cur] g cur "neighber" if e == pre:
            continue

         e == pre e pre continue s.append( (cur,e) ) s.append s append (cur,e) cur e ans = '' ans '' for i in range(1,n+1):
    ans += str( g[i]["num"] ) + " " i range(1,n+1) range 1 n+1 n 1 ans += str( g[i]["num"] ) + " " ans str( g[i]["num"] ) + " " str( g[i]["num"] ) str g[i]["num"] [i] g i "num" " " print( ans[:-1] ) print ans[:-1] ans :-1 -1 c = sorted( map(int, input().split()) ) sorted( map(int, input().split()) ) c g = [{"num":0,"neighber":[]} for _ in range(n+1) ] [{"num":0,"neighber":[]} for _ in range(n+1) ] g s = [(0,1)] [(0,1)] s cur = s[i] s[i] cur pre, cur = s[i] s[i] pre ans = '' '' ans ans += str( g[i]["num"] ) + " " str( g[i]["num"] ) + " " ans a, b = map(int, input().split()) map(int, input().split()) a i += 1 1 i n = int(input()) int(input()) n i = 0 0 i b = map(int, input().split()) map(int, input().split()) b


# https://atcoder.jp/contests/m-solutions2019/submissions/5738704


from collections import deque, _heapq

n = int(input())

g = [{"num":0,"neighber":[]} for _ in range(n+1) ]

for _ in range(n-1):

    a, b = map(int, input().split())

    g[a]["neighber"].append(b)
    g[b]["neighber"].append(a)


c = sorted( map(int, input().split()) )

# sum(c[:-1]) 
print( sum(c) - c[-1] )


i = 0
s = [(0,1)]

while i < len(s):
    
    pre, cur = s[i]
    i += 1

    g[cur]["num"] = c.pop()

    for e in g[cur]["neighber"]:
        if e == pre:
            continue

        s.append( (cur,e) )

ans = ''
for i in range(1,n+1):
    ans += str( g[i]["num"] ) + " "



print( ans[:-1] )


