# -*- coding: utf-8 -*-


def main():
    from collections import deque

    n = int(input())
    graph = [list() for _ in range(n)]

    for i in range(n - 1):
        ai, bi = map(int, input().split())
        graph[ai - 1].append(bi - 1)
        graph[bi - 1].append(ai - 1)

    c = sorted(list(map(int, input().split())), reverse=True)

    # スコアの最大値：数列cのうち，最大値を除いた合計
    print(sum(c[1:]))

    # 書き込み方：根から大きな値を割り当てるとよさそう
    visited = [False for _ in range(n)]
    visited[0] = True
    d = deque()
    d.append(0)
    ans = [0 for _ in range(n)]
    ans[0] = c[0]
    count = 1

    while d:
        di = d.popleft()

        for dii in graph[di]:
            if not visited[dii]:
                ans[dii] = c[count]
                count += 1
                visited[dii] = True
                d.append(dii)

    print(' '.join(map(str, ans)))


if __name__ == '__main__':
    main() def main():
    from collections import deque

    n = int(input())
    graph = [list() for _ in range(n)]

    for i in range(n - 1):
        ai, bi = map(int, input().split())
        graph[ai - 1].append(bi - 1)
        graph[bi - 1].append(ai - 1)

    c = sorted(list(map(int, input().split())), reverse=True)

    # スコアの最大値：数列cのうち，最大値を除いた合計
    print(sum(c[1:]))

    # 書き込み方：根から大きな値を割り当てるとよさそう
    visited = [False for _ in range(n)]
    visited[0] = True
    d = deque()
    d.append(0)
    ans = [0 for _ in range(n)]
    ans[0] = c[0]
    count = 1

    while d:
        di = d.popleft()

        for dii in graph[di]:
            if not visited[dii]:
                ans[dii] = c[count]
                count += 1
                visited[dii] = True
                d.append(dii)

    print(' '.join(map(str, ans))) main from collections import deque n = int(input()) n int(input()) int input() input list() for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) list() list graph = [list() for _ in range(n)] graph [list() for _ in range(n)] for i in range(n - 1):
        ai, bi = map(int, input().split())
        graph[ai - 1].append(bi - 1)
        graph[bi - 1].append(ai - 1)

     i range(n - 1) range n - 1 n 1 ai, bi = map(int, input().split()) ai map(int, input().split()) map int input().split() ().split () input split bi graph[ai - 1].append(bi - 1) [ai - 1].append [ai - 1] graph ai - 1 ai 1 append bi - 1 bi 1 graph[bi - 1].append(ai - 1) [bi - 1].append [bi - 1] graph bi - 1 bi 1 append ai - 1 ai 1 c = sorted(list(map(int, input().split())), reverse=True) c sorted(list(map(int, input().split())), reverse=True) sorted list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split reverse=True reverse True print(sum(c[1:])) print sum(c[1:]) sum c[1:] c 1: 1 False for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) False visited = [False for _ in range(n)] visited [False for _ in range(n)] visited[0] = True visited[0] visited 0 True d = deque() d deque() deque d.append(0) d.append d append 0 0 for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) 0 ans = [0 for _ in range(n)] ans [0 for _ in range(n)] ans[0] = c[0] ans[0] ans 0 c[0] c 0 count = 1 count 1 while d:
        di = d.popleft()

        for dii in graph[di]:
            if not visited[dii]:
                ans[dii] = c[count]
                count += 1
                visited[dii] = True
                d.append(dii)

     d di = d.popleft() di d.popleft() d.popleft d popleft for dii in graph[di]:
            if not visited[dii]:
                ans[dii] = c[count]
                count += 1
                visited[dii] = True
                d.append(dii)

     dii graph[di] graph di if not visited[dii]:
                ans[dii] = c[count]
                count += 1
                visited[dii] = True
                d.append(dii)

     not visited[dii] visited[dii] visited dii ans[dii] = c[count] ans[dii] ans dii c[count] c count count += 1 count 1 visited[dii] = True visited[dii] visited dii True d.append(dii) d.append d append dii print(' '.join(map(str, ans))) print ' '.join(map(str, ans)) ' '.join ' ' join map(str, ans) map str ans if __name__ == '__main__':
    main() __name__ == '__main__' __name__ '__main__' main() main def main():
    from collections import deque

    n = int(input())
    graph = [list() for _ in range(n)]

    for i in range(n - 1):
        ai, bi = map(int, input().split())
        graph[ai - 1].append(bi - 1)
        graph[bi - 1].append(ai - 1)

    c = sorted(list(map(int, input().split())), reverse=True)

    # スコアの最大値：数列cのうち，最大値を除いた合計
    print(sum(c[1:]))

    # 書き込み方：根から大きな値を割り当てるとよさそう
    visited = [False for _ in range(n)]
    visited[0] = True
    d = deque()
    d.append(0)
    ans = [0 for _ in range(n)]
    ans[0] = c[0]
    count = 1

    while d:
        di = d.popleft()

        for dii in graph[di]:
            if not visited[dii]:
                ans[dii] = c[count]
                count += 1
                visited[dii] = True
                d.append(dii)

    print(' '.join(map(str, ans))) def main():
    from collections import deque

    n = int(input())
    graph = [list() for _ in range(n)]

    for i in range(n - 1):
        ai, bi = map(int, input().split())
        graph[ai - 1].append(bi - 1)
        graph[bi - 1].append(ai - 1)

    c = sorted(list(map(int, input().split())), reverse=True)

    # スコアの最大値：数列cのうち，最大値を除いた合計
    print(sum(c[1:]))

    # 書き込み方：根から大きな値を割り当てるとよさそう
    visited = [False for _ in range(n)]
    visited[0] = True
    d = deque()
    d.append(0)
    ans = [0 for _ in range(n)]
    ans[0] = c[0]
    count = 1

    while d:
        di = d.popleft()

        for dii in graph[di]:
            if not visited[dii]:
                ans[dii] = c[count]
                count += 1
                visited[dii] = True
                d.append(dii)

    print(' '.join(map(str, ans))) main
# -*- coding: utf-8 -*-


def main():
    from collections import deque

    n = int(input())
    graph = [list() for _ in range(n)]

    for i in range(n - 1):
        ai, bi = map(int, input().split())
        graph[ai - 1].append(bi - 1)
        graph[bi - 1].append(ai - 1)

    c = sorted(list(map(int, input().split())), reverse=True)

    # スコアの最大値：数列cのうち，最大値を除いた合計
    print(sum(c[1:]))

    # 書き込み方：根から大きな値を割り当てるとよさそう
    visited = [False for _ in range(n)]
    visited[0] = True
    d = deque()
    d.append(0)
    ans = [0 for _ in range(n)]
    ans[0] = c[0]
    count = 1

    while d:
        di = d.popleft()

        for dii in graph[di]:
            if not visited[dii]:
                ans[dii] = c[count]
                count += 1
                visited[dii] = True
                d.append(dii)

    print(' '.join(map(str, ans)))


if __name__ == '__main__':
    main()
