Language: C++

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <complex>
using namespace std;

using ll = long long;
using ull = uint64_t;
using P = pair<int, int>;
constexpr double EPS = 1e-9;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

template<class T>
inline bool chmax(T& x, T y){
    if(x < y){
        x = y;
        return true;
    }
    return false;
}
template<class T>
inline bool chmin(T& x, T y){
    if(x > y){
        x = y;
        return true;
    }
    return false;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;
    int N = n * 3;
    vector<int> a(N);
    for(int i = 0; i < N; ++i)  cin >> a[i];
    ll S1 = 0, S2 = 0;
    priority_queue<int, vector<int>, greater<int>> Q1;
    for(int i = 0; i < n; ++i){
        S1 += a[i];
        Q1.push(a[i]);
    }
    multiset<int> st, st2;
    for(int i = n; i < N; ++i){
        S2 += a[i];
        st.insert(a[i]);
    }
    int c = n;
    while(c-- > 0){
        int val = *(st.rbegin());
        st.erase(st.rbegin());
        S2 -= val;
        st2.insert(val);
    }
    ll ans = S1 - S2;
    for(int i = n; i < N - n; ++i){
        if(a[i] > Q1.top()){
            S1 -= Q1.top();
            Q1.pop();
            Q1.push(a[i]);
            S1 += a[i];
        }
        auto it = st.find(a[i]);
        if(it != st.end()){
            S2 -= a[i];
            st.erase(it);
            int val = *(st2.begin());
            S2 += val;
            st2.erase(st2.begin());
            st.insert(val);
        }
        else{
            st2.erase(st2.find(a[i]));
        }
        chmax(ans, S1 - S2);
    }
    cout << ans << endl;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:73:17: error: no matching function for call to 'std::multiset<int>::erase(std::multiset<int>::reverse_iterator)'
   73 |         st.erase(st.rbegin());
      |         ~~~~~~~~^~~~~~~~~~~~~
In file included from /usr/include/c++/14/set:64,
                 from a.cc:8:
/usr/include/c++/14/bits/stl_multiset.h:641:7: note: candidate: 'std::multiset<_Key, _Compare, _Alloc>::iterator std::multiset<_Key, _Compare, _Alloc>::erase(const_iterator) [with _Key = int; _Compare = std::less<int>; _Alloc = std::allocator<int>; iterator = std::_Rb_tree<int, int, std::_Identity<int>, std::less<int>, std::allocator<int> >::const_iterator; const_iterator = std::_Rb_tree<int, int, std::_Identity<int>, std::less<int>, std::allocator<int> >::const_iterator]'
  641 |       erase(const_iterator __position)
      |       ^~~~~
/usr/include/c++/14/bits/stl_multiset.h:641:28: note:   no known conversion for argument 1 from 'std::multiset<int>::reverse_iterator' {aka 'std::reverse_iterator<std::_Rb_tree_const_iterator<int> >'} to 'std::multiset<int>::const_iterator' {aka 'std::_Rb_tree<int, int, std::_Identity<int>, std::less<int>, std::allocator<int> >::const_iterator'}
  641 |       erase(const_iterator __position)
      |             ~~~~~~~~~~~~~~~^~~~~~~~~~
/usr/include/c++/14/bits/stl_multiset.h:671:7: note: candidate: 'std::multiset<_Key, _Compare, _Alloc>::size_type std::multiset<_Key, _Compare, _Alloc>::erase(const key_type&) [with _Key = int; _Compare = std::less<int>; _Alloc = std::allocator<int>; size_type = long unsigned int; key_type = int]'
  671 |       erase(const key_type& __x)
      |       ^~~~~
/usr/include/c++/14/bits/stl_multiset.h:671:29: note:   no known conversion for argument 1 from 'std::multiset<int>::reverse_iterator' {aka 'std::reverse_iterator<std::_Rb_tree_const_iterator<int> >'} to 'const std::multiset<int>::key_type&' {aka 'const int&'}
  671 |       erase(const key_type& __x)
      |             ~~~~~~~~~~~~~~~~^~~
/usr/include/c++/14/bits/stl_multiset.h:693:7: note: candidate: 'std::multiset<_Key, _Compare, _Alloc>::iterator std::multiset<_Key, _Compare, _Alloc>::erase(const_iterator, const_iterator) [with _Key = int; _Compare = std::less<int>; _Alloc = std::allocator<int>; iterator = std::_Rb_tree<int, int, std::_Identity<int>, std::less<int>, std::allocator<int> >::const_iterator; const_iterator = std::_Rb_tree<int, int, std::_Identity<int>, std::less<int>, std::allocator<int> >::const_iterator]'
  693 |       erase(const_iterator __first, const_iterator __last)
      |       ^~~~~
/usr/include/c++/14/bits/stl_multiset.h:693:7: note:   candidate expects 2 arguments, 1 provided