import sys, math, collections, heapq, itertools

def gcd(a, b):

    a, b = max(a, b), min(a, b)

    while a % b > 0: a, b = b, a % b

    return b

  

def solve():

    file = sys.stdin.readline #single: int(file()), line: map(int, file().split())

    INF = 10 ** 25

    mod = 7 + 10 ** 9



    N = int(file())

    edge = [[] for i in range(N)]

    for i in range(N-1):

        a, b = list(map(int, file().split()))

        edge[a-1].append(b-1)

        edge[b-1].append(a-1)

    c = [int(i) for i in file().split()]

    c.sort(reverse = True)



    que = collections.deque()

    last = 0

    forward = [-1] * N

    que.append((0, 0))

    while que:

        now, dist = que.popleft()

        if forward[now] == -1:

            forward[now] = dist

            last = now

            for e in edge[now]:

                if forward[e] == -1: que.append((e, dist + 1))

    backward = [-1] * N

    que.append(last)

    count = 0

    while que:

        now = que.popleft()

        if backward[now] == -1:

            backward[now] = c[count]

            count += 1

            last = now

            for e in edge[now]:

                if backward[e] == -1: que.append(e)



    total = 0

    que.append((last, backward[last]))

    visited = [False] * N

    while que:

        now, weight = que.popleft()

        if visited[now] == False:

            visited[now] = True

            for e in edge[now]:

                if visited[e] == False:

                    total += min(weight, backward[e])

                    que.append((e, backward[e]))



    print(total)

    print((" ".join(map(str, backward))))



    return 0

  

if __name__ == "__main__":

    solve()
import sys

from collections import deque



def solve():

    input = sys.stdin.readline

    N = int(eval(input()))

    Edge = [[] for _ in range(N)]

    for _ in range(N-1):

        a, b = list(map(int, input().split()))

        Edge[a-1].append(b-1)

        Edge[b-1].append(a-1)

    C = [int(c) for c in input().split()]

    C.sort(reverse  = True)

    Color = [None] * N

    Color[0] = C[0]

    q = deque()

    for e in Edge[0]:

        q.append((e, 0))

    ans = 0

    for i in range(1, N):

        nowN, preN = q.popleft()

        ans += C[i]

        Color[nowN] = C[i]

        for e in Edge[nowN]:

            if e != preN: q.append((e, nowN))

    print(ans)

    print((" ".join(map(str, Color))))



    return 0



if __name__ == "__main__":

    solve()
import sys, math, collections, heapq, itertools


def gcd(a, b):
    a, b = max(a, b), min(a, b)
    while a % b > 0:
        a, b = b, a % b
    return b


def solve():
    file = sys.stdin.readline  # single: int(file()), line: map(int, file().split())
    INF = 10**25
    mod = 7 + 10**9
    N = int(file())
    edge = [[] for i in range(N)]
    for i in range(N - 1):
        a, b = list(map(int, file().split()))
        edge[a - 1].append(b - 1)
        edge[b - 1].append(a - 1)
    c = [int(i) for i in file().split()]
    c.sort(reverse=True)
    que = collections.deque()
    last = 0
    forward = [-1] * N
    que.append((0, 0))
    while que:
        now, dist = que.popleft()
        if forward[now] == -1:
            forward[now] = dist
            last = now
            for e in edge[now]:
                if forward[e] == -1:
                    que.append((e, dist + 1))
    backward = [-1] * N
    que.append(last)
    count = 0
    while que:
        now = que.popleft()
        if backward[now] == -1:
            backward[now] = c[count]
            count += 1
            last = now
            for e in edge[now]:
                if backward[e] == -1:
                    que.append(e)
    total = 0
    que.append((last, backward[last]))
    visited = [False] * N
    while que:
        now, weight = que.popleft()
        if visited[now] == False:
            visited[now] = True
            for e in edge[now]:
                if visited[e] == False:
                    total += min(weight, backward[e])
                    que.append((e, backward[e]))
    print(total)
    print((" ".join(map(str, backward))))
    return 0


if __name__ == "__main__":
    solve()

import sys
from collections import deque


def solve():
    input = sys.stdin.readline
    N = int(eval(input()))
    Edge = [[] for _ in range(N)]
    for _ in range(N - 1):
        a, b = list(map(int, input().split()))
        Edge[a - 1].append(b - 1)
        Edge[b - 1].append(a - 1)
    C = [int(c) for c in input().split()]
    C.sort(reverse=True)
    Color = [None] * N
    Color[0] = C[0]
    q = deque()
    for e in Edge[0]:
        q.append((e, 0))
    ans = 0
    for i in range(1, N):
        nowN, preN = q.popleft()
        ans += C[i]
        Color[nowN] = C[i]
        for e in Edge[nowN]:
            if e != preN:
                q.append((e, nowN))
    print(ans)
    print((" ".join(map(str, Color))))
    return 0


if __name__ == "__main__":
    solve()

-import sys, math, collections, heapq, itertools - - -def gcd(a, b): -    a, b = max(a, b), min(a, b) -    while a % b > 0: -        a, b = b, a % b -    return b +import sys +from collections import deque -    file = sys.stdin.readline  # single: int(file()), line: map(int, file().split()) -    INF = 10**25 -    mod = 7 + 10**9 -    N = int(file()) -    edge = [[] for i in range(N)] -    for i in range(N - 1): -        a, b = list(map(int, file().split())) -        edge[a - 1].append(b - 1) -        edge[b - 1].append(a - 1) -    c = [int(i) for i in file().split()] -    c.sort(reverse=True) -    que = collections.deque() -    last = 0 -    forward = [-1] * N -    que.append((0, 0)) -    while que: -        now, dist = que.popleft() -        if forward[now] == -1: -            forward[now] = dist -            last = now -            for e in edge[now]: -                if forward[e] == -1: -                    que.append((e, dist + 1)) -    backward = [-1] * N -    que.append(last) -    count = 0 -    while que: -        now = que.popleft() -        if backward[now] == -1: -            backward[now] = c[count] -            count += 1 -            last = now -            for e in edge[now]: -                if backward[e] == -1: -                    que.append(e) -    total = 0 -    que.append((last, backward[last])) -    visited = [False] * N -    while que: -        now, weight = que.popleft() -        if visited[now] == False: -            visited[now] = True -            for e in edge[now]: -                if visited[e] == False: -                    total += min(weight, backward[e]) -                    que.append((e, backward[e])) -    print(total) -    print((" ".join(map(str, backward)))) +    input = sys.stdin.readline +    N = int(eval(input())) +    Edge = [[] for _ in range(N)] +    for _ in range(N - 1): +        a, b = list(map(int, input().split())) +        Edge[a - 1].append(b - 1) +        Edge[b - 1].append(a - 1) +    C = [int(c) for c in input().split()] +    C.sort(reverse=True) +    Color = [None] * N +    Color[0] = C[0] +    q = deque() +    for e in Edge[0]: +        q.append((e, 0)) +    ans = 0 +    for i in range(1, N): +        nowN, preN = q.popleft() +        ans += C[i] +        Color[nowN] = C[i] +        for e in Edge[nowN]: +            if e != preN: +                q.append((e, nowN)) +    print(ans) +    print((" ".join(map(str, Color))))