from collections import defaultdict
import sys
sys.setrecursionlimit(20000000)
connection = defaultdict(lambda: [])

n = int(input())

ab = []
for i in range(n-1):
    a, b = map(int, input().split())
    connection[a].append(b)
    connection[b].append(a)
    ab.append((a, b))

c = list(map(int, input().split()))
c.sort()

ans = [0] * (n+1)

checked_list = []
def dfs(num):
    checked_list.append(num)
    ans[num] = c.pop()
    for next_num in connection[num]:
        if next_num not in checked_list:
            dfs(next_num)


l = []
for i in range(1, n+1):
    l.append([i, len(connection[i])])
l.sort(key=lambda x: x[1], reverse=True)
dfs(1)

score = 0

for a, b in ab:
    score += min(ans[a], ans[b])

print(score)
print(" ".join(map(str, ans[1:]))) from collections import defaultdict import sys sys sys.setrecursionlimit(20000000) sys.setrecursionlimit sys setrecursionlimit 20000000 connection = defaultdict(lambda: []) connection defaultdict(lambda: []) defaultdict lambda: [] [] n = int(input()) n int(input()) int input() input ab = [] ab [] for i in range(n-1):
    a, b = map(int, input().split())
    connection[a].append(b)
    connection[b].append(a)
    ab.append((a, b)) i 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 connection[a].append(b) [a].append [a] connection a append b connection[b].append(a) [b].append [b] connection b append a ab.append((a, b)) ab.append ab append (a, b) a b 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() c.sort c sort ans = [0] * (n+1) ans [0] * (n+1) [0] 0 n+1 n 1 checked_list = [] checked_list [] def dfs(num):
    checked_list.append(num)
    ans[num] = c.pop()
    for next_num in connection[num]:
        if next_num not in checked_list:
            dfs(next_num) dfs checked_list.append(num) checked_list.append checked_list append num ans[num] = c.pop() ans[num] ans num c.pop() c.pop c pop for next_num in connection[num]:
        if next_num not in checked_list:
            dfs(next_num) next_num connection[num] connection num if next_num not in checked_list:
            dfs(next_num) next_num not in checked_list next_num checked_list dfs(next_num) dfs next_num num num l = [] l [] for i in range(1, n+1):
    l.append([i, len(connection[i])]) i range(1, n+1) range 1 n+1 n 1 l.append([i, len(connection[i])]) l.append l append [i, len(connection[i])] i len(connection[i]) len connection[i] connection i l.sort(key=lambda x: x[1], reverse=True) l.sort l sort key=lambda x: x[1] key lambda x: x[1] x[1] x 1 x reverse=True reverse True dfs(1) dfs 1 score = 0 score 0 for a, b in ab:
    score += min(ans[a], ans[b]) a b ab score += min(ans[a], ans[b]) score min(ans[a], ans[b]) min ans[a] ans a ans[b] ans b print(score) print score print(" ".join(map(str, ans[1:]))) print " ".join(map(str, ans[1:])) " ".join " " join map(str, ans[1:]) map str ans[1:] ans 1: 1 def dfs(num):
    checked_list.append(num)
    ans[num] = c.pop()
    for next_num in connection[num]:
        if next_num not in checked_list:
            dfs(next_num) def dfs(num):
    checked_list.append(num)
    ans[num] = c.pop()
    for next_num in connection[num]:
        if next_num not in checked_list:
            dfs(next_num) dfs a, b = map(int, input().split()) map(int, input().split()) a checked_list = [] [] checked_list connection = defaultdict(lambda: []) defaultdict(lambda: []) connection l = [] [] l ab = [] [] ab score += min(ans[a], ans[b]) min(ans[a], ans[b]) score score = 0 0 score n = int(input()) int(input()) n b = map(int, input().split()) map(int, input().split()) b c = list(map(int, input().split())) list(map(int, input().split())) c ans = [0] * (n+1) [0] * (n+1) ans
from collections import defaultdict
import sys
sys.setrecursionlimit(20000000)
connection = defaultdict(lambda: [])

n = int(input())

ab = []
for i in range(n-1):
    a, b = map(int, input().split())
    connection[a].append(b)
    connection[b].append(a)
    ab.append((a, b))

c = list(map(int, input().split()))
c.sort()

ans = [0] * (n+1)

checked_list = []
def dfs(num):
    checked_list.append(num)
    ans[num] = c.pop()
    for next_num in connection[num]:
        if next_num not in checked_list:
            dfs(next_num)


l = []
for i in range(1, n+1):
    l.append([i, len(connection[i])])
l.sort(key=lambda x: x[1], reverse=True)
dfs(1)

score = 0

for a, b in ab:
    score += min(ans[a], ans[b])

print(score)
print(" ".join(map(str, ans[1:])))
