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

MAXN = 10000
G = [[] for _ in range(MAXN)]
color = [-1] * MAXN

def dfs(i,idx,c):
    color[i] = c[idx]
    for e in G[i]:
        if color[e] != -1:
            continue
        idx = dfs(e,idx + 1,c)
    return idx

def main():
    n = int(input())
    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)

    dfs(0,0,c)
    ans = sum(c) - c[0]
    print(ans)
    print(*color[:n])

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 MAXN = 10000 MAXN 10000 [] for _ in range(MAXN) for _ in range(MAXN) _ range(MAXN) range MAXN for _ in range(MAXN) [] G = [[] for _ in range(MAXN)] G [[] for _ in range(MAXN)] color = [-1] * MAXN color [-1] * MAXN [-1] -1 MAXN def dfs(i,idx,c):
    color[i] = c[idx]
    for e in G[i]:
        if color[e] != -1:
            continue
        idx = dfs(e,idx + 1,c)
    return idx dfs color[i] = c[idx] color[i] color i c[idx] c idx for e in G[i]:
        if color[e] != -1:
            continue
        idx = dfs(e,idx + 1,c)
     e G[i] G i if color[e] != -1:
            continue
         color[e] != -1 color[e] color e -1 continue idx = dfs(e,idx + 1,c) idx dfs(e,idx + 1,c) dfs e idx + 1 idx 1 c return idx idx i i idx idx c c def main():
    n = int(input())
    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)

    dfs(0,0,c)
    ans = sum(c) - c[0]
    print(ans)
    print(*color[:n]) main n = int(input()) n int(input()) int input() input 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 dfs(0,0,c) dfs 0 0 c ans = sum(c) - c[0] ans sum(c) - c[0] sum(c) sum c c[0] c 0 print(ans) print ans print(*color[:n]) print *color[:n] color[:n] color :n n if __name__ =='__main__':
    main()   __name__ =='__main__' __name__ '__main__' main() main MOD = 10 ** 9 + 7 10 ** 9 + 7 MOD def main():
    n = int(input())
    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)

    dfs(0,0,c)
    ans = sum(c) - c[0]
    print(ans)
    print(*color[:n]) def main():
    n = int(input())
    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)

    dfs(0,0,c)
    ans = sum(c) - c[0]
    print(ans)
    print(*color[:n]) main def dfs(i,idx,c):
    color[i] = c[idx]
    for e in G[i]:
        if color[e] != -1:
            continue
        idx = dfs(e,idx + 1,c)
    return idx def dfs(i,idx,c):
    color[i] = c[idx]
    for e in G[i]:
        if color[e] != -1:
            continue
        idx = dfs(e,idx + 1,c)
    return idx dfs INF = 10 ** 10 10 ** 10 INF dx = (0,1,0,-1) (0,1,0,-1) dx G = [[] for _ in range(MAXN)] [[] for _ in range(MAXN)] G MAXN = 10000 10000 MAXN dy = (-1,0,1,0) (-1,0,1,0) dy color = [-1] * MAXN [-1] * MAXN color
MOD = 10 ** 9 + 7
INF = 10 ** 10
import sys
sys.setrecursionlimit(100000000)
dy = (-1,0,1,0)
dx = (0,1,0,-1)

MAXN = 10000
G = [[] for _ in range(MAXN)]
color = [-1] * MAXN

def dfs(i,idx,c):
    color[i] = c[idx]
    for e in G[i]:
        if color[e] != -1:
            continue
        idx = dfs(e,idx + 1,c)
    return idx

def main():
    n = int(input())
    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)

    dfs(0,0,c)
    ans = sum(c) - c[0]
    print(ans)
    print(*color[:n])

if __name__ =='__main__':
    main()  