#include <bits/stdc++.h>
#define int long long
using namespace std;
int a;
vector <pair<int,int>> z;
vector <pair<int,int>> z1;
vector <pair<int,int>> z2;
struct node{
int x,y,t;
};
node sadge[1000005];
int del[1000005];
int max1=-1;
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> a;
for (int i=1;i<=a;i++){
int x,y,t;
cin >> x >> y >> t;
z.push_back({x,i});
z1.push_back({y,i});
z2.push_back({t,i});
sadge[i]={x,y,t};
}
sort(z.begin(),z.end());
sort(z1.begin(),z1.end());
sort(z2.begin(),z2.end());
while (z.size() && z1.size() && z2.size()){
// cerr << z.back().second << " " << z1.back().second << " " << z2.back().second << "\n";
if (del[z.back().second]){
z.pop_back();
continue;
}else if (del[z1.back().second]){
z1.pop_back();
continue;
}else if (del[z2.back().second]){
z2.pop_back();
continue;
}
if (z.back().second==z1.back().second){
del[z.back().second]=1;
z.pop_back();
z1.pop_back();
continue;
}
if (z.back().second==z2.back().second){
del[z.back().second]=1;
z.pop_back();
z2.pop_back();
continue;
}
if (z1.back().second==z2.back().second){
del[z1.back().second]=1;
z1.pop_back();
z2.pop_back();
continue;
}
int x=z.back().second;
int y=z1.back().second;
int t=z2.back().second;
if (sadge[y].x==sadge[x].x || sadge[y].t==sadge[t].t){
del[y]=1;
z1.pop_back();
continue;
}
if (sadge[x].y==sadge[y].y || sadge[x].t==sadge[t].t){
del[x]=1;
z.pop_back();
continue;
}
if (sadge[t].x==sadge[x].x || sadge[t].y==sadge[y].y){
del[t]=1;
z2.pop_back();
continue;
}
cout << z1.back().first+z2.back().first+z.back().first << "\n" ;
return 0;
}
cout << -1 << "\n";
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... |