Language: C++

#include <cstdio>
#include <iostream>
#define ll long long
using namespace std;

ll tmp[1005];
ll tmp1[1005];

ll ans_l[1005];
ll ans_r[1005];

void clear(int n,ll tmp[]){
	for(int t = 1; t <= 2*n; t++){
		tmp[t] = 0;
	}
}

void array_copy_right(int A[], ll tmp[], int end, int n){
	for(int i = 3*n; i > end; i--){
		tmp[3*n-i+1] = A[i];
	}
}

void array_copy(ll A[], ll tmp[], int n){
	for(int i = 1; i <=n; i++){
		tmp[i] = A[i];
	}
}
//交换两个数
void swap(int *x, int *y)
{
	int temp = *x;
	*x = *y;
	*y = temp;
}

void heapify(ll nums[], int n, int i)
{
	//递归结束条件i比左右孩子都大或者i是叶子结点

	int largest = i;
	int l = 2 * i; //左孩子
	int r = 2 * i + 1; //右孩子

	//如果左右孩子有一个比父结点i大，那么就把孩子结点赋给largest
	if (l < n && nums[l] > nums[largest])
		largest = l;
	if (r < n && nums[r] > nums[largest])
		largest = r;

	//largest不等于i说明左右孩子至少有一个比i大或者i是叶子结点
	if (largest != i)
	{
		swap(&nums[i], &nums[largest]); //交换父结点i和大的那个孩子结点
		heapify(nums, n, largest); //继续向下沉
	}
}

//堆排序
void heap_sort(ll nums[], int n)
{
	//把原始数组建成一个大顶堆（从最后一个非叶子结点开始堆化操作）
	int i;
	for (i = n / 2 ; i >= 1; i--)
		heapify(nums, n, i);
	
	//把最大得数（堆顶）交换到数组最后，
	//再把交换之后的堆顶进行堆化操作，
	//然后把堆的大小减1（数组实际大小没变）
	for (i = n ; i > 0; i--)
	{
		swap(&nums[1], &nums[i]);
		heapify(nums, i, 1);
	}
}

int main(){
	/*1-index*/
	//int A[]= {0,8,2,2,7,4,6,5,3,8};
	//int n = 3;
	ll A[1005];
	int n;
	cin >> n;
	for(int i = 1; i <= 3*n; i++){
		cin >> A[i];
	}
	long long max = -10000000000;

	for(int i = 0; i <= n; i++){
		if(i == 0){
			array_copy(A,tmp,n+i);
			array_copy_right(A,tmp1,2*n-i,n);
		}
		else{
		tmp[n+i] = A[n+i];
		tmp1[n+i] = A[2*n+1-i];
		}
		
		
		heap_sort(tmp,n+i);
		heap_sort(tmp1,n+i);
		
		ll left = 0;
		ll right = 0;
		for(int t = 0; t < n; t++){
			left += tmp[n+i-t];
			
			right += tmp1[1+t];
			
		}
		ans_l[i+1] = left;
		ans_r[n+1-i] = right;

		//clear(n,tmp1);
		
	}


	for(int i = 0; i <= n+1; i++){
		if((ans_l[i] - ans_r[i]) > max ) max = ans_l[i] - ans_r[i];
	}
	cout << max <<endl;
	//heap_sort(A,3*n);
	//




}

/*
for(int i = 1; i <= n+1; i++){
		array_copy(A, tmp, i+n-1);
		heap_sort(tmp, n+i-1); 
		for(int i = 1; i <= 2*n; i++){
	cout << tmp[i] << " ";
	}
	cout << endl;
		int result_left = tmp[n+i-1] + tmp[n+i-2] + tmp[n+i-3];

		for(int t = 0; t < 10; t++) tmp1[t] = 0;
		array_copy_right(A,tmp1,n-1+i,n);
		
		heap_sort(tmp1,2*n-i+1);
		for(int i = 1; i <= 2*n; i++){
	cout << tmp1[i] << " ";
	}
	cout << endl;

		int result_right = tmp1[1] + tmp1[2] + tmp1[3];
		if(result_left-result_right > max) max = result_left-result_right;

		
	}



	cout << max <<endl;

*/

Compiler output:
a.cc: In function 'void heapify(long long int*, int, int)':
a.cc:54:21: error: no matching function for call to 'swap(long long int*, long long int*)'
   54 |                 swap(&nums[i], &nums[largest]); //交换父结点i和大的那个孩子结点
      |                 ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/bits/exception_ptr.h:41,
                 from /usr/include/c++/14/exception:166,
                 from /usr/include/c++/14/ios:41,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:2:
/usr/include/c++/14/bits/move.h:226:5: note: candidate: 'std::_Require<std::__not_<std::__is_tuple_like<_Tp> >, std::is_move_constructible<_Tp>, std::is_move_assignable<_Tp> > std::swap(_Tp&, _Tp&) [with _Tp = long long int*; _Require<__not_<__is_tuple_like<_Tp> >, is_move_constructible<_Tp>, is_move_assignable<_Tp> > = void]' (near match)
  226 |     swap(_Tp& __a, _Tp& __b)
      |     ^~~~
/usr/include/c++/14/bits/move.h:226:5: note:   conversion of argument 2 would be ill-formed:
a.cc:54:32: error: cannot bind non-const lvalue reference of type 'long long int*&' to an rvalue of type 'long long int*'
   54 |                 swap(&nums[i], &nums[largest]); //交换父结点i和大的那个孩子结点
      |                                ^~~~~~~~~~~~~~
/usr/include/c++/14/bits/exception_ptr.h:229:5: note: candidate: 'void std::__exception_ptr::swap(exception_ptr&, exception_ptr&)'
  229 |     swap(exception_ptr& __lhs, exception_ptr& __rhs)
      |     ^~~~
/usr/include/c++/14/bits/exception_ptr.h:229:25: note:   no known conversion for argument 1 from 'long long int*' to 'std::__exception_ptr::exception_ptr&'
  229 |     swap(exception_ptr& __lhs, exception_ptr& __rhs)
      |          ~~~~~~~~~~~~~~~^~~~~
/usr/include/c++/14/bits/move.h:250:5: note: candidate: 'template<class _Tp, long unsigned int _Nm> std::__enable_if_t<((bool)std::__is_swappable<_Tp>::value)> std::swap(_Tp (&)[_Nm], _Tp (&)[_Nm])'
  250 |     swap(_Tp (&__a)[_Nm], _Tp (&__b)[_Nm])
      |     ^~~~
/usr/include/c++/14/bits/move.h:250:5: note:   template argument deduction/substitution failed:
a.cc:54:21: note:   mismatched types '_Tp [_Nm]' and 'long long int*'
   54 |                 swap(&nums[i], &nums[largest]); //交换父结点i和大的那个孩子结点
      |                 ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/bits/stl_algobase.h:64,
                 from /usr/include/c++/14/string:51,
                 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:
/usr/include/c++/14/bits/stl_pair.h:1089:5: note: candidate: 'template<class _T1, class _T2> typename std::enable_if<std::__and_<std::__is_swappable<_T1>, std::__is_swappable<_T2> >::value>::type std::swap(pair<_T1, _T2>&, pair<_T1, _T2>&)'
 1089 |     swap(pair<_T1, _T2>& __x, pair<_T1, _T2>& __y)
      |     ^~~~
/usr/include/c++/14/bits/stl_pair.h:1089:5: note:   template argument deduction/substitution failed:
a.cc:54:21: note:   mismatched types 'std::pair<_T1, _T2>' and 'long long int*'
   54 |                 swap(&nums[i], &nums[largest]); //交换父结点i和大的那个孩子结点
      |                 ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_pair.h:1106:5: note: candidate: 'template<class _T1, class _T2> typename std::enable_if<(! std::__and_<std::__is_swappable<_T1>, std::__is_swappable<_T2> >::value)>::type std::swap(pair<_T1, _T2>&, pair<_T1, _T2>&)' (deleted)
 1106 |     swap(pair<_T1, _T2>&, pair<_T1, _T2>&) = delete;
      |     ^~~~
/usr/include/c++/14/bits/stl_pair.h:1106:5: note:   template argument deduction/substitution failed:
a.cc:54:21: note:   mismatched types 'std::pair<_T1, _T2>' and 'long long int*'
   54 |                 swap(&nums[i], &nums[largest]); //交换父结点i和大的那个孩子结点
      |                 ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/string:54:
/usr/include/c++/14/bits/basic_string.h:4039:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> void std::swap(__cxx11::basic_string<_CharT, _Traits, _Allocator>&, __cxx11::basic_string<_CharT, _Traits, _Allocator>&)'
 4039 |     swap(basic_string<_CharT, _Traits, _Alloc>& __lhs,
      |     ^~~~
/usr/include/c++/14/bits/basic_string.h:4039:5: note:   template argument deduction/substitution failed:
a.cc:54:21: note:   mismatched types 'std::__cxx11::basic_string<_CharT, _Traits, _Allocator>' and 'long long int*'
   54 |                 swap(&nums[i], &nums[largest]); //交换父结点i和大的那个孩子结点
      |                 ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/bits/memory_resource.h:47,
                 from /usr/include/c++/14/string:68:
/usr/include/c++/14/tuple:2805:5: note: candidate: 'template<class ... _Elements> typename std::enable_if<std::__and_<std::__is_swappable<_Elements>...>::value>::type std::swap(tuple<_UTypes ...>&, tuple<_UTypes ...>&)'
 2805 |     swap(tuple<_Elements...>& __x, tuple<_Elements...>& __y)
      |     ^~~~
/usr/include/c++/14/tuple:2805:5: note:   template argument deduction/substitution failed:
a.cc:54:21: note:   mismatched types 'std::tuple<_UTypes ...>' and 'long long int*'
   54 |                 swap(&nums[i], &nums[largest]); //交换父结点i和大的那个孩子结点
      |                 ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/tuple:2823:5: note: candidate: 'template<class ... _Elements> typename std::enable_if<(! std::__and_<std::__is_swappable<_Elements>...>::value)>::type std::swap(tuple<_UTypes ...>&, tuple<_UTypes ...>&)' (deleted)
 2823 |     swap(tuple<_Elements...>&, tuple<_Elements...>&) = delete;
      |     ^~~~
/usr/include/c++/14/tuple:2823:5: note:   template argument deduction/substitution failed:
a.cc:54:21: note:   mismatched types 'std::tuple<_UTypes ...>' and 'long long int*'
   54 |                 swap(&nums[i], &nums[largest]); //交换父结点i和大的那个孩子结点
      |                 ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:30:6: note: candidate: 'void swap(int*, int*)'
   30 | void swap(int *x, int *y)
      |      ^~~~
a.cc:30:16: note:   no known conversion for argument 1 from 'long long int*' to 'int*'
   30 | void swap(int *x, int *y)
      |           ~~~~~^
a.cc: In function 'void heap_sort(long long int*, int)':
a.cc:72:21: error: no matching function for call to 'swap(long long int*, long long int*)'
   72 |                 swap(&nums[1], &nums[i]);
      |                 ~~~~^~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/move.h:226:5: note: candidate: 'std::_Require<std::__not_<std::__is_tuple_like<_Tp> >, std::is_move_constructible<_Tp>, std::is_move_assignable<_Tp> > std::swap(_Tp&, _Tp&) [with _Tp = long long int*; _Require<__not_<__is_tuple_like<_Tp> >, is_move_constructible<_Tp>, is_move_assignable<_Tp> > = void]' (near match)
  226 |     swap(_Tp& __a, _Tp& __b)
      |     ^~~~
/usr/include/c++/14/bits/move.h:226:5: note:   conversion of argument 2 would be ill-formed:
a.cc:72:32: error: cannot bind non-const lvalue reference of type 'long long int*&' to an rvalue of type 'long long int*'
   72 |                 swap(&nums[1], &nums[i]);
      |                                ^~~~~~~~
/usr/include/c++/14/bits/exception_ptr.h:229:5: note: candidate: 'void std::__exception_ptr::swap(exception_ptr&, exception_ptr&)'
  229 |     swap(exception_ptr& __lhs, exception_ptr& __rhs)
      |     ^~~~
/usr/include/c++/14/bits/exception_ptr.h:229:25: note:   no known conversion for argument 1 from 'long long int*' to 'std::__exception_ptr::exception_ptr&'
  229 |     swap(exception_ptr& __lhs, exception_ptr& __rhs)
      |          ~~~~~~~~~~~~~~~^~~~~
/usr/include/c++/14/bits/move.h:250:5: note: candidate: 'template<class _Tp, long unsigned int _Nm> std::__enable_if_t<((bool)std::__is_swappable<_Tp>::value)> std::swap(_Tp (&)[_Nm], _Tp (&)[_Nm])'
  250 |     swap(_Tp (&__a)[_Nm], _Tp (&__b)[_Nm])
      |     ^~~~
/usr/include/c++/14/bits/move.h:250:5: note:   template argument deduction/substitution failed:
a.cc:72:21: note:   mismatched types '_Tp [_Nm]' and 'long long int*'
   72 |                 swap(&nums[1], &nums[i]);
      |                 ~~~~^~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_pair.h:1089:5: note: candidate: 'template<class _T1, class _T2> typename std::enable_if<std::__and_<std::__is_swappable<_T1>, std::__is_swappable<_T2> >::value>::type std::swap(pair<_T1, _T2>&, pair<_T1, _T2>&)'
 1089 |     swap(pair<_T1, _T2>& __x, pair<_T1, _T2>& __y)
      |     ^~~~
/usr/include/c++/14/bits/stl_pair.h:1089:5: note:   template argument deduction/substitution failed:
a.cc:72:21: note:   mismatched types 'std::pair<_T1, _T2>' and 'long long int*'
   72 |                 swap(&nums[1], &nums[i]);
      |                 ~~~~^~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_pair.h:1106:5: note: candidate: 'template<class _T1, class _T2> typename std::enable_if<(! std::__and_<std::__is_swappable<_T1>, std::__is_swappable<_T2> >::value)>::type std::swap(pair<_T1, _T2>&, pair<_T1, _T2>&)' (deleted)
 1106 |     swap(pair<_T1, _T2>&, pair<_T1, _T2>&) = delete;
      |     ^~~~
/usr/include/c++/14/bits/stl_pair.h:1106:5: note:   template argument deduction/substitution failed:
a.cc:72:21: note:   mismatched types 'std::pair<_T1, _T2>' and 'long long int*'
   72 |                 swap(&nums[1], &nums[i]);
      |                 ~~~~^~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/basic_string.h:4039:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> void std::swap(__cxx11::basic_string<_CharT, _Traits, _Allocator>&, __cxx11::basic_string<_CharT, _Traits, _Allocator>&)'
 4039 |     swap(basic_string<_CharT, _Traits, _Alloc>& __lhs,
      |     ^~~~
/usr/include/c++/14/bits/basic_string.h:4039:5: note:   template argument deduction/substitution failed:
a.cc:72:21: note:   mismatched types 'std::__cxx11::basic_string<_CharT, _Traits, _Allocator>' and 'long long int*'
   72 |                 swap(&nums[1], &nums[i]);