def msol19_d():
    n = int(input())
    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 = sorted(map(int, input().split()), reverse=True)

    score = sum(C[1:])
    label = [-1]*n
    from collections import deque
    que = deque([0])
    idx = 0
    while que:
        u = que.popleft()
        label[u] = C[idx]
        idx += 1
        for v in graph[u]:
            if label[v] != -1: continue
            que.append(v)
    print(score)
    print(*label, sep=' ')

if __name__ == '__main__':
    msol19_d() def msol19_d():
    n = int(input())
    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 = sorted(map(int, input().split()), reverse=True)

    score = sum(C[1:])
    label = [-1]*n
    from collections import deque
    que = deque([0])
    idx = 0
    while que:
        u = que.popleft()
        label[u] = C[idx]
        idx += 1
        for v in graph[u]:
            if label[v] != -1: continue
            que.append(v)
    print(score)
    print(*label, sep=' ') msol19_d 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) [] 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 = sorted(map(int, input().split()), reverse=True) C sorted(map(int, input().split()), reverse=True) sorted map(int, input().split()) map int input().split() ().split () input split reverse=True reverse True score = sum(C[1:]) score sum(C[1:]) sum C[1:] C 1: 1 label = [-1]*n label [-1]*n [-1] -1 n from collections import deque que = deque([0]) que deque([0]) deque [0] 0 idx = 0 idx 0 while que:
        u = que.popleft()
        label[u] = C[idx]
        idx += 1
        for v in graph[u]:
            if label[v] != -1: continue
            que.append(v)
     que u = que.popleft() u que.popleft() que.popleft que popleft label[u] = C[idx] label[u] label u C[idx] C idx idx += 1 idx 1 for v in graph[u]:
            if label[v] != -1: continue
            que.append(v)
     v graph[u] graph u if label[v] != -1: continue
             label[v] != -1 label[v] label v -1 que.append(v) que.append que append v print(score) print score print(*label, sep=' ') print *label label sep=' ' sep ' ' if __name__ == '__main__':
    msol19_d() __name__ == '__main__' __name__ '__main__' msol19_d() msol19_d def msol19_d():
    n = int(input())
    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 = sorted(map(int, input().split()), reverse=True)

    score = sum(C[1:])
    label = [-1]*n
    from collections import deque
    que = deque([0])
    idx = 0
    while que:
        u = que.popleft()
        label[u] = C[idx]
        idx += 1
        for v in graph[u]:
            if label[v] != -1: continue
            que.append(v)
    print(score)
    print(*label, sep=' ') def msol19_d():
    n = int(input())
    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 = sorted(map(int, input().split()), reverse=True)

    score = sum(C[1:])
    label = [-1]*n
    from collections import deque
    que = deque([0])
    idx = 0
    while que:
        u = que.popleft()
        label[u] = C[idx]
        idx += 1
        for v in graph[u]:
            if label[v] != -1: continue
            que.append(v)
    print(score)
    print(*label, sep=' ') msol19_d
def msol19_d():
    n = int(input())
    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 = sorted(map(int, input().split()), reverse=True)

    score = sum(C[1:])
    label = [-1]*n
    from collections import deque
    que = deque([0])
    idx = 0
    while que:
        u = que.popleft()
        label[u] = C[idx]
        idx += 1
        for v in graph[u]:
            if label[v] != -1: continue
            que.append(v)
    print(score)
    print(*label, sep=' ')

if __name__ == '__main__':
    msol19_d()