import math, string, itertools, fractions, heapq, collections, re, array, bisect, copy, functools, random
import sys
from collections import deque, defaultdict, Counter; from heapq import heappush, heappop
from itertools import permutations, combinations, product, accumulate, groupby
from bisect import bisect_left, bisect_right, insort_left, insort_right
from operator import itemgetter as ig
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20; INF = float("INF"); ans = 0; tmp = 0; tmpli = []; candili = []
eps = 1.0 / 10 ** 10; mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]; ddn = dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)]; ddn9 = ddn + [(0, 0)]
"""for dx, dy in dd:
        nx = j + dx; ny = i + dy
            if 0 <= nx < w and 0 <= ny < h:"""
def wi(): return list(map(int, sys.stdin.readline().split()))
def wip(): return [int(x) - 1 for x in sys.stdin.readline().split()]#WideIntPoint
def ws(): return sys.stdin.readline().split()
def i(): return int(sys.stdin.readline())
def s(): return input()
def hi(n): return [i() for _ in range(n)]
def hs(n): return [s() for _ in range(n)]#HeightString
def mi(n): return [wi() for _ in range(n)]#MatrixInt
def mip(n): return [wip() for _ in range(n)]
def ms(n): return [ws() for _ in range(n)]

n = i()
ab = mip(n - 1)
c = wi()
c.sort(reverse=True)

connect = [[] for _ in range(n)]

for x, y in ab:
    connect[x].append(y)
    connect[y].append(x)

flagli = [0] * n
ansli = [0] * n

queue = deque()
queue.append(0)
cnt = 0
while queue != deque():
    kk = queue.popleft()
    flagli[kk] = 1

    ansli[kk] = c[cnt]
    cnt += 1

    for root in connect[kk]:
        if flagli[root] == 0:
            queue.append(root)

sumans = sum(c) - max(c)
print(sumans)
print(*ansli) import math, string, itertools, fractions, heapq, collections, re, array, bisect, copy, functools, random math string itertools fractions heapq collections re array bisect copy functools random import sys sys from collections import deque, defaultdict, Counter from heapq import heappush, heappop from itertools import permutations, combinations, product, accumulate, groupby from bisect import bisect_left, bisect_right, insort_left, insort_right from operator import itemgetter as ig sys.setrecursionlimit(10 ** 7) sys.setrecursionlimit sys setrecursionlimit 10 ** 7 10 7 inf = 10 ** 20 inf 10 ** 20 10 20 INF = float("INF") INF float("INF") float "INF" ans = 0 ans 0 tmp = 0 tmp 0 tmpli = [] tmpli [] candili = [] candili [] eps = 1.0 / 10 ** 10 eps 1.0 / 10 ** 10 1.0 10 ** 10 10 10 mod = 10 ** 9 + 7 mod 10 ** 9 + 7 10 ** 9 10 9 7 dd = [(-1, 0), (0, 1), (1, 0), (0, -1)] dd [(-1, 0), (0, 1), (1, 0), (0, -1)] (-1, 0) -1 0 (0, 1) 0 1 (1, 0) 1 0 (0, -1) 0 -1 ddn = dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)] ddn dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)] dd [(-1, 1), (1, 1), (1, -1), (-1, -1)] (-1, 1) -1 1 (1, 1) 1 1 (1, -1) 1 -1 (-1, -1) -1 -1 ddn9 = ddn + [(0, 0)] ddn9 ddn + [(0, 0)] ddn [(0, 0)] (0, 0) 0 0 """for dx, dy in dd:
        nx = j + dx; ny = i + dy
            if 0 <= nx < w and 0 <= ny < h:""" def wi(): return list(map(int, sys.stdin.readline().split())) wi int(x) - 1 for x in sys.stdin.readline().split() for x in sys.stdin.readline().split() x sys.stdin.readline().split() sys.stdin.readline().split sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline split for x in sys.stdin.readline().split() int(x) - 1 int(x) int x 1 def wip(): return [int(x) - 1 for x in sys.stdin.readline().split()]#WideIntPoint wip def ws(): return sys.stdin.readline().split() ws def i(): return int(sys.stdin.readline()) i def s(): return input() s i() for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) i() i def hi(n): return [i() for _ in range(n)] hi n n s() for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) s() s def hs(n): return [s() for _ in range(n)]#HeightString hs n n wi() for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) wi() wi def mi(n): return [wi() for _ in range(n)]#MatrixInt mi n n wip() for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) wip() wip def mip(n): return [wip() for _ in range(n)] mip n n ws() for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) ws() ws def ms(n): return [ws() for _ in range(n)] ms n n n = i() n i() i ab = mip(n - 1) ab mip(n - 1) mip n - 1 n 1 c = wi() c wi() wi c.sort(reverse=True) c.sort c sort reverse=True reverse True [] for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) [] connect = [[] for _ in range(n)] connect [[] for _ in range(n)] for x, y in ab:
    connect[x].append(y)
    connect[y].append(x) x y ab connect[x].append(y) [x].append [x] connect x append y connect[y].append(x) [y].append [y] connect y append x flagli = [0] * n flagli [0] * n [0] 0 n ansli = [0] * n ansli [0] * n [0] 0 n queue = deque() queue deque() deque queue.append(0) queue.append queue append 0 cnt = 0 cnt 0 while queue != deque():
    kk = queue.popleft()
    flagli[kk] = 1

    ansli[kk] = c[cnt]
    cnt += 1

    for root in connect[kk]:
        if flagli[root] == 0:
            queue.append(root) queue != deque() queue deque() deque kk = queue.popleft() kk queue.popleft() queue.popleft queue popleft flagli[kk] = 1 flagli[kk] flagli kk 1 ansli[kk] = c[cnt] ansli[kk] ansli kk c[cnt] c cnt cnt += 1 cnt 1 for root in connect[kk]:
        if flagli[root] == 0:
            queue.append(root) root connect[kk] connect kk if flagli[root] == 0:
            queue.append(root) flagli[root] == 0 flagli[root] flagli root 0 queue.append(root) queue.append queue append root sumans = sum(c) - max(c) sumans sum(c) - max(c) sum(c) sum c max(c) max c print(sumans) print sumans print(*ansli) print *ansli ansli def hs(n): return [s() for _ in range(n)]#HeightString def hs(n): return [s() for _ in range(n)]#HeightString hs eps = 1.0 / 10 ** 10 1.0 / 10 ** 10 eps candili = [] [] candili ab = mip(n - 1) mip(n - 1) ab flagli = [0] * n [0] * n flagli def wip(): return [int(x) - 1 for x in sys.stdin.readline().split()]#WideIntPoint def wip(): return [int(x) - 1 for x in sys.stdin.readline().split()]#WideIntPoint wip cnt += 1 1 cnt def wi(): return list(map(int, sys.stdin.readline().split())) def wi(): return list(map(int, sys.stdin.readline().split())) wi def ms(n): return [ws() for _ in range(n)] def ms(n): return [ws() for _ in range(n)] ms cnt = 0 0 cnt def ws(): return sys.stdin.readline().split() def ws(): return sys.stdin.readline().split() ws tmpli = [] [] tmpli ansli = [0] * n [0] * n ansli ddn9 = ddn + [(0, 0)] ddn + [(0, 0)] ddn9 kk = queue.popleft() queue.popleft() kk ans = 0 0 ans tmp = 0 0 tmp def s(): return input() def s(): return input() s def mip(n): return [wip() for _ in range(n)] def mip(n): return [wip() for _ in range(n)] mip ddn = dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)] dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)] ddn def mi(n): return [wi() for _ in range(n)]#MatrixInt def mi(n): return [wi() for _ in range(n)]#MatrixInt mi n = i() i() n dd = [(-1, 0), (0, 1), (1, 0), (0, -1)] [(-1, 0), (0, 1), (1, 0), (0, -1)] dd c = wi() wi() c mod = 10 ** 9 + 7 10 ** 9 + 7 mod def hi(n): return [i() for _ in range(n)] def hi(n): return [i() for _ in range(n)] hi inf = 10 ** 20 10 ** 20 inf queue = deque() deque() queue INF = float("INF") float("INF") INF sumans = sum(c) - max(c) sum(c) - max(c) sumans def i(): return int(sys.stdin.readline()) def i(): return int(sys.stdin.readline()) i connect = [[] for _ in range(n)] [[] for _ in range(n)] connect
