Language: C++

#include<bits/stdc++.h>
using namespace std;
#define FIO ios_base::sync_with_stdio (false) ;cin.tie(NULL);
#define tc  int t;cin>>t;for(int i=0;i<t;i++)
#define ll long long int
#define forn(i,n) for(int i=0;i<n;i++)
#define forn1(i,a,b) for(int i=a;i<=b;i++)
#define pb push_back
#define um(a,b) unordered_map<a,b>
#define vv(a) vector<a>
#define pp(a,b) pair<a,b>
ll power(ll a,ll b,ll mod){  ll ans=1;  while(b>0){
  if(b&1){ ans=(ans*a)%mod;} a=(a*a)%mod;  b=b/2; } return ans;}
struct compare{
bool operator()(const pp(ll,ll)&a,const pp(ll,ll)&b){
   ll r=min(a.first,a.second);
   ll f=min(b.first,b.second);
   ll e=max(a.first,a.second);
   ll d=max(b.first,b.second);
   if(r>=f){
     return true;
   }else{
     return false;
   }
}
};
int main(){
FIO
  ll n;
  cin>>n;
  ll arr[n];
  forn(i,n){
   cin>>arr[i];
  }
 sort(arr,arr+n,greater<int>());
  ll ans;
  if(n==1){
    ans=0;
  }else{
  multiset<pp(ll,ll),compare> temp;
   ans=arr[0];
  temp.insert({arr[0],arr[1]});
  for(ll i=2;i<n;i++){
    auto k=(temp.begin());
    ll a=min(k->first,k->second);
    temp.insert({k->first,arr[i]});
    temp.insert({k->second,arr[i]});
    ans+=a;
    temp.erase(k);
  }
  cout<<ans<<endl;

  }

}


Compiler output:
In file included from /usr/include/c++/14/map:62,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:152,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_tree.h: In instantiation of 'static const _Key& std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_S_key(_Const_Link_type) [with _Key = std::pair<long long int, long long int>; _Val = std::pair<long long int, long long int>; _KeyOfValue = std::_Identity<std::pair<long long int, long long int> >; _Compare = compare; _Alloc = std::allocator<std::pair<long long int, long long int> >; _Const_Link_type = const std::_Rb_tree_node<std::pair<long long int, long long int> >*]':
/usr/include/c++/14/bits/stl_tree.h:2147:44:   required from 'std::pair<std::_Rb_tree_node_base*, std::_Rb_tree_node_base*> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_get_insert_equal_pos(const key_type&) [with _Key = std::pair<long long int, long long int>; _Val = std::pair<long long int, long long int>; _KeyOfValue = std::_Identity<std::pair<long long int, long long int> >; _Compare = compare; _Alloc = std::allocator<std::pair<long long int, long long int> >; key_type = std::pair<long long int, long long int>]'
 2147 |           __x = _M_impl._M_key_compare(__k, _S_key(__x)) ?
      |                                             ~~~~~~^~~~~
/usr/include/c++/14/bits/stl_tree.h:2196:4:   required from 'std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_equal(_Arg&&) [with _Arg = std::pair<long long int, long long int>; _Key = std::pair<long long int, long long int>; _Val = std::pair<long long int, long long int>; _KeyOfValue = std::_Identity<std::pair<long long int, long long int> >; _Compare = compare; _Alloc = std::allocator<std::pair<long long int, long long int> >; iterator = std::_Rb_tree<std::pair<long long int, long long int>, std::pair<long long int, long long int>, std::_Identity<std::pair<long long int, long long int> >, compare, std::allocator<std::pair<long long int, long long int> > >::iterator]'
 2196 |         = _M_get_insert_equal_pos(_KeyOfValue()(__v));
      |           ^~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_multiset.h:510:36:   required from 'std::multiset<_Key, _Compare, _Alloc>::iterator std::multiset<_Key, _Compare, _Alloc>::insert(value_type&&) [with _Key = std::pair<long long int, long long int>; _Compare = compare; _Alloc = std::allocator<std::pair<long long int, long long int> >; iterator = std::_Rb_tree<std::pair<long long int, long long int>, std::pair<long long int, long long int>, std::_Identity<std::pair<long long int, long long int> >, compare, std::allocator<std::pair<long long int, long long int> > >::const_iterator; value_type = std::pair<long long int, long long int>]'
  510 |       { return _M_t._M_insert_equal(std::move(__x)); }
      |                ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~
a.cc:42:14:   required from here
   42 |   temp.insert({arr[0],arr[1]});
      |   ~~~~~~~~~~~^~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_tree.h:768:15: error: static assertion failed: comparison object must be invocable as const
  768 |               is_invocable_v<const _Compare&, const _Key&, const _Key&>,
      |               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_tree.h:768:15: note: 'std::is_invocable_v<const compare&, const std::pair<long long int, long long int>&, const std::pair<long long int, long long int>&>' evaluates to false