from collections import defaultdict
import sys


sys.setrecursionlimit(10**6)


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

    # 深さ優先で番号の大きい順に埋めていく
    C.sort(reverse=True)
    ans = [0] * N

    def dfs(node=0, parent=None, c_index=0):
        ans[node] = C[c_index]
        next_c_index = c_index + 1
        for child in adj[node]:
            if child == parent:
                continue
            next_c_index = dfs(child, node, next_c_index)
        return next_c_index

    dfs()
    print(sum(C[1:]))
    print(' '.join(map(str, ans)))


if __name__ == '__main__':
    main() from collections import defaultdict import sys sys sys.setrecursionlimit(10**6) sys.setrecursionlimit sys setrecursionlimit 10**6 10 6 def main():
    N = int(input())
    adj = defaultdict(list)
    for _ in range(N - 1):
        a, b = list(map(int, input().split()))
        adj[a - 1].append(b - 1)
        adj[b - 1].append(a - 1)
    C = list(map(int, input().split()))

    # 深さ優先で番号の大きい順に埋めていく
    C.sort(reverse=True)
    ans = [0] * N

    def dfs(node=0, parent=None, c_index=0):
        ans[node] = C[c_index]
        next_c_index = c_index + 1
        for child in adj[node]:
            if child == parent:
                continue
            next_c_index = dfs(child, node, next_c_index)
        return next_c_index

    dfs()
    print(sum(C[1:]))
    print(' '.join(map(str, ans))) main N = int(input()) N int(input()) int input() input adj = defaultdict(list) adj defaultdict(list) defaultdict list for _ in range(N - 1):
        a, b = list(map(int, input().split()))
        adj[a - 1].append(b - 1)
        adj[b - 1].append(a - 1)
     _ range(N - 1) range N - 1 N 1 a, b = list(map(int, input().split())) a list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split b adj[a - 1].append(b - 1) [a - 1].append [a - 1] adj a - 1 a 1 append b - 1 b 1 adj[b - 1].append(a - 1) [b - 1].append [b - 1] adj b - 1 b 1 append a - 1 a 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(reverse=True) C.sort C sort reverse=True reverse True ans = [0] * N ans [0] * N [0] 0 N def dfs(node=0, parent=None, c_index=0):
        ans[node] = C[c_index]
        next_c_index = c_index + 1
        for child in adj[node]:
            if child == parent:
                continue
            next_c_index = dfs(child, node, next_c_index)
        return next_c_index

     dfs ans[node] = C[c_index] ans[node] ans node C[c_index] C c_index next_c_index = c_index + 1 next_c_index c_index + 1 c_index 1 for child in adj[node]:
            if child == parent:
                continue
            next_c_index = dfs(child, node, next_c_index)
         child adj[node] adj node if child == parent:
                continue
             child == parent child parent continue next_c_index = dfs(child, node, next_c_index) next_c_index dfs(child, node, next_c_index) dfs child node next_c_index return next_c_index next_c_index node=0 node 0 parent=None parent None c_index=0 c_index 0 dfs() dfs print(sum(C[1:])) print sum(C[1:]) sum C[1:] C 1: 1 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():
    N = int(input())
    adj = defaultdict(list)
    for _ in range(N - 1):
        a, b = list(map(int, input().split()))
        adj[a - 1].append(b - 1)
        adj[b - 1].append(a - 1)
    C = list(map(int, input().split()))

    # 深さ優先で番号の大きい順に埋めていく
    C.sort(reverse=True)
    ans = [0] * N

    def dfs(node=0, parent=None, c_index=0):
        ans[node] = C[c_index]
        next_c_index = c_index + 1
        for child in adj[node]:
            if child == parent:
                continue
            next_c_index = dfs(child, node, next_c_index)
        return next_c_index

    dfs()
    print(sum(C[1:]))
    print(' '.join(map(str, ans))) def main():
    N = int(input())
    adj = defaultdict(list)
    for _ in range(N - 1):
        a, b = list(map(int, input().split()))
        adj[a - 1].append(b - 1)
        adj[b - 1].append(a - 1)
    C = list(map(int, input().split()))

    # 深さ優先で番号の大きい順に埋めていく
    C.sort(reverse=True)
    ans = [0] * N

    def dfs(node=0, parent=None, c_index=0):
        ans[node] = C[c_index]
        next_c_index = c_index + 1
        for child in adj[node]:
            if child == parent:
                continue
            next_c_index = dfs(child, node, next_c_index)
        return next_c_index

    dfs()
    print(sum(C[1:]))
    print(' '.join(map(str, ans))) main
from collections import defaultdict
import sys


sys.setrecursionlimit(10**6)


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

    # 深さ優先で番号の大きい順に埋めていく
    C.sort(reverse=True)
    ans = [0] * N

    def dfs(node=0, parent=None, c_index=0):
        ans[node] = C[c_index]
        next_c_index = c_index + 1
        for child in adj[node]:
            if child == parent:
                continue
            next_c_index = dfs(child, node, next_c_index)
        return next_c_index

    dfs()
    print(sum(C[1:]))
    print(' '.join(map(str, ans)))


if __name__ == '__main__':
    main()
