import sys
from collections import deque

sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


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


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

    C = list(map(int, input().split()))
    C.sort(reverse=True)

    s = sum(C[1:])
    ans = [None] * N

    # c, node
    q = deque([(0, 0)])
    ans[0] = C[0]
    visited = [False] * N
    visited[0] = True
    used = [False] * N
    used[0] = True

    while q:
        c, node = q.popleft()

        for j in edges[node]:
            if visited[j]:
                continue

            for k in range(c + 1, N):
                if used[k]:
                    continue
                else:
                    next_c = k
                    break

            visited[j] = True
            used[next_c] = True
            ans[j] = C[next_c]
            q.append((next_c, j))

    print(s)
    print(*ans, sep=" ")


if __name__ == "__main__":
    main() import sys sys from collections import deque sys.setrecursionlimit(10 ** 6) sys.setrecursionlimit sys setrecursionlimit 10 ** 6 10 6 INF = float("inf") INF float("inf") float "inf" MOD = 10 ** 9 + 7 MOD 10 ** 9 + 7 10 ** 9 10 9 7 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 def main():
    N = int(input())
    edges = [[] for _ in range(N)]
    for _ in range(N - 1):
        a, b = map(int, input().split())
        a -= 1
        b -= 1
        edges[a].append(b)
        edges[b].append(a)

    C = list(map(int, input().split()))
    C.sort(reverse=True)

    s = sum(C[1:])
    ans = [None] * N

    # c, node
    q = deque([(0, 0)])
    ans[0] = C[0]
    visited = [False] * N
    visited[0] = True
    used = [False] * N
    used[0] = True

    while q:
        c, node = q.popleft()

        for j in edges[node]:
            if visited[j]:
                continue

            for k in range(c + 1, N):
                if used[k]:
                    continue
                else:
                    next_c = k
                    break

            visited[j] = True
            used[next_c] = True
            ans[j] = C[next_c]
            q.append((next_c, j))

    print(s)
    print(*ans, sep=" ") 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) [] edges = [[] for _ in range(N)] edges [[] for _ in range(N)] for _ in range(N - 1):
        a, b = map(int, input().split())
        a -= 1
        b -= 1
        edges[a].append(b)
        edges[b].append(a)

     _ 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 a -= 1 a 1 b -= 1 b 1 edges[a].append(b) [a].append [a] edges a append b edges[b].append(a) [b].append [b] edges b append a 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 s = sum(C[1:]) s sum(C[1:]) sum C[1:] C 1: 1 ans = [None] * N ans [None] * N [None] None N q = deque([(0, 0)]) q deque([(0, 0)]) deque [(0, 0)] (0, 0) 0 0 ans[0] = C[0] ans[0] ans 0 C[0] C 0 visited = [False] * N visited [False] * N [False] False N visited[0] = True visited[0] visited 0 True used = [False] * N used [False] * N [False] False N used[0] = True used[0] used 0 True while q:
        c, node = q.popleft()

        for j in edges[node]:
            if visited[j]:
                continue

            for k in range(c + 1, N):
                if used[k]:
                    continue
                else:
                    next_c = k
                    break

            visited[j] = True
            used[next_c] = True
            ans[j] = C[next_c]
            q.append((next_c, j))

     q c, node = q.popleft() c q.popleft() q.popleft q popleft node for j in edges[node]:
            if visited[j]:
                continue

            for k in range(c + 1, N):
                if used[k]:
                    continue
                else:
                    next_c = k
                    break

            visited[j] = True
            used[next_c] = True
            ans[j] = C[next_c]
            q.append((next_c, j))

     j edges[node] edges node if visited[j]:
                continue

             visited[j] visited j continue for k in range(c + 1, N):
                if used[k]:
                    continue
                else:
                    next_c = k
                    break

             k range(c + 1, N) range c + 1 c 1 N if used[k]:
                    continue
                else:
                    next_c = k
                    break

             used[k] used k continue next_c = k next_c k break visited[j] = True visited[j] visited j True used[next_c] = True used[next_c] used next_c True ans[j] = C[next_c] ans[j] ans j C[next_c] C next_c q.append((next_c, j)) q.append q append (next_c, j) next_c j print(s) print s print(*ans, sep=" ") print *ans ans sep=" " sep " " if __name__ == "__main__":
    main() __name__ == "__main__" __name__ "__main__" main() main MOD = 10 ** 9 + 7 10 ** 9 + 7 MOD def input():
    return sys.stdin.readline().strip() def input():
    return sys.stdin.readline().strip() input def main():
    N = int(input())
    edges = [[] for _ in range(N)]
    for _ in range(N - 1):
        a, b = map(int, input().split())
        a -= 1
        b -= 1
        edges[a].append(b)
        edges[b].append(a)

    C = list(map(int, input().split()))
    C.sort(reverse=True)

    s = sum(C[1:])
    ans = [None] * N

    # c, node
    q = deque([(0, 0)])
    ans[0] = C[0]
    visited = [False] * N
    visited[0] = True
    used = [False] * N
    used[0] = True

    while q:
        c, node = q.popleft()

        for j in edges[node]:
            if visited[j]:
                continue

            for k in range(c + 1, N):
                if used[k]:
                    continue
                else:
                    next_c = k
                    break

            visited[j] = True
            used[next_c] = True
            ans[j] = C[next_c]
            q.append((next_c, j))

    print(s)
    print(*ans, sep=" ") def main():
    N = int(input())
    edges = [[] for _ in range(N)]
    for _ in range(N - 1):
        a, b = map(int, input().split())
        a -= 1
        b -= 1
        edges[a].append(b)
        edges[b].append(a)

    C = list(map(int, input().split()))
    C.sort(reverse=True)

    s = sum(C[1:])
    ans = [None] * N

    # c, node
    q = deque([(0, 0)])
    ans[0] = C[0]
    visited = [False] * N
    visited[0] = True
    used = [False] * N
    used[0] = True

    while q:
        c, node = q.popleft()

        for j in edges[node]:
            if visited[j]:
                continue

            for k in range(c + 1, N):
                if used[k]:
                    continue
                else:
                    next_c = k
                    break

            visited[j] = True
            used[next_c] = True
            ans[j] = C[next_c]
            q.append((next_c, j))

    print(s)
    print(*ans, sep=" ") main INF = float("inf") float("inf") INF
import sys
from collections import deque

sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


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


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

    C = list(map(int, input().split()))
    C.sort(reverse=True)

    s = sum(C[1:])
    ans = [None] * N

    # c, node
    q = deque([(0, 0)])
    ans[0] = C[0]
    visited = [False] * N
    visited[0] = True
    used = [False] * N
    used[0] = True

    while q:
        c, node = q.popleft()

        for j in edges[node]:
            if visited[j]:
                continue

            for k in range(c + 1, N):
                if used[k]:
                    continue
                else:
                    next_c = k
                    break

            visited[j] = True
            used[next_c] = True
            ans[j] = C[next_c]
            q.append((next_c, j))

    print(s)
    print(*ans, sep=" ")


if __name__ == "__main__":
    main()
