from collections import deque


def nearlist(N, LIST):  # 隣接リスト
    NEAR = [set() for _ in range(N)]
    for a, b in LIST:
        NEAR[a - 1].add(b - 1)
        NEAR[b - 1].add(a - 1)
    return NEAR


def bfs(NEAR, S):  # 幅優先探索  # キュー
    ans[S] = c.pop()
    que, frag = deque([S]), set([S])

    while len(que) > 0:
        q = que.popleft()
        for i in NEAR[q]:  # 移動先の候補
            if i in frag:  # 処理済みか否か
                continue
            ans[i] = c.pop()
            que.append(i), frag.add(i)
    return ans


n = int(input())
ab = [list(map(int, input().split())) for _ in range(n - 1)]
c = sorted(map(int, input().split()))

ans = [0] * n
near = nearlist(n, ab)
print(sum(c) - max(c))
print(*bfs(near, 0)) from collections import deque def nearlist(N, LIST):  # 隣接リスト
    NEAR = [set() for _ in range(N)]
    for a, b in LIST:
        NEAR[a - 1].add(b - 1)
        NEAR[b - 1].add(a - 1)
    return NEAR nearlist set() for _ in range(N) for _ in range(N) _ range(N) range N for _ in range(N) set() set NEAR = [set() for _ in range(N)] NEAR [set() for _ in range(N)] for a, b in LIST:
        NEAR[a - 1].add(b - 1)
        NEAR[b - 1].add(a - 1)
     a b LIST NEAR[a - 1].add(b - 1) [a - 1].add [a - 1] NEAR a - 1 a 1 add b - 1 b 1 NEAR[b - 1].add(a - 1) [b - 1].add [b - 1] NEAR b - 1 b 1 add a - 1 a 1 return NEAR NEAR N N LIST LIST def bfs(NEAR, S):  # 幅優先探索  # キュー
    ans[S] = c.pop()
    que, frag = deque([S]), set([S])

    while len(que) > 0:
        q = que.popleft()
        for i in NEAR[q]:  # 移動先の候補
            if i in frag:  # 処理済みか否か
                continue
            ans[i] = c.pop()
            que.append(i), frag.add(i)
    return ans bfs ans[S] = c.pop() ans[S] ans S c.pop() c.pop c pop que, frag = deque([S]), set([S]) que deque([S]) deque [S] S frag set([S]) set [S] S while len(que) > 0:
        q = que.popleft()
        for i in NEAR[q]:  # 移動先の候補
            if i in frag:  # 処理済みか否か
                continue
            ans[i] = c.pop()
            que.append(i), frag.add(i)
     len(que) > 0 len(que) len que 0 q = que.popleft() q que.popleft() que.popleft que popleft for i in NEAR[q]:  # 移動先の候補
            if i in frag:  # 処理済みか否か
                continue
            ans[i] = c.pop()
            que.append(i), frag.add(i)
     i NEAR[q] NEAR q if i in frag:  # 処理済みか否か
                continue
             i in frag i frag continue ans[i] = c.pop() ans[i] ans i c.pop() c.pop c pop que.append(i) que.append que append i frag.add(i) frag.add frag add i return ans ans NEAR NEAR S S n = int(input()) n int(input()) int input() input list(map(int, input().split())) for _ in range(n - 1) for _ in range(n - 1) _ range(n - 1) range n - 1 n 1 for _ in range(n - 1) list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split ab = [list(map(int, input().split())) for _ in range(n - 1)] ab [list(map(int, input().split())) for _ in range(n - 1)] c = sorted(map(int, input().split())) c sorted(map(int, input().split())) sorted map(int, input().split()) map int input().split() ().split () input split ans = [0] * n ans [0] * n [0] 0 n near = nearlist(n, ab) near nearlist(n, ab) nearlist n ab print(sum(c) - max(c)) print sum(c) - max(c) sum(c) sum c max(c) max c print(*bfs(near, 0)) print *bfs(near, 0) bfs(near, 0) bfs near 0 ans = [0] * n [0] * n ans near = nearlist(n, ab) nearlist(n, ab) near def bfs(NEAR, S):  # 幅優先探索  # キュー
    ans[S] = c.pop()
    que, frag = deque([S]), set([S])

    while len(que) > 0:
        q = que.popleft()
        for i in NEAR[q]:  # 移動先の候補
            if i in frag:  # 処理済みか否か
                continue
            ans[i] = c.pop()
            que.append(i), frag.add(i)
    return ans def bfs(NEAR, S):  # 幅優先探索  # キュー
    ans[S] = c.pop()
    que, frag = deque([S]), set([S])

    while len(que) > 0:
        q = que.popleft()
        for i in NEAR[q]:  # 移動先の候補
            if i in frag:  # 処理済みか否か
                continue
            ans[i] = c.pop()
            que.append(i), frag.add(i)
    return ans bfs ab = [list(map(int, input().split())) for _ in range(n - 1)] [list(map(int, input().split())) for _ in range(n - 1)] ab n = int(input()) int(input()) n c = sorted(map(int, input().split())) sorted(map(int, input().split())) c def nearlist(N, LIST):  # 隣接リスト
    NEAR = [set() for _ in range(N)]
    for a, b in LIST:
        NEAR[a - 1].add(b - 1)
        NEAR[b - 1].add(a - 1)
    return NEAR def nearlist(N, LIST):  # 隣接リスト
    NEAR = [set() for _ in range(N)]
    for a, b in LIST:
        NEAR[a - 1].add(b - 1)
        NEAR[b - 1].add(a - 1)
    return NEAR nearlist
from collections import deque


def nearlist(N, LIST):  # 隣接リスト
    NEAR = [set() for _ in range(N)]
    for a, b in LIST:
        NEAR[a - 1].add(b - 1)
        NEAR[b - 1].add(a - 1)
    return NEAR


def bfs(NEAR, S):  # 幅優先探索  # キュー
    ans[S] = c.pop()
    que, frag = deque([S]), set([S])

    while len(que) > 0:
        q = que.popleft()
        for i in NEAR[q]:  # 移動先の候補
            if i in frag:  # 処理済みか否か
                continue
            ans[i] = c.pop()
            que.append(i), frag.add(i)
    return ans


n = int(input())
ab = [list(map(int, input().split())) for _ in range(n - 1)]
c = sorted(map(int, input().split()))

ans = [0] * n
near = nearlist(n, ab)
print(sum(c) - max(c))
print(*bfs(near, 0))
