#include<bits/stdc++.h>
#define ll long long
#define task ""
using namespace std;
const int maxn = 1e6 + 2;
int n, x[maxn], y[maxn], z[maxn];
priority_queue<pair<int, int>> pq[3];
bool deleted[maxn];
int main()
{
//freopen(task".INP", "r", stdin);
//freopen(task".OUT", "w", stdout);
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> x[i] >> y[i] >> z[i];
pq[0].push({x[i], i});
pq[1].push({y[i], i});
pq[2].push({z[i], i});
}
while (!pq[0].empty() && !pq[1].empty() && !pq[2].empty())
{
while (!pq[0].empty() && deleted[pq[0].top().second]) pq[0].pop();
while (!pq[1].empty() && deleted[pq[1].top().second]) pq[1].pop();
while (!pq[2].empty() && deleted[pq[2].top().second]) pq[2].pop();
if (pq[0].empty() || pq[1].empty() || pq[2].empty()) break;
if (pq[0].top().second == pq[1].top().second)
{
deleted[pq[0].top().second] = true;
pq[0].pop(); pq[1].pop();
continue;
}
if (pq[1].top().second == pq[2].top().second)
{
deleted[pq[1].top().second] = true;
pq[1].pop(); pq[2].pop();
continue;
}
if (pq[2].top().second == pq[0].top().second)
{
deleted[pq[2].top().second] = true;
pq[2].pop(); pq[0].pop();
continue;
}
if (y[pq[0].top().second] == pq[1].top().first)
{
deleted[pq[0].top().second] = true;
pq[0].pop();
continue;
}
if (y[pq[0].top().second] == pq[1].top().first || z[pq[0].top().second] == pq[2].top().first)
{
deleted[pq[0].top().second] = true;
pq[0].pop();
continue;
}
if (x[pq[1].top().second] == pq[0].top().first || z[pq[1].top().second] == pq[2].top().first)
{
deleted[pq[1].top().second] = true;
pq[1].pop();
continue;
}
if (x[pq[2].top().second] == pq[0].top().first || y[pq[2].top().second] == pq[1].top().first)
{
deleted[pq[2].top().second] = true;
pq[2].pop();
continue;
}
cout << pq[0].top().first + pq[1].top().first + pq[2].top().first;
return 0;
}
cout << -1;
}
| # | 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... |