Language: C++

#include<bits/stdc++.h>
#define ll                 long long int
#define mod                1000000007
#define pi                 3.14159265358979323846264338327950288419716939937510
#define setbit(x)          __builtin_popcountll(x)
#define db                 long double
#define pp(x)              pair<x,x>
#define ff                 first
#define ss                 second
#define FIO                ios_base::sync_with_stdio(false); cin.tie(NULL);
#define tc                 ll tc; cin>>tc; for(ll number = 1 ; number <= tc ; number ++ )
#define vv(x)              vector<x>
#define pb                 push_back
#define pob                pop_back
#define pf                 push_front
#define pof                pop_front
#define endl               "\n"
#define pq                 priority_queue
#define dbg(x)             { cout<< #x << ": " << (x) << endl; }
#define dbg2(x,y)          { cout<< #x << ": " << (x) << " , " << #y << ": " << (y) << endl; }
#define bp                 ll kkk; cin >>kkk;
#define dbgArr(a,n)        cout<<"Array "<<#a<<endl; for(ll i=0 ; i<n ; i++) cout<<a[i]<<" "; cout<<endl;
#define maxE(a,n)          *max_element(a,a+n);
#define minE(a,n)          *min_element(a,a+n);
#define forn(i,s,n)        for(ll i=s ; i<n ; i++)
#define fornr(i,e,s)       for(ll i=e-1 ; i>=s ; i--)
#define arr(a,n)           for(ll i=0;i<n;i++){ cin>>a[i]; }
#define arr1(a,n)          for(ll i=1;i<=n;i++){ cin>>a[i]; }
#define file freopen("input.txt","r",stdin); freopen("output.txt","w",stdout)
#define print cout<<"Case #"<<number<<": "
#define all(x) x.begin(), x.end()
ll power(ll x, ll b,ll m) {ll p = 1;while (b > 0) {if(b&1) {p = p * x; p %= m;} b >>= 1; x *= x; x %= m;} return p % m;}
using namespace std;
# define INF 1e15
ll n;
struct lex_compare {
    bool operator() (pair<ll,pp(ll)> p1, pair<ll,pp(ll)> p2){
        if(p1.ff == n){
            if(p2.ff == n){
                return p1.ss.ff > p2.ss.ff;
            }else{
                return p1.ss.ff > max(p2.ss.ff, p2.ss.ss);
            }
        }else if(p2.ff == n){
            return max(p1.ss.ff, p1.ss.ss) > p2.ss.ff;
        }

        if(max(p1.ss.ff, p1.ss.ss) > max(p2.ss.ff, p2.ss.ss)){
            return 1;
        }
        return 0;
    }
};
#define MAXN 200005
bool comp(pair<ll,pp(ll)> p1, pair<ll,pp(ll)> p2){
    if(p1.ff == n){
        if(p2.ff == n){
            return p1.ss.ff > p2.ss.ff;
        }else{
            return p1.ss.ff > max(p2.ss.ff, p2.ss.ss);
        }
    }else if(p2.ff == n){
        return max(p1.ss.ff, p1.ss.ss) > p2.ss.ff;
    }

    if(max(p1.ss.ff, p1.ss.ss) > max(p2.ss.ff, p2.ss.ss)){
        return 1;
    }
    return 0;
}


