import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


from collections import defaultdict
n = int(input())
ns = defaultdict(set)
uvs = [None]*(n-1)
for i in range(n-1):
    u,v = map(int, input().split())
    u -= 1
    v -= 1
    ns[u].add(v)
    ns[v].add(u)
    uvs[i] = (u,v)
c = list(map(int, input().split()))
c.sort()
q = [0]
ans = [None]*n
i = 0
def sub(u, prev):
    global i
    for v in ns[u]:
        if prev==v:
            continue
        sub(v, u)
    ans[u] = c[i]
    i += 1
sub(0, -1)
val = 0
for u,v in uvs:
    val += min(ans[u], ans[v])
print(val)
write(" ".join(map(str, ans))) import sys sys input = lambda : sys.stdin.readline().rstrip() input lambda : sys.stdin.readline().rstrip() sys.stdin.readline().rstrip() sys.stdin.readline().rstrip sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline rstrip sys.setrecursionlimit(max(1000, 10**9)) sys.setrecursionlimit sys setrecursionlimit max(1000, 10**9) max 1000 10**9 10 9 write = lambda x: sys.stdout.write(x+"\n") write lambda x: sys.stdout.write(x+"\n") sys.stdout.write(x+"\n") sys.stdout.write sys.stdout sys stdout write x+"\n" x "\n" x from collections import defaultdict n = int(input()) n int(input()) int input() input ns = defaultdict(set) ns defaultdict(set) defaultdict set uvs = [None]*(n-1) uvs [None]*(n-1) [None] None n-1 n 1 for i in range(n-1):
    u,v = map(int, input().split())
    u -= 1
    v -= 1
    ns[u].add(v)
    ns[v].add(u)
    uvs[i] = (u,v) i range(n-1) range n-1 n 1 u,v = map(int, input().split()) u map(int, input().split()) map int input().split() ().split () input split v u -= 1 u 1 v -= 1 v 1 ns[u].add(v) [u].add [u] ns u add v ns[v].add(u) [v].add [v] ns v add u uvs[i] = (u,v) uvs[i] uvs i (u,v) u v c = list(map(int, input().split())) c list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split c.sort() c.sort c sort q = [0] q [0] 0 ans = [None]*n ans [None]*n [None] None n i = 0 i 0 def sub(u, prev):
    global i
    for v in ns[u]:
        if prev==v:
            continue
        sub(v, u)
    ans[u] = c[i]
    i += 1 sub global i for v in ns[u]:
        if prev==v:
            continue
        sub(v, u)
     v ns[u] ns u if prev==v:
            continue
         prev==v prev v continue sub(v, u) sub v u ans[u] = c[i] ans[u] ans u c[i] c i i += 1 i 1 u u prev prev sub(0, -1) sub 0 -1 val = 0 val 0 for u,v in uvs:
    val += min(ans[u], ans[v]) u v uvs val += min(ans[u], ans[v]) val min(ans[u], ans[v]) min ans[u] ans u ans[v] ans v print(val) print val write(" ".join(map(str, ans))) write " ".join(map(str, ans)) " ".join " " join map(str, ans) map str ans write = lambda x: sys.stdout.write(x+"\n") lambda x: sys.stdout.write(x+"\n") write val = 0 0 val q = [0] [0] q u -= 1 1 u i = 0 0 i v -= 1 1 v val += min(ans[u], ans[v]) min(ans[u], ans[v]) val ans = [None]*n [None]*n ans u,v = map(int, input().split()) map(int, input().split()) u def sub(u, prev):
    global i
    for v in ns[u]:
        if prev==v:
            continue
        sub(v, u)
    ans[u] = c[i]
    i += 1 def sub(u, prev):
    global i
    for v in ns[u]:
        if prev==v:
            continue
        sub(v, u)
    ans[u] = c[i]
    i += 1 sub ns = defaultdict(set) defaultdict(set) ns uvs = [None]*(n-1) [None]*(n-1) uvs c = list(map(int, input().split())) list(map(int, input().split())) c n = int(input()) int(input()) n input = lambda : sys.stdin.readline().rstrip() lambda : sys.stdin.readline().rstrip() input v = map(int, input().split()) map(int, input().split()) v
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


from collections import defaultdict
n = int(input())
ns = defaultdict(set)
uvs = [None]*(n-1)
for i in range(n-1):
    u,v = map(int, input().split())
    u -= 1
    v -= 1
    ns[u].add(v)
    ns[v].add(u)
    uvs[i] = (u,v)
c = list(map(int, input().split()))
c.sort()
q = [0]
ans = [None]*n
i = 0
def sub(u, prev):
    global i
    for v in ns[u]:
        if prev==v:
            continue
        sub(v, u)
    ans[u] = c[i]
    i += 1
sub(0, -1)
val = 0
for u,v in uvs:
    val += min(ans[u], ans[v])
print(val)
write(" ".join(map(str, ans)))