from collections import deque


N = int(input())
to = [[] for _ in range(N)]

for _ in range(N - 1):
    a, b = map(lambda x:int(x) - 1, input().split())
    to[a].append(b)
    to[b].append(a)

c = list(map(int, input().split()))
c.sort()


def main():
    ans = [0] * N
    ans[0] = c.pop()
    S = 0

    seen = [0] * N
    seen[0] = 1
    que = deque([0])

    while que:
        v = que.popleft()
        for nv in to[v]:
            if not seen[nv]:
                seen[nv] = 1
                ans[nv] = c.pop()
                S += ans[nv]
                que.append(nv)

    print(S)
    print(*ans)


if __name__ == "__main__":
    main() from collections import deque N = int(input()) N int(input()) int input() input [] for _ in range(N) for _ in range(N) _ range(N) range N for _ in range(N) [] to = [[] for _ in range(N)] to [[] for _ in range(N)] for _ in range(N - 1):
    a, b = map(lambda x:int(x) - 1, input().split())
    to[a].append(b)
    to[b].append(a) _ range(N - 1) range N - 1 N 1 a, b = map(lambda x:int(x) - 1, input().split()) a map(lambda x:int(x) - 1, input().split()) map lambda x:int(x) - 1 int(x) - 1 int(x) int x 1 x input().split() ().split () input split b to[a].append(b) [a].append [a] to a append b to[b].append(a) [b].append [b] to b append a 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() c.sort c sort def main():
    ans = [0] * N
    ans[0] = c.pop()
    S = 0

    seen = [0] * N
    seen[0] = 1
    que = deque([0])

    while que:
        v = que.popleft()
        for nv in to[v]:
            if not seen[nv]:
                seen[nv] = 1
                ans[nv] = c.pop()
                S += ans[nv]
                que.append(nv)

    print(S)
    print(*ans) main ans = [0] * N ans [0] * N [0] 0 N ans[0] = c.pop() ans[0] ans 0 c.pop() c.pop c pop S = 0 S 0 seen = [0] * N seen [0] * N [0] 0 N seen[0] = 1 seen[0] seen 0 1 que = deque([0]) que deque([0]) deque [0] 0 while que:
        v = que.popleft()
        for nv in to[v]:
            if not seen[nv]:
                seen[nv] = 1
                ans[nv] = c.pop()
                S += ans[nv]
                que.append(nv)

     que v = que.popleft() v que.popleft() que.popleft que popleft for nv in to[v]:
            if not seen[nv]:
                seen[nv] = 1
                ans[nv] = c.pop()
                S += ans[nv]
                que.append(nv)

     nv to[v] to v if not seen[nv]:
                seen[nv] = 1
                ans[nv] = c.pop()
                S += ans[nv]
                que.append(nv)

     not seen[nv] seen[nv] seen nv seen[nv] = 1 seen[nv] seen nv 1 ans[nv] = c.pop() ans[nv] ans nv c.pop() c.pop c pop S += ans[nv] S ans[nv] ans nv que.append(nv) que.append que append nv print(S) print S print(*ans) print *ans ans if __name__ == "__main__":
    main() __name__ == "__main__" __name__ "__main__" main() main b = map(lambda x:int(x) - 1, input().split()) map(lambda x:int(x) - 1, input().split()) b N = int(input()) int(input()) N a, b = map(lambda x:int(x) - 1, input().split()) map(lambda x:int(x) - 1, input().split()) a def main():
    ans = [0] * N
    ans[0] = c.pop()
    S = 0

    seen = [0] * N
    seen[0] = 1
    que = deque([0])

    while que:
        v = que.popleft()
        for nv in to[v]:
            if not seen[nv]:
                seen[nv] = 1
                ans[nv] = c.pop()
                S += ans[nv]
                que.append(nv)

    print(S)
    print(*ans) def main():
    ans = [0] * N
    ans[0] = c.pop()
    S = 0

    seen = [0] * N
    seen[0] = 1
    que = deque([0])

    while que:
        v = que.popleft()
        for nv in to[v]:
            if not seen[nv]:
                seen[nv] = 1
                ans[nv] = c.pop()
                S += ans[nv]
                que.append(nv)

    print(S)
    print(*ans) main to = [[] for _ in range(N)] [[] for _ in range(N)] to c = list(map(int, input().split())) list(map(int, input().split())) c
from collections import deque


N = int(input())
to = [[] for _ in range(N)]

for _ in range(N - 1):
    a, b = map(lambda x:int(x) - 1, input().split())
    to[a].append(b)
    to[b].append(a)

c = list(map(int, input().split()))
c.sort()


def main():
    ans = [0] * N
    ans[0] = c.pop()
    S = 0

    seen = [0] * N
    seen[0] = 1
    que = deque([0])

    while que:
        v = que.popleft()
        for nv in to[v]:
            if not seen[nv]:
                seen[nv] = 1
                ans[nv] = c.pop()
                S += ans[nv]
                que.append(nv)

    print(S)
    print(*ans)


if __name__ == "__main__":
    main()