import sys
from collections import deque

def solve():
    input = sys.stdin.readline
    N = int(input())
    Edge = [[] for _ in range(N)]
    for _ in range(N-1):
        a, b = map(int, input().split())
        Edge[a-1].append(b-1)
        Edge[b-1].append(a-1)
    C = [int(c) for c in input().split()]
    C.sort(reverse  = True)
    Color = [None] * N
    Color[0] = C[0]
    q = deque()
    for e in Edge[0]:
        q.append((e, 0))
    ans = 0
    for i in range(1, N):
        nowN, preN = q.popleft()
        ans += C[i]
        Color[nowN] = C[i]
        for e in Edge[nowN]:
            if e != preN: q.append((e, nowN))
    print(ans)
    print(" ".join(map(str, Color)))

    return 0

if __name__ == "__main__":
    solve() import sys sys from collections import deque def solve():
    input = sys.stdin.readline
    N = int(input())
    Edge = [[] for _ in range(N)]
    for _ in range(N-1):
        a, b = map(int, input().split())
        Edge[a-1].append(b-1)
        Edge[b-1].append(a-1)
    C = [int(c) for c in input().split()]
    C.sort(reverse  = True)
    Color = [None] * N
    Color[0] = C[0]
    q = deque()
    for e in Edge[0]:
        q.append((e, 0))
    ans = 0
    for i in range(1, N):
        nowN, preN = q.popleft()
        ans += C[i]
        Color[nowN] = C[i]
        for e in Edge[nowN]:
            if e != preN: q.append((e, nowN))
    print(ans)
    print(" ".join(map(str, Color)))

    return 0 solve input = sys.stdin.readline input sys.stdin.readline sys.stdin sys stdin readline 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) [] Edge = [[] for _ in range(N)] Edge [[] for _ in range(N)] for _ in range(N-1):
        a, b = map(int, input().split())
        Edge[a-1].append(b-1)
        Edge[b-1].append(a-1)
     _ 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 Edge[a-1].append(b-1) [a-1].append [a-1] Edge a-1 a 1 append b-1 b 1 Edge[b-1].append(a-1) [b-1].append [b-1] Edge b-1 b 1 append a-1 a 1 int(c) for c in input().split() for c in input().split() c input().split() ().split () input split for c in input().split() int(c) int c C = [int(c) for c in input().split()] C [int(c) for c in input().split()] C.sort(reverse  = True) C.sort C sort reverse  = True reverse True Color = [None] * N Color [None] * N [None] None N Color[0] = C[0] Color[0] Color 0 C[0] C 0 q = deque() q deque() deque for e in Edge[0]:
        q.append((e, 0))
     e Edge[0] Edge 0 q.append((e, 0)) q.append q append (e, 0) e 0 ans = 0 ans 0 for i in range(1, N):
        nowN, preN = q.popleft()
        ans += C[i]
        Color[nowN] = C[i]
        for e in Edge[nowN]:
            if e != preN: q.append((e, nowN))
     i range(1, N) range 1 N nowN, preN = q.popleft() nowN q.popleft() q.popleft q popleft preN ans += C[i] ans C[i] C i Color[nowN] = C[i] Color[nowN] Color nowN C[i] C i for e in Edge[nowN]:
            if e != preN: q.append((e, nowN))
     e Edge[nowN] Edge nowN if e != preN: q.append((e, nowN))
     e != preN e preN print(ans) print ans print(" ".join(map(str, Color))) print " ".join(map(str, Color)) " ".join " " join map(str, Color) map str Color return 0 0 if __name__ == "__main__":
    solve() __name__ == "__main__" __name__ "__main__" solve() solve def solve():
    input = sys.stdin.readline
    N = int(input())
    Edge = [[] for _ in range(N)]
    for _ in range(N-1):
        a, b = map(int, input().split())
        Edge[a-1].append(b-1)
        Edge[b-1].append(a-1)
    C = [int(c) for c in input().split()]
    C.sort(reverse  = True)
    Color = [None] * N
    Color[0] = C[0]
    q = deque()
    for e in Edge[0]:
        q.append((e, 0))
    ans = 0
    for i in range(1, N):
        nowN, preN = q.popleft()
        ans += C[i]
        Color[nowN] = C[i]
        for e in Edge[nowN]:
            if e != preN: q.append((e, nowN))
    print(ans)
    print(" ".join(map(str, Color)))

    return 0 def solve():
    input = sys.stdin.readline
    N = int(input())
    Edge = [[] for _ in range(N)]
    for _ in range(N-1):
        a, b = map(int, input().split())
        Edge[a-1].append(b-1)
        Edge[b-1].append(a-1)
    C = [int(c) for c in input().split()]
    C.sort(reverse  = True)
    Color = [None] * N
    Color[0] = C[0]
    q = deque()
    for e in Edge[0]:
        q.append((e, 0))
    ans = 0
    for i in range(1, N):
        nowN, preN = q.popleft()
        ans += C[i]
        Color[nowN] = C[i]
        for e in Edge[nowN]:
            if e != preN: q.append((e, nowN))
    print(ans)
    print(" ".join(map(str, Color)))

    return 0 solve
import sys
from collections import deque

def solve():
    input = sys.stdin.readline
    N = int(input())
    Edge = [[] for _ in range(N)]
    for _ in range(N-1):
        a, b = map(int, input().split())
        Edge[a-1].append(b-1)
        Edge[b-1].append(a-1)
    C = [int(c) for c in input().split()]
    C.sort(reverse  = True)
    Color = [None] * N
    Color[0] = C[0]
    q = deque()
    for e in Edge[0]:
        q.append((e, 0))
    ans = 0
    for i in range(1, N):
        nowN, preN = q.popleft()
        ans += C[i]
        Color[nowN] = C[i]
        for e in Edge[nowN]:
            if e != preN: q.append((e, nowN))
    print(ans)
    print(" ".join(map(str, Color)))

    return 0

if __name__ == "__main__":
    solve()