import sys
input=sys.stdin.readline
n=int(input())
Edges=[[] for _ in range(n)]
Cnt=[0]*n
for _ in range(n-1):
    a,b=map(int,input().split())
    Edges[a-1].append(b-1)
    Edges[b-1].append(a-1)
C=list(map(int,input().split()))
C.sort()
print(sum(C)-C[-1])
Used=[False]*n
Ans=[0]*n
q=[0]
Used[0]=True
while q:
    v=q.pop()
    Ans[v]=C.pop()
    for u in Edges[v]:
        if not Used[u]:
            Used[u]=True
            q.append(u)
print(*Ans) import sys sys input=sys.stdin.readline input sys.stdin.readline sys.stdin sys stdin readline n=int(input()) n int(input()) int input() input [] for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) [] Edges=[[] for _ in range(n)] Edges [[] for _ in range(n)] Cnt=[0]*n Cnt [0]*n [0] 0 n for _ in range(n-1):
    a,b=map(int,input().split())
    Edges[a-1].append(b-1)
    Edges[b-1].append(a-1) _ 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 Edges[a-1].append(b-1) [a-1].append [a-1] Edges a-1 a 1 append b-1 b 1 Edges[b-1].append(a-1) [b-1].append [b-1] Edges 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 C.sort() C.sort C sort print(sum(C)-C[-1]) print sum(C)-C[-1] sum(C) sum C C[-1] C -1 Used=[False]*n Used [False]*n [False] False n Ans=[0]*n Ans [0]*n [0] 0 n q=[0] q [0] 0 Used[0]=True Used[0] Used 0 True while q:
    v=q.pop()
    Ans[v]=C.pop()
    for u in Edges[v]:
        if not Used[u]:
            Used[u]=True
            q.append(u) q v=q.pop() v q.pop() q.pop q pop Ans[v]=C.pop() Ans[v] Ans v C.pop() C.pop C pop for u in Edges[v]:
        if not Used[u]:
            Used[u]=True
            q.append(u) u Edges[v] Edges v if not Used[u]:
            Used[u]=True
            q.append(u) not Used[u] Used[u] Used u Used[u]=True Used[u] Used u True q.append(u) q.append q append u print(*Ans) print *Ans Ans C=list(map(int,input().split())) list(map(int,input().split())) C Edges=[[] for _ in range(n)] [[] for _ in range(n)] Edges a,b=map(int,input().split()) map(int,input().split()) a q=[0] [0] q input=sys.stdin.readline sys.stdin.readline input n=int(input()) int(input()) n Used=[False]*n [False]*n Used Cnt=[0]*n [0]*n Cnt Ans=[0]*n [0]*n Ans v=q.pop() q.pop() v b=map(int,input().split()) map(int,input().split()) b
import sys
input=sys.stdin.readline
n=int(input())
Edges=[[] for _ in range(n)]
Cnt=[0]*n
for _ in range(n-1):
    a,b=map(int,input().split())
    Edges[a-1].append(b-1)
    Edges[b-1].append(a-1)
C=list(map(int,input().split()))
C.sort()
print(sum(C)-C[-1])
Used=[False]*n
Ans=[0]*n
q=[0]
Used[0]=True
while q:
    v=q.pop()
    Ans[v]=C.pop()
    for u in Edges[v]:
        if not Used[u]:
            Used[u]=True
            q.append(u)
print(*Ans)