int main(){
    FIO;
    ll t; cin >> t;
    while(t--){
        cin >> n;
        set<pair<ll,pp(ll)>,lex_compare> scamels;
        forn(i,0,n) {
            pair<ll,pp(ll)> temp;
            cin >> temp.ff >> temp.ss.ff >> temp.ss.ss;
            scamels.insert(temp);
        }
        //sort(arr, arr+n, comp);
        set<ll> s;
        forn(i,0,n) s.insert(i);
        ll ans = 0;
        while(!scamels.empty()){
            ll k = scamels.begin()->ff;
            k--;
            ll l = scamels.begin()->ss.ff;
            ll r = scamels.begin()->ss.ss;
            scamels.erase(scamels.begin());
            if(l > r  || k == n-1){
                auto fin = s.lower_bound(k);
                if(fin == s.end()){
                    fin--;
                    ans += l;
                    s.erase(fin);
                }else if(*fin == k){
                    ans += l;
                    s.erase(fin);
                }else{
                    if(fin == s.begin()){
                        ans += r;
                    }else{
                        fin--;
                        ans += l;
                        s.erase(fin);
                    }
                }
            }else{
                auto fin = s.lower_bound(k);
                if(fin == s.end()){
                    ans += l;
                }else{
                    s.erase(fin);
                    ans += r;
                }
            }
        }
        cout << ans << endl;

    }
    return 0;
}


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, std::pair<long long int, long long int> >; _Val = std::pair<long long int, std::pair<long long int, long long int> >; _KeyOfValue = std::_Identity<std::pair<long long int, std::pair<long long int, long long int> > >; _Compare = lex_compare; _Alloc = std::allocator<std::pair<long long int, std::pair<long long int, long long int> > >; _Const_Link_type = const std::_Rb_tree_node<std::pair<long long int, std::pair<long long int, long long int> > >*]':
/usr/include/c++/14/bits/stl_tree.h:2116:47:   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_unique_pos(const key_type&) [with _Key = std::pair<long long int, std::pair<long long int, long long int> >; _Val = std::pair<long long int, std::pair<long long int, long long int> >; _KeyOfValue = std::_Identity<std::pair<long long int, std::pair<long long int, long long int> > >; _Compare = lex_compare; _Alloc = std::allocator<std::pair<long long int, std::pair<long long int, long long int> > >; key_type = std::pair<long long int, std::pair<long long int, long long int> >]'
 2116 |           __comp = _M_impl._M_key_compare(__k, _S_key(__x));
      |                                                ~~~~~~^~~~~
/usr/include/c++/14/bits/stl_tree.h:2169:4:   required from 'std::pair<std::_Rb_tree_iterator<_Val>, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(_Arg&&) [with _Arg = const std::pair<long long int, std::pair<long long int, long long int> >&; _Key = std::pair<long long int, std::pair<long long int, long long int> >; _Val = std::pair<long long int, std::pair<long long int, long long int> >; _KeyOfValue = std::_Identity<std::pair<long long int, std::pair<long long int, long long int> > >; _Compare = lex_compare; _Alloc = std::allocator<std::pair<long long int, std::pair<long long int, long long int> > >]'
 2169 |         = _M_get_insert_unique_pos(_KeyOfValue()(__v));
      |           ^~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_set.h:514:25:   required from 'std::pair<typename std::_Rb_tree<_Key, _Key, std::_Identity<_Tp>, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other>::const_iterator, bool> std::set<_Key, _Compare, _Alloc>::insert(const value_type&) [with _Key = std::pair<long long int, std::pair<long long int, long long int> >; _Compare = lex_compare; _Alloc = std::allocator<std::pair<long long int, std::pair<long long int, long long int> > >; typename std::_Rb_tree<_Key, _Key, std::_Identity<_Tp>, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other>::const_iterator = std::_Rb_tree<std::pair<long long int, std::pair<long long int, long long int> >, std::pair<long long int, std::pair<long long int, long long int> >, std::_Identity<std::pair<long long int, std::pair<long long int, long long int> > >, lex_compare, std::allocator<std::pair<long long int, std::pair<long long int, long long int> > > >::const_iterator; typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other = std::allocator<std::pair<long long int, std::pair<long long int, long long int> > >; typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key> = __gnu_cxx::__alloc_traits<std::allocator<std::pair<long long int, std::pair<long long int, long long int> > >, std::pair<long long int, std::pair<long long int, long long int> > >::rebind<std::pair<long long int, std::pair<long long int, long long int> > >; typename _Alloc::value_type = std::pair<long long int, std::pair<long long int, long long int> >; value_type = std::pair<long long int, std::pair<long long int, long long int> >]'
  514 |           _M_t._M_insert_unique(__x);
      |           ~~~~~~~~~~~~~~~~~~~~~^~~~~
a.cc:82:27:   required from here
   82 |             scamels.insert(temp);
      |             ~~~~~~~~~~~~~~^~~~~~
/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 lex_compare&, const std::pair<long long int, std::pair<long long int, long long int> >&, const std::pair<long long int, std::pair<long long int, long long int> >&>' evaluates to false