def examD():
    N = I()
    V = [[]for _ in range(N)]
    for _ in range(N-1):
        A, B = LI()
        V[A-1].append(B-1)
        V[B-1].append(A-1)
    C = LI(); C.sort()
    e = 0
    low = 0; up = N-1
    ans = 0; W = [-1] * N
    # 各点の値
    W[e] = C[up]; up -=1
    que = deque()
    que.append(e)
    while que or low<up:
        now = que.popleft()
        if len(V[now])==1 and now!=0:
            W[now] = C[low]
            ans += W[now]
            low +=1
        elif now!=0:
            W[now] = C[up]
            up -= 1
            ans += W[now]
        for ne in V[now]:
            if W[ne] == -1:
                que.append(ne)
    print(ans)
    print(" ".join(map(str,W)))

from string import digits
import sys,copy,bisect,itertools,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

examD() def examD():
    N = I()
    V = [[]for _ in range(N)]
    for _ in range(N-1):
        A, B = LI()
        V[A-1].append(B-1)
        V[B-1].append(A-1)
    C = LI(); C.sort()
    e = 0
    low = 0; up = N-1
    ans = 0; W = [-1] * N
    # 各点の値
    W[e] = C[up]; up -=1
    que = deque()
    que.append(e)
    while que or low<up:
        now = que.popleft()
        if len(V[now])==1 and now!=0:
            W[now] = C[low]
            ans += W[now]
            low +=1
        elif now!=0:
            W[now] = C[up]
            up -= 1
            ans += W[now]
        for ne in V[now]:
            if W[ne] == -1:
                que.append(ne)
    print(ans)
    print(" ".join(map(str,W))) examD N = I() N I() I []for _ in range(N) for _ in range(N) _ range(N) range N for _ in range(N) [] V = [[]for _ in range(N)] V [[]for _ in range(N)] for _ in range(N-1):
        A, B = LI()
        V[A-1].append(B-1)
        V[B-1].append(A-1)
     _ range(N-1) range N-1 N 1 A, B = LI() A LI() LI B V[A-1].append(B-1) [A-1].append [A-1] V A-1 A 1 append B-1 B 1 V[B-1].append(A-1) [B-1].append [B-1] V B-1 B 1 append A-1 A 1 C = LI() C LI() LI C.sort() C.sort C sort e = 0 e 0 low = 0 low 0 up = N-1 up N-1 N 1 ans = 0 ans 0 W = [-1] * N W [-1] * N [-1] -1 N W[e] = C[up] W[e] W e C[up] C up up -=1 up 1 que = deque() que deque() deque que.append(e) que.append que append e while que or low<up:
        now = que.popleft()
        if len(V[now])==1 and now!=0:
            W[now] = C[low]
            ans += W[now]
            low +=1
        elif now!=0:
            W[now] = C[up]
            up -= 1
            ans += W[now]
        for ne in V[now]:
            if W[ne] == -1:
                que.append(ne)
     que or low<up que low<up low up now = que.popleft() now que.popleft() que.popleft que popleft if len(V[now])==1 and now!=0:
            W[now] = C[low]
            ans += W[now]
            low +=1
        elif now!=0:
            W[now] = C[up]
            up -= 1
            ans += W[now]
         len(V[now])==1 and now!=0 len(V[now])==1 len(V[now]) len V[now] V now 1 now!=0 now 0 W[now] = C[low] W[now] W now C[low] C low ans += W[now] ans W[now] W now low +=1 low 1 elif now!=0:
            W[now] = C[up]
            up -= 1
            ans += W[now]
         now!=0 now 0 W[now] = C[up] W[now] W now C[up] C up up -= 1 up 1 ans += W[now] ans W[now] W now for ne in V[now]:
            if W[ne] == -1:
                que.append(ne)
     ne V[now] V now if W[ne] == -1:
                que.append(ne)
     W[ne] == -1 W[ne] W ne -1 que.append(ne) que.append que append ne print(ans) print ans print(" ".join(map(str,W))) print " ".join(map(str,W)) " ".join " " join map(str,W) map str W from string import digits import sys,copy,bisect,itertools,math sys copy bisect itertools math from heapq import heappop,heappush,heapify from collections import Counter,defaultdict,deque def I(): return int(sys.stdin.readline()) I def LI(): return list(map(int,sys.stdin.readline().split())) LI def LS(): return sys.stdin.readline().split() LS def SI(): return sys.stdin.readline().strip() SI mod = 10**9 + 7 mod 10**9 + 7 10**9 10 9 7 inf = float('inf') inf float('inf') float 'inf' examD() examD mod = 10**9 + 7 10**9 + 7 mod def SI(): return sys.stdin.readline().strip() def SI(): return sys.stdin.readline().strip() SI inf = float('inf') float('inf') inf def examD():
    N = I()
    V = [[]for _ in range(N)]
    for _ in range(N-1):
        A, B = LI()
        V[A-1].append(B-1)
        V[B-1].append(A-1)
    C = LI(); C.sort()
    e = 0
    low = 0; up = N-1
    ans = 0; W = [-1] * N
    # 各点の値
    W[e] = C[up]; up -=1
    que = deque()
    que.append(e)
    while que or low<up:
        now = que.popleft()
        if len(V[now])==1 and now!=0:
            W[now] = C[low]
            ans += W[now]
            low +=1
        elif now!=0:
            W[now] = C[up]
            up -= 1
            ans += W[now]
        for ne in V[now]:
            if W[ne] == -1:
                que.append(ne)
    print(ans)
    print(" ".join(map(str,W))) def examD():
    N = I()
    V = [[]for _ in range(N)]
    for _ in range(N-1):
        A, B = LI()
        V[A-1].append(B-1)
        V[B-1].append(A-1)
    C = LI(); C.sort()
    e = 0
    low = 0; up = N-1
    ans = 0; W = [-1] * N
    # 各点の値
    W[e] = C[up]; up -=1
    que = deque()
    que.append(e)
    while que or low<up:
        now = que.popleft()
        if len(V[now])==1 and now!=0:
            W[now] = C[low]
            ans += W[now]
            low +=1
        elif now!=0:
            W[now] = C[up]
            up -= 1
            ans += W[now]
        for ne in V[now]:
            if W[ne] == -1:
                que.append(ne)
    print(ans)
    print(" ".join(map(str,W))) examD def I(): return int(sys.stdin.readline()) def I(): return int(sys.stdin.readline()) I def LI(): return list(map(int,sys.stdin.readline().split())) def LI(): return list(map(int,sys.stdin.readline().split())) LI def LS(): return sys.stdin.readline().split() def LS(): return sys.stdin.readline().split() LS
def examD():
    N = I()
    V = [[]for _ in range(N)]
    for _ in range(N-1):
        A, B = LI()
        V[A-1].append(B-1)
        V[B-1].append(A-1)
    C = LI(); C.sort()
    e = 0
    low = 0; up = N-1
    ans = 0; W = [-1] * N
    # 各点の値
    W[e] = C[up]; up -=1
    que = deque()
    que.append(e)
    while que or low<up:
        now = que.popleft()
        if len(V[now])==1 and now!=0:
            W[now] = C[low]
            ans += W[now]
            low +=1
        elif now!=0:
            W[now] = C[up]
            up -= 1
            ans += W[now]
        for ne in V[now]:
            if W[ne] == -1:
                que.append(ne)
    print(ans)
    print(" ".join(map(str,W)))

from string import digits
import sys,copy,bisect,itertools,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

examD()
