from collections import deque

N = int(input())
dic = {}
lnum = [0] * N
q = deque([])
alis = [0] * N


for i in range(N-1):

    a,b = map(int,input().split())
    a -= 1
    b -= 1

    lnum[a] += 1
    lnum[b] += 1

    if a not in dic:
        dic[a] = []

    if b not in dic:
        dic[b] = []

    dic[a].append(b)
    dic[b].append(a)

for i in range(N):

    if lnum[i] == 1:
        q.append(i)

c = list(map(int,input().split()))
c.sort()
#print (c)
for i in range(N):

    now = q.popleft()
    lnum[now] = 0

    alis[now] = c[i]
    #print (q,now,alis)

    for nex in dic[now]:

        lnum[nex] -= 1
        if lnum[nex] == 1:
            q.append(nex)

print (sum(c) - c[-1])
print (" ".join(map(str,alis))) from collections import deque N = int(input()) N int(input()) int input() input dic = {} dic {} lnum = [0] * N lnum [0] * N [0] 0 N q = deque([]) q deque([]) deque [] alis = [0] * N alis [0] * N [0] 0 N for i in range(N-1):

    a,b = map(int,input().split())
    a -= 1
    b -= 1

    lnum[a] += 1
    lnum[b] += 1

    if a not in dic:
        dic[a] = []

    if b not in dic:
        dic[b] = []

    dic[a].append(b)
    dic[b].append(a) 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 a -= 1 a 1 b -= 1 b 1 lnum[a] += 1 lnum[a] lnum a 1 lnum[b] += 1 lnum[b] lnum b 1 if a not in dic:
        dic[a] = []

     a not in dic a dic dic[a] = [] dic[a] dic a [] if b not in dic:
        dic[b] = []

     b not in dic b dic dic[b] = [] dic[b] dic b [] dic[a].append(b) [a].append [a] dic a append b dic[b].append(a) [b].append [b] dic b append a for i in range(N):

    if lnum[i] == 1:
        q.append(i) i range(N) range N if lnum[i] == 1:
        q.append(i) lnum[i] == 1 lnum[i] lnum i 1 q.append(i) q.append q append i 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 for i in range(N):

    now = q.popleft()
    lnum[now] = 0

    alis[now] = c[i]
    #print (q,now,alis)

    for nex in dic[now]:

        lnum[nex] -= 1
        if lnum[nex] == 1:
            q.append(nex) i range(N) range N now = q.popleft() now q.popleft() q.popleft q popleft lnum[now] = 0 lnum[now] lnum now 0 alis[now] = c[i] alis[now] alis now c[i] c i for nex in dic[now]:

        lnum[nex] -= 1
        if lnum[nex] == 1:
            q.append(nex) nex dic[now] dic now lnum[nex] -= 1 lnum[nex] lnum nex 1 if lnum[nex] == 1:
            q.append(nex) lnum[nex] == 1 lnum[nex] lnum nex 1 q.append(nex) q.append q append nex print (sum(c) - c[-1]) print sum(c) - c[-1] sum(c) sum c c[-1] c -1 print (" ".join(map(str,alis))) print " ".join(map(str,alis)) " ".join " " join map(str,alis) map str alis alis = [0] * N [0] * N alis N = int(input()) int(input()) N a,b = map(int,input().split()) map(int,input().split()) a a -= 1 1 a b -= 1 1 b dic = {} {} dic now = q.popleft() q.popleft() now lnum = [0] * N [0] * N lnum b = map(int,input().split()) map(int,input().split()) b q = deque([]) deque([]) q c = list(map(int,input().split())) list(map(int,input().split())) c
from collections import deque

N = int(input())
dic = {}
lnum = [0] * N
q = deque([])
alis = [0] * N


for i in range(N-1):

    a,b = map(int,input().split())
    a -= 1
    b -= 1

    lnum[a] += 1
    lnum[b] += 1

    if a not in dic:
        dic[a] = []

    if b not in dic:
        dic[b] = []

    dic[a].append(b)
    dic[b].append(a)

for i in range(N):

    if lnum[i] == 1:
        q.append(i)

c = list(map(int,input().split()))
c.sort()
#print (c)
for i in range(N):

    now = q.popleft()
    lnum[now] = 0

    alis[now] = c[i]
    #print (q,now,alis)

    for nex in dic[now]:

        lnum[nex] -= 1
        if lnum[nex] == 1:
            q.append(nex)

print (sum(c) - c[-1])
print (" ".join(map(str,alis)))
