from collections import deque, defaultdict

N = int(input())
ab = []
for i in range(N - 1):
    ab.append(tuple(map(int, input().split())))

c_list = list(map(int, input().split()))
c_list.sort()
c_que = deque(c_list)

cnt = defaultdict(int)
outs = defaultdict(list)
for a, b in ab:
    cnt[a - 1] += 1
    cnt[b - 1] += 1
    outs[a - 1].append(b - 1)
    outs[b - 1].append(a - 1)

top = 0
tar = defaultdict(lambda: 0)

import sys

sys.setrecursionlimit(10 ** 9)
visit = [False] * N


def dfs(start):
    tmp = []
    visit[start] = True
    for v2 in outs[start]:
        if visit[v2]:
            continue
        q = c_que.pop()
        tar[v2] = q
        tmp.append(v2)
    for v2 in tmp:
        if visit[v2]:
            continue
        dfs(v2)


tar[top] = c_que.pop()
dfs(top)
ans = 0
for a, b in ab:
    a -= 1
    b -= 1
    ans += min(tar[a], tar[b])
print(ans)
for i in range(N):
    print(tar[i], end=" ") from collections import deque, defaultdict N = int(input()) N int(input()) int input() input ab = [] ab [] for i in range(N - 1):
    ab.append(tuple(map(int, input().split()))) i range(N - 1) range N - 1 N 1 ab.append(tuple(map(int, input().split()))) ab.append ab append tuple(map(int, input().split())) tuple map(int, input().split()) map int input().split() ().split () input split c_list = list(map(int, input().split())) c_list list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split c_list.sort() c_list.sort c_list sort c_que = deque(c_list) c_que deque(c_list) deque c_list cnt = defaultdict(int) cnt defaultdict(int) defaultdict int outs = defaultdict(list) outs defaultdict(list) defaultdict list for a, b in ab:
    cnt[a - 1] += 1
    cnt[b - 1] += 1
    outs[a - 1].append(b - 1)
    outs[b - 1].append(a - 1) a b ab cnt[a - 1] += 1 cnt[a - 1] cnt a - 1 a 1 1 cnt[b - 1] += 1 cnt[b - 1] cnt b - 1 b 1 1 outs[a - 1].append(b - 1) [a - 1].append [a - 1] outs a - 1 a 1 append b - 1 b 1 outs[b - 1].append(a - 1) [b - 1].append [b - 1] outs b - 1 b 1 append a - 1 a 1 top = 0 top 0 tar = defaultdict(lambda: 0) tar defaultdict(lambda: 0) defaultdict lambda: 0 0 import sys sys sys.setrecursionlimit(10 ** 9) sys.setrecursionlimit sys setrecursionlimit 10 ** 9 10 9 visit = [False] * N visit [False] * N [False] False N def dfs(start):
    tmp = []
    visit[start] = True
    for v2 in outs[start]:
        if visit[v2]:
            continue
        q = c_que.pop()
        tar[v2] = q
        tmp.append(v2)
    for v2 in tmp:
        if visit[v2]:
            continue
        dfs(v2) dfs tmp = [] tmp [] visit[start] = True visit[start] visit start True for v2 in outs[start]:
        if visit[v2]:
            continue
        q = c_que.pop()
        tar[v2] = q
        tmp.append(v2)
     v2 outs[start] outs start if visit[v2]:
            continue
         visit[v2] visit v2 continue q = c_que.pop() q c_que.pop() c_que.pop c_que pop tar[v2] = q tar[v2] tar v2 q tmp.append(v2) tmp.append tmp append v2 for v2 in tmp:
        if visit[v2]:
            continue
        dfs(v2) v2 tmp if visit[v2]:
            continue
         visit[v2] visit v2 continue dfs(v2) dfs v2 start start tar[top] = c_que.pop() tar[top] tar top c_que.pop() c_que.pop c_que pop dfs(top) dfs top ans = 0 ans 0 for a, b in ab:
    a -= 1
    b -= 1
    ans += min(tar[a], tar[b]) a b ab a -= 1 a 1 b -= 1 b 1 ans += min(tar[a], tar[b]) ans min(tar[a], tar[b]) min tar[a] tar a tar[b] tar b print(ans) print ans for i in range(N):
    print(tar[i], end=" ") i range(N) range N print(tar[i], end=" ") print tar[i] tar i end=" " end " " N = int(input()) int(input()) N ans = 0 0 ans a -= 1 1 a c_list = list(map(int, input().split())) list(map(int, input().split())) c_list outs = defaultdict(list) defaultdict(list) outs ab = [] [] ab ans += min(tar[a], tar[b]) min(tar[a], tar[b]) ans tar = defaultdict(lambda: 0) defaultdict(lambda: 0) tar cnt = defaultdict(int) defaultdict(int) cnt top = 0 0 top b -= 1 1 b c_que = deque(c_list) deque(c_list) c_que def dfs(start):
    tmp = []
    visit[start] = True
    for v2 in outs[start]:
        if visit[v2]:
            continue
        q = c_que.pop()
        tar[v2] = q
        tmp.append(v2)
    for v2 in tmp:
        if visit[v2]:
            continue
        dfs(v2) def dfs(start):
    tmp = []
    visit[start] = True
    for v2 in outs[start]:
        if visit[v2]:
            continue
        q = c_que.pop()
        tar[v2] = q
        tmp.append(v2)
    for v2 in tmp:
        if visit[v2]:
            continue
        dfs(v2) dfs visit = [False] * N [False] * N visit
from collections import deque, defaultdict

N = int(input())
ab = []
for i in range(N - 1):
    ab.append(tuple(map(int, input().split())))

c_list = list(map(int, input().split()))
c_list.sort()
c_que = deque(c_list)

cnt = defaultdict(int)
outs = defaultdict(list)
for a, b in ab:
    cnt[a - 1] += 1
    cnt[b - 1] += 1
    outs[a - 1].append(b - 1)
    outs[b - 1].append(a - 1)

top = 0
tar = defaultdict(lambda: 0)

import sys

sys.setrecursionlimit(10 ** 9)
visit = [False] * N


def dfs(start):
    tmp = []
    visit[start] = True
    for v2 in outs[start]:
        if visit[v2]:
            continue
        q = c_que.pop()
        tar[v2] = q
        tmp.append(v2)
    for v2 in tmp:
        if visit[v2]:
            continue
        dfs(v2)


tar[top] = c_que.pop()
dfs(top)
ans = 0
for a, b in ab:
    a -= 1
    b -= 1
    ans += min(tar[a], tar[b])
print(ans)
for i in range(N):
    print(tar[i], end=" ")
