import sys
import math
import copy
from heapq import heappush, heappop, heapify
from functools import cmp_to_key
from bisect import bisect_left, bisect_right
from collections import defaultdict, deque, Counter
# sys.setrecursionlimit(1000000)

# input aliases
input = sys.stdin.readline
getS = lambda: input().strip()
getN = lambda: int(input())
getList = lambda: list(map(int, input().split()))
getZList = lambda: [int(x) - 1 for x in input().split()]

INF = float("inf")
MOD = 10**9 + 7
divide = lambda x: pow(x, MOD-2, MOD)


def solve():
    n = getN()
    g = [[] for i in range(n)]
    ans = [-1 for i in range(n)]
    ji = [0 for i in range(n)]

    for i in range(n-1):
        a, b = getZList()
        g[a].append(b)
        g[b].append(a)
        ji[a] += 1
        ji[b] += 1

    cn = getList()
    cn.sort()
    h = []
    for i, j in enumerate(ji):
        heappush(h, (j, i))

    anss = 0

    for cnn in cn:
        while True:
            cj, ci = heappop(h)
            if ans[ci] == -1:
                ans[ci] = cnn
                anss += cnn * cj
                for gg in g[ci]:
                    ji[gg] -= 1
                    heappush(h, (ji[gg], gg))
                break

    print(anss)
    print(*ans)
def main():
    n = getN()
    for _ in range(n):
        solve()

    return
if __name__ == "__main__":
    # main()
    solve() import sys sys import math math import copy copy from heapq import heappush, heappop, heapify from functools import cmp_to_key from bisect import bisect_left, bisect_right from collections import defaultdict, deque, Counter input = sys.stdin.readline input sys.stdin.readline sys.stdin sys stdin readline getS = lambda: input().strip() getS lambda: input().strip() input().strip() ().strip () input strip getN = lambda: int(input()) getN lambda: int(input()) int(input()) int input() input getList = lambda: list(map(int, input().split())) getList lambda: list(map(int, input().split())) list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split 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 getZList = lambda: [int(x) - 1 for x in input().split()] getZList lambda: [int(x) - 1 for x in input().split()] [int(x) - 1 for x in input().split()] INF = float("inf") INF float("inf") float "inf" MOD = 10**9 + 7 MOD 10**9 + 7 10**9 10 9 7 divide = lambda x: pow(x, MOD-2, MOD) divide lambda x: pow(x, MOD-2, MOD) pow(x, MOD-2, MOD) pow x MOD-2 MOD 2 MOD x def solve():
    n = getN()
    g = [[] for i in range(n)]
    ans = [-1 for i in range(n)]
    ji = [0 for i in range(n)]

    for i in range(n-1):
        a, b = getZList()
        g[a].append(b)
        g[b].append(a)
        ji[a] += 1
        ji[b] += 1

    cn = getList()
    cn.sort()
    h = []
    for i, j in enumerate(ji):
        heappush(h, (j, i))

    anss = 0

    for cnn in cn:
        while True:
            cj, ci = heappop(h)
            if ans[ci] == -1:
                ans[ci] = cnn
                anss += cnn * cj
                for gg in g[ci]:
                    ji[gg] -= 1
                    heappush(h, (ji[gg], gg))
                break

    print(anss)
    print(*ans) solve n = getN() n getN() getN [] for i in range(n) for i in range(n) i range(n) range n for i in range(n) [] g = [[] for i in range(n)] g [[] for i in range(n)] -1 for i in range(n) for i in range(n) i range(n) range n for i in range(n) -1 ans = [-1 for i in range(n)] ans [-1 for i in range(n)] 0 for i in range(n) for i in range(n) i range(n) range n for i in range(n) 0 ji = [0 for i in range(n)] ji [0 for i in range(n)] for i in range(n-1):
        a, b = getZList()
        g[a].append(b)
        g[b].append(a)
        ji[a] += 1
        ji[b] += 1

     i range(n-1) range n-1 n 1 a, b = getZList() a getZList() getZList b g[a].append(b) [a].append [a] g a append b g[b].append(a) [b].append [b] g b append a ji[a] += 1 ji[a] ji a 1 ji[b] += 1 ji[b] ji b 1 cn = getList() cn getList() getList cn.sort() cn.sort cn sort h = [] h [] for i, j in enumerate(ji):
        heappush(h, (j, i))

     i j enumerate(ji) enumerate ji heappush(h, (j, i)) heappush h (j, i) j i anss = 0 anss 0 for cnn in cn:
        while True:
            cj, ci = heappop(h)
            if ans[ci] == -1:
                ans[ci] = cnn
                anss += cnn * cj
                for gg in g[ci]:
                    ji[gg] -= 1
                    heappush(h, (ji[gg], gg))
                break

     cnn cn while True:
            cj, ci = heappop(h)
            if ans[ci] == -1:
                ans[ci] = cnn
                anss += cnn * cj
                for gg in g[ci]:
                    ji[gg] -= 1
                    heappush(h, (ji[gg], gg))
                break

     True cj, ci = heappop(h) cj heappop(h) heappop h ci if ans[ci] == -1:
                ans[ci] = cnn
                anss += cnn * cj
                for gg in g[ci]:
                    ji[gg] -= 1
                    heappush(h, (ji[gg], gg))
                break

     ans[ci] == -1 ans[ci] ans ci -1 ans[ci] = cnn ans[ci] ans ci cnn anss += cnn * cj anss cnn * cj cnn cj for gg in g[ci]:
                    ji[gg] -= 1
                    heappush(h, (ji[gg], gg))
                 gg g[ci] g ci ji[gg] -= 1 ji[gg] ji gg 1 heappush(h, (ji[gg], gg)) heappush h (ji[gg], gg) ji[gg] ji gg gg break print(anss) print anss print(*ans) print *ans ans def main():
    n = getN()
    for _ in range(n):
        solve()

    return main n = getN() n getN() getN for _ in range(n):
        solve()

     _ range(n) range n solve() solve return if __name__ == "__main__":
    # main()
    solve() __name__ == "__main__" __name__ "__main__" solve() solve MOD = 10**9 + 7 10**9 + 7 MOD getN = lambda: int(input()) lambda: int(input()) getN getList = lambda: list(map(int, input().split())) lambda: list(map(int, input().split())) getList getS = lambda: input().strip() lambda: input().strip() getS input = sys.stdin.readline sys.stdin.readline input def solve():
    n = getN()
    g = [[] for i in range(n)]
    ans = [-1 for i in range(n)]
    ji = [0 for i in range(n)]

    for i in range(n-1):
        a, b = getZList()
        g[a].append(b)
        g[b].append(a)
        ji[a] += 1
        ji[b] += 1

    cn = getList()
    cn.sort()
    h = []
    for i, j in enumerate(ji):
        heappush(h, (j, i))

    anss = 0

    for cnn in cn:
        while True:
            cj, ci = heappop(h)
            if ans[ci] == -1:
                ans[ci] = cnn
                anss += cnn * cj
                for gg in g[ci]:
                    ji[gg] -= 1
                    heappush(h, (ji[gg], gg))
                break

    print(anss)
    print(*ans) def solve():
    n = getN()
    g = [[] for i in range(n)]
    ans = [-1 for i in range(n)]
    ji = [0 for i in range(n)]

    for i in range(n-1):
        a, b = getZList()
        g[a].append(b)
        g[b].append(a)
        ji[a] += 1
        ji[b] += 1

    cn = getList()
    cn.sort()
    h = []
    for i, j in enumerate(ji):
        heappush(h, (j, i))

    anss = 0

    for cnn in cn:
        while True:
            cj, ci = heappop(h)
            if ans[ci] == -1:
                ans[ci] = cnn
                anss += cnn * cj
                for gg in g[ci]:
                    ji[gg] -= 1
                    heappush(h, (ji[gg], gg))
                break

    print(anss)
    print(*ans) solve divide = lambda x: pow(x, MOD-2, MOD) lambda x: pow(x, MOD-2, MOD) divide def main():
    n = getN()
    for _ in range(n):
        solve()

    return def main():
    n = getN()
    for _ in range(n):
        solve()

    return main INF = float("inf") float("inf") INF getZList = lambda: [int(x) - 1 for x in input().split()] lambda: [int(x) - 1 for x in input().split()] getZList
