#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,sqrt,factorial,hypot,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from fractions import gcd
from random import randint
def ceil(a,b): return (a+b-1)//b
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N=I()
    edge = [[] for _ in range(N)]
    indegree = [0]*N
    for _ in range(N-1):
        a,b = MI_()
        indegree[a] += 1
        indegree[b] += 1
        edge[a].append(b)
        edge[b].append(a)

    ans = [-1]*N
    s = []
    for i in range(N):
        if(indegree[i]==1):
            s.append(i)  
    C = sorted(LI(), reverse=True)
    ans_sum = sum(C)-C[0]
    while(s):
        v = s.pop()
        ans[v] = C.pop()
        for to in edge[v]:
            indegree[to]-=1
            if(indegree[to] == 1):
                s.append(to)

    print(ans_sum)
    print(*ans)
if __name__ == '__main__':
    main() import sys sys sys.setrecursionlimit(10**6) sys.setrecursionlimit sys setrecursionlimit 10**6 10 6 input=sys.stdin.readline input sys.stdin.readline sys.stdin sys stdin readline from math import floor,sqrt,factorial,hypot,log from heapq import heappop, heappush, heappushpop from collections import Counter,defaultdict,deque from itertools import accumulate,permutations,combinations,product,combinations_with_replacement from bisect import bisect_left,bisect_right from copy import deepcopy from fractions import gcd from random import randint def ceil(a,b): return (a+b-1)//b ceil a a b b inf=float('inf') inf float('inf') float 'inf' mod = 10**9+7 mod 10**9+7 10**9 10 9 7 def pprint(*A): 
    for a in A:     print(*a,sep='\n') pprint for a in A:     print(*a,sep=' a A *A def INT_(n): return int(n)-1 INT_ n n def MI(): return map(int,input().split()) MI def MF(): return map(float, input().split()) MF def MI_(): return map(INT_,input().split()) MI_ def LI(): return list(MI()) LI int(x) - 1 for x in input().split() for x in input().split() x input().split() ().split () input split for x in input().split() int(x) - 1 int(x) int x 1 def LI_(): return [int(x) - 1 for x in input().split()] LI_ def LF(): return list(MF()) LF I() for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) I() I def LIN(n:int): return [I() for _ in range(n)] LIN n:int n LI() for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) LI() LI def LLIN(n: int): return [LI() for _ in range(n)] LLIN n: int n LI_() for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) LI_() LI_ def LLIN_(n: int): return [LI_() for _ in range(n)] LLIN_ n: int n list(map(int, l.split() )) for l in input() for l in input() l input() input for l in input() list(map(int, l.split() )) list map(int, l.split() ) map int l.split() l.split l split def LLI(): return [list(map(int, l.split() )) for l in input()] LLI def I(): return int(input()) I def F(): return float(input()) F def ST(): return input().replace('\n', '') ST def main():
    N=I()
    edge = [[] for _ in range(N)]
    indegree = [0]*N
    for _ in range(N-1):
        a,b = MI_()
        indegree[a] += 1
        indegree[b] += 1
        edge[a].append(b)
        edge[b].append(a)

    ans = [-1]*N
    s = []
    for i in range(N):
        if(indegree[i]==1):
            s.append(i)  
    C = sorted(LI(), reverse=True)
    ans_sum = sum(C)-C[0]
    while(s):
        v = s.pop()
        ans[v] = C.pop()
        for to in edge[v]:
            indegree[to]-=1
            if(indegree[to] == 1):
                s.append(to)

    print(ans_sum)
    print(*ans) main N=I() N I() I [] for _ in range(N) for _ in range(N) _ range(N) range N for _ in range(N) [] edge = [[] for _ in range(N)] edge [[] for _ in range(N)] indegree = [0]*N indegree [0]*N [0] 0 N for _ in range(N-1):
        a,b = MI_()
        indegree[a] += 1
        indegree[b] += 1
        edge[a].append(b)
        edge[b].append(a)

     _ range(N-1) range N-1 N 1 a,b = MI_() a MI_() MI_ b indegree[a] += 1 indegree[a] indegree a 1 indegree[b] += 1 indegree[b] indegree b 1 edge[a].append(b) [a].append [a] edge a append b edge[b].append(a) [b].append [b] edge b append a ans = [-1]*N ans [-1]*N [-1] -1 N s = [] s [] for i in range(N):
        if(indegree[i]==1):
            s.append(i)  
     i range(N) range N if(indegree[i]==1):
            s.append(i)  
     indegree[i]==1 indegree[i] indegree i 1 s.append(i) s.append s append i C = sorted(LI(), reverse=True) C sorted(LI(), reverse=True) sorted LI() LI reverse=True reverse True ans_sum = sum(C)-C[0] ans_sum sum(C)-C[0] sum(C) sum C C[0] C 0 while(s):
        v = s.pop()
        ans[v] = C.pop()
        for to in edge[v]:
            indegree[to]-=1
            if(indegree[to] == 1):
                s.append(to)

     s v = s.pop() v s.pop() s.pop s pop ans[v] = C.pop() ans[v] ans v C.pop() C.pop C pop for to in edge[v]:
            indegree[to]-=1
            if(indegree[to] == 1):
                s.append(to)

     to edge[v] edge v indegree[to]-=1 indegree[to] indegree to 1 if(indegree[to] == 1):
                s.append(to)

     indegree[to] == 1 indegree[to] indegree to 1 s.append(to) s.append s append to print(ans_sum) print ans_sum print(*ans) print *ans ans if __name__ == '__main__':
    main() __name__ == '__main__' __name__ '__main__' main() main def I(): return int(input()) def I(): return int(input()) I def main():
    N=I()
    edge = [[] for _ in range(N)]
    indegree = [0]*N
    for _ in range(N-1):
        a,b = MI_()
        indegree[a] += 1
        indegree[b] += 1
        edge[a].append(b)
        edge[b].append(a)

    ans = [-1]*N
    s = []
    for i in range(N):
        if(indegree[i]==1):
            s.append(i)  
    C = sorted(LI(), reverse=True)
    ans_sum = sum(C)-C[0]
    while(s):
        v = s.pop()
        ans[v] = C.pop()
        for to in edge[v]:
            indegree[to]-=1
            if(indegree[to] == 1):
                s.append(to)

    print(ans_sum)
    print(*ans) def main():
    N=I()
    edge = [[] for _ in range(N)]
    indegree = [0]*N
    for _ in range(N-1):
        a,b = MI_()
        indegree[a] += 1
        indegree[b] += 1
        edge[a].append(b)
        edge[b].append(a)

    ans = [-1]*N
    s = []
    for i in range(N):
        if(indegree[i]==1):
            s.append(i)  
    C = sorted(LI(), reverse=True)
    ans_sum = sum(C)-C[0]
    while(s):
        v = s.pop()
        ans[v] = C.pop()
        for to in edge[v]:
            indegree[to]-=1
            if(indegree[to] == 1):
                s.append(to)

    print(ans_sum)
    print(*ans) main def LLIN(n: int): return [LI() for _ in range(n)] def LLIN(n: int): return [LI() for _ in range(n)] LLIN def MF(): return map(float, input().split()) def MF(): return map(float, input().split()) MF def LI(): return list(MI()) def LI(): return list(MI()) LI def LLIN_(n: int): return [LI_() for _ in range(n)] def LLIN_(n: int): return [LI_() for _ in range(n)] LLIN_ def LI_(): return [int(x) - 1 for x in input().split()] def LI_(): return [int(x) - 1 for x in input().split()] LI_ def MI(): return map(int,input().split()) def MI(): return map(int,input().split()) MI def ceil(a,b): return (a+b-1)//b def ceil(a,b): return (a+b-1)//b ceil def LLI(): return [list(map(int, l.split() )) for l in input()] def LLI(): return [list(map(int, l.split() )) for l in input()] LLI def pprint(*A): 
    for a in A:     print(*a,sep='\n') def pprint(*A): 
    for a in A:     print(*a,sep='\n') pprint def MI_(): return map(INT_,input().split()) def MI_(): return map(INT_,input().split()) MI_ def INT_(n): return int(n)-1 def INT_(n): return int(n)-1 INT_ inf=float('inf') float('inf') inf def LF(): return list(MF()) def LF(): return list(MF()) LF def LIN(n:int): return [I() for _ in range(n)] def LIN(n:int): return [I() for _ in range(n)] LIN def F(): return float(input()) def F(): return float(input()) F input=sys.stdin.readline sys.stdin.readline input mod = 10**9+7 10**9+7 mod def ST(): return input().replace('\n', '') def ST(): return input().replace('\n', '') ST
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,sqrt,factorial,hypot,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from fractions import gcd
from random import randint
def ceil(a,b): return (a+b-1)//b
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N=I()
    edge = [[] for _ in range(N)]
    indegree = [0]*N
    for _ in range(N-1):
        a,b = MI_()
        indegree[a] += 1
        indegree[b] += 1
        edge[a].append(b)
        edge[b].append(a)

    ans = [-1]*N
    s = []
    for i in range(N):
        if(indegree[i]==1):
            s.append(i)  
    C = sorted(LI(), reverse=True)
    ans_sum = sum(C)-C[0]
    while(s):
        v = s.pop()
        ans[v] = C.pop()
        for to in edge[v]:
            indegree[to]-=1
            if(indegree[to] == 1):
                s.append(to)

    print(ans_sum)
    print(*ans)
if __name__ == '__main__':
    main()