N = int(input())

es = [[] for idx in range(N)]

# es[idx]:=[隣接リスト]



for _ in range(N - 1):

    a, b = map(int, input().split())

    a -= 1

    b -= 1

    es[a].append(b)

    es[b].append(a)



h = [len(es[i]) for i in range(N)]

# 次数



c = list(map(int, input().split()))

c.sort()

# スコアが小さい順にソート



v = [-1] * N

cur = 0

ans = 0

for _ in range(N - 1):

    for target in range(N):

        # print(target, h, v, ans)

        if h[target] == 1:

            h[target] = 0

            v[target] = c[cur]

            ans += c[cur]

            cur += 1

            for nv in es[target]:

                h[nv] -= 1

            break

for target in range(N):

    if v[target] == -1:

        v[target] = c[cur]

print(ans)

print(*v, sep='\n')

def main():

    import sys

    sys.setrecursionlimit(10 ** 7)



    def dfs(v):

        nonlocal curr



        num[v] = 0  # 0: visited, but not written yet



        for u in g[v]:

            if num[u] >= 0:

                continue

            dfs(u)

        num[v] = c[curr]

        curr += 1



    n = int(eval(input()))



    g = [set() for _ in range(n)]

    for _ in range(n - 1):

        a, b = (int(x) - 1 for x in input().split())

        g[a].add(b)

        g[b].add(a)

        # 出次数は、len(g[x])-1

        # 親を除いた残りが子

    *c, = sorted(map(int, input().split()))



    num = [-1] * n

    curr = 0

    dfs(0)



    print((sum(c[:-1])))

    print((*num))





if __name__ == '__main__':

    main()

N = int(input())
es = [[] for idx in range(N)]
# es[idx]:=[隣接リスト]
for _ in range(N - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    es[a].append(b)
    es[b].append(a)
h = [len(es[i]) for i in range(N)]
# 次数
c = list(map(int, input().split()))
c.sort()
# スコアが小さい順にソート
v = [-1] * N
cur = 0
ans = 0
for _ in range(N - 1):
    for target in range(N):
        # print(target, h, v, ans)
        if h[target] == 1:
            h[target] = 0
            v[target] = c[cur]
            ans += c[cur]
            cur += 1
            for nv in es[target]:
                h[nv] -= 1
            break
for target in range(N):
    if v[target] == -1:
        v[target] = c[cur]
print(ans)
print(*v, sep="\n")

def main():
    import sys

    sys.setrecursionlimit(10**7)

    def dfs(v):
        nonlocal curr
        num[v] = 0  # 0: visited, but not written yet
        for u in g[v]:
            if num[u] >= 0:
                continue
            dfs(u)
        num[v] = c[curr]
        curr += 1

    n = int(eval(input()))
    g = [set() for _ in range(n)]
    for _ in range(n - 1):
        a, b = (int(x) - 1 for x in input().split())
        g[a].add(b)
        g[b].add(a)
        # 出次数は、len(g[x])-1
        # 親を除いた残りが子
    (*c,) = sorted(map(int, input().split()))
    num = [-1] * n
    curr = 0
    dfs(0)
    print((sum(c[:-1])))
    print((*num))


if __name__ == "__main__":
    main()

-N = int(input()) -es = [[] for idx in range(N)] -# es[idx]:=[隣接リスト] -for _ in range(N - 1): -    a, b = map(int, input().split()) -    a -= 1 -    b -= 1 -    es[a].append(b) -    es[b].append(a) -h = [len(es[i]) for i in range(N)] -# 次数 -c = list(map(int, input().split())) -c.sort() -# スコアが小さい順にソート -v = [-1] * N -cur = 0 -ans = 0 -for _ in range(N - 1): -    for target in range(N): -        # print(target, h, v, ans) -        if h[target] == 1: -            h[target] = 0 -            v[target] = c[cur] -            ans += c[cur] -            cur += 1 -            for nv in es[target]: -                h[nv] -= 1 -            break -for target in range(N): -    if v[target] == -1: -        v[target] = c[cur] -print(ans) -print(*v, sep="\n") +def main(): +    import sys + +    sys.setrecursionlimit(10**7) + +    def dfs(v): +        nonlocal curr +        num[v] = 0  # 0: visited, but not written yet +        for u in g[v]: +            if num[u] >= 0: +                continue +            dfs(u) +        num[v] = c[curr] +        curr += 1 + +    n = int(eval(input())) +    g = [set() for _ in range(n)] +    for _ in range(n - 1): +        a, b = (int(x) - 1 for x in input().split()) +        g[a].add(b) +        g[b].add(a) +        # 出次数は、len(g[x])-1 +        # 親を除いた残りが子 +    (*c,) = sorted(map(int, input().split())) +    num = [-1] * n +    curr = 0 +    dfs(0) +    print((sum(c[:-1]))) +    print((*num)) + + +if __name__ == "__main__": +    main()