Language: C++

#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define eb emplace_back
#define all(v) (v).begin(), (v).end() 
#define sz(v) (int)((v).size())
#define fs first
#define sd second

typedef long long ll;

typedef pair < int, int > pi;
typedef pair < pi, int > ppi;

typedef vector < int > vi;
typedef vector < vector < int > > vvi;
typedef vector < pi > vpi;
typedef vector < vpi > vvpi;
typedef vector < ppi > vppi;

void fast() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
}

vector<string> vec_splitter(string s) {
	s += ',';
	vector<string> res;
	while(!s.empty()) {
		res.push_back(s.substr(0, s.find(',')));
		s = s.substr(s.find(',') + 1);
	}
	return res;
}

void debug_out(
vector<string> __attribute__ ((unused)) args,
__attribute__ ((unused)) int idx, 
__attribute__ ((unused)) int LINE_NUM) { cerr << endl; } 
template <typename Head, typename... Tail>
void debug_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T) {
	if(idx > 0) cerr << ", "; else cerr << "Line(" << LINE_NUM << ") ";
	stringstream ss; ss << H;
	cerr << args[idx] << " = " << ss.str();
	debug_out(args, idx + 1, LINE_NUM, T...);
}

#ifdef LOCAL
#define debug(...) debug_out(vec_splitter(#__VA_ARGS__), 0, __LINE__, __VA_ARGS__)
#else
#define debug(...) 42
#endif

double get_time() {
	return 1.0 * clock() / CLOCKS_PER_SEC;
}

vector<pair<int, int>> vals;
struct comp {
	bool operator() (const int i, const int j) const {
		return vals[i].second - vals[i].first > vals[j].second - vals[j].first;
	}
};

void solve() {
	int n;
	cin >> n;
	
	vals.clear();
	vector<vector<int>> camels(n);
	vector<int> len(n), taken(n);
	// camels.resize(n);
	for(int i = 0; i < n; i++) {
		int k, l, r;
		cin >> k >> l >> r;
		--k;
		vals.emplace_back(l, r);
		camels[k].push_back(i);
		len[i] = k;
	}
	// auto comp = [&](int i, int j) {
	// 	return vals[i].second - vals[i].first > vals[j].second - vals[j].first;
	// };
	set<int, comp> active, inactive;
	long long res = 0;
	for(int i = 0; i < n; i++)
		for(auto pp : camels[i])
			active.insert(pp);
	for(int i = n - 1; i >= 0; i--) {
		for(int pos : camels[i]) {
			if(active.find(pos) != active.end()) {
				active.erase(pos);
				inactive.insert(pos);
			}
		}
		if(active.empty()) {
			// int pos = *inactive.rbegin();
			// inactive.erase(pos);
			res += vals[pos].first;
			continue;
		}
		int idx = *active.begin();
		// debug(i, idx);
		if(inactive.empty() || vals[idx].second >= vals[idx].first) {
			taken[i] = true;
			active.erase(active.begin());
			res += vals[idx].second;
		}
		else {
			int pos = *inactive.rbegin();
			inactive.erase(pos);
			res += vals[pos].first;
		}
	}

	// debug((int) active.size(), (int) inactive.size());
	// for(int i = 0; i < n; i++)
	// 	cout << taken[i] << ' ';
	// cout << '\n';

	// for(int i = n - 1; i >= 0; i--) {
	// 	if(taken[i])
	// 		continue;
	// 	for(int pos : camels[i]) {
	// 		if(active.find(pos) != active.end()) {
	// 			active.erase(pos);
	// 			inactive.insert(pos);
	// 		}
	// 	}
	// 	if(active.empty() || (vals[*active.begin()].second < vals[*active.begin()].first) && !inactive.empty()) {
	// 		int idx = *inactive.rbegin();
	// 		inactive.erase(idx);
	// 		debug(i, idx);
	// 		res += vals[idx].first;
	// 	}
	// 	else {
	// 		int idx = *active.begin();
	// 		active.erase(idx);
	// 		debug(i, idx);
	// 		res += vals[idx].second;
	// 	}
	// }
	// assert(active.empty());
	// assert(inactive.empty());
	cout << res << '\n';
}

int main() {
	fast();
	int t;
	cin >> t;
	while(t--)
		solve();
}


Compiler output:
a.cc: In function 'void solve()':
a.cc:100:37: error: 'pos' was not declared in this scope; did you mean 'pow'?
  100 |                         res += vals[pos].first;
      |                                     ^~~
      |                                     pow