import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    from collections import deque, defaultdict
    n = int(readline())
    edge = defaultdict(list)

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

    c = list(map(int, readline().split()))
    c.sort(reverse=True)

    nums = [0] * (n + 1)

    que = deque()
    que.append((1, 0))

    cnt = 0
    m = 0

    while que:
        cur, prev_num = que.popleft()
        num = c[cnt]
        nums[cur] = num
        m += min(num, prev_num)
        cnt += 1
        for nx in edge[cur]:
            if nums[nx] == 0:
                que.append((nx, num))

    print(m)
    print(*nums[1:])


if __name__ == '__main__':
    main() import sys sys readline = sys.stdin.readline readline sys.stdin.readline sys.stdin sys stdin readline MOD = 10 ** 9 + 7 MOD 10 ** 9 + 7 10 ** 9 10 9 7 INF = float('INF') INF float('INF') float 'INF' sys.setrecursionlimit(10 ** 5) sys.setrecursionlimit sys setrecursionlimit 10 ** 5 10 5 def main():
    from collections import deque, defaultdict
    n = int(readline())
    edge = defaultdict(list)

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

    c = list(map(int, readline().split()))
    c.sort(reverse=True)

    nums = [0] * (n + 1)

    que = deque()
    que.append((1, 0))

    cnt = 0
    m = 0

    while que:
        cur, prev_num = que.popleft()
        num = c[cnt]
        nums[cur] = num
        m += min(num, prev_num)
        cnt += 1
        for nx in edge[cur]:
            if nums[nx] == 0:
                que.append((nx, num))

    print(m)
    print(*nums[1:]) main from collections import deque, defaultdict n = int(readline()) n int(readline()) int readline() readline edge = defaultdict(list) edge defaultdict(list) defaultdict list for _ in range(n - 1):
        a, b = map(int, readline().split())
        edge[a].append(b)
        edge[b].append(a)

     _ range(n - 1) range n - 1 n 1 a, b = map(int, readline().split()) a map(int, readline().split()) map int readline().split() ().split () readline split b edge[a].append(b) [a].append [a] edge a append b edge[b].append(a) [b].append [b] edge b append a c = list(map(int, readline().split())) c list(map(int, readline().split())) list map(int, readline().split()) map int readline().split() ().split () readline split c.sort(reverse=True) c.sort c sort reverse=True reverse True nums = [0] * (n + 1) nums [0] * (n + 1) [0] 0 n + 1 n 1 que = deque() que deque() deque que.append((1, 0)) que.append que append (1, 0) 1 0 cnt = 0 cnt 0 m = 0 m 0 while que:
        cur, prev_num = que.popleft()
        num = c[cnt]
        nums[cur] = num
        m += min(num, prev_num)
        cnt += 1
        for nx in edge[cur]:
            if nums[nx] == 0:
                que.append((nx, num))

     que cur, prev_num = que.popleft() cur que.popleft() que.popleft que popleft prev_num num = c[cnt] num c[cnt] c cnt nums[cur] = num nums[cur] nums cur num m += min(num, prev_num) m min(num, prev_num) min num prev_num cnt += 1 cnt 1 for nx in edge[cur]:
            if nums[nx] == 0:
                que.append((nx, num))

     nx edge[cur] edge cur if nums[nx] == 0:
                que.append((nx, num))

     nums[nx] == 0 nums[nx] nums nx 0 que.append((nx, num)) que.append que append (nx, num) nx num print(m) print m print(*nums[1:]) print *nums[1:] nums[1:] nums 1: 1 if __name__ == '__main__':
    main() __name__ == '__main__' __name__ '__main__' main() main MOD = 10 ** 9 + 7 10 ** 9 + 7 MOD INF = float('INF') float('INF') INF def main():
    from collections import deque, defaultdict
    n = int(readline())
    edge = defaultdict(list)

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

    c = list(map(int, readline().split()))
    c.sort(reverse=True)

    nums = [0] * (n + 1)

    que = deque()
    que.append((1, 0))

    cnt = 0
    m = 0

    while que:
        cur, prev_num = que.popleft()
        num = c[cnt]
        nums[cur] = num
        m += min(num, prev_num)
        cnt += 1
        for nx in edge[cur]:
            if nums[nx] == 0:
                que.append((nx, num))

    print(m)
    print(*nums[1:]) def main():
    from collections import deque, defaultdict
    n = int(readline())
    edge = defaultdict(list)

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

    c = list(map(int, readline().split()))
    c.sort(reverse=True)

    nums = [0] * (n + 1)

    que = deque()
    que.append((1, 0))

    cnt = 0
    m = 0

    while que:
        cur, prev_num = que.popleft()
        num = c[cnt]
        nums[cur] = num
        m += min(num, prev_num)
        cnt += 1
        for nx in edge[cur]:
            if nums[nx] == 0:
                que.append((nx, num))

    print(m)
    print(*nums[1:]) main readline = sys.stdin.readline sys.stdin.readline readline
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    from collections import deque, defaultdict
    n = int(readline())
    edge = defaultdict(list)

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

    c = list(map(int, readline().split()))
    c.sort(reverse=True)

    nums = [0] * (n + 1)

    que = deque()
    que.append((1, 0))

    cnt = 0
    m = 0

    while que:
        cur, prev_num = que.popleft()
        num = c[cnt]
        nums[cur] = num
        m += min(num, prev_num)
        cnt += 1
        for nx in edge[cur]:
            if nums[nx] == 0:
                que.append((nx, num))

    print(m)
    print(*nums[1:])


if __name__ == '__main__':
    main()
