import bisect, collections, copy, heapq, itertools, math, string, sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = float('inf')
def I(): return int(input())
def F(): return float(input())
def SS(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LSS(): return input().split()

def resolve():
    N = I()
    G = [[] for _ in range(N)]
    for _ in range(N - 1):
        a, b = LI_()
        G[a].append(b)
        G[b].append(a)
    c = LI()
    c.sort()

    # 最大の数は使われない、2番目に大きい数は、最大の数の隣のときだけ使われる
    # 深くなるほど小さくなるように置けば良い
    # DFSで置いていく
    ans = [0] * N
    def dfs(cur):
        ans[cur] = c.pop()
        for nxt in G[cur]:
            if ans[nxt] == 0:
                dfs(nxt)

    dfs(0)

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

if __name__ == '__main__':
    resolve() import bisect, collections, copy, heapq, itertools, math, string, sys bisect collections copy heapq itertools math string sys input = lambda: sys.stdin.readline().rstrip() input lambda: sys.stdin.readline().rstrip() sys.stdin.readline().rstrip() sys.stdin.readline().rstrip sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline rstrip sys.setrecursionlimit(10**7) sys.setrecursionlimit sys setrecursionlimit 10**7 10 7 INF = float('inf') INF float('inf') float 'inf' def I(): return int(input()) I def F(): return float(input()) F def SS(): return input() SS int(x) for x in input().split() for x in input().split() x input().split() ().split () input split for x in input().split() int(x) int x def LI(): return [int(x) for x in input().split()] 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_ float(x) for x in input().split() for x in input().split() x input().split() ().split () input split for x in input().split() float(x) float x def LF(): return [float(x) for x in input().split()] LF def LSS(): return input().split() LSS def resolve():
    N = I()
    G = [[] for _ in range(N)]
    for _ in range(N - 1):
        a, b = LI_()
        G[a].append(b)
        G[b].append(a)
    c = LI()
    c.sort()

    # 最大の数は使われない、2番目に大きい数は、最大の数の隣のときだけ使われる
    # 深くなるほど小さくなるように置けば良い
    # DFSで置いていく
    ans = [0] * N
    def dfs(cur):
        ans[cur] = c.pop()
        for nxt in G[cur]:
            if ans[nxt] == 0:
                dfs(nxt)

    dfs(0)

    print(sum(ans[1:]))
    print(*ans) resolve N = I() N I() I [] 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 = LI_()
        G[a].append(b)
        G[b].append(a)
     _ range(N - 1) range N - 1 N 1 a, b = LI_() a LI_() LI_ b G[a].append(b) [a].append [a] G a append b G[b].append(a) [b].append [b] G b append a c = LI() c LI() LI c.sort() c.sort c sort ans = [0] * N ans [0] * N [0] 0 N def dfs(cur):
        ans[cur] = c.pop()
        for nxt in G[cur]:
            if ans[nxt] == 0:
                dfs(nxt)

     dfs ans[cur] = c.pop() ans[cur] ans cur c.pop() c.pop c pop for nxt in G[cur]:
            if ans[nxt] == 0:
                dfs(nxt)

     nxt G[cur] G cur if ans[nxt] == 0:
                dfs(nxt)

     ans[nxt] == 0 ans[nxt] ans nxt 0 dfs(nxt) dfs nxt cur cur dfs(0) dfs 0 print(sum(ans[1:])) print sum(ans[1:]) sum ans[1:] ans 1: 1 print(*ans) print *ans ans if __name__ == '__main__':
    resolve() __name__ == '__main__' __name__ '__main__' resolve() resolve def LI(): return [int(x) for x in input().split()] def LI(): return [int(x) for x in input().split()] LI def LI_(): return [int(x)-1 for x in input().split()] def LI_(): return [int(x)-1 for x in input().split()] LI_ INF = float('inf') float('inf') INF def F(): return float(input()) def F(): return float(input()) F def LF(): return [float(x) for x in input().split()] def LF(): return [float(x) for x in input().split()] LF def LSS(): return input().split() def LSS(): return input().split() LSS def resolve():
    N = I()
    G = [[] for _ in range(N)]
    for _ in range(N - 1):
        a, b = LI_()
        G[a].append(b)
        G[b].append(a)
    c = LI()
    c.sort()

    # 最大の数は使われない、2番目に大きい数は、最大の数の隣のときだけ使われる
    # 深くなるほど小さくなるように置けば良い
    # DFSで置いていく
    ans = [0] * N
    def dfs(cur):
        ans[cur] = c.pop()
        for nxt in G[cur]:
            if ans[nxt] == 0:
                dfs(nxt)

    dfs(0)

    print(sum(ans[1:]))
    print(*ans) def resolve():
    N = I()
    G = [[] for _ in range(N)]
    for _ in range(N - 1):
        a, b = LI_()
        G[a].append(b)
        G[b].append(a)
    c = LI()
    c.sort()

    # 最大の数は使われない、2番目に大きい数は、最大の数の隣のときだけ使われる
    # 深くなるほど小さくなるように置けば良い
    # DFSで置いていく
    ans = [0] * N
    def dfs(cur):
        ans[cur] = c.pop()
        for nxt in G[cur]:
            if ans[nxt] == 0:
                dfs(nxt)

    dfs(0)

    print(sum(ans[1:]))
    print(*ans) resolve input = lambda: sys.stdin.readline().rstrip() lambda: sys.stdin.readline().rstrip() input def SS(): return input() def SS(): return input() SS def I(): return int(input()) def I(): return int(input()) I
import bisect, collections, copy, heapq, itertools, math, string, sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = float('inf')
def I(): return int(input())
def F(): return float(input())
def SS(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LSS(): return input().split()

def resolve():
    N = I()
    G = [[] for _ in range(N)]
    for _ in range(N - 1):
        a, b = LI_()
        G[a].append(b)
        G[b].append(a)
    c = LI()
    c.sort()

    # 最大の数は使われない、2番目に大きい数は、最大の数の隣のときだけ使われる
    # 深くなるほど小さくなるように置けば良い
    # DFSで置いていく
    ans = [0] * N
    def dfs(cur):
        ans[cur] = c.pop()
        for nxt in G[cur]:
            if ans[nxt] == 0:
                dfs(nxt)

    dfs(0)

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

if __name__ == '__main__':
    resolve()
