from heapq import heappush, heappop
import sys
input = sys.stdin.readline

N = int(input())
G = [[] for _ in range(N+1)]
degree = [0] * (N+1)
for _ in range(N-1):
    a, b = map(int, input().split())
    G[a].append(b)
    G[b].append(a)
    degree[a] += 1
    degree[b] += 1
c = list(map(int, input().split()))
c.sort(reverse=True)

heap = []
for i in range(1, N+1):
    heappush(heap, (degree[i], i))

M = 0
num = [0] * (N+1)
while heap:
    d, i = heappop(heap)
    if num[i] != 0:
        continue
    num[i] = c.pop()
    for j in G[i]:
        if num[j] == 0:
            M += num[i]
            degree[i] -= 1
            degree[j] -= 1
            heappush(heap, (degree[j], j))
print(M)
print(' '.join(map(str, num[1:]))) from heapq import heappush, heappop import sys sys input = sys.stdin.readline input sys.stdin.readline sys.stdin sys stdin readline N = int(input()) N int(input()) int input() input [] for _ in range(N+1) for _ in range(N+1) _ range(N+1) range N+1 N 1 for _ in range(N+1) [] G = [[] for _ in range(N+1)] G [[] for _ in range(N+1)] degree = [0] * (N+1) degree [0] * (N+1) [0] 0 N+1 N 1 for _ in range(N-1):
    a, b = map(int, input().split())
    G[a].append(b)
    G[b].append(a)
    degree[a] += 1
    degree[b] += 1 _ 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].append(b) [a].append [a] G a append b G[b].append(a) [b].append [b] G b append a degree[a] += 1 degree[a] degree a 1 degree[b] += 1 degree[b] degree b 1 c = list(map(int, input().split())) c list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split c.sort(reverse=True) c.sort c sort reverse=True reverse True heap = [] heap [] for i in range(1, N+1):
    heappush(heap, (degree[i], i)) i range(1, N+1) range 1 N+1 N 1 heappush(heap, (degree[i], i)) heappush heap (degree[i], i) degree[i] degree i i M = 0 M 0 num = [0] * (N+1) num [0] * (N+1) [0] 0 N+1 N 1 while heap:
    d, i = heappop(heap)
    if num[i] != 0:
        continue
    num[i] = c.pop()
    for j in G[i]:
        if num[j] == 0:
            M += num[i]
            degree[i] -= 1
            degree[j] -= 1
            heappush(heap, (degree[j], j)) heap d, i = heappop(heap) d heappop(heap) heappop heap i if num[i] != 0:
        continue
     num[i] != 0 num[i] num i 0 continue num[i] = c.pop() num[i] num i c.pop() c.pop c pop for j in G[i]:
        if num[j] == 0:
            M += num[i]
            degree[i] -= 1
            degree[j] -= 1
            heappush(heap, (degree[j], j)) j G[i] G i if num[j] == 0:
            M += num[i]
            degree[i] -= 1
            degree[j] -= 1
            heappush(heap, (degree[j], j)) num[j] == 0 num[j] num j 0 M += num[i] M num[i] num i degree[i] -= 1 degree[i] degree i 1 degree[j] -= 1 degree[j] degree j 1 heappush(heap, (degree[j], j)) heappush heap (degree[j], j) degree[j] degree j j print(M) print M print(' '.join(map(str, num[1:]))) print ' '.join(map(str, num[1:])) ' '.join ' ' join map(str, num[1:]) map str num[1:] num 1: 1 heap = [] [] heap degree = [0] * (N+1) [0] * (N+1) degree b = map(int, input().split()) map(int, input().split()) b N = int(input()) int(input()) N M += num[i] num[i] M num = [0] * (N+1) [0] * (N+1) num G = [[] for _ in range(N+1)] [[] for _ in range(N+1)] G M = 0 0 M i = heappop(heap) heappop(heap) i input = sys.stdin.readline sys.stdin.readline input a, b = map(int, input().split()) map(int, input().split()) a d, i = heappop(heap) heappop(heap) d c = list(map(int, input().split())) list(map(int, input().split())) c
from heapq import heappush, heappop
import sys
input = sys.stdin.readline

N = int(input())
G = [[] for _ in range(N+1)]
degree = [0] * (N+1)
for _ in range(N-1):
    a, b = map(int, input().split())
    G[a].append(b)
    G[b].append(a)
    degree[a] += 1
    degree[b] += 1
c = list(map(int, input().split()))
c.sort(reverse=True)

heap = []
for i in range(1, N+1):
    heappush(heap, (degree[i], i))

M = 0
num = [0] * (N+1)
while heap:
    d, i = heappop(heap)
    if num[i] != 0:
        continue
    num[i] = c.pop()
    for j in G[i]:
        if num[j] == 0:
            M += num[i]
            degree[i] -= 1
            degree[j] -= 1
            heappush(heap, (degree[j], j))
print(M)
print(' '.join(map(str, num[1:])))