from collections import defaultdict
from sys import setrecursionlimit, getrecursionlimit

setrecursionlimit(100*getrecursionlimit())

N = int(input())
g = defaultdict(list)
K = []
for i in range(N-1):
    a, b = map(int, input().split())
    g[a].append(b)
    g[b].append(a)
    K.append((a,b))

C = sorted([int(i) for i in input().split()], reverse=True)

a = {i:0 for i in range(1, N+1)}

visited = defaultdict(bool)
ct = 0
def dfs(v):
    global ct
    visited[v] = True
    a[v] = C[ct]
    ct += 1
    for c in g[v]:
        if not visited[c]:
            dfs(c)

dfs(1)

ans1 = 0
ans2 = ' '.join([str(b) for b in a.values()])
for b, c in K:
    ans1 += min(a[b], a[c])

print(ans1)
print(ans2) from collections import defaultdict from sys import setrecursionlimit, getrecursionlimit setrecursionlimit(100*getrecursionlimit()) setrecursionlimit 100*getrecursionlimit() 100 getrecursionlimit() getrecursionlimit N = int(input()) N int(input()) int input() input g = defaultdict(list) g defaultdict(list) defaultdict list K = [] K [] for i in range(N-1):
    a, b = map(int, input().split())
    g[a].append(b)
    g[b].append(a)
    K.append((a,b)) i 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 g[a].append(b) [a].append [a] g a append b g[b].append(a) [b].append [b] g b append a K.append((a,b)) K.append K append (a,b) a b int(i) for i in input().split() for i in input().split() i input().split() ().split () input split for i in input().split() int(i) int i C = sorted([int(i) for i in input().split()], reverse=True) C sorted([int(i) for i in input().split()], reverse=True) sorted [int(i) for i in input().split()] reverse=True reverse True i:0 for i in range(1, N+1) for i in range(1, N+1) i range(1, N+1) range 1 N+1 N 1 for i in range(1, N+1) i 0 a = {i:0 for i in range(1, N+1)} a {i:0 for i in range(1, N+1)} visited = defaultdict(bool) visited defaultdict(bool) defaultdict bool ct = 0 ct 0 def dfs(v):
    global ct
    visited[v] = True
    a[v] = C[ct]
    ct += 1
    for c in g[v]:
        if not visited[c]:
            dfs(c) dfs global ct visited[v] = True visited[v] visited v True a[v] = C[ct] a[v] a v C[ct] C ct ct += 1 ct 1 for c in g[v]:
        if not visited[c]:
            dfs(c) c g[v] g v if not visited[c]:
            dfs(c) not visited[c] visited[c] visited c dfs(c) dfs c v v dfs(1) dfs 1 ans1 = 0 ans1 0 str(b) for b in a.values() for b in a.values() b a.values() a.values a values for b in a.values() str(b) str b ans2 = ' '.join([str(b) for b in a.values()]) ans2 ' '.join([str(b) for b in a.values()]) ' '.join ' ' join [str(b) for b in a.values()] for b, c in K:
    ans1 += min(a[b], a[c]) b c K ans1 += min(a[b], a[c]) ans1 min(a[b], a[c]) min a[b] a b a[c] a c print(ans1) print ans1 print(ans2) print ans2 ct = 0 0 ct N = int(input()) int(input()) N a, b = map(int, input().split()) map(int, input().split()) a def dfs(v):
    global ct
    visited[v] = True
    a[v] = C[ct]
    ct += 1
    for c in g[v]:
        if not visited[c]:
            dfs(c) def dfs(v):
    global ct
    visited[v] = True
    a[v] = C[ct]
    ct += 1
    for c in g[v]:
        if not visited[c]:
            dfs(c) dfs C = sorted([int(i) for i in input().split()], reverse=True) sorted([int(i) for i in input().split()], reverse=True) C g = defaultdict(list) defaultdict(list) g visited = defaultdict(bool) defaultdict(bool) visited ans1 += min(a[b], a[c]) min(a[b], a[c]) ans1 K = [] [] K ans2 = ' '.join([str(b) for b in a.values()]) ' '.join([str(b) for b in a.values()]) ans2 b = map(int, input().split()) map(int, input().split()) b a = {i:0 for i in range(1, N+1)} {i:0 for i in range(1, N+1)} a ans1 = 0 0 ans1
from collections import defaultdict
from sys import setrecursionlimit, getrecursionlimit

setrecursionlimit(100*getrecursionlimit())

N = int(input())
g = defaultdict(list)
K = []
for i in range(N-1):
    a, b = map(int, input().split())
    g[a].append(b)
    g[b].append(a)
    K.append((a,b))

C = sorted([int(i) for i in input().split()], reverse=True)

a = {i:0 for i in range(1, N+1)}

visited = defaultdict(bool)
ct = 0
def dfs(v):
    global ct
    visited[v] = True
    a[v] = C[ct]
    ct += 1
    for c in g[v]:
        if not visited[c]:
            dfs(c)

dfs(1)

ans1 = 0
ans2 = ' '.join([str(b) for b in a.values()])
for b, c in K:
    ans1 += min(a[b], a[c])

print(ans1)
print(ans2)