Language: C++

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <bitset>
#include <string>
#include <cmath>
#include <set>
#include <map>
#define Rep(i, x, y) for (int i = x; i <= y; i ++)
#define Dwn(i, x, y) for (int i = x; i >= y; i --)
#define RepE(i, x) for (int i = pos[x]; i; i = g[i].nex)
#define ev g[i].y
#define eps 1e-10
#define mk make_pair<ll, int>
#define fr first
#define sd second
using namespace std;
typedef long long ll;
typedef double db;
const int N = 100005, mod = 1000000007;
int p[4], a[N][4], n, c[N], mp, mq, mr;
ll ans;
priority_queue< pair<ll, int> > w[4][4];
void add(int i, int j) {
	int i0 = i;
	Rep(k, 1, 3) if (j != k) {
		w[j][k].push( mk((ll)a[i][k] - a[i][j], i0) );
	}
}
int main()
{
	Rep(i, 1, 3) scanf ("%d", &p[i]), n += p[i];
	Rep(i, 1, n) {
		int mx = 0;
		Rep(j, 1, 3) scanf ("%d", &a[i][j]), mx = max(a[i][j], mx);
		Rep(j, 1, 3) if (a[i][j] == mx) {
			c[i] = j;
			p[j] --;
			ans += mx;
			add(i, j);
			break ;
		}
	}
	while (1) {
		// cout << p[1]<<" "<<p[2]<<" "<<p[3]<<" "<<ans<<endl;
		Rep(i, 1, 3) 
			Rep(j, 1, 3) {
				while (!w[j][i].empty() && c[ w[j][i].top().sd ] != j) w[j][i].pop();
			}
		bool fl = 1;
		ll mx = -(1ll << 60);
		Rep(i, 1, 3) if (p[i] > 0) {
			fl = 0;
			Rep(j, 1, 3) if (i != j && !w[j][i].empty()) {
				if (p[j] < 0) {
					if (w[j][i].top().fr > mx) {
						mx = w[j][i].top().fr;
						mp = i, mq = j; mr = 0;
					}
				}
				Rep(k, 1, 3) if (i != j && j != k && p[k] < 0 && !w[k][j].empty()) {
					if (w[j][i].top().fr + w[k][j].top().fr > mx) {
						mx = w[j][i].top().fr + w[k][j].top().fr;
						mp = i, mq = j, mr = k;
					}
				}
			}
			break ;
		}
		if (fl) break ;
		ans += mx;
		if (mr) {
			int x = w[mr][mq].top().sd;
			c[x] = mq; p[mr] ++; p[mq] --;
			add(x, mq);
		}
		int x = w[mq][mp].top().sd;
		c[x] = mp; p[mq] ++; p[mp] --;
		add(x, mp);
	}
	cout << ans << endl;

	return 0;
}

Compiler output:
a.cc: In function 'void add(int, int)':
a.cc:30:57: error: cannot bind rvalue reference of type 'int&&' to lvalue of type 'int'
   30 |                 w[j][k].push( mk((ll)a[i][k] - a[i][j], i0) );
      |                                                         ^~
In file included from /usr/include/c++/14/bits/stl_algobase.h:64,
                 from /usr/include/c++/14/string:51,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_pair.h:1132:32: note:   initializing argument 2 of 'constexpr std::pair<typename std::__strip_reference_wrapper<typename std::decay<_Tp>::type>::__type, typename std::__strip_reference_wrapper<typename std::decay<_Tp2>::type>::__type> std::make_pair(_T1&&, _T2&&) [with _T1 = long long int; _T2 = int; typename __strip_reference_wrapper<typename decay<_Tp>::type>::__type = long long int; typename decay<_Tp>::type = long long int; typename __strip_reference_wrapper<typename decay<_Tp2>::type>::__type = int; typename decay<_Tp2>::type = int]'
 1132 |     make_pair(_T1&& __x, _T2&& __y)
      |                          ~~~~~~^~~