Language: C++

#include <bits/stdc++.h>
#define pb push_back
#define int long long
#define all(x) (x).begin(), (x).end()
using namespace std;

const int N = 3e5 + 5;

struct cell {
    int k, l, r;
};

struct forset {

    bool operator ()(const cell &a, const cell &b) {
        return a.l < b.l;
    }
};

int n;
vector<cell> a, b, c;

bool cmp1(cell a, cell b) {
    return a.k < b.k;
}


signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    while (t--) {
    a.clear();
    b.clear();
    c.clear();

    cin >> n;
    for (int i = 1; i <= n; i++) {
        int k, l, r;
        cin >> k >> l >> r;
        if (r > l)
            c.pb({k, l, r});
        else
            a.pb({k, l, r});
    }
    sort(c.begin(), c.end(), cmp1);
    sort(a.begin(), a.end(), cmp1);
    int cur = 0;
    multiset<cell, forset> q;
    for (int i = 1; i <= n; i++) {
        while (cur < a.size() && a[cur].k <= i)
            cur++, q.insert(a[cur - 1]);
        if (q.size() > i) {
            while (q.size() != i) {
                b.pb(*q.begin());
                q.erase(q.begin());
            }
        }
    }
    sort(b.begin(), b.end(), cmp1);
    vector<cell> all;
    for (auto x: q)
        all.pb(x);
    sort(all.begin(), all.end(), cmp1);
    for (auto x: b)
        all.pb(x);
    for (auto x: c)
        all.pb(x);
    int ans = 0;
    for (int i = 0; i < all.size(); i++)
        if (all[i].k >= i + 1)
            ans += all[i].l;
        else
            ans += all[i].r;
    cout << ans << "\n";
    }
    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 = cell; _Val = cell; _KeyOfValue = std::_Identity<cell>; _Compare = forset; _Alloc = std::allocator<cell>; _Const_Link_type = const std::_Rb_tree_node<cell>*]':
/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 = cell; _Val = cell; _KeyOfValue = std::_Identity<cell>; _Compare = forset; _Alloc = std::allocator<cell>; key_type = cell]'
 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 = const cell&; _Key = cell; _Val = cell; _KeyOfValue = std::_Identity<cell>; _Compare = forset; _Alloc = std::allocator<cell>; iterator = std::_Rb_tree<cell, cell, std::_Identity<cell>, forset, std::allocator<cell> >::iterator]'
 2196 |         = _M_get_insert_equal_pos(_KeyOfValue()(__v));
      |           ^~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_multiset.h:505:36:   required from 'std::multiset<_Key, _Compare, _Alloc>::iterator std::multiset<_Key, _Compare, _Alloc>::insert(const value_type&) [with _Key = cell; _Compare = forset; _Alloc = std::allocator<cell>; iterator = std::_Rb_tree<cell, cell, std::_Identity<cell>, forset, std::allocator<cell> >::const_iterator; value_type = cell]'
  505 |       { return _M_t._M_insert_equal(__x); }
      |                ~~~~~~~~~~~~~~~~~~~~^~~~~
a.cc:55:28:   required from here
   55 |             cur++, q.insert(a[cur - 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 forset&, const cell&, const cell&>' evaluates to false