import sys
input = sys.stdin.readline

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N=I()
    adj=[[]for _ in range(N)]
    for i in range(N-1):
        a,b=MI()
        a-=1
        b-=1
        adj[a].append(b)
        adj[b].append(a)
    c=LI()
    c.sort(reverse=True)    
    
    import queue
    q=queue.Queue()
    q.put(0)
    ans=[0]*N
    ans[0]=c[0]
    now=0
    
    while not q.empty():
        v=q.get()
        for nv in adj[v]:
            if ans[nv]==0:
                now+=1
                ans[nv]=c[now]
                q.put(nv)
                
    S=sum(c)-c[0]
    print(S)
    print(' '.join(map(str, ans)))
        

    

main() import sys sys input = sys.stdin.readline input sys.stdin.readline sys.stdin sys stdin readline def I(): return int(input()) I def MI(): return map(int, input().split()) MI def LI(): return list(map(int, input().split())) LI def main():
    mod=10**9+7
    N=I()
    adj=[[]for _ in range(N)]
    for i in range(N-1):
        a,b=MI()
        a-=1
        b-=1
        adj[a].append(b)
        adj[b].append(a)
    c=LI()
    c.sort(reverse=True)    
    
    import queue
    q=queue.Queue()
    q.put(0)
    ans=[0]*N
    ans[0]=c[0]
    now=0
    
    while not q.empty():
        v=q.get()
        for nv in adj[v]:
            if ans[nv]==0:
                now+=1
                ans[nv]=c[now]
                q.put(nv)
                
    S=sum(c)-c[0]
    print(S)
    print(' '.join(map(str, ans)))
        

     main mod=10**9+7 mod 10**9+7 10**9 10 9 7 N=I() N I() I []for _ in range(N) for _ in range(N) _ range(N) range N for _ in range(N) [] adj=[[]for _ in range(N)] adj [[]for _ in range(N)] for i in range(N-1):
        a,b=MI()
        a-=1
        b-=1
        adj[a].append(b)
        adj[b].append(a)
     i range(N-1) range N-1 N 1 a,b=MI() a MI() MI b a-=1 a 1 b-=1 b 1 adj[a].append(b) [a].append [a] adj a append b adj[b].append(a) [b].append [b] adj b append a c=LI() c LI() LI c.sort(reverse=True) c.sort c sort reverse=True reverse True import queue queue q=queue.Queue() q queue.Queue() queue.Queue queue Queue q.put(0) q.put q put 0 ans=[0]*N ans [0]*N [0] 0 N ans[0]=c[0] ans[0] ans 0 c[0] c 0 now=0 now 0 while not q.empty():
        v=q.get()
        for nv in adj[v]:
            if ans[nv]==0:
                now+=1
                ans[nv]=c[now]
                q.put(nv)
                
     not q.empty() q.empty() q.empty q empty v=q.get() v q.get() q.get q get for nv in adj[v]:
            if ans[nv]==0:
                now+=1
                ans[nv]=c[now]
                q.put(nv)
                
     nv adj[v] adj v if ans[nv]==0:
                now+=1
                ans[nv]=c[now]
                q.put(nv)
                
     ans[nv]==0 ans[nv] ans nv 0 now+=1 now 1 ans[nv]=c[now] ans[nv] ans nv c[now] c now q.put(nv) q.put q put nv S=sum(c)-c[0] S sum(c)-c[0] sum(c) sum c c[0] c 0 print(S) print S print(' '.join(map(str, ans))) print ' '.join(map(str, ans)) ' '.join ' ' join map(str, ans) map str ans main() main def LI(): return list(map(int, input().split())) def LI(): return list(map(int, input().split())) LI def I(): return int(input()) def I(): return int(input()) I def MI(): return map(int, input().split()) def MI(): return map(int, input().split()) MI def main():
    mod=10**9+7
    N=I()
    adj=[[]for _ in range(N)]
    for i in range(N-1):
        a,b=MI()
        a-=1
        b-=1
        adj[a].append(b)
        adj[b].append(a)
    c=LI()
    c.sort(reverse=True)    
    
    import queue
    q=queue.Queue()
    q.put(0)
    ans=[0]*N
    ans[0]=c[0]
    now=0
    
    while not q.empty():
        v=q.get()
        for nv in adj[v]:
            if ans[nv]==0:
                now+=1
                ans[nv]=c[now]
                q.put(nv)
                
    S=sum(c)-c[0]
    print(S)
    print(' '.join(map(str, ans)))
        

     def main():
    mod=10**9+7
    N=I()
    adj=[[]for _ in range(N)]
    for i in range(N-1):
        a,b=MI()
        a-=1
        b-=1
        adj[a].append(b)
        adj[b].append(a)
    c=LI()
    c.sort(reverse=True)    
    
    import queue
    q=queue.Queue()
    q.put(0)
    ans=[0]*N
    ans[0]=c[0]
    now=0
    
    while not q.empty():
        v=q.get()
        for nv in adj[v]:
            if ans[nv]==0:
                now+=1
                ans[nv]=c[now]
                q.put(nv)
                
    S=sum(c)-c[0]
    print(S)
    print(' '.join(map(str, ans)))
        

     main input = sys.stdin.readline sys.stdin.readline input
import sys
input = sys.stdin.readline

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N=I()
    adj=[[]for _ in range(N)]
    for i in range(N-1):
        a,b=MI()
        a-=1
        b-=1
        adj[a].append(b)
        adj[b].append(a)
    c=LI()
    c.sort(reverse=True)    
    
    import queue
    q=queue.Queue()
    q.put(0)
    ans=[0]*N
    ans[0]=c[0]
    now=0
    
    while not q.empty():
        v=q.get()
        for nv in adj[v]:
            if ans[nv]==0:
                now+=1
                ans[nv]=c[now]
                q.put(nv)
                
    S=sum(c)-c[0]
    print(S)
    print(' '.join(map(str, ans)))
        

    

main()
