import sys
import re
import math
import collections
import bisect
import itertools
import fractions
import functools
import copy
import heapq
import decimal
import statistics
import queue

# import numpy as np

sys.setrecursionlimit(10 ** 9)
INF = 10 ** 16
MOD = 10 ** 9 + 7
# MOD = 998244353

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))
na1 = lambda: list(map(lambda x: int(x) - 1, sys.stdin.readline().split()))


# ===CODE===


def main():
    n = ni()
    e = [[] for _ in range(n)]
    for _ in range(n - 1):
        a, b = ns()
        a, b = a - 1, b - 1
        e[a].append(b)
        e[b].append(a)

    c = na()
    c.sort(reverse=True)
    cost = collections.deque(c)

    notvisited = [True for _ in range(n)]
    ans = [-1 for _ in range(n)]

    que = collections.deque(e[0])
    notvisited[0] = False
    ans[0] = cost.popleft()

    while que:
        for _ in range(len(que)):
            q = que.popleft()
            if notvisited[q]:
                ans[q] = cost.popleft()
                notvisited[q] = False
                for ei in e[q]:
                    que.append(ei)

    res = 0
    for i in range(n):
        for ei in e[i]:
            if i < ei:
                res += min(ans[i], ans[ei])

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


if __name__ == '__main__':
    main() import sys sys import re re import math math import collections collections import bisect bisect import itertools itertools import fractions fractions import functools functools import copy copy import heapq heapq import decimal decimal import statistics statistics import queue queue sys.setrecursionlimit(10 ** 9) sys.setrecursionlimit sys setrecursionlimit 10 ** 9 10 9 INF = 10 ** 16 INF 10 ** 16 10 16 MOD = 10 ** 9 + 7 MOD 10 ** 9 + 7 10 ** 9 10 9 7 ni = lambda: int(sys.stdin.readline()) ni lambda: int(sys.stdin.readline()) int(sys.stdin.readline()) int sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline ns = lambda: map(int, sys.stdin.readline().split()) ns lambda: map(int, sys.stdin.readline().split()) map(int, sys.stdin.readline().split()) map int sys.stdin.readline().split() sys.stdin.readline().split sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline split na = lambda: list(map(int, sys.stdin.readline().split())) na lambda: list(map(int, sys.stdin.readline().split())) list(map(int, sys.stdin.readline().split())) list map(int, sys.stdin.readline().split()) map int sys.stdin.readline().split() sys.stdin.readline().split sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline split na1 = lambda: list(map(lambda x: int(x) - 1, sys.stdin.readline().split())) na1 lambda: list(map(lambda x: int(x) - 1, sys.stdin.readline().split())) list(map(lambda x: int(x) - 1, sys.stdin.readline().split())) list map(lambda x: int(x) - 1, sys.stdin.readline().split()) map lambda x: int(x) - 1 int(x) - 1 int(x) int x 1 x sys.stdin.readline().split() sys.stdin.readline().split sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline split def main():
    n = ni()
    e = [[] for _ in range(n)]
    for _ in range(n - 1):
        a, b = ns()
        a, b = a - 1, b - 1
        e[a].append(b)
        e[b].append(a)

    c = na()
    c.sort(reverse=True)
    cost = collections.deque(c)

    notvisited = [True for _ in range(n)]
    ans = [-1 for _ in range(n)]

    que = collections.deque(e[0])
    notvisited[0] = False
    ans[0] = cost.popleft()

    while que:
        for _ in range(len(que)):
            q = que.popleft()
            if notvisited[q]:
                ans[q] = cost.popleft()
                notvisited[q] = False
                for ei in e[q]:
                    que.append(ei)

    res = 0
    for i in range(n):
        for ei in e[i]:
            if i < ei:
                res += min(ans[i], ans[ei])

    print(res)
    print(*ans, sep=" ") main n = ni() n ni() ni [] for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) [] e = [[] for _ in range(n)] e [[] for _ in range(n)] for _ in range(n - 1):
        a, b = ns()
        a, b = a - 1, b - 1
        e[a].append(b)
        e[b].append(a)

     _ range(n - 1) range n - 1 n 1 a, b = ns() a ns() ns b a, b = a - 1, b - 1 a a - 1 a 1 b b - 1 b 1 e[a].append(b) [a].append [a] e a append b e[b].append(a) [b].append [b] e b append a c = na() c na() na c.sort(reverse=True) c.sort c sort reverse=True reverse True cost = collections.deque(c) cost collections.deque(c) collections.deque collections deque c True for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) True notvisited = [True for _ in range(n)] notvisited [True for _ in range(n)] -1 for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) -1 ans = [-1 for _ in range(n)] ans [-1 for _ in range(n)] que = collections.deque(e[0]) que collections.deque(e[0]) collections.deque collections deque e[0] e 0 notvisited[0] = False notvisited[0] notvisited 0 False ans[0] = cost.popleft() ans[0] ans 0 cost.popleft() cost.popleft cost popleft while que:
        for _ in range(len(que)):
            q = que.popleft()
            if notvisited[q]:
                ans[q] = cost.popleft()
                notvisited[q] = False
                for ei in e[q]:
                    que.append(ei)

     que for _ in range(len(que)):
            q = que.popleft()
            if notvisited[q]:
                ans[q] = cost.popleft()
                notvisited[q] = False
                for ei in e[q]:
                    que.append(ei)

     _ range(len(que)) range len(que) len que q = que.popleft() q que.popleft() que.popleft que popleft if notvisited[q]:
                ans[q] = cost.popleft()
                notvisited[q] = False
                for ei in e[q]:
                    que.append(ei)

     notvisited[q] notvisited q ans[q] = cost.popleft() ans[q] ans q cost.popleft() cost.popleft cost popleft notvisited[q] = False notvisited[q] notvisited q False for ei in e[q]:
                    que.append(ei)

     ei e[q] e q que.append(ei) que.append que append ei res = 0 res 0 for i in range(n):
        for ei in e[i]:
            if i < ei:
                res += min(ans[i], ans[ei])

     i range(n) range n for ei in e[i]:
            if i < ei:
                res += min(ans[i], ans[ei])

     ei e[i] e i if i < ei:
                res += min(ans[i], ans[ei])

     i < ei i ei res += min(ans[i], ans[ei]) res min(ans[i], ans[ei]) min ans[i] ans i ans[ei] ans ei print(res) print res print(*ans, sep=" ") print *ans ans sep=" " sep " " if __name__ == '__main__':
    main() __name__ == '__main__' __name__ '__main__' main() main ni = lambda: int(sys.stdin.readline()) lambda: int(sys.stdin.readline()) ni MOD = 10 ** 9 + 7 10 ** 9 + 7 MOD na = lambda: list(map(int, sys.stdin.readline().split())) lambda: list(map(int, sys.stdin.readline().split())) na ns = lambda: map(int, sys.stdin.readline().split()) lambda: map(int, sys.stdin.readline().split()) ns def main():
    n = ni()
    e = [[] for _ in range(n)]
    for _ in range(n - 1):
        a, b = ns()
        a, b = a - 1, b - 1
        e[a].append(b)
        e[b].append(a)

    c = na()
    c.sort(reverse=True)
    cost = collections.deque(c)

    notvisited = [True for _ in range(n)]
    ans = [-1 for _ in range(n)]

    que = collections.deque(e[0])
    notvisited[0] = False
    ans[0] = cost.popleft()

    while que:
        for _ in range(len(que)):
            q = que.popleft()
            if notvisited[q]:
                ans[q] = cost.popleft()
                notvisited[q] = False
                for ei in e[q]:
                    que.append(ei)

    res = 0
    for i in range(n):
        for ei in e[i]:
            if i < ei:
                res += min(ans[i], ans[ei])

    print(res)
    print(*ans, sep=" ") def main():
    n = ni()
    e = [[] for _ in range(n)]
    for _ in range(n - 1):
        a, b = ns()
        a, b = a - 1, b - 1
        e[a].append(b)
        e[b].append(a)

    c = na()
    c.sort(reverse=True)
    cost = collections.deque(c)

    notvisited = [True for _ in range(n)]
    ans = [-1 for _ in range(n)]

    que = collections.deque(e[0])
    notvisited[0] = False
    ans[0] = cost.popleft()

    while que:
        for _ in range(len(que)):
            q = que.popleft()
            if notvisited[q]:
                ans[q] = cost.popleft()
                notvisited[q] = False
                for ei in e[q]:
                    que.append(ei)

    res = 0
    for i in range(n):
        for ei in e[i]:
            if i < ei:
                res += min(ans[i], ans[ei])

    print(res)
    print(*ans, sep=" ") main INF = 10 ** 16 10 ** 16 INF na1 = lambda: list(map(lambda x: int(x) - 1, sys.stdin.readline().split())) lambda: list(map(lambda x: int(x) - 1, sys.stdin.readline().split())) na1
