import sys
from collections import deque
from collections import defaultdict
import math
sys.setrecursionlimit(20000000)
input = sys.stdin.readline
 
n = int(input())
g = [[]for i in range(n)]
for i in range(n-1):
        a,b=map(int,input().split())
        g[a-1].append(b-1)
        g[b-1].append(a-1)
c = list(map(int,input().split()))
kyo = [float("inf")] * n
def dfs(x,y,z):
  kyo[x] = y
  for i in g[x]:
    if i == z:
            continue
    dfs(i,y+1,x)
dfs(0,0,-1)
kyo2 = []
for i in range(n):
        kyo2.append([kyo[i],i])
ans = [0]*n
kyo2.sort()
kyo2 = kyo2[::-1]
c.sort()
for i in range(n):
        ans[kyo2[i][1]] = c[i]
print(sum(c)-c[-1])
print(*ans) import sys sys from collections import deque from collections import defaultdict import math math sys.setrecursionlimit(20000000) sys.setrecursionlimit sys setrecursionlimit 20000000 input = sys.stdin.readline input sys.stdin.readline sys.stdin sys stdin readline n = int(input()) n int(input()) int input() input []for i in range(n) for i in range(n) i range(n) range n for i in range(n) [] g = [[]for i in range(n)] g [[]for i in range(n)] for i in range(n-1):
        a,b=map(int,input().split())
        g[a-1].append(b-1)
        g[b-1].append(a-1) 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-1].append(b-1) [a-1].append [a-1] g a-1 a 1 append b-1 b 1 g[b-1].append(a-1) [b-1].append [b-1] g 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 kyo = [float("inf")] * n kyo [float("inf")] * n [float("inf")] float("inf") float "inf" n def dfs(x,y,z):
  kyo[x] = y
  for i in g[x]:
    if i == z:
            continue
    dfs(i,y+1,x) dfs kyo[x] = y kyo[x] kyo x y for i in g[x]:
    if i == z:
            continue
    dfs(i,y+1,x) i g[x] g x if i == z:
            continue
     i == z i z continue dfs(i,y+1,x) dfs i y+1 y 1 x x x y y z z dfs(0,0,-1) dfs 0 0 -1 kyo2 = [] kyo2 [] for i in range(n):
        kyo2.append([kyo[i],i]) i range(n) range n kyo2.append([kyo[i],i]) kyo2.append kyo2 append [kyo[i],i] kyo[i] kyo i i ans = [0]*n ans [0]*n [0] 0 n kyo2.sort() kyo2.sort kyo2 sort kyo2 = kyo2[::-1] kyo2 kyo2[::-1] kyo2 ::-1 -1 c.sort() c.sort c sort for i in range(n):
        ans[kyo2[i][1]] = c[i] i range(n) range n ans[kyo2[i][1]] = c[i] ans[kyo2[i][1]] ans kyo2[i][1] [i] kyo2 i 1 c[i] c i print(sum(c)-c[-1]) print sum(c)-c[-1] sum(c) sum c c[-1] c -1 print(*ans) print *ans ans ans = [0]*n [0]*n ans a,b=map(int,input().split()) map(int,input().split()) a def dfs(x,y,z):
  kyo[x] = y
  for i in g[x]:
    if i == z:
            continue
    dfs(i,y+1,x) def dfs(x,y,z):
  kyo[x] = y
  for i in g[x]:
    if i == z:
            continue
    dfs(i,y+1,x) dfs kyo2 = kyo2[::-1] kyo2[::-1] kyo2 input = sys.stdin.readline sys.stdin.readline input kyo2 = [] [] kyo2 n = int(input()) int(input()) n c = list(map(int,input().split())) list(map(int,input().split())) c g = [[]for i in range(n)] [[]for i in range(n)] g b=map(int,input().split()) map(int,input().split()) b kyo = [float("inf")] * n [float("inf")] * n kyo
import sys
from collections import deque
from collections import defaultdict
import math
sys.setrecursionlimit(20000000)
input = sys.stdin.readline
 
n = int(input())
g = [[]for i in range(n)]
for i in range(n-1):
        a,b=map(int,input().split())
        g[a-1].append(b-1)
        g[b-1].append(a-1)
c = list(map(int,input().split()))
kyo = [float("inf")] * n
def dfs(x,y,z):
  kyo[x] = y
  for i in g[x]:
    if i == z:
            continue
    dfs(i,y+1,x)
dfs(0,0,-1)
kyo2 = []
for i in range(n):
        kyo2.append([kyo[i],i])
ans = [0]*n
kyo2.sort()
kyo2 = kyo2[::-1]
c.sort()
for i in range(n):
        ans[kyo2[i][1]] = c[i]
print(sum(c)-c[-1])
print(*ans)