import sys
from collections import deque

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_s = lambda: readline().rstrip().decode('utf-8')
in_nl = lambda: list(map(int, readline().split()))
in_nl2 = lambda H: [in_nl() for _ in range(H)]
in_map = lambda: [s == ord('.') for s in readline() if s != ord('\n')]
in_map2 = lambda H: [in_map() for _ in range(H)]
in_all = lambda: map(int, read().split())


def bfs(N, v0, edge, C):

    search = [0] * N
    C = deque(C)
    search[v0] = C.popleft()
    q = deque()
    q.append(v0)

    while q:
        v = q.popleft()
        for nv in edge[v]:
            if search[nv] == 0:
                q.append(nv)
                search[nv] = C.popleft()

    return search


def main():

    N = in_n()
    ab = in_nl2(N - 1)
    edge = [[] for _ in range(N)]

    for i in range(N - 1):
        a, b = ab[i]
        a, b = a - 1, b - 1
        edge[a].append(b)
        edge[b].append(a)

    C = sorted(in_nl(), reverse=True)
    V = bfs(N, 0, edge, C)

    score = 0
    for i in range(N - 1):
        a, b = ab[i]
        a, b = a - 1, b - 1
        score += min(V[a], V[b])

    print(score)
    print(' '.join(map(str, V)))


if __name__ == '__main__':
    main() import sys sys from collections import deque read = sys.stdin.buffer.read read sys.stdin.buffer.read sys.stdin.buffer sys.stdin sys stdin buffer read readline = sys.stdin.buffer.readline readline sys.stdin.buffer.readline sys.stdin.buffer sys.stdin sys stdin buffer readline readlines = sys.stdin.buffer.readlines readlines sys.stdin.buffer.readlines sys.stdin.buffer sys.stdin sys stdin buffer readlines in_n = lambda: int(readline()) in_n lambda: int(readline()) int(readline()) int readline() readline in_nn = lambda: map(int, readline().split()) in_nn lambda: map(int, readline().split()) map(int, readline().split()) map int readline().split() ().split () readline split in_s = lambda: readline().rstrip().decode('utf-8') in_s lambda: readline().rstrip().decode('utf-8') readline().rstrip().decode('utf-8') ().rstrip().decode ().rstrip() ().rstrip () readline rstrip decode 'utf-8' in_nl = lambda: list(map(int, readline().split())) in_nl lambda: list(map(int, readline().split())) list(map(int, readline().split())) list map(int, readline().split()) map int readline().split() ().split () readline split in_nl() for _ in range(H) for _ in range(H) _ range(H) range H for _ in range(H) in_nl() in_nl in_nl2 = lambda H: [in_nl() for _ in range(H)] in_nl2 lambda H: [in_nl() for _ in range(H)] [in_nl() for _ in range(H)] H s == ord('.') for s in readline() if s != ord('\n') for s in readline() if s != ord('\n') s readline() readline s != ord('\n') s ord('\n') ord '\n' if s != ord('\n') s == ord('.') s ord('.') ord '.' in_map = lambda: [s == ord('.') for s in readline() if s != ord('\n')] in_map lambda: [s == ord('.') for s in readline() if s != ord('\n')] [s == ord('.') for s in readline() if s != ord('\n')] in_map() for _ in range(H) for _ in range(H) _ range(H) range H for _ in range(H) in_map() in_map in_map2 = lambda H: [in_map() for _ in range(H)] in_map2 lambda H: [in_map() for _ in range(H)] [in_map() for _ in range(H)] H in_all = lambda: map(int, read().split()) in_all lambda: map(int, read().split()) map(int, read().split()) map int read().split() ().split () read split def bfs(N, v0, edge, C):

    search = [0] * N
    C = deque(C)
    search[v0] = C.popleft()
    q = deque()
    q.append(v0)

    while q:
        v = q.popleft()
        for nv in edge[v]:
            if search[nv] == 0:
                q.append(nv)
                search[nv] = C.popleft()

    return search bfs search = [0] * N search [0] * N [0] 0 N C = deque(C) C deque(C) deque C search[v0] = C.popleft() search[v0] search v0 C.popleft() C.popleft C popleft q = deque() q deque() deque q.append(v0) q.append q append v0 while q:
        v = q.popleft()
        for nv in edge[v]:
            if search[nv] == 0:
                q.append(nv)
                search[nv] = C.popleft()

     q v = q.popleft() v q.popleft() q.popleft q popleft for nv in edge[v]:
            if search[nv] == 0:
                q.append(nv)
                search[nv] = C.popleft()

     nv edge[v] edge v if search[nv] == 0:
                q.append(nv)
                search[nv] = C.popleft()

     search[nv] == 0 search[nv] search nv 0 q.append(nv) q.append q append nv search[nv] = C.popleft() search[nv] search nv C.popleft() C.popleft C popleft return search search N N v0 v0 edge edge C C def main():

    N = in_n()
    ab = in_nl2(N - 1)
    edge = [[] for _ in range(N)]

    for i in range(N - 1):
        a, b = ab[i]
        a, b = a - 1, b - 1
        edge[a].append(b)
        edge[b].append(a)

    C = sorted(in_nl(), reverse=True)
    V = bfs(N, 0, edge, C)

    score = 0
    for i in range(N - 1):
        a, b = ab[i]
        a, b = a - 1, b - 1
        score += min(V[a], V[b])

    print(score)
    print(' '.join(map(str, V))) main N = in_n() N in_n() in_n ab = in_nl2(N - 1) ab in_nl2(N - 1) in_nl2 N - 1 N 1 [] 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 i in range(N - 1):
        a, b = ab[i]
        a, b = a - 1, b - 1
        edge[a].append(b)
        edge[b].append(a)

     i range(N - 1) range N - 1 N 1 a, b = ab[i] a ab[i] ab i b a, b = a - 1, b - 1 a a - 1 a 1 b b - 1 b 1 edge[a].append(b) [a].append [a] edge a append b edge[b].append(a) [b].append [b] edge b append a C = sorted(in_nl(), reverse=True) C sorted(in_nl(), reverse=True) sorted in_nl() in_nl reverse=True reverse True V = bfs(N, 0, edge, C) V bfs(N, 0, edge, C) bfs N 0 edge C score = 0 score 0 for i in range(N - 1):
        a, b = ab[i]
        a, b = a - 1, b - 1
        score += min(V[a], V[b])

     i range(N - 1) range N - 1 N 1 a, b = ab[i] a ab[i] ab i b a, b = a - 1, b - 1 a a - 1 a 1 b b - 1 b 1 score += min(V[a], V[b]) score min(V[a], V[b]) min V[a] V a V[b] V b print(score) print score print(' '.join(map(str, V))) print ' '.join(map(str, V)) ' '.join ' ' join map(str, V) map str V if __name__ == '__main__':
    main() __name__ == '__main__' __name__ '__main__' main() main read = sys.stdin.buffer.read sys.stdin.buffer.read read readline = sys.stdin.buffer.readline sys.stdin.buffer.readline readline in_all = lambda: map(int, read().split()) lambda: map(int, read().split()) in_all def main():

    N = in_n()
    ab = in_nl2(N - 1)
    edge = [[] for _ in range(N)]

    for i in range(N - 1):
        a, b = ab[i]
        a, b = a - 1, b - 1
        edge[a].append(b)
        edge[b].append(a)

    C = sorted(in_nl(), reverse=True)
    V = bfs(N, 0, edge, C)

    score = 0
    for i in range(N - 1):
        a, b = ab[i]
        a, b = a - 1, b - 1
        score += min(V[a], V[b])

    print(score)
    print(' '.join(map(str, V))) def main():

    N = in_n()
    ab = in_nl2(N - 1)
    edge = [[] for _ in range(N)]

    for i in range(N - 1):
        a, b = ab[i]
        a, b = a - 1, b - 1
        edge[a].append(b)
        edge[b].append(a)

    C = sorted(in_nl(), reverse=True)
    V = bfs(N, 0, edge, C)

    score = 0
    for i in range(N - 1):
        a, b = ab[i]
        a, b = a - 1, b - 1
        score += min(V[a], V[b])

    print(score)
    print(' '.join(map(str, V))) main in_nl2 = lambda H: [in_nl() for _ in range(H)] lambda H: [in_nl() for _ in range(H)] in_nl2 in_s = lambda: readline().rstrip().decode('utf-8') lambda: readline().rstrip().decode('utf-8') in_s in_map2 = lambda H: [in_map() for _ in range(H)] lambda H: [in_map() for _ in range(H)] in_map2 def bfs(N, v0, edge, C):

    search = [0] * N
    C = deque(C)
    search[v0] = C.popleft()
    q = deque()
    q.append(v0)

    while q:
        v = q.popleft()
        for nv in edge[v]:
            if search[nv] == 0:
                q.append(nv)
                search[nv] = C.popleft()

    return search def bfs(N, v0, edge, C):

    search = [0] * N
    C = deque(C)
    search[v0] = C.popleft()
    q = deque()
    q.append(v0)

    while q:
        v = q.popleft()
        for nv in edge[v]:
            if search[nv] == 0:
                q.append(nv)
                search[nv] = C.popleft()

    return search bfs readlines = sys.stdin.buffer.readlines sys.stdin.buffer.readlines readlines in_n = lambda: int(readline()) lambda: int(readline()) in_n in_nl = lambda: list(map(int, readline().split())) lambda: list(map(int, readline().split())) in_nl in_nn = lambda: map(int, readline().split()) lambda: map(int, readline().split()) in_nn in_map = lambda: [s == ord('.') for s in readline() if s != ord('\n')] lambda: [s == ord('.') for s in readline() if s != ord('\n')] in_map