import sys
import re
import math
import collections
import bisect
import itertools
import fractions
import functools
import copy
import heapq
import decimal
import statistics
import queue

# import numpy as np

sys.setrecursionlimit(10 ** 9)
INF = 10 ** 16
MOD = 10 ** 9 + 7
# MOD = 998244353

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))
na1 = lambda: list(map(lambda x: int(x) - 1, sys.stdin.readline().split()))


# ===CODE===


def main():
    n = ni()
    e = [[] for _ in range(n)]
    for _ in range(n - 1):
        a, b = ns()
        a, b = a - 1, b - 1
        e[a].append(b)
        e[b].append(a)

    c = na()
    c.sort(reverse=True)
    cost = collections.deque(c)

    notvisited = [True for _ in range(n)]
    ans = [-1 for _ in range(n)]

    que = collections.deque(e[0])
    notvisited[0] = False
    ans[0] = cost.popleft()

    while que:
        for _ in range(len(que)):
            q = que.popleft()
            if notvisited[q]:
                ans[q] = cost.popleft()
                notvisited[q] = False
                for ei in e[q]:
                    que.append(ei)

    res = 0
    for i in range(n):
        for ei in e[i]:
            if i < ei:
                res += min(ans[i], ans[ei])

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


if __name__ == '__main__':
    main()
