MOD = 10 ** 9 + 7
INF = 10 ** 10
import sys
sys.setrecursionlimit(100000000)
dy = (-1,0,1,0)
dx = (0,1,0,-1)
from collections import deque

def main():
    n = int(input())
    G = [[] for _ in range(n)]
    for _ in range(n - 1):
        a,b = map(int,input().split())
        a -= 1
        b -= 1
        G[a].append(b)
        G[b].append(a)
    
    c = list(map(int,input().split()))
    c.sort(reverse = True)
    
    color = [-1] * n
    s = deque([0])
    i = 0
    while len(s):
        v = s.pop()
        color[v] = c[i]
        for e in G[v]:
            if color[e] != -1:
                continue
            s.append(e)
        i += 1
    
    print(sum(c) - c[0])
    print(*color)
if __name__ =='__main__':
    main()   MOD = 10 ** 9 + 7 MOD 10 ** 9 + 7 10 ** 9 10 9 7 INF = 10 ** 10 INF 10 ** 10 10 10 import sys sys sys.setrecursionlimit(100000000) sys.setrecursionlimit sys setrecursionlimit 100000000 dy = (-1,0,1,0) dy (-1,0,1,0) -1 0 1 0 dx = (0,1,0,-1) dx (0,1,0,-1) 0 1 0 -1 from collections import deque def main():
    n = int(input())
    G = [[] for _ in range(n)]
    for _ in range(n - 1):
        a,b = map(int,input().split())
        a -= 1
        b -= 1
        G[a].append(b)
        G[b].append(a)
    
    c = list(map(int,input().split()))
    c.sort(reverse = True)
    
    color = [-1] * n
    s = deque([0])
    i = 0
    while len(s):
        v = s.pop()
        color[v] = c[i]
        for e in G[v]:
            if color[e] != -1:
                continue
            s.append(e)
        i += 1
    
    print(sum(c) - c[0])
    print(*color) main 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) [] G = [[] for _ in range(n)] G [[] for _ in range(n)] for _ in range(n - 1):
        a,b = map(int,input().split())
        a -= 1
        b -= 1
        G[a].append(b)
        G[b].append(a)
    
     _ 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 a -= 1 a 1 b -= 1 b 1 G[a].append(b) [a].append [a] G a append b G[b].append(a) [b].append [b] G b append a 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(reverse = True) c.sort c sort reverse = True reverse True color = [-1] * n color [-1] * n [-1] -1 n s = deque([0]) s deque([0]) deque [0] 0 i = 0 i 0 while len(s):
        v = s.pop()
        color[v] = c[i]
        for e in G[v]:
            if color[e] != -1:
                continue
            s.append(e)
        i += 1
    
     len(s) len s v = s.pop() v s.pop() s.pop s pop color[v] = c[i] color[v] color v c[i] c i for e in G[v]:
            if color[e] != -1:
                continue
            s.append(e)
         e G[v] G v if color[e] != -1:
                continue
             color[e] != -1 color[e] color e -1 continue s.append(e) s.append s append e i += 1 i 1 print(sum(c) - c[0]) print sum(c) - c[0] sum(c) sum c c[0] c 0 print(*color) print *color color if __name__ =='__main__':
    main()   __name__ =='__main__' __name__ '__main__' main() main INF = 10 ** 10 10 ** 10 INF MOD = 10 ** 9 + 7 10 ** 9 + 7 MOD dx = (0,1,0,-1) (0,1,0,-1) dx def main():
    n = int(input())
    G = [[] for _ in range(n)]
    for _ in range(n - 1):
        a,b = map(int,input().split())
        a -= 1
        b -= 1
        G[a].append(b)
        G[b].append(a)
    
    c = list(map(int,input().split()))
    c.sort(reverse = True)
    
    color = [-1] * n
    s = deque([0])
    i = 0
    while len(s):
        v = s.pop()
        color[v] = c[i]
        for e in G[v]:
            if color[e] != -1:
                continue
            s.append(e)
        i += 1
    
    print(sum(c) - c[0])
    print(*color) def main():
    n = int(input())
    G = [[] for _ in range(n)]
    for _ in range(n - 1):
        a,b = map(int,input().split())
        a -= 1
        b -= 1
        G[a].append(b)
        G[b].append(a)
    
    c = list(map(int,input().split()))
    c.sort(reverse = True)
    
    color = [-1] * n
    s = deque([0])
    i = 0
    while len(s):
        v = s.pop()
        color[v] = c[i]
        for e in G[v]:
            if color[e] != -1:
                continue
            s.append(e)
        i += 1
    
    print(sum(c) - c[0])
    print(*color) main dy = (-1,0,1,0) (-1,0,1,0) dy
MOD = 10 ** 9 + 7
INF = 10 ** 10
import sys
sys.setrecursionlimit(100000000)
dy = (-1,0,1,0)
dx = (0,1,0,-1)
from collections import deque

def main():
    n = int(input())
    G = [[] for _ in range(n)]
    for _ in range(n - 1):
        a,b = map(int,input().split())
        a -= 1
        b -= 1
        G[a].append(b)
        G[b].append(a)
    
    c = list(map(int,input().split()))
    c.sort(reverse = True)
    
    color = [-1] * n
    s = deque([0])
    i = 0
    while len(s):
        v = s.pop()
        color[v] = c[i]
        for e in G[v]:
            if color[e] != -1:
                continue
            s.append(e)
        i += 1
    
    print(sum(c) - c[0])
    print(*color)
if __name__ =='__main__':
    main()  