



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





from collections import deque, _heapq



n = int(eval(input()))



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



for _ in range(n-1):



    a, b = list(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] ))







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







#x = [1,2,3]

#while x:

#    if x : 

#        print(x)

#        x.pop()





n = int(eval(input()))



abn = [list(map(int,input().split())) for _ in range(n-1)]



cn = list( map(int, input().split()) )

cn.sort(reverse = True)





links = [set() for _ in range(n)]



for abi in abn:

    a, b = abi



    a -= 1; b-=1

    links[a].add(b)

    links[b].add(a)







# points of vertices

pv = [0]*n

ans = 0





#リンクを嘗めてsizeが1のもののindexだけ選ぶ

leaves = { i for i, l in enumerate(links) if len(l) == 1 }



# print(*abn)

# print(*cn)

# print(*leaves)

while leaves:

    

    index = leaves.pop()

    cmax = cn.pop()#popなので最小値から利用していく=>大きな値が残っていく

    

    pv[index] = cmax



    if not cn: # 最後のcmax (= 与えられた数列cnの最大値 )を加算する前に抜ける

        break



    ans += cmax

    for ci in links[index]:

        lu = links[ci]

        lu.remove(index)

        if len(lu) == 1:

            leaves.add(ci)



print(ans)

print((*pv))

# https://atcoder.jp/contests/m-solutions2019/submissions/5738704
from collections import deque, _heapq

n = int(eval(input()))
g = [{"num": 0, "neighber": []} for _ in range(n + 1)]
for _ in range(n - 1):
    a, b = list(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]))

# https://atcoder.jp/contests/m-solutions2019/submissions/5730635
# x = [1,2,3]
# while x:
#    if x :
#        print(x)
#        x.pop()
n = int(eval(input()))
abn = [list(map(int, input().split())) for _ in range(n - 1)]
cn = list(map(int, input().split()))
cn.sort(reverse=True)
links = [set() for _ in range(n)]
for abi in abn:
    a, b = abi
    a -= 1
    b -= 1
    links[a].add(b)
    links[b].add(a)
# points of vertices
pv = [0] * n
ans = 0
# リンクを嘗めてsizeが1のもののindexだけ選ぶ
leaves = {i for i, l in enumerate(links) if len(l) == 1}
# print(*abn)
# print(*cn)
# print(*leaves)
while leaves:
    index = leaves.pop()
    cmax = cn.pop()  # popなので最小値から利用していく=>大きな値が残っていく
    pv[index] = cmax
    if not cn:  # 最後のcmax (= 与えられた数列cnの最大値 )を加算する前に抜ける
        break
    ans += cmax
    for ci in links[index]:
        lu = links[ci]
        lu.remove(index)
        if len(lu) == 1:
            leaves.add(ci)
print(ans)
print((*pv))

-# https://atcoder.jp/contests/m-solutions2019/submissions/5738704 -from collections import deque, _heapq - +# https://atcoder.jp/contests/m-solutions2019/submissions/5730635 +# x = [1,2,3] +# while x: +#    if x : +#        print(x) +#        x.pop() -g = [{"num": 0, "neighber": []} for _ in range(n + 1)] -for _ in range(n - 1): -    a, b = list(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])) +abn = [list(map(int, input().split())) for _ in range(n - 1)] +cn = list(map(int, input().split())) +cn.sort(reverse=True) +links = [set() for _ in range(n)] +for abi in abn: +    a, b = abi +    a -= 1 +    b -= 1 +    links[a].add(b) +    links[b].add(a) +# points of vertices +pv = [0] * n +ans = 0 +# リンクを嘗めてsizeが1のもののindexだけ選ぶ +leaves = {i for i, l in enumerate(links) if len(l) == 1} +# print(*abn) +# print(*cn) +# print(*leaves) +while leaves: +    index = leaves.pop() +    cmax = cn.pop()  # popなので最小値から利用していく=>大きな値が残っていく +    pv[index] = cmax +    if not cn:  # 最後のcmax (= 与えられた数列cnの最大値 )を加算する前に抜ける +        break +    ans += cmax +    for ci in links[index]: +        lu = links[ci] +        lu.remove(index) +        if len(lu) == 1: +            leaves.add(ci) +print(ans) +print((*pv))