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



#x = [1,2,3]
#while x:
#    if x : 
#        print(x)
#        x.pop()


n = int(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) n = int(input()) n int(input()) int input() input list(map(int,input().split())) for _ in range(n-1) for _ in range(n-1) _ range(n-1) range n-1 n 1 for _ in range(n-1) list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split abn = [list(map(int,input().split())) for _ in range(n-1)] abn [list(map(int,input().split())) for _ in range(n-1)] cn = list( map(int, input().split()) ) cn list( map(int, input().split()) ) list map(int, input().split()) map int input().split() ().split () input split cn.sort(reverse = True) cn.sort cn sort reverse = True reverse True set() for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) set() set links = [set() for _ in range(n)] 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 abi abn a, b = abi a abi b a -= 1 a 1 b-=1 b 1 links[a].add(b) [a].add [a] links a add b links[b].add(a) [b].add [b] links b add a pv = [0]*n pv [0]*n [0] 0 n ans = 0 ans 0 i for i, l in enumerate(links) if len(l) == 1 for i, l in enumerate(links) if len(l) == 1 i l enumerate(links) enumerate links len(l) == 1 len(l) len l 1 if len(l) == 1 i leaves = { i for i, l in enumerate(links) if len(l) == 1 } 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) leaves index = leaves.pop() index leaves.pop() leaves.pop leaves pop cmax = cn.pop() cmax cn.pop() cn.pop cn pop pv[index] = cmax pv[index] pv index cmax if not cn: # 最後のcmax (= 与えられた数列cnの最大値 )を加算する前に抜ける
        break

     not cn cn break ans += cmax ans cmax for ci in links[index]:
        lu = links[ci]
        lu.remove(index)
        if len(lu) == 1:
            leaves.add(ci) ci links[index] links index lu = links[ci] lu links[ci] links ci lu.remove(index) lu.remove lu remove index if len(lu) == 1:
            leaves.add(ci) len(lu) == 1 len(lu) len lu 1 leaves.add(ci) leaves.add leaves add ci print(ans) print ans print(*pv) print *pv pv leaves = { i for i, l in enumerate(links) if len(l) == 1 } leaves abn = [list(map(int,input().split())) for _ in range(n-1)] [list(map(int,input().split())) for _ in range(n-1)] abn lu = links[ci] links[ci] lu ans = 0 0 ans ans += cmax cmax ans b-=1 1 b a -= 1 1 a links = [set() for _ in range(n)] [set() for _ in range(n)] links index = leaves.pop() leaves.pop() index cn = list( map(int, input().split()) ) list( map(int, input().split()) ) cn a, b = abi abi a b = abi abi b n = int(input()) int(input()) n pv = [0]*n [0]*n pv cmax = cn.pop() cn.pop() cmax

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



#x = [1,2,3]
#while x:
#    if x : 
#        print(x)
#        x.pop()


n = int(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)
