import heapq
import sys
from collections import deque


def input():
    return sys.stdin.readline().strip()


sys.setrecursionlimit(20000000)

MOD = 10 ** 9 + 7
INF = float("inf")


def main():
    N = int(input())
    tree = [[] for _ in range(N)]
    edge = deque([])

    for _ in range(N - 1):
        a, b = map(int, input().split())
        tree[a - 1].append(b - 1)
        tree[b - 1].append(a - 1)
        edge.append((a - 1, b - 1))

    C = list(map(int, input().split()))
    C.sort()
    D = []
    for c in C:
        heapq.heappush(D, -c)

    answer = [0] * N
    a, b = edge.popleft()
    c = heapq.heappop(D) * (-1)
    d = heapq.heappop(D) * (-1)
    if len(tree[a]) == 0:
        answer[a] = c
        answer[b] = d
    else:
        answer[a] = d
        answer[b] = c

    new_node = deque([])
    for n in tree[a]:
        new_node.append(n)
    for n in tree[b]:
        new_node.append(n)

    while new_node:
        s = new_node.popleft()
        if answer[s] == 0:
            answer[s] = heapq.heappop(D) * (-1)
        for n in tree[s]:
            if answer[n] == 0:
                new_node.append(n)

    print(sum(C[:-1]))
    print(*answer)