import sys
import math
import copy
from heapq import heappush, heappop, heapify
from functools import cmp_to_key
from bisect import bisect_left, bisect_right
from collections import defaultdict, deque, Counter
# sys.setrecursionlimit(1000000)

# input aliases
input = sys.stdin.readline
getS = lambda: input().strip()
getN = lambda: int(input())
getList = lambda: list(map(int, input().split()))
getZList = lambda: [int(x) - 1 for x in input().split()]

INF = float("inf")
MOD = 10**9 + 7
divide = lambda x: pow(x, MOD-2, MOD)


def solve():
    n = getN()
    g = [[] for i in range(n)]
    ans = [-1 for i in range(n)]
    ji = [0 for i in range(n)]

    for i in range(n-1):
        a, b = getZList()
        g[a].append(b)
        g[b].append(a)
        ji[a] += 1
        ji[b] += 1

    cn = getList()
    cn.sort()
    h = []
    for i, j in enumerate(ji):
        heappush(h, (j, i))

    anss = 0

    for cnn in cn:
        while True:
            cj, ci = heappop(h)
            if ans[ci] == -1:
                ans[ci] = cnn
                anss += cnn * cj
                for gg in g[ci]:
                    ji[gg] -= 1
                    heappush(h, (ji[gg], gg))
                break

    print(anss)
    print(*ans)
def main():
    n = getN()
    for _ in range(n):
        solve()

    return
if __name__ == "__main__":
    # main()
    solve()