from collections import deque


def main():
    n = int(input())
    ans = [0 for _ in range(n)]
    graph = [[] for _ in range(n)]
    for _ in range(n - 1):
        a, b = map(lambda x: int(x) - 1, input().split())
        graph[a].append(b)
        graph[b].append(a)
    c = list(map(int, input().split()))
    c.sort(reverse=True)
    root = 0
    max_deg = 0
    for i in range(n):
        if max_deg < len(graph[i]):
            root = i
            max_deg = len(graph[i])
    q = deque([root])
    ans[root] = c[0]
    idx = 1
    while q:
        now_v = q.pop()
        for next_v in graph[now_v]:
            if ans[next_v] == 0:
                ans[next_v] = c[idx]
                idx += 1
                q.append(next_v)
    print(sum(c[1:]))
    print(" ".join(map(str, ans)))


if __name__ == '__main__':
    main() from collections import deque def main():
    n = int(input())
    ans = [0 for _ in range(n)]
    graph = [[] for _ in range(n)]
    for _ in range(n - 1):
        a, b = map(lambda x: int(x) - 1, input().split())
        graph[a].append(b)
        graph[b].append(a)
    c = list(map(int, input().split()))
    c.sort(reverse=True)
    root = 0
    max_deg = 0
    for i in range(n):
        if max_deg < len(graph[i]):
            root = i
            max_deg = len(graph[i])
    q = deque([root])
    ans[root] = c[0]
    idx = 1
    while q:
        now_v = q.pop()
        for next_v in graph[now_v]:
            if ans[next_v] == 0:
                ans[next_v] = c[idx]
                idx += 1
                q.append(next_v)
    print(sum(c[1:]))
    print(" ".join(map(str, ans))) main n = int(input()) n int(input()) int input() input 0 for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) 0 ans = [0 for _ in range(n)] ans [0 for _ in range(n)] [] for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) [] graph = [[] for _ in range(n)] graph [[] for _ in range(n)] for _ in range(n - 1):
        a, b = map(lambda x: int(x) - 1, input().split())
        graph[a].append(b)
        graph[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 graph[a].append(b) [a].append [a] graph a append b graph[b].append(a) [b].append [b] graph 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(reverse=True) c.sort c sort reverse=True reverse True root = 0 root 0 max_deg = 0 max_deg 0 for i in range(n):
        if max_deg < len(graph[i]):
            root = i
            max_deg = len(graph[i])
     i range(n) range n if max_deg < len(graph[i]):
            root = i
            max_deg = len(graph[i])
     max_deg < len(graph[i]) max_deg len(graph[i]) len graph[i] graph i root = i root i max_deg = len(graph[i]) max_deg len(graph[i]) len graph[i] graph i q = deque([root]) q deque([root]) deque [root] root ans[root] = c[0] ans[root] ans root c[0] c 0 idx = 1 idx 1 while q:
        now_v = q.pop()
        for next_v in graph[now_v]:
            if ans[next_v] == 0:
                ans[next_v] = c[idx]
                idx += 1
                q.append(next_v)
     q now_v = q.pop() now_v q.pop() q.pop q pop for next_v in graph[now_v]:
            if ans[next_v] == 0:
                ans[next_v] = c[idx]
                idx += 1
                q.append(next_v)
     next_v graph[now_v] graph now_v if ans[next_v] == 0:
                ans[next_v] = c[idx]
                idx += 1
                q.append(next_v)
     ans[next_v] == 0 ans[next_v] ans next_v 0 ans[next_v] = c[idx] ans[next_v] ans next_v c[idx] c idx idx += 1 idx 1 q.append(next_v) q.append q append next_v 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())
    ans = [0 for _ in range(n)]
    graph = [[] for _ in range(n)]
    for _ in range(n - 1):
        a, b = map(lambda x: int(x) - 1, input().split())
        graph[a].append(b)
        graph[b].append(a)
    c = list(map(int, input().split()))
    c.sort(reverse=True)
    root = 0
    max_deg = 0
    for i in range(n):
        if max_deg < len(graph[i]):
            root = i
            max_deg = len(graph[i])
    q = deque([root])
    ans[root] = c[0]
    idx = 1
    while q:
        now_v = q.pop()
        for next_v in graph[now_v]:
            if ans[next_v] == 0:
                ans[next_v] = c[idx]
                idx += 1
                q.append(next_v)
    print(sum(c[1:]))
    print(" ".join(map(str, ans))) def main():
    n = int(input())
    ans = [0 for _ in range(n)]
    graph = [[] for _ in range(n)]
    for _ in range(n - 1):
        a, b = map(lambda x: int(x) - 1, input().split())
        graph[a].append(b)
        graph[b].append(a)
    c = list(map(int, input().split()))
    c.sort(reverse=True)
    root = 0
    max_deg = 0
    for i in range(n):
        if max_deg < len(graph[i]):
            root = i
            max_deg = len(graph[i])
    q = deque([root])
    ans[root] = c[0]
    idx = 1
    while q:
        now_v = q.pop()
        for next_v in graph[now_v]:
            if ans[next_v] == 0:
                ans[next_v] = c[idx]
                idx += 1
                q.append(next_v)
    print(sum(c[1:]))
    print(" ".join(map(str, ans))) main
from collections import deque


def main():
    n = int(input())
    ans = [0 for _ in range(n)]
    graph = [[] for _ in range(n)]
    for _ in range(n - 1):
        a, b = map(lambda x: int(x) - 1, input().split())
        graph[a].append(b)
        graph[b].append(a)
    c = list(map(int, input().split()))
    c.sort(reverse=True)
    root = 0
    max_deg = 0
    for i in range(n):
        if max_deg < len(graph[i]):
            root = i
            max_deg = len(graph[i])
    q = deque([root])
    ans[root] = c[0]
    idx = 1
    while q:
        now_v = q.pop()
        for next_v in graph[now_v]:
            if ans[next_v] == 0:
                ans[next_v] = c[idx]
                idx += 1
                q.append(next_v)
    print(sum(c[1:]))
    print(" ".join(map(str, ans)))


if __name__ == '__main__':
    main()
