import queue
def main():
    n = int(input())
    g = [[] for _ in range(n)]
    for _ in range(n-1):
        a, b = map(int, input().split())
        g[a-1].append(b-1)
        g[b-1].append(a-1)
    c = list(map(int, input().split()))
    c.sort(reverse=True)
    ans = [None]*n
    a = [i for i in range(n)]
    a.sort(key=lambda x: len(g[x]), reverse=True)
    f = [True]*n
    f[a[0]] = False
    q = queue.Queue()
    q.put(a[0])
    ans[a[0]] = c[0]
    x = 1
    while not q.empty():
        target = q.get()
        for v in g[target]:
            if f[v]:
                f[v] = False
                ans[v] = c[x]
                q.put(v)
                x += 1
    score = 0
    f = [True]*n
    f[0] = False
    q = queue.Queue()
    q.put(0)
    while not q.empty():
        target = q.get()
        for v in g[target]:
            if f[v]:
                f[v] = False
                score += min(ans[target], ans[v])
                q.put(v)
    print(score)
    for i in range(n):
        print(ans[i], end="")
        if i < n-1:
            print(" ",end="")
        else:
            print("")

if __name__ == "__main__":
    main() import queue queue def main():
    n = int(input())
    g = [[] for _ in range(n)]
    for _ in range(n-1):
        a, b = map(int, input().split())
        g[a-1].append(b-1)
        g[b-1].append(a-1)
    c = list(map(int, input().split()))
    c.sort(reverse=True)
    ans = [None]*n
    a = [i for i in range(n)]
    a.sort(key=lambda x: len(g[x]), reverse=True)
    f = [True]*n
    f[a[0]] = False
    q = queue.Queue()
    q.put(a[0])
    ans[a[0]] = c[0]
    x = 1
    while not q.empty():
        target = q.get()
        for v in g[target]:
            if f[v]:
                f[v] = False
                ans[v] = c[x]
                q.put(v)
                x += 1
    score = 0
    f = [True]*n
    f[0] = False
    q = queue.Queue()
    q.put(0)
    while not q.empty():
        target = q.get()
        for v in g[target]:
            if f[v]:
                f[v] = False
                score += min(ans[target], ans[v])
                q.put(v)
    print(score)
    for i in range(n):
        print(ans[i], end="")
        if i < n-1:
            print(" ",end="")
        else:
            print("") main 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) [] g = [[] for _ in range(n)] g [[] for _ in range(n)] for _ in range(n-1):
        a, b = map(int, input().split())
        g[a-1].append(b-1)
        g[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 g[a-1].append(b-1) [a-1].append [a-1] g a-1 a 1 append b-1 b 1 g[b-1].append(a-1) [b-1].append [b-1] g b-1 b 1 append a-1 a 1 c = list(map(int, input().split())) c list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split c.sort(reverse=True) c.sort c sort reverse=True reverse True ans = [None]*n ans [None]*n [None] None n i for i in range(n) for i in range(n) i range(n) range n for i in range(n) i a = [i for i in range(n)] a [i for i in range(n)] a.sort(key=lambda x: len(g[x]), reverse=True) a.sort a sort key=lambda x: len(g[x]) key lambda x: len(g[x]) len(g[x]) len g[x] g x x reverse=True reverse True f = [True]*n f [True]*n [True] True n f[a[0]] = False f[a[0]] f a[0] a 0 False q = queue.Queue() q queue.Queue() queue.Queue queue Queue q.put(a[0]) q.put q put a[0] a 0 ans[a[0]] = c[0] ans[a[0]] ans a[0] a 0 c[0] c 0 x = 1 x 1 while not q.empty():
        target = q.get()
        for v in g[target]:
            if f[v]:
                f[v] = False
                ans[v] = c[x]
                q.put(v)
                x += 1
     not q.empty() q.empty() q.empty q empty target = q.get() target q.get() q.get q get for v in g[target]:
            if f[v]:
                f[v] = False
                ans[v] = c[x]
                q.put(v)
                x += 1
     v g[target] g target if f[v]:
                f[v] = False
                ans[v] = c[x]
                q.put(v)
                x += 1
     f[v] f v f[v] = False f[v] f v False ans[v] = c[x] ans[v] ans v c[x] c x q.put(v) q.put q put v x += 1 x 1 score = 0 score 0 f = [True]*n f [True]*n [True] True n f[0] = False f[0] f 0 False q = queue.Queue() q queue.Queue() queue.Queue queue Queue q.put(0) q.put q put 0 while not q.empty():
        target = q.get()
        for v in g[target]:
            if f[v]:
                f[v] = False
                score += min(ans[target], ans[v])
                q.put(v)
     not q.empty() q.empty() q.empty q empty target = q.get() target q.get() q.get q get for v in g[target]:
            if f[v]:
                f[v] = False
                score += min(ans[target], ans[v])
                q.put(v)
     v g[target] g target if f[v]:
                f[v] = False
                score += min(ans[target], ans[v])
                q.put(v)
     f[v] f v f[v] = False f[v] f v False score += min(ans[target], ans[v]) score min(ans[target], ans[v]) min ans[target] ans target ans[v] ans v q.put(v) q.put q put v print(score) print score for i in range(n):
        print(ans[i], end="")
        if i < n-1:
            print(" ",end="")
        else:
            print("") i range(n) range n print(ans[i], end="") print ans[i] ans i end="" end "" if i < n-1:
            print(" ",end="")
        else:
            print("") i < n-1 i n-1 n 1 print(" ",end="") print " " end="" end "" print("") print "" if __name__ == "__main__":
    main() __name__ == "__main__" __name__ "__main__" main() main def main():
    n = int(input())
    g = [[] for _ in range(n)]
    for _ in range(n-1):
        a, b = map(int, input().split())
        g[a-1].append(b-1)
        g[b-1].append(a-1)
    c = list(map(int, input().split()))
    c.sort(reverse=True)
    ans = [None]*n
    a = [i for i in range(n)]
    a.sort(key=lambda x: len(g[x]), reverse=True)
    f = [True]*n
    f[a[0]] = False
    q = queue.Queue()
    q.put(a[0])
    ans[a[0]] = c[0]
    x = 1
    while not q.empty():
        target = q.get()
        for v in g[target]:
            if f[v]:
                f[v] = False
                ans[v] = c[x]
                q.put(v)
                x += 1
    score = 0
    f = [True]*n
    f[0] = False
    q = queue.Queue()
    q.put(0)
    while not q.empty():
        target = q.get()
        for v in g[target]:
            if f[v]:
                f[v] = False
                score += min(ans[target], ans[v])
                q.put(v)
    print(score)
    for i in range(n):
        print(ans[i], end="")
        if i < n-1:
            print(" ",end="")
        else:
            print("") def main():
    n = int(input())
    g = [[] for _ in range(n)]
    for _ in range(n-1):
        a, b = map(int, input().split())
        g[a-1].append(b-1)
        g[b-1].append(a-1)
    c = list(map(int, input().split()))
    c.sort(reverse=True)
    ans = [None]*n
    a = [i for i in range(n)]
    a.sort(key=lambda x: len(g[x]), reverse=True)
    f = [True]*n
    f[a[0]] = False
    q = queue.Queue()
    q.put(a[0])
    ans[a[0]] = c[0]
    x = 1
    while not q.empty():
        target = q.get()
        for v in g[target]:
            if f[v]:
                f[v] = False
                ans[v] = c[x]
                q.put(v)
                x += 1
    score = 0
    f = [True]*n
    f[0] = False
    q = queue.Queue()
    q.put(0)
    while not q.empty():
        target = q.get()
        for v in g[target]:
            if f[v]:
                f[v] = False
                score += min(ans[target], ans[v])
                q.put(v)
    print(score)
    for i in range(n):
        print(ans[i], end="")
        if i < n-1:
            print(" ",end="")
        else:
            print("") main
import queue
def main():
    n = int(input())
    g = [[] for _ in range(n)]
    for _ in range(n-1):
        a, b = map(int, input().split())
        g[a-1].append(b-1)
        g[b-1].append(a-1)
    c = list(map(int, input().split()))
    c.sort(reverse=True)
    ans = [None]*n
    a = [i for i in range(n)]
    a.sort(key=lambda x: len(g[x]), reverse=True)
    f = [True]*n
    f[a[0]] = False
    q = queue.Queue()
    q.put(a[0])
    ans[a[0]] = c[0]
    x = 1
    while not q.empty():
        target = q.get()
        for v in g[target]:
            if f[v]:
                f[v] = False
                ans[v] = c[x]
                q.put(v)
                x += 1
    score = 0
    f = [True]*n
    f[0] = False
    q = queue.Queue()
    q.put(0)
    while not q.empty():
        target = q.get()
        for v in g[target]:
            if f[v]:
                f[v] = False
                score += min(ans[target], ans[v])
                q.put(v)
    print(score)
    for i in range(n):
        print(ans[i], end="")
        if i < n-1:
            print(" ",end="")
        else:
            print("")

if __name__ == "__main__":
    main()
