import sys
input = sys.stdin.readline
n=int(input())
edge=[set() for i in range(n+1)]
for i in range(n-1):
    a,b=[int(j) for j in input().split()]
    edge[a].add(b)
    edge[b].add(a)

c=[int(j) for j in input().split()]
d=[len(i) for i in edge]

d1=[i for i,x in enumerate(d) if x==1]

ans=[None]*(n+1)
c.sort()
c.reverse()
m=sum(c)-c[0]

while d1:
    v=d1.pop()
    if not d1:
        ans[v]=c.pop()
        break
    w=edge[v].pop()
    edge[w].remove(v)
    d[w]-=1
    if d[w]==1:
        d1.append(w)
    ans[v]=c.pop()

print(m)
print(*ans[1:])
 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 set() for i in range(n+1) for i in range(n+1) i range(n+1) range n+1 n 1 for i in range(n+1) set() set edge=[set() for i in range(n+1)] edge [set() for i in range(n+1)] for i in range(n-1):
    a,b=[int(j) for j in input().split()]
    edge[a].add(b)
    edge[b].add(a) i range(n-1) range n-1 n 1 int(j) for j in input().split() for j in input().split() j input().split() ().split () input split for j in input().split() int(j) int j a,b=[int(j) for j in input().split()] a [int(j) for j in input().split()] b edge[a].add(b) [a].add [a] edge a add b edge[b].add(a) [b].add [b] edge b add a int(j) for j in input().split() for j in input().split() j input().split() ().split () input split for j in input().split() int(j) int j c=[int(j) for j in input().split()] c [int(j) for j in input().split()] len(i) for i in edge for i in edge i edge for i in edge len(i) len i d=[len(i) for i in edge] d [len(i) for i in edge] i for i,x in enumerate(d) if x==1 for i,x in enumerate(d) if x==1 i x enumerate(d) enumerate d x==1 x 1 if x==1 i d1=[i for i,x in enumerate(d) if x==1] d1 [i for i,x in enumerate(d) if x==1] ans=[None]*(n+1) ans [None]*(n+1) [None] None n+1 n 1 c.sort() c.sort c sort c.reverse() c.reverse c reverse m=sum(c)-c[0] m sum(c)-c[0] sum(c) sum c c[0] c 0 while d1:
    v=d1.pop()
    if not d1:
        ans[v]=c.pop()
        break
    w=edge[v].pop()
    edge[w].remove(v)
    d[w]-=1
    if d[w]==1:
        d1.append(w)
    ans[v]=c.pop() d1 v=d1.pop() v d1.pop() d1.pop d1 pop if not d1:
        ans[v]=c.pop()
        break
     not d1 d1 ans[v]=c.pop() ans[v] ans v c.pop() c.pop c pop break w=edge[v].pop() w edge[v].pop() [v].pop [v] edge v pop edge[w].remove(v) [w].remove [w] edge w remove v d[w]-=1 d[w] d w 1 if d[w]==1:
        d1.append(w)
     d[w]==1 d[w] d w 1 d1.append(w) d1.append d1 append w ans[v]=c.pop() ans[v] ans v c.pop() c.pop c pop print(m) print m print(*ans[1:]) print *ans[1:] ans[1:] ans 1: 1 input = sys.stdin.readline sys.stdin.readline input d=[len(i) for i in edge] [len(i) for i in edge] d ans=[None]*(n+1) [None]*(n+1) ans edge=[set() for i in range(n+1)] [set() for i in range(n+1)] edge b=[int(j) for j in input().split()] [int(j) for j in input().split()] b v=d1.pop() d1.pop() v m=sum(c)-c[0] sum(c)-c[0] m w=edge[v].pop() edge[v].pop() w a,b=[int(j) for j in input().split()] [int(j) for j in input().split()] a d1=[i for i,x in enumerate(d) if x==1] [i for i,x in enumerate(d) if x==1] d1 c=[int(j) for j in input().split()] [int(j) for j in input().split()] c n=int(input()) int(input()) n
import sys
input = sys.stdin.readline
n=int(input())
edge=[set() for i in range(n+1)]
for i in range(n-1):
    a,b=[int(j) for j in input().split()]
    edge[a].add(b)
    edge[b].add(a)

c=[int(j) for j in input().split()]
d=[len(i) for i in edge]

d1=[i for i,x in enumerate(d) if x==1]

ans=[None]*(n+1)
c.sort()
c.reverse()
m=sum(c)-c[0]

while d1:
    v=d1.pop()
    if not d1:
        ans[v]=c.pop()
        break
    w=edge[v].pop()
    edge[w].remove(v)
    d[w]-=1
    if d[w]==1:
        d1.append(w)
    ans[v]=c.pop()

print(m)
print(*ans[1:])