if __name__ == "__main__":
    main() import heapq heapq import sys sys from collections import deque def input():
    return sys.stdin.readline().strip() input return sys.stdin.readline().strip() sys.stdin.readline().strip() sys.stdin.readline().strip sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline strip sys.setrecursionlimit(20000000) sys.setrecursionlimit sys setrecursionlimit 20000000 MOD = 10 ** 9 + 7 MOD 10 ** 9 + 7 10 ** 9 10 9 7 INF = float("inf") INF float("inf") float "inf" def main():
    N = int(input())
    tree = [[] for _ in range(N)]
    edge = deque([])

    for _ in range(N - 1):
        a, b = map(int, input().split())
        tree[a - 1].append(b - 1)
        tree[b - 1].append(a - 1)
        edge.append((a - 1, b - 1))

    C = list(map(int, input().split()))
    C.sort()
    D = []
    for c in C:
        heapq.heappush(D, -c)

    answer = [0] * N
    a, b = edge.popleft()
    c = heapq.heappop(D) * (-1)
    d = heapq.heappop(D) * (-1)
    if len(tree[a]) == 0:
        answer[a] = c
        answer[b] = d
    else:
        answer[a] = d
        answer[b] = c

    new_node = deque([])
    for n in tree[a]:
        new_node.append(n)
    for n in tree[b]:
        new_node.append(n)

    while new_node:
        s = new_node.popleft()
        if answer[s] == 0:
            answer[s] = heapq.heappop(D) * (-1)
        for n in tree[s]:
            if answer[n] == 0:
                new_node.append(n)

    print(sum(C[:-1]))
    print(*answer) main N = int(input()) N int(input()) int input() input [] for _ in range(N) for _ in range(N) _ range(N) range N for _ in range(N) [] tree = [[] for _ in range(N)] tree [[] for _ in range(N)] edge = deque([]) edge deque([]) deque [] for _ in range(N - 1):
        a, b = map(int, input().split())
        tree[a - 1].append(b - 1)
        tree[b - 1].append(a - 1)
        edge.append((a - 1, 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 tree[a - 1].append(b - 1) [a - 1].append [a - 1] tree a - 1 a 1 append b - 1 b 1 tree[b - 1].append(a - 1) [b - 1].append [b - 1] tree b - 1 b 1 append a - 1 a 1 edge.append((a - 1, b - 1)) edge.append edge append (a - 1, b - 1) a - 1 a 1 b - 1 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() C.sort C sort D = [] D [] for c in C:
        heapq.heappush(D, -c)

     c C heapq.heappush(D, -c) heapq.heappush heapq heappush D -c c answer = [0] * N answer [0] * N [0] 0 N a, b = edge.popleft() a edge.popleft() edge.popleft edge popleft b c = heapq.heappop(D) * (-1) c heapq.heappop(D) * (-1) heapq.heappop(D) heapq.heappop heapq heappop D -1 d = heapq.heappop(D) * (-1) d heapq.heappop(D) * (-1) heapq.heappop(D) heapq.heappop heapq heappop D -1 if len(tree[a]) == 0:
        answer[a] = c
        answer[b] = d
    else:
        answer[a] = d
        answer[b] = c

     len(tree[a]) == 0 len(tree[a]) len tree[a] tree a 0 answer[a] = c answer[a] answer a c answer[b] = d answer[b] answer b d answer[a] = d answer[a] answer a d answer[b] = c answer[b] answer b c new_node = deque([]) new_node deque([]) deque [] for n in tree[a]:
        new_node.append(n)
     n tree[a] tree a new_node.append(n) new_node.append new_node append n for n in tree[b]:
        new_node.append(n)

     n tree[b] tree b new_node.append(n) new_node.append new_node append n while new_node:
        s = new_node.popleft()
        if answer[s] == 0:
            answer[s] = heapq.heappop(D) * (-1)
        for n in tree[s]:
            if answer[n] == 0:
                new_node.append(n)

     new_node s = new_node.popleft() s new_node.popleft() new_node.popleft new_node popleft if answer[s] == 0:
            answer[s] = heapq.heappop(D) * (-1)
         answer[s] == 0 answer[s] answer s 0 answer[s] = heapq.heappop(D) * (-1) answer[s] answer s heapq.heappop(D) * (-1) heapq.heappop(D) heapq.heappop heapq heappop D -1 for n in tree[s]:
            if answer[n] == 0:
                new_node.append(n)

     n tree[s] tree s if answer[n] == 0:
                new_node.append(n)

     answer[n] == 0 answer[n] answer n 0 new_node.append(n) new_node.append new_node append n print(sum(C[:-1])) print sum(C[:-1]) sum C[:-1] C :-1 -1 print(*answer) print *answer answer if __name__ == "__main__":
    main() __name__ == "__main__" __name__ "__main__" main() main MOD = 10 ** 9 + 7 10 ** 9 + 7 MOD def main():
    N = int(input())
    tree = [[] for _ in range(N)]
    edge = deque([])

    for _ in range(N - 1):
        a, b = map(int, input().split())
        tree[a - 1].append(b - 1)
        tree[b - 1].append(a - 1)
        edge.append((a - 1, b - 1))

    C = list(map(int, input().split()))
    C.sort()
    D = []
    for c in C:
        heapq.heappush(D, -c)

    answer = [0] * N
    a, b = edge.popleft()
    c = heapq.heappop(D) * (-1)
    d = heapq.heappop(D) * (-1)
    if len(tree[a]) == 0:
        answer[a] = c
        answer[b] = d
    else:
        answer[a] = d
        answer[b] = c

    new_node = deque([])
    for n in tree[a]:
        new_node.append(n)
    for n in tree[b]:
        new_node.append(n)

    while new_node:
        s = new_node.popleft()
        if answer[s] == 0:
            answer[s] = heapq.heappop(D) * (-1)
        for n in tree[s]:
            if answer[n] == 0:
                new_node.append(n)

    print(sum(C[:-1]))
    print(*answer) def main():
    N = int(input())
    tree = [[] for _ in range(N)]
    edge = deque([])

    for _ in range(N - 1):
        a, b = map(int, input().split())
        tree[a - 1].append(b - 1)
        tree[b - 1].append(a - 1)
        edge.append((a - 1, b - 1))

    C = list(map(int, input().split()))
    C.sort()
    D = []
    for c in C:
        heapq.heappush(D, -c)

    answer = [0] * N
    a, b = edge.popleft()
    c = heapq.heappop(D) * (-1)
    d = heapq.heappop(D) * (-1)
    if len(tree[a]) == 0:
        answer[a] = c
        answer[b] = d
    else:
        answer[a] = d
        answer[b] = c

    new_node = deque([])
    for n in tree[a]:
        new_node.append(n)
    for n in tree[b]:
        new_node.append(n)

    while new_node:
        s = new_node.popleft()
        if answer[s] == 0:
            answer[s] = heapq.heappop(D) * (-1)
        for n in tree[s]:
            if answer[n] == 0:
                new_node.append(n)

    print(sum(C[:-1]))
    print(*answer) main def input():
    return sys.stdin.readline().strip() def input():
    return sys.stdin.readline().strip() input INF = float("inf") float("inf") INF
import heapq
import sys
from collections import deque


def input():
    return sys.stdin.readline().strip()


sys.setrecursionlimit(20000000)

MOD = 10 ** 9 + 7
INF = float("inf")


def main():
    N = int(input())
    tree = [[] for _ in range(N)]
    edge = deque([])

    for _ in range(N - 1):
        a, b = map(int, input().split())
        tree[a - 1].append(b - 1)
        tree[b - 1].append(a - 1)
        edge.append((a - 1, b - 1))

    C = list(map(int, input().split()))
    C.sort()
    D = []
    for c in C:
        heapq.heappush(D, -c)

    answer = [0] * N
    a, b = edge.popleft()
    c = heapq.heappop(D) * (-1)
    d = heapq.heappop(D) * (-1)
    if len(tree[a]) == 0:
        answer[a] = c
        answer[b] = d
    else:
        answer[a] = d
        answer[b] = c

    new_node = deque([])
    for n in tree[a]:
        new_node.append(n)
    for n in tree[b]:
        new_node.append(n)

    while new_node:
        s = new_node.popleft()
        if answer[s] == 0:
            answer[s] = heapq.heappop(D) * (-1)
        for n in tree[s]:
            if answer[n] == 0:
                new_node.append(n)

    print(sum(C[:-1]))
    print(*answer)


if __name__ == "__main__":
    main()