import math, string, itertools, fractions, heapq, collections, re, array, bisect, copy, functools, random
import sys
from collections import deque, defaultdict, Counter; from heapq import heappush, heappop
from itertools import permutations, combinations, product, accumulate, groupby
from bisect import bisect_left, bisect_right, insort_left, insort_right
from operator import itemgetter as ig
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20; INF = float("INF"); ans = 0; tmp = 0; tmpli = []; candili = []
eps = 1.0 / 10 ** 10; mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]; ddn = dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)]; ddn9 = ddn + [(0, 0)]
"""for dx, dy in dd:
        nx = j + dx; ny = i + dy
            if 0 <= nx < w and 0 <= ny < h:"""
def wi(): return list(map(int, sys.stdin.readline().split()))
def wip(): return [int(x) - 1 for x in sys.stdin.readline().split()]#WideIntPoint
def ws(): return sys.stdin.readline().split()
def i(): return int(sys.stdin.readline())
def s(): return input()
def hi(n): return [i() for _ in range(n)]
def hs(n): return [s() for _ in range(n)]#HeightString
def mi(n): return [wi() for _ in range(n)]#MatrixInt
def mip(n): return [wip() for _ in range(n)]
def ms(n): return [ws() for _ in range(n)]

n = i()
ab = mip(n - 1)
c = wi()
c.sort(reverse=True)

connect = [[] for _ in range(n)]

for x, y in ab:
    connect[x].append(y)
    connect[y].append(x)

flagli = [0] * n
ansli = [0] * n

queue = deque()
queue.append(0)
cnt = 0
while queue != deque():
    kk = queue.popleft()
    flagli[kk] = 1

    ansli[kk] = c[cnt]
    cnt += 1

    for root in connect[kk]:
        if flagli[root] == 0:
            queue.append(root)

sumans = sum(c) - max(c)
print(sumans)
print(*ansli)