This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pi pair <int, int>
pair <pi, int> p[150005];
bool cmp1(int x, int y){
	return p[x].fi.fi > p[y].fi.fi;
}
bool cmp2(int x, int y){
	return p[x].fi.se > p[y].fi.se;
}
bool cmp3(int x, int y){
	return p[x].se > p[y].se;
}
int32_t main(){
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	int n, ptr1 = 0, ptr2 = 0, ptr3 = 0, xp, yp, zp, markcnt = 0; cin >> n;
	bool marked[n];
	fill(marked, marked + n, 0);
	for (int i = 0; i < n; i++){
		cin >> p[i].fi.fi >> p[i].fi.se >> p[i].se;
	}
	int a[n], b[n], c[n];
	for (int i = 0; i < n; i++) a[i] = b[i] = c[i] = i;
	sort(a, a + n, cmp1);
	sort(b, b + n, cmp2);
	sort(c, c + n, cmp3);
	while (ptr1 != n and ptr2 != n and ptr3 != n){
		while (marked[a[ptr1]]) ptr1++;
		while (marked[b[ptr2]]) ptr2++;
		while (marked[c[ptr3]]) ptr3++;
		xp = a[ptr1]; yp = b[ptr2]; zp = c[ptr3];
		//cerr << xp << ' ' << yp << ' ' << zp << '\n';
		if (p[xp].fi.fi <= max(p[yp].fi.fi, p[zp].fi.fi)){
			if (p[xp].fi.fi <= p[yp].fi.fi) marked[yp] = true;
			else marked[zp] = true;
			markcnt += 1;
			if (markcnt == n){
				cout << -1;
				return 0;
			}
			continue;
		}
		if (p[yp].fi.se <= max(p[xp].fi.se, p[zp].fi.se)){
			if (p[yp].fi.se <= p[xp].fi.se) marked[xp] = true;
			else marked[zp] = true;
			markcnt += 1;
			if (markcnt == n){
				cout << -1;
				return 0;
			}
			continue;
		}
		if (p[zp].se <= max(p[xp].se, p[yp].se)){
			if (p[zp].se <= p[xp].se) marked[xp] = true;
			else marked[yp] = true;
			markcnt += 1;
			if (markcnt == n){
				cout << -1;
				return 0;
			}
			continue;
		}
		cout << p[xp].fi.fi + p[yp].fi.se + p[zp].se;
		return 0;
	}
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |