import sys
sys.setrecursionlimit(4100000)

N = int(input())
edges = [[] for _ in range(N)]
for _ in range(N-1):
    a, b = map(int,input().split())
    edges[a-1].append(b-1)
    edges[b-1].append(a-1)
c = list(map(int,input().split()))
L = [0] * N

c.sort(reverse = True)

def dfs(x, par, index):
    L[x] = c[index]
    for u in edges[x]:
        if u != par:
            index += 1
            index = dfs(u, x, index)
    return index

dfs(0, -1, 0)

print(sum(L[1:]))
print(*L) import sys sys sys.setrecursionlimit(4100000) sys.setrecursionlimit sys setrecursionlimit 4100000 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) [] edges = [[] for _ in range(N)] edges [[] for _ in range(N)] for _ in range(N-1):
    a, b = map(int,input().split())
    edges[a-1].append(b-1)
    edges[b-1].append(a-1) _ range(N-1) range N-1 N 1 a, b = map(int,input().split()) a map(int,input().split()) map int input().split() ().split () input split b edges[a-1].append(b-1) [a-1].append [a-1] edges a-1 a 1 append b-1 b 1 edges[b-1].append(a-1) [b-1].append [b-1] edges b-1 b 1 append a-1 a 1 c = list(map(int,input().split())) c list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split L = [0] * N L [0] * N [0] 0 N c.sort(reverse = True) c.sort c sort reverse = True reverse True def dfs(x, par, index):
    L[x] = c[index]
    for u in edges[x]:
        if u != par:
            index += 1
            index = dfs(u, x, index)
    return index dfs L[x] = c[index] L[x] L x c[index] c index for u in edges[x]:
        if u != par:
            index += 1
            index = dfs(u, x, index)
     u edges[x] edges x if u != par:
            index += 1
            index = dfs(u, x, index)
     u != par u par index += 1 index 1 index = dfs(u, x, index) index dfs(u, x, index) dfs u x index return index index x x par par index index dfs(0, -1, 0) dfs 0 -1 0 print(sum(L[1:])) print sum(L[1:]) sum L[1:] L 1: 1 print(*L) print *L L a, b = map(int,input().split()) map(int,input().split()) a b = map(int,input().split()) map(int,input().split()) b L = [0] * N [0] * N L c = list(map(int,input().split())) list(map(int,input().split())) c edges = [[] for _ in range(N)] [[] for _ in range(N)] edges N = int(input()) int(input()) N def dfs(x, par, index):
    L[x] = c[index]
    for u in edges[x]:
        if u != par:
            index += 1
            index = dfs(u, x, index)
    return index def dfs(x, par, index):
    L[x] = c[index]
    for u in edges[x]:
        if u != par:
            index += 1
            index = dfs(u, x, index)
    return index dfs
import sys
sys.setrecursionlimit(4100000)

N = int(input())
edges = [[] for _ in range(N)]
for _ in range(N-1):
    a, b = map(int,input().split())
    edges[a-1].append(b-1)
    edges[b-1].append(a-1)
c = list(map(int,input().split()))
L = [0] * N

c.sort(reverse = True)

def dfs(x, par, index):
    L[x] = c[index]
    for u in edges[x]:
        if u != par:
            index += 1
            index = dfs(u, x, index)
    return index

dfs(0, -1, 0)

print(sum(L[1:]))
print(*L)