import sys
from collections import deque

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_s = lambda: readline().rstrip().decode('utf-8')
in_nl = lambda: list(map(int, readline().split()))
in_nl2 = lambda H: [in_nl() for _ in range(H)]
in_map = lambda: [s == ord('.') for s in readline() if s != ord('\n')]
in_map2 = lambda H: [in_map() for _ in range(H)]
in_all = lambda: map(int, read().split())


def bfs(N, v0, edge, C):

    search = [0] * N
    C = deque(C)
    search[v0] = C.popleft()
    q = deque()
    q.append(v0)

    while q:
        v = q.popleft()
        for nv in edge[v]:
            if search[nv] == 0:
                q.append(nv)
                search[nv] = C.popleft()

    return search


def main():

    N = in_n()
    ab = in_nl2(N - 1)
    edge = [[] for _ in range(N)]

    for i in range(N - 1):
        a, b = ab[i]
        a, b = a - 1, b - 1
        edge[a].append(b)
        edge[b].append(a)

    C = sorted(in_nl(), reverse=True)
    V = bfs(N, 0, edge, C)

    score = 0
    for i in range(N - 1):
        a, b = ab[i]
        a, b = a - 1, b - 1
        score += min(V[a], V[b])

    print(score)
    print(' '.join(map(str, V)))


if __name__ == '__main__':
    main()
