import collections, heapq, sys

n = int(sys.stdin.readline().rstrip())
g = collections.defaultdict(list)
visited = set()
deg = collections.defaultdict(int)
for i in range(n-1):
    a, b = map(int, sys.stdin.readline().rstrip().split(' '))
    deg[a] += 1
    deg[b] += 1
    g[a].append(b)
    g[b].append(a)

pq = []
for i in range(1, n+1):
    heapq.heappush(pq, [deg[i], i])
wts = map(int, sys.stdin.readline().rstrip().split(' '))
wts.sort(reverse=True)
    
vToWt = [-1 for _ in range(n+1)]
sm = 0
while len(pq):
    _, v = heapq.heappop(pq)
    
    if vToWt[v] != -1:
        continue
    vToWt[v] = wts.pop()
    for nei in g[v]:
        if vToWt[nei] != -1:
            continue
        sm += vToWt[v]
        deg[nei] -= 1
        heapq.heappush(pq, [deg[nei], nei])

print(sm)
print(' '.join(map(str, [x for x in vToWt[1:]])))

 import collections, heapq, sys collections heapq sys n = int(sys.stdin.readline().rstrip()) n int(sys.stdin.readline().rstrip()) int sys.stdin.readline().rstrip() sys.stdin.readline().rstrip sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline rstrip g = collections.defaultdict(list) g collections.defaultdict(list) collections.defaultdict collections defaultdict list visited = set() visited set() set deg = collections.defaultdict(int) deg collections.defaultdict(int) collections.defaultdict collections defaultdict int for i in range(n-1):
    a, b = map(int, sys.stdin.readline().rstrip().split(' '))
    deg[a] += 1
    deg[b] += 1
    g[a].append(b)
    g[b].append(a) i range(n-1) range n-1 n 1 a, b = map(int, sys.stdin.readline().rstrip().split(' ')) a map(int, sys.stdin.readline().rstrip().split(' ')) map int sys.stdin.readline().rstrip().split(' ') sys.stdin.readline().rstrip().split sys.stdin.readline().rstrip() sys.stdin.readline().rstrip sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline rstrip split ' ' b deg[a] += 1 deg[a] deg a 1 deg[b] += 1 deg[b] deg b 1 g[a].append(b) [a].append [a] g a append b g[b].append(a) [b].append [b] g b append a pq = [] pq [] for i in range(1, n+1):
    heapq.heappush(pq, [deg[i], i]) i range(1, n+1) range 1 n+1 n 1 heapq.heappush(pq, [deg[i], i]) heapq.heappush heapq heappush pq [deg[i], i] deg[i] deg i i wts = map(int, sys.stdin.readline().rstrip().split(' ')) wts map(int, sys.stdin.readline().rstrip().split(' ')) map int sys.stdin.readline().rstrip().split(' ') sys.stdin.readline().rstrip().split sys.stdin.readline().rstrip() sys.stdin.readline().rstrip sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline rstrip split ' ' wts.sort(reverse=True) wts.sort wts sort reverse=True reverse True -1 for _ in range(n+1) for _ in range(n+1) _ range(n+1) range n+1 n 1 for _ in range(n+1) -1 vToWt = [-1 for _ in range(n+1)] vToWt [-1 for _ in range(n+1)] sm = 0 sm 0 while len(pq):
    _, v = heapq.heappop(pq)
    
    if vToWt[v] != -1:
        continue
    vToWt[v] = wts.pop()
    for nei in g[v]:
        if vToWt[nei] != -1:
            continue
        sm += vToWt[v]
        deg[nei] -= 1
        heapq.heappush(pq, [deg[nei], nei]) len(pq) len pq _, v = heapq.heappop(pq) _ heapq.heappop(pq) heapq.heappop heapq heappop pq v if vToWt[v] != -1:
        continue
     vToWt[v] != -1 vToWt[v] vToWt v -1 continue vToWt[v] = wts.pop() vToWt[v] vToWt v wts.pop() wts.pop wts pop for nei in g[v]:
        if vToWt[nei] != -1:
            continue
        sm += vToWt[v]
        deg[nei] -= 1
        heapq.heappush(pq, [deg[nei], nei]) nei g[v] g v if vToWt[nei] != -1:
            continue
         vToWt[nei] != -1 vToWt[nei] vToWt nei -1 continue sm += vToWt[v] sm vToWt[v] vToWt v deg[nei] -= 1 deg[nei] deg nei 1 heapq.heappush(pq, [deg[nei], nei]) heapq.heappush heapq heappush pq [deg[nei], nei] deg[nei] deg nei nei print(sm) print sm x for x in vToWt[1:] for x in vToWt[1:] x vToWt[1:] vToWt 1: 1 for x in vToWt[1:] x print(' '.join(map(str, [x for x in vToWt[1:]]))) print ' '.join(map(str, [x for x in vToWt[1:]])) ' '.join ' ' join map(str, [x for x in vToWt[1:]]) map str [x for x in vToWt[1:]] _, v = heapq.heappop(pq) heapq.heappop(pq) _ pq = [] [] pq vToWt = [-1 for _ in range(n+1)] [-1 for _ in range(n+1)] vToWt a, b = map(int, sys.stdin.readline().rstrip().split(' ')) map(int, sys.stdin.readline().rstrip().split(' ')) a deg = collections.defaultdict(int) collections.defaultdict(int) deg n = int(sys.stdin.readline().rstrip()) int(sys.stdin.readline().rstrip()) n b = map(int, sys.stdin.readline().rstrip().split(' ')) map(int, sys.stdin.readline().rstrip().split(' ')) b g = collections.defaultdict(list) collections.defaultdict(list) g wts = map(int, sys.stdin.readline().rstrip().split(' ')) map(int, sys.stdin.readline().rstrip().split(' ')) wts sm = 0 0 sm v = heapq.heappop(pq) heapq.heappop(pq) v visited = set() set() visited sm += vToWt[v] vToWt[v] sm
import collections, heapq, sys

n = int(sys.stdin.readline().rstrip())
g = collections.defaultdict(list)
visited = set()
deg = collections.defaultdict(int)
for i in range(n-1):
    a, b = map(int, sys.stdin.readline().rstrip().split(' '))
    deg[a] += 1
    deg[b] += 1
    g[a].append(b)
    g[b].append(a)

pq = []
for i in range(1, n+1):
    heapq.heappush(pq, [deg[i], i])
wts = map(int, sys.stdin.readline().rstrip().split(' '))
wts.sort(reverse=True)
    
vToWt = [-1 for _ in range(n+1)]
sm = 0
while len(pq):
    _, v = heapq.heappop(pq)
    
    if vToWt[v] != -1:
        continue
    vToWt[v] = wts.pop()
    for nei in g[v]:
        if vToWt[nei] != -1:
            continue
        sm += vToWt[v]
        deg[nei] -= 1
        heapq.heappush(pq, [deg[nei], nei])

print(sm)
print(' '.join(map(str, [x for x in vToWt[1:]])))


