inpl = lambda: list(map(int,input().split()))
class SegmentTree:
    def __init__(self, value, N=0, comp=lambda x,y: x<=y):
        M = max(len(value),N)
        N = 2**(len(bin(M))-3)
        if N < M: N *= 2
        self.N = N
        self.node = [0] * (2*N-1)
        for i in range(N):
            self.node[N-1+i] = i
        self.value = [None] * N
        for i, v in enumerate(value):
            self.value[i] = v
        self.comp = lambda x, y: True if y is None else False if x is None else comp(x,y)
        for i in range(N-2,-1,-1):
            left_i, right_i = self.node[2*i+1], self.node[2*i+2]
            left_v, right_v = self.get_value(left_i), self.get_value(right_i)
            if self.comp(left_v, right_v):
                self.node[i] = left_i
            else:
                self.node[i] = right_i

    def set_value(self, n, v):
        self.value[n] = v
        i = (self.N-1) + n
        while i > 0:
            i = (i-1)//2
            left_i, right_i = self.node[2*i+1], self.node[2*i+2]
            left_v, right_v = self.get_value(left_i), self.get_value(right_i)
            if self.comp(left_v, right_v):
                new_i = left_i
            else:
                new_i = right_i
            if new_i == self.node[i] and new_i != n:
                break
            else:
                self.node[i] = new_i

    def get_value(self, n):
        return self.value[n]

    def get_min(self, a=0, b=-1):
        return self.value[self.get_min_index(a,b)]

    def get_min_index(self,
                     a=0, b=-1,
                     k=0, l=0, r=-1):
        if b < 0:
            b = self.N
        if r < 0:
            r = self.N
        if a <= l and r <= b:
            return self.node[k]
        elif r <= a or b <= l:
            return None
        else:
            left_i = self.get_min_index(a,b,2*k+1,l,(l+r)//2)
            right_i = self.get_min_index(a,b,2*k+2,(l+r)//2,r)
            left_v, right_v = self.get_value(left_i), self.get_value(right_i)
            if self.comp(left_v, right_v):
                return left_i
            else:
                return right_i

N = int(input())
partners = []
orders = [0]*N
for i in range(N):
    partners.append(set())
for i in range(N-1):
    a, b = inpl()
    a -= 1
    b -= 1
    partners[a].add(b)
    partners[b].add(a)
    orders[a] += 1
    orders[b] += 1
sg = SegmentTree(orders)
C = sorted(inpl())
D = [0]*N
M = 0
# print(sg.value, sg.node)
for i in range(N-1):
    k = sg.get_min_index()
    D[k] = C[i]
    M += C[i]
    for p in partners[k]:
        partners[p].remove(k)
        sg.set_value(p, sg.get_value(p)-1)
        # print(sg.value,sg.node)
    sg.set_value(k, None)
    # print(sg.value,sg.node)
k = sg.get_min_index()
D[k] = C[N-1]

print(M)
print(' '.join(map(str,D))) inpl = lambda: list(map(int,input().split())) inpl lambda: list(map(int,input().split())) list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split class SegmentTree:
    def __init__(self, value, N=0, comp=lambda x,y: x<=y):
        M = max(len(value),N)
        N = 2**(len(bin(M))-3)
        if N < M: N *= 2
        self.N = N
        self.node = [0] * (2*N-1)
        for i in range(N):
            self.node[N-1+i] = i
        self.value = [None] * N
        for i, v in enumerate(value):
            self.value[i] = v
        self.comp = lambda x, y: True if y is None else False if x is None else comp(x,y)
        for i in range(N-2,-1,-1):
            left_i, right_i = self.node[2*i+1], self.node[2*i+2]
            left_v, right_v = self.get_value(left_i), self.get_value(right_i)
            if self.comp(left_v, right_v):
                self.node[i] = left_i
            else:
                self.node[i] = right_i

    def set_value(self, n, v):
        self.value[n] = v
        i = (self.N-1) + n
        while i > 0:
            i = (i-1)//2
            left_i, right_i = self.node[2*i+1], self.node[2*i+2]
            left_v, right_v = self.get_value(left_i), self.get_value(right_i)
            if self.comp(left_v, right_v):
                new_i = left_i
            else:
                new_i = right_i
            if new_i == self.node[i] and new_i != n:
                break
            else:
                self.node[i] = new_i

    def get_value(self, n):
        return self.value[n]

    def get_min(self, a=0, b=-1):
        return self.value[self.get_min_index(a,b)]

    def get_min_index(self,
                     a=0, b=-1,
                     k=0, l=0, r=-1):
        if b < 0:
            b = self.N
        if r < 0:
            r = self.N
        if a <= l and r <= b:
            return self.node[k]
        elif r <= a or b <= l:
            return None
        else:
            left_i = self.get_min_index(a,b,2*k+1,l,(l+r)//2)
            right_i = self.get_min_index(a,b,2*k+2,(l+r)//2,r)
            left_v, right_v = self.get_value(left_i), self.get_value(right_i)
            if self.comp(left_v, right_v):
                return left_i
            else:
                return right_i SegmentTree def __init__(self, value, N=0, comp=lambda x,y: x<=y):
        M = max(len(value),N)
        N = 2**(len(bin(M))-3)
        if N < M: N *= 2
        self.N = N
        self.node = [0] * (2*N-1)
        for i in range(N):
            self.node[N-1+i] = i
        self.value = [None] * N
        for i, v in enumerate(value):
            self.value[i] = v
        self.comp = lambda x, y: True if y is None else False if x is None else comp(x,y)
        for i in range(N-2,-1,-1):
            left_i, right_i = self.node[2*i+1], self.node[2*i+2]
            left_v, right_v = self.get_value(left_i), self.get_value(right_i)
            if self.comp(left_v, right_v):
                self.node[i] = left_i
            else:
                self.node[i] = right_i

     __init__ M = max(len(value),N) M max(len(value),N) max len(value) len value N N = 2**(len(bin(M))-3) N 2**(len(bin(M))-3) 2 len(bin(M))-3 len(bin(M)) len bin(M) bin M 3 if N < M: N *= 2
         N < M N M self.N = N self.N self N N self.node = [0] * (2*N-1) self.node self node [0] * (2*N-1) [0] 0 2*N-1 2*N 2 N 1 for i in range(N):
            self.node[N-1+i] = i
         i range(N) range N self.node[N-1+i] = i self.node[N-1+i] self.node self node N-1+i N-1 N 1 i i self.value = [None] * N self.value self value [None] * N [None] None N for i, v in enumerate(value):
            self.value[i] = v
         i v enumerate(value) enumerate value self.value[i] = v self.value[i] self.value self value i v self.comp = lambda x, y: True if y is None else False if x is None else comp(x,y) self.comp self comp lambda x, y: True if y is None else False if x is None else comp(x,y) True if y is None else False if x is None else comp(x,y) y is None y None True False if x is None else comp(x,y) x is None x None False comp(x,y) comp x y x y for i in range(N-2,-1,-1):
            left_i, right_i = self.node[2*i+1], self.node[2*i+2]
            left_v, right_v = self.get_value(left_i), self.get_value(right_i)
            if self.comp(left_v, right_v):
                self.node[i] = left_i
            else:
                self.node[i] = right_i

     i range(N-2,-1,-1) range N-2 N 2 -1 -1 left_i, right_i = self.node[2*i+1], self.node[2*i+2] left_i self.node[2*i+1] self.node self node 2*i+1 2*i 2 i 1 right_i self.node[2*i+2] self.node self node 2*i+2 2*i 2 i 2 left_v, right_v = self.get_value(left_i), self.get_value(right_i) left_v self.get_value(left_i) self.get_value self get_value left_i right_v self.get_value(right_i) self.get_value self get_value right_i if self.comp(left_v, right_v):
                self.node[i] = left_i
            else:
                self.node[i] = right_i

     self.comp(left_v, right_v) self.comp self comp left_v right_v self.node[i] = left_i self.node[i] self.node self node i left_i self.node[i] = right_i self.node[i] self.node self node i right_i self self value value N=0 N 0 comp=lambda x,y: x<=y comp lambda x,y: x<=y x<=y x y x y def set_value(self, n, v):
        self.value[n] = v
        i = (self.N-1) + n
        while i > 0:
            i = (i-1)//2
            left_i, right_i = self.node[2*i+1], self.node[2*i+2]
            left_v, right_v = self.get_value(left_i), self.get_value(right_i)
            if self.comp(left_v, right_v):
                new_i = left_i
            else:
                new_i = right_i
            if new_i == self.node[i] and new_i != n:
                break
            else:
                self.node[i] = new_i

     set_value self.value[n] = v self.value[n] self.value self value n v i = (self.N-1) + n i (self.N-1) + n self.N-1 self.N self N 1 n while i > 0:
            i = (i-1)//2
            left_i, right_i = self.node[2*i+1], self.node[2*i+2]
            left_v, right_v = self.get_value(left_i), self.get_value(right_i)
            if self.comp(left_v, right_v):
                new_i = left_i
            else:
                new_i = right_i
            if new_i == self.node[i] and new_i != n:
                break
            else:
                self.node[i] = new_i

     i > 0 i 0 i = (i-1)//2 i (i-1)//2 i-1 i 1 2 left_i, right_i = self.node[2*i+1], self.node[2*i+2] left_i self.node[2*i+1] self.node self node 2*i+1 2*i 2 i 1 right_i self.node[2*i+2] self.node self node 2*i+2 2*i 2 i 2 left_v, right_v = self.get_value(left_i), self.get_value(right_i) left_v self.get_value(left_i) self.get_value self get_value left_i right_v self.get_value(right_i) self.get_value self get_value right_i if self.comp(left_v, right_v):
                new_i = left_i
            else:
                new_i = right_i
             self.comp(left_v, right_v) self.comp self comp left_v right_v new_i = left_i new_i left_i new_i = right_i new_i right_i if new_i == self.node[i] and new_i != n:
                break
            else:
                self.node[i] = new_i

     new_i == self.node[i] and new_i != n new_i == self.node[i] new_i self.node[i] self.node self node i new_i != n new_i n break self.node[i] = new_i self.node[i] self.node self node i new_i self self n n v v def get_value(self, n):
        return self.value[n]

     get_value return self.value[n] self.value[n] self.value self value n self self n n def get_min(self, a=0, b=-1):
        return self.value[self.get_min_index(a,b)]

     get_min return self.value[self.get_min_index(a,b)] self.value[self.get_min_index(a,b)] self.value self value self.get_min_index(a,b) self.get_min_index self get_min_index a b self self a=0 a 0 b=-1 b -1 def get_min_index(self,
                     a=0, b=-1,
                     k=0, l=0, r=-1):
        if b < 0:
            b = self.N
        if r < 0:
            r = self.N
        if a <= l and r <= b:
            return self.node[k]
        elif r <= a or b <= l:
            return None
        else:
            left_i = self.get_min_index(a,b,2*k+1,l,(l+r)//2)
            right_i = self.get_min_index(a,b,2*k+2,(l+r)//2,r)
            left_v, right_v = self.get_value(left_i), self.get_value(right_i)
            if self.comp(left_v, right_v):
                return left_i
            else:
                return right_i get_min_index if b < 0:
            b = self.N
         b < 0 b 0 b = self.N b self.N self N if r < 0:
            r = self.N
         r < 0 r 0 r = self.N r self.N self N if a <= l and r <= b:
            return self.node[k]
        elif r <= a or b <= l:
            return None
        else:
            left_i = self.get_min_index(a,b,2*k+1,l,(l+r)//2)
            right_i = self.get_min_index(a,b,2*k+2,(l+r)//2,r)
            left_v, right_v = self.get_value(left_i), self.get_value(right_i)
            if self.comp(left_v, right_v):
                return left_i
            else:
                return right_i a <= l and r <= b a <= l a l r <= b r b return self.node[k] self.node[k] self.node self node k elif r <= a or b <= l:
            return None
         r <= a or b <= l r <= a r a b <= l b l return None None left_i = self.get_min_index(a,b,2*k+1,l,(l+r)//2) left_i self.get_min_index(a,b,2*k+1,l,(l+r)//2) self.get_min_index self get_min_index a b 2*k+1 2*k 2 k 1 l (l+r)//2 l+r l r 2 right_i = self.get_min_index(a,b,2*k+2,(l+r)//2,r) right_i self.get_min_index(a,b,2*k+2,(l+r)//2,r) self.get_min_index self get_min_index a b 2*k+2 2*k 2 k 2 (l+r)//2 l+r l r 2 r left_v, right_v = self.get_value(left_i), self.get_value(right_i) left_v self.get_value(left_i) self.get_value self get_value left_i right_v self.get_value(right_i) self.get_value self get_value right_i if self.comp(left_v, right_v):
                return left_i
            else:
                return right_i self.comp(left_v, right_v) self.comp self comp left_v right_v return left_i left_i return right_i right_i self self a=0 a 0 b=-1 b -1 k=0 k 0 l=0 l 0 r=-1 r -1 N = int(input()) N int(input()) int input() input partners = [] partners [] orders = [0]*N orders [0]*N [0] 0 N for i in range(N):
    partners.append(set()) i range(N) range N partners.append(set()) partners.append partners append set() set for i in range(N-1):
    a, b = inpl()
    a -= 1
    b -= 1
    partners[a].add(b)
    partners[b].add(a)
    orders[a] += 1
    orders[b] += 1 i range(N-1) range N-1 N 1 a, b = inpl() a inpl() inpl b a -= 1 a 1 b -= 1 b 1 partners[a].add(b) [a].add [a] partners a add b partners[b].add(a) [b].add [b] partners b add a orders[a] += 1 orders[a] orders a 1 orders[b] += 1 orders[b] orders b 1 sg = SegmentTree(orders) sg SegmentTree(orders) SegmentTree orders C = sorted(inpl()) C sorted(inpl()) sorted inpl() inpl D = [0]*N D [0]*N [0] 0 N M = 0 M 0 for i in range(N-1):
    k = sg.get_min_index()
    D[k] = C[i]
    M += C[i]
    for p in partners[k]:
        partners[p].remove(k)
        sg.set_value(p, sg.get_value(p)-1)
        # print(sg.value,sg.node)
    sg.set_value(k, None)
    # print(sg.value,sg.node) i range(N-1) range N-1 N 1 k = sg.get_min_index() k sg.get_min_index() sg.get_min_index sg get_min_index D[k] = C[i] D[k] D k C[i] C i M += C[i] M C[i] C i for p in partners[k]:
        partners[p].remove(k)
        sg.set_value(p, sg.get_value(p)-1)
        # print(sg.value,sg.node)
     p partners[k] partners k partners[p].remove(k) [p].remove [p] partners p remove k sg.set_value(p, sg.get_value(p)-1) sg.set_value sg set_value p sg.get_value(p)-1 sg.get_value(p) sg.get_value sg get_value p 1 sg.set_value(k, None) sg.set_value sg set_value k None k = sg.get_min_index() k sg.get_min_index() sg.get_min_index sg get_min_index D[k] = C[N-1] D[k] D k C[N-1] C N-1 N 1 print(M) print M print(' '.join(map(str,D))) print ' '.join(map(str,D)) ' '.join ' ' join map(str,D) map str D partners = [] [] partners orders = [0]*N [0]*N orders class SegmentTree:
    def __init__(self, value, N=0, comp=lambda x,y: x<=y):
        M = max(len(value),N)
        N = 2**(len(bin(M))-3)
        if N < M: N *= 2
        self.N = N
        self.node = [0] * (2*N-1)
        for i in range(N):
            self.node[N-1+i] = i
        self.value = [None] * N
        for i, v in enumerate(value):
            self.value[i] = v
        self.comp = lambda x, y: True if y is None else False if x is None else comp(x,y)
        for i in range(N-2,-1,-1):
            left_i, right_i = self.node[2*i+1], self.node[2*i+2]
            left_v, right_v = self.get_value(left_i), self.get_value(right_i)
            if self.comp(left_v, right_v):
                self.node[i] = left_i
            else:
                self.node[i] = right_i

    def set_value(self, n, v):
        self.value[n] = v
        i = (self.N-1) + n
        while i > 0:
            i = (i-1)//2
            left_i, right_i = self.node[2*i+1], self.node[2*i+2]
            left_v, right_v = self.get_value(left_i), self.get_value(right_i)
            if self.comp(left_v, right_v):
                new_i = left_i
            else:
                new_i = right_i
            if new_i == self.node[i] and new_i != n:
                break
            else:
                self.node[i] = new_i

    def get_value(self, n):
        return self.value[n]

    def get_min(self, a=0, b=-1):
        return self.value[self.get_min_index(a,b)]

    def get_min_index(self,
                     a=0, b=-1,
                     k=0, l=0, r=-1):
        if b < 0:
            b = self.N
        if r < 0:
            r = self.N
        if a <= l and r <= b:
            return self.node[k]
        elif r <= a or b <= l:
            return None
        else:
            left_i = self.get_min_index(a,b,2*k+1,l,(l+r)//2)
            right_i = self.get_min_index(a,b,2*k+2,(l+r)//2,r)
            left_v, right_v = self.get_value(left_i), self.get_value(right_i)
            if self.comp(left_v, right_v):
                return left_i
            else:
                return right_i class SegmentTree:
    def __init__(self, value, N=0, comp=lambda x,y: x<=y):
        M = max(len(value),N)
        N = 2**(len(bin(M))-3)
        if N < M: N *= 2
        self.N = N
        self.node = [0] * (2*N-1)
        for i in range(N):
            self.node[N-1+i] = i
        self.value = [None] * N
        for i, v in enumerate(value):
            self.value[i] = v
        self.comp = lambda x, y: True if y is None else False if x is None else comp(x,y)
        for i in range(N-2,-1,-1):
            left_i, right_i = self.node[2*i+1], self.node[2*i+2]
            left_v, right_v = self.get_value(left_i), self.get_value(right_i)
            if self.comp(left_v, right_v):
                self.node[i] = left_i
            else:
                self.node[i] = right_i

    def set_value(self, n, v):
        self.value[n] = v
        i = (self.N-1) + n
        while i > 0:
            i = (i-1)//2
            left_i, right_i = self.node[2*i+1], self.node[2*i+2]
            left_v, right_v = self.get_value(left_i), self.get_value(right_i)
            if self.comp(left_v, right_v):
                new_i = left_i
            else:
                new_i = right_i
            if new_i == self.node[i] and new_i != n:
                break
            else:
                self.node[i] = new_i

    def get_value(self, n):
        return self.value[n]

    def get_min(self, a=0, b=-1):
        return self.value[self.get_min_index(a,b)]

    def get_min_index(self,
                     a=0, b=-1,
                     k=0, l=0, r=-1):
        if b < 0:
            b = self.N
        if r < 0:
            r = self.N
        if a <= l and r <= b:
            return self.node[k]
        elif r <= a or b <= l:
            return None
        else:
            left_i = self.get_min_index(a,b,2*k+1,l,(l+r)//2)
            right_i = self.get_min_index(a,b,2*k+2,(l+r)//2,r)
            left_v, right_v = self.get_value(left_i), self.get_value(right_i)
            if self.comp(left_v, right_v):
                return left_i
            else:
                return right_i SegmentTree a -= 1 1 a b -= 1 1 b b = inpl() inpl() b a, b = inpl() inpl() a inpl = lambda: list(map(int,input().split())) lambda: list(map(int,input().split())) inpl k = sg.get_min_index() sg.get_min_index() k M = 0 0 M C = sorted(inpl()) sorted(inpl()) C M += C[i] C[i] M sg = SegmentTree(orders) SegmentTree(orders) sg D = [0]*N [0]*N D k = sg.get_min_index() sg.get_min_index() k N = int(input()) int(input()) N
inpl = lambda: list(map(int,input().split()))
class SegmentTree:
    def __init__(self, value, N=0, comp=lambda x,y: x<=y):
        M = max(len(value),N)
        N = 2**(len(bin(M))-3)
        if N < M: N *= 2
        self.N = N
        self.node = [0] * (2*N-1)
        for i in range(N):
            self.node[N-1+i] = i
        self.value = [None] * N
        for i, v in enumerate(value):
            self.value[i] = v
        self.comp = lambda x, y: True if y is None else False if x is None else comp(x,y)
        for i in range(N-2,-1,-1):
            left_i, right_i = self.node[2*i+1], self.node[2*i+2]
            left_v, right_v = self.get_value(left_i), self.get_value(right_i)
            if self.comp(left_v, right_v):
                self.node[i] = left_i
            else:
                self.node[i] = right_i

    def set_value(self, n, v):
        self.value[n] = v
        i = (self.N-1) + n
        while i > 0:
            i = (i-1)//2
            left_i, right_i = self.node[2*i+1], self.node[2*i+2]
            left_v, right_v = self.get_value(left_i), self.get_value(right_i)
            if self.comp(left_v, right_v):
                new_i = left_i
            else:
                new_i = right_i
            if new_i == self.node[i] and new_i != n:
                break
            else:
                self.node[i] = new_i

    def get_value(self, n):
        return self.value[n]

    def get_min(self, a=0, b=-1):
        return self.value[self.get_min_index(a,b)]

    def get_min_index(self,
                     a=0, b=-1,
                     k=0, l=0, r=-1):
        if b < 0:
            b = self.N
        if r < 0:
            r = self.N
        if a <= l and r <= b:
            return self.node[k]
        elif r <= a or b <= l:
            return None
        else:
            left_i = self.get_min_index(a,b,2*k+1,l,(l+r)//2)
            right_i = self.get_min_index(a,b,2*k+2,(l+r)//2,r)
            left_v, right_v = self.get_value(left_i), self.get_value(right_i)
            if self.comp(left_v, right_v):
                return left_i
            else:
                return right_i

N = int(input())
partners = []
orders = [0]*N
for i in range(N):
    partners.append(set())
for i in range(N-1):
    a, b = inpl()
    a -= 1
    b -= 1
    partners[a].add(b)
    partners[b].add(a)
    orders[a] += 1
    orders[b] += 1
sg = SegmentTree(orders)
C = sorted(inpl())
D = [0]*N
M = 0
# print(sg.value, sg.node)
for i in range(N-1):
    k = sg.get_min_index()
    D[k] = C[i]
    M += C[i]
    for p in partners[k]:
        partners[p].remove(k)
        sg.set_value(p, sg.get_value(p)-1)
        # print(sg.value,sg.node)
    sg.set_value(k, None)
    # print(sg.value,sg.node)
k = sg.get_min_index()
D[k] = C[N-1]

print(M)
print(' '.join(map(str,D)))