Language: C++

//{{{
#include<algorithm>
#include<cmath>
#include<deque>
#include<fstream>
#include<iomanip>
#include<iostream>
#include<map>
#include<numeric>
#include<queue>
#include<random>
#include<set>
#include<sstream>
#include<sys/time.h>
#include<unordered_map>
#include<unordered_set>
#include<vector>
using ll = long long;
enum : int { M = (int)1e9 + 7 };
enum : ll { MLL = (ll)1e18L + 9 };
using namespace std;
#ifdef LOCAL
#include"rprint2.hpp"
#else
#define FUNC(name) template <ostream& out = cout, class... T> void name(T&&...){ }
FUNC(printde) FUNC(printdbe) FUNC(printdwe) FUNC(printdu);
#endif
template <template <class T, class = std::allocator<T>> class V, class E>
istream& operator >> (istream& in, V<E>& v){ for(auto& e : v){ in >> e; } return in; }
//}}}

struct Stuple {
    int first; int second; int third;
    Stuple() = default;
    Stuple(int first, int second, int third): first(first), second(second), third(third){ }
    operator tuple<int, int, int> () const { return make_tuple(first, second, third); }
    bool operator < (const Stuple& e) const { return tie(first, second, third) < tie(e.first, e.second, e.third); }
    ostream& operator >> (ostream& out) const { prints(tie(first, second, third)); return out; }
};

struct UnionFind {
	vector<Stuple> elems;
	// vector<pair<int, int>> elems;
	UnionFind(int n):elems(n){
		for(int i = 0; i < n; i++){
			elems[i].first = i;
			elems[i].second = 1;
			elems[i].third = 0;
		}
	}
	bool same(int a, int b){
		return parent(a) == parent(b);
	}
	void unite(int a, int b){
		if(elems[parent(a)].first == parent(b)){ return; }
		int sum = getSum(a) + getSum(b);
		int sum2 = getSum2(a) + getSum2(b);
		//cout<<"sum "<<sum<<endl;
		elems[parent(a)].first = parent(b);
		elems[parent(b)].second = sum;
		elems[parent(b)].third = sum2;
	}
	int getSum(int a){
		if(a == elems[a].first){
			return elems[a].second;
		}
		return getSum(elems[a].first);
	}

	int getSum2(int a){
		if(a == elems[a].first){
            return elems[a].third;
		}
		return getSum2(elems[a].first);
	}
	void addSum2(int a){
		if(a == elems[a].first){
			elems[a].third++;
		}else{
            addSum2(elems[a].first);
        }
	}
private:
	int parent(int a){
		if(a == elems[a].first){
			return a;
		}
		return elems[a].first = parent(elems[a].first);
	}
};

int main(){
    cin.tie(0); ios::sync_with_stdio(false);
    int n, h, w;
    cin >> n >> h >> w;
    vector<int> rs(n), cs(n), as(n);
    for(int i = 0; i < n; i++){
        cin >> rs[i] >> cs[i] >> as[i];
    }
    vector<int> is(n);
    iota(is.begin(), is.end(), 0);
    sort(is.rbegin(), is.rend(), [&](int i, int j){ return as[i] < as[j]; });
    UnionFind uf(h + w + 2);
    ll ans = 0;
    for(auto& i : is){
        int r = rs[i], c = cs[i], a = as[i];
        uf.unite(r, h + 1 + c);
        printde(i, a, r, c, uf.getSum(r), uf.getSum2(r));
        if(uf.getSum(r) > uf.getSum2(r)){
            // printde(ans, i, a);
            uf.addSum2(r);
            ans += a;
            printde(ans, r, c);
        }
    }
    cout << ans << '\n';
}


Compiler output:
a.cc: In member function 'std::ostream& Stuple::operator>>(std::ostream&) const':
a.cc:38:49: error: 'prints' was not declared in this scope; did you mean 'printf'?
   38 |     ostream& operator >> (ostream& out) const { prints(tie(first, second, third)); return out; }
      |                                                 ^~~~~~
      |                                                 printf