Language: C++

#include <bits/stdc++.h>
using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> a(3 * n);
  for (int i = 0; i < 3 * n; i++) {
    int a_;
    cin >> a_;
    a[i] = a_;
  }
  vector<int> dp(n);
  vector<long long> maxv(n + 1, 0);
  for (int i = 0; i < n; i++) {
    dp[i] = a[i];
    maxv[0] += (long long)a[i];
  }
  sort(dp.begin(), dp.end());
  for (int i = 0; i < n; i++) {
    int crr = a[n + i];
    maxv[i + 1] = maxv[i];
    if (crr > dp[0]) {
      maxv[i + 1] += (long long)crr;
      maxv[i + 1] -= (long long)dp[0];
      int lb = lower_bound(dp.begin(), dp.end(), crr) - dp.begin();
      dp.insert(dp.begin() + lb, crr);
      dp.erase(dp.begin());
    }
  }
  vector<int> ep(n);
  vector<long long> minv(n + 1, 0);
  for (int i = 0; i < n; i++) {
    ep[i] = a[2 * n + i];
    minv[0] += (long long)a[2 * n + i];
  }
  sort(ep.begin(), ep.end());
  for (int i = 0; i < n; i++) {
    int crr = a[2 * n - 1 - i];
    minv[i + 1] = minv[i];
    if (crr < ep[n - 1]) {
      minv[i + 1] += (long long)crr;
      minv[i + 1] -= (long long)ep[n - 1];
      int lb = lower_bound(ep.begin(), ep.end(), crr) - ep.begin();
      ep.insert(ep.begin() + lb, crr);
      ep.pop_back();
    }
  }
  int ans = -1e9;
  for (int i = 0; i <= n; i++) {
    ans = max(ans, maxv[i] - minv[n - i]);
  }
  cout << ans << '\n';
  return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:53:14: error: no matching function for call to 'max(int&, __gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type)'
   53 |     ans = max(ans, maxv[i] - minv[n - i]);
      |           ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:53:14: note:   deduced conflicting types for parameter 'const _Tp' ('int' and '__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type' {aka 'long long int'})
   53 |     ans = max(ans, maxv[i] - minv[n - i]);
      |           ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61:
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:53:14: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
   53 |     ans = max(ans, maxv[i] - minv[n - i]);
      |           ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~