import sys

from collections import defaultdict as dd

import heapq

input = sys.stdin.readline

sys.setrecursionlimit(10 ** 6)

N = int(eval(input()))

e = dd(list)

inc = [0] * (N + 1)

for _ in range(N - 1):

  u, v = list(map(int, input().split()))

  e[u].append(v)

  e[v].append(u)

  inc[u] += 1

  inc[v] += 1

x = 0

for i in range(1, N + 1):

  if inc[x] < inc[i]: x = i



c = list(map(int, input().split()))

c.sort()

ressum = sum(c[: -1])

vis = set()

res = [0] * (N + 1)

res[x] = c[-1]

def dfs(y):

  global vis

  global res

  global c

  res[y] = c.pop()

  for z in e[y]:

    if z in vis: continue

    vis.add(z)

    dfs(z)

vis.add(x)

dfs(x)

print(ressum)

print((*res[1: ]))
import sys

from collections import defaultdict as dd

input = sys.stdin.readline

sys.setrecursionlimit(10 ** 6)

N = int(eval(input()))

e = dd(list)

for _ in range(N - 1):

  u, v = list(map(int, input().split()))

  e[u].append(v)

  e[v].append(u)

c = list(map(int, input().split()))

c.sort()

sm = sum(c) - c[-1]

res = [0] * (N + 1)

def dfs(x):

  global res

  res[x] = c.pop()

  for y in e[x]:

    if res[y]: continue

    dfs(y)

dfs(1)

print(sm)

print((*res[1: ]))
import sys
from collections import defaultdict as dd
import heapq

input = sys.stdin.readline
sys.setrecursionlimit(10**6)
N = int(eval(input()))
e = dd(list)
inc = [0] * (N + 1)
for _ in range(N - 1):
    u, v = list(map(int, input().split()))
    e[u].append(v)
    e[v].append(u)
    inc[u] += 1
    inc[v] += 1
x = 0
for i in range(1, N + 1):
    if inc[x] < inc[i]:
        x = i
c = list(map(int, input().split()))
c.sort()
ressum = sum(c[:-1])
vis = set()
res = [0] * (N + 1)
res[x] = c[-1]


def dfs(y):
    global vis
    global res
    global c
    res[y] = c.pop()
    for z in e[y]:
        if z in vis:
            continue
        vis.add(z)
        dfs(z)


vis.add(x)
dfs(x)
print(ressum)
print((*res[1:]))

import sys
from collections import defaultdict as dd

input = sys.stdin.readline
sys.setrecursionlimit(10**6)
N = int(eval(input()))
e = dd(list)
for _ in range(N - 1):
    u, v = list(map(int, input().split()))
    e[u].append(v)
    e[v].append(u)
c = list(map(int, input().split()))
c.sort()
sm = sum(c) - c[-1]
res = [0] * (N + 1)


def dfs(x):
    global res
    res[x] = c.pop()
    for y in e[x]:
        if res[y]:
            continue
        dfs(y)


dfs(1)
print(sm)
print((*res[1:]))

-import heapq -inc = [0] * (N + 1) -    inc[u] += 1 -    inc[v] += 1 -x = 0 -for i in range(1, N + 1): -    if inc[x] < inc[i]: -        x = i -ressum = sum(c[:-1]) -vis = set() +sm = sum(c) - c[-1] -res[x] = c[-1] -def dfs(y): -    global vis +def dfs(x): -    global c -    res[y] = c.pop() -    for z in e[y]: -        if z in vis: +    res[x] = c.pop() +    for y in e[x]: +        if res[y]: -        vis.add(z) -        dfs(z) +        dfs(y) -vis.add(x) -dfs(x) -print(ressum) +dfs(1) +print(sm)