import sys
sys.setrecursionlimit(10**7)
n=int(input())

g=[[] for _ in range(n+1)]
v=[0 for _ in range(n+1)]
for i in range(n-1):
    a,b=map(int,input().split())
    g[a].append(b)
    g[b].append(a)
    
c=sorted([int(i) for i in input().split()])[::-1]

def bfs(x):
    p=[]
    q=[x]
    while q:
        y=q.pop()
        v[y]=1
        p.append(y)
        for nb in g[y]:
            if v[nb]!=1:
                q.append(nb)
    return p

l=bfs(1)
ans=[0]*(n)
for i in range(n):
    ans[l[i]-1]=c[i]

print(sum(c[1:]))
print(*ans) import sys sys sys.setrecursionlimit(10**7) sys.setrecursionlimit sys setrecursionlimit 10**7 10 7 n=int(input()) n int(input()) int input() input [] for _ in range(n+1) for _ in range(n+1) _ range(n+1) range n+1 n 1 for _ in range(n+1) [] g=[[] for _ in range(n+1)] g [[] for _ in range(n+1)] 0 for _ in range(n+1) for _ in range(n+1) _ range(n+1) range n+1 n 1 for _ in range(n+1) 0 v=[0 for _ in range(n+1)] v [0 for _ in range(n+1)] for i in range(n-1):
    a,b=map(int,input().split())
    g[a].append(b)
    g[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 g[a].append(b) [a].append [a] g a append b g[b].append(a) [b].append [b] g b append a int(i) for i in input().split() for i in input().split() i input().split() ().split () input split for i in input().split() int(i) int i c=sorted([int(i) for i in input().split()])[::-1] c sorted([int(i) for i in input().split()])[::-1] ([int(i) for i in input().split()]) sorted [int(i) for i in input().split()] ::-1 -1 def bfs(x):
    p=[]
    q=[x]
    while q:
        y=q.pop()
        v[y]=1
        p.append(y)
        for nb in g[y]:
            if v[nb]!=1:
                q.append(nb)
    return p bfs p=[] p [] q=[x] q [x] x while q:
        y=q.pop()
        v[y]=1
        p.append(y)
        for nb in g[y]:
            if v[nb]!=1:
                q.append(nb)
     q y=q.pop() y q.pop() q.pop q pop v[y]=1 v[y] v y 1 p.append(y) p.append p append y for nb in g[y]:
            if v[nb]!=1:
                q.append(nb)
     nb g[y] g y if v[nb]!=1:
                q.append(nb)
     v[nb]!=1 v[nb] v nb 1 q.append(nb) q.append q append nb return p p x x l=bfs(1) l bfs(1) bfs 1 ans=[0]*(n) ans [0]*(n) [0] 0 n for i in range(n):
    ans[l[i]-1]=c[i] i range(n) range n ans[l[i]-1]=c[i] ans[l[i]-1] ans l[i]-1 l[i] l i 1 c[i] c i print(sum(c[1:])) print sum(c[1:]) sum c[1:] c 1: 1 print(*ans) print *ans ans b=map(int,input().split()) map(int,input().split()) b def bfs(x):
    p=[]
    q=[x]
    while q:
        y=q.pop()
        v[y]=1
        p.append(y)
        for nb in g[y]:
            if v[nb]!=1:
                q.append(nb)
    return p def bfs(x):
    p=[]
    q=[x]
    while q:
        y=q.pop()
        v[y]=1
        p.append(y)
        for nb in g[y]:
            if v[nb]!=1:
                q.append(nb)
    return p bfs g=[[] for _ in range(n+1)] [[] for _ in range(n+1)] g v=[0 for _ in range(n+1)] [0 for _ in range(n+1)] v a,b=map(int,input().split()) map(int,input().split()) a c=sorted([int(i) for i in input().split()])[::-1] sorted([int(i) for i in input().split()])[::-1] c ans=[0]*(n) [0]*(n) ans n=int(input()) int(input()) n l=bfs(1) bfs(1) l
import sys
sys.setrecursionlimit(10**7)
n=int(input())

g=[[] for _ in range(n+1)]
v=[0 for _ in range(n+1)]
for i in range(n-1):
    a,b=map(int,input().split())
    g[a].append(b)
    g[b].append(a)
    
c=sorted([int(i) for i in input().split()])[::-1]

def bfs(x):
    p=[]
    q=[x]
    while q:
        y=q.pop()
        v[y]=1
        p.append(y)
        for nb in g[y]:
            if v[nb]!=1:
                q.append(nb)
    return p

l=bfs(1)
ans=[0]*(n)
for i in range(n):
    ans[l[i]-1]=c[i]

print(sum(c[1:]))
print(*ans)
