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()( pp(ll,ll)&a, 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 if(r<f){
     return false;
   }else{
     if(e>=d)
        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 '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>]':
/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:48:14:   required from here
   48 |   temp.insert({arr[0],arr[1]});
      |   ~~~~~~~~~~~^~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_tree.h:2147:39: error: no match for call to '(compare) (const 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> > >::key_type&, const std::pair<long long int, long long int>&)'
 2147 |           __x = _M_impl._M_key_compare(__k, _S_key(__x)) ?
      |                 ~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~
a.cc:15:6: note: candidate: 'bool compare::operator()(std::pair<long long int, long long int>&, std::pair<long long int, long long int>&)' (near match)
   15 | bool operator()( pp(ll,ll)&a, pp(ll,ll)&b){
      |      ^~~~~~~~
a.cc:15:6: note:   conversion of argument 2 would be ill-formed:
a.cc:15:6: error: binding reference of type 'std::pair<long long int, long long int>&' to 'const std::pair<long long int, long long int>' discards qualifiers
/usr/include/c++/14/bits/stl_tree.h: In instantiation of 'std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_(_Base_ptr, _Base_ptr, _Arg&&, _NodeGen&) [with _Arg = std::pair<long long int, long long int>; _NodeGen = 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> > >::_Alloc_node; _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; _Base_ptr = std::_Rb_tree_node_base*]':
/usr/include/c++/14/bits/stl_tree.h:2198:24:   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]'
 2198 |       return _M_insert_(__res.first, __res.second,
      |              ~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
 2199 |                         _GLIBCXX_FORWARD(_Arg, __v), __an);
      |                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/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:48:14:   required from here
   48 |   temp.insert({arr[0],arr[1]});
      |   ~~~~~~~~~~~^~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_tree.h:1824:56: error: no match for call to '(compare) (std::pair<long long int, long long int>&, const std::pair<long long int, long long int>&)'
 1824 |                               || _M_impl._M_key_compare(_KeyOfValue()(__v),
      |                                  ~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~
 1825 |                                                         _S_key(__p)));
      |                                                         ~~~~~~~~~~~~
a.cc:15:6: note: candidate: 'bool compare::operator()(std::pair<long long int, long long int>&, std::pair<long long int, long long int>&)' (near match)
   15 | bool operator()( pp(ll,ll)&a, pp(ll,ll)&b){
      |      ^~~~~~~~
a.cc:15:6: note:   conversion of argument 2 would be ill-formed:
a.cc:15:6: error: binding reference of type 'std::pair<long long int, long long int>&' to 'const std::pair<long long int, long long int>' discards qualifiers
/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:48:14:   required from here
   48 |   temp.insert({arr[0],arr[1]});
      |   ~~~~~~~~~~~^~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_tree.h:760:23: error: static assertion faile