Language: C++

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	int num,x,y;
	vector<int[2]> sides;
	vector<int> nums;
	nums.reserve(num);
	sides.reserve(num - 1);
	cin >> num;

	for (int i = 0; i < num - 1; i++) {
		cin >> x >> y;
		sides.push_back({ x,y });
	}

	for (int i = 0; i < num; i++) {
		cin >> x;
		nums.push_back(x);
	}

	sort(nums.begin(), nums.end(),greater<int>());

	vector<int> outputOrder(num);
	outputOrder.reserve(num);
	int index = 2;
	int sum = nums[1];
	outputOrder[sides[0][0]] = nums[0];
	outputOrder[sides[0][1]] = nums[1];
	for (int i = 1; i < num-1; i++) {
		sum += nums[index];
		if (outputOrder[sides[i][0]])
			outputOrder[sides[i][1]] = nums[index++];
		else
			outputOrder[sides[i][0]] = nums[index++];
	}

	cout << sum << endl;
	for (int i = 0; i < num; i++)
		cout << outputOrder[i] << " ";
}

Compiler output:
In file included from /usr/include/c++/14/vector:65,
                 from a.cc:2:
/usr/include/c++/14/bits/stl_uninitialized.h: In instantiation of 'constexpr bool std::__check_constructible() [with _ValueType = int [2]; _Tp = int (&&)[2]]':
/usr/include/c++/14/bits/stl_uninitialized.h:182:4:   required from '_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = move_iterator<int (*)[2]>; _ForwardIterator = int (*)[2]]'
  182 |         = _GLIBCXX_USE_ASSIGN_FOR_INIT(_ValueType2, _From);
      |           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_uninitialized.h:373:37:   required from '_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, allocator<_Tp>&) [with _InputIterator = move_iterator<int (*)[2]>; _ForwardIterator = int (*)[2]; _Tp = int [2]]'
  373 |       return std::uninitialized_copy(__first, __last, __result);
      |              ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:1624:35:   required from 'std::vector<_Tp, _Alloc>::pointer std::vector<_Tp, _Alloc>::_M_allocate_and_copy(size_type, _ForwardIterator, _ForwardIterator) [with _ForwardIterator = std::move_iterator<int (*)[2]>; _Tp = int [2]; _Alloc = std::allocator<int [2]>; pointer = int (*)[2]; size_type = long unsigned int]'
 1624 |               std::__uninitialized_copy_a(__first, __last, __result,
      |               ~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
 1625 |                                           _M_get_Tp_allocator());
      |                                           ~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/vector.tcc:86:36:   required from 'void std::vector<_Tp, _Alloc>::reserve(size_type) [with _Tp = int [2]; _Alloc = std::allocator<int [2]>; size_type = long unsigned int]'
   86 |               __tmp = _M_allocate_and_copy(__n,
      |                       ~~~~~~~~~~~~~~~~~~~~^~~~~
   87 |                 _GLIBCXX_MAKE_MOVE_IF_NOEXCEPT_ITERATOR(this->_M_impl._M_start),
      |                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   88 |                 _GLIBCXX_MAKE_MOVE_IF_NOEXCEPT_ITERATOR(this->_M_impl._M_finish));
      |                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:13:15:   required from here
   13 |         sides.reserve(num - 1);
      |         ~~~~~~~~~~~~~^~~~~~~~~
/usr/include/c++/14/bits/stl_uninitialized.h:90:56: error: static assertion failed: result type must be constructible from input type
   90 |       static_assert(is_constructible<_ValueType, _Tp>::value,
      |                                                        ^~~~~
/usr/include/c++/14/bits/stl_uninitialized.h:90:56: note: 'std::integral_constant<bool, false>::value' evaluates to false
In file included from /usr/include/x86_64-linux-gnu/c++/14/bits/c++allocator.h:33,
                 from /usr/include/c++/14/bits/allocator.h:46,
                 from /usr/include/c++/14/string:43,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/new_allocator.h: In instantiation of 'void std::__new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = int [2]; _Args = {int [2]}; _Tp = int [2]]':
/usr/include/c++/14/bits/alloc_traits.h:575:17:   required from 'static void std::allocator_traits<std::allocator<_CharT> >::construct(allocator_type&, _Up*, _Args&& ...) [with _Up = int [2]; _Args = {int [2]}; _Tp = int [2]; allocator_type = std::allocator<int [2]>]'
  575 |           __a.construct(__p, std::forward<_Args>(__args)...);
      |           ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/vector.tcc:117:30:   required from 'std::vector<_Tp, _Alloc>::reference std::vector<_Tp, _Alloc>::emplace_back(_Args&& ...) [with _Args = {int [2]}; _Tp = int [2]; _Alloc = std::allocator<int [2]>; reference = int (&)[2]]'
  117 |             _Alloc_traits::construct(this->_M_impl, this->_M_impl._M_finish,
      |             ~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  118 |                                      std::forward<_Args>(__args)...);
      |                                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:1301:21:   required from 'void std::vector<_Tp, _Alloc>::push_back(value_type&&) [with _Tp = int [2]; _Alloc = std::allocator<int [2]>; value_type = int [2]]'
 1301 |       { emplace_back(std::move(__x)); }
      |         ~~~~~~~~~~~~^~~~~~~~~~~~~~~~
a.cc:18:18:   required from here
   18 |                 sides.push_back({ x,y });
      |                 ~~~~~~~~~~~~~~~^~~~~~~~~
/usr/include/c++/14/bits/new_allocator.h:191:11: error: parenthesized initializer in array new
  191 |         { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
      |           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~