#include <bits/stdc++.h>
#define pb push_back
#define ll long long
#define s second
#define f first
#define left (node<<1),l,((l+r)>>1)
#define right ((node<<1)|1),((l+r)>>1) + 1,r
// #define int ll
using namespace std;
const int N = 150005;
bool f[N],fix[N];
int x[N],y[N],z[N],cnt[N],mx[6],mxold[6];
int d[3*N],rev[3*N];
map <int,int> mp;
multiset <int> st[5];
vector <int> v[5][N];
signed main() {
ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL);
int n;
cin>>n;
int id=0;
for (int i = 1; i <= n; i++){
cin>>x[i]>>y[i]>>z[i];
d[++id] = x[i];
d[++id] = y[i];
d[++id] = z[i];
}
sort(d+1,d+id+1);
int val=0;
for (int i = 1; i <= id; i++){
if (i == 1 || d[i] != d[i - 1]) ++val;
mp[d[i]] = val;
rev[val] = d[i];
}
for (int i = 1; i <= n; i++){
x[i] = mp[x[i]];
y[i] = mp[y[i]];
z[i] = mp[z[i]];
mx[1] = max(mx[1],x[i]);
mx[2] = max(mx[2],y[i]);
mx[3] = max(mx[3],z[i]);
}
queue <int> q;
for (int i = 1; i <= n; i++){
if (x[i] == mx[1]) ++cnt[i];
if (y[i] == mx[2]) ++cnt[i];
if (z[i] == mx[3]) ++cnt[i];
if (cnt[i] > 1) {
q.push(i),fix[i]=1;
}
v[1][x[i]].pb(i);
v[2][y[i]].pb(i);
v[3][z[i]].pb(i);
st[1].insert(x[i]);
st[2].insert(y[i]);
st[3].insert(z[i]);
}
while (q.size()){
int idx = q.front();
q.pop();
if (f[idx]) continue;
f[idx] = 1;
st[1].erase(st[1].find(x[idx]));
st[2].erase(st[2].find(y[idx]));
st[3].erase(st[3].find(z[idx]));
if(st[1].size() == 0){
cout<<-1;
exit(0);
}
for (int j = 1; j <= 3; j++){
mxold[j] = mx[j];
mx[j] = *st[j].rbegin();
}
for (int j = 1; j <= 3; j++){
if (mxold[j] == mx[j]) continue;
for (int p: v[j][mx[j]]){
if (!fix[p]){
cnt[p] = 0;
for (int k = 1; k <= 3; k++)
cnt[p] += (mx[k] == x[k]);
if (cnt[p] >= 2) {
fix[p] = 1;
q.push(p);
}
}
}
}
}
cout<<rev[mx[1]] + rev[mx[2]] + rev[mx[3]];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
18004 KB |
Output is correct |
2 |
Correct |
8 ms |
17896 KB |
Output is correct |
3 |
Correct |
8 ms |
17876 KB |
Output is correct |
4 |
Correct |
8 ms |
17876 KB |
Output is correct |
5 |
Correct |
7 ms |
17872 KB |
Output is correct |
6 |
Incorrect |
9 ms |
17876 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
18004 KB |
Output is correct |
2 |
Correct |
8 ms |
17896 KB |
Output is correct |
3 |
Correct |
8 ms |
17876 KB |
Output is correct |
4 |
Correct |
8 ms |
17876 KB |
Output is correct |
5 |
Correct |
7 ms |
17872 KB |
Output is correct |
6 |
Incorrect |
9 ms |
17876 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
17876 KB |
Output is correct |
2 |
Correct |
9 ms |
17940 KB |
Output is correct |
3 |
Correct |
7 ms |
17952 KB |
Output is correct |
4 |
Correct |
7 ms |
17912 KB |
Output is correct |
5 |
Correct |
8 ms |
17876 KB |
Output is correct |
6 |
Incorrect |
8 ms |
17876 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
17876 KB |
Output is correct |
2 |
Correct |
9 ms |
17940 KB |
Output is correct |
3 |
Correct |
7 ms |
17952 KB |
Output is correct |
4 |
Correct |
7 ms |
17912 KB |
Output is correct |
5 |
Correct |
8 ms |
17876 KB |
Output is correct |
6 |
Incorrect |
8 ms |
17876 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
17876 KB |
Output is correct |
2 |
Correct |
9 ms |
17940 KB |
Output is correct |
3 |
Correct |
7 ms |
17952 KB |
Output is correct |
4 |
Correct |
7 ms |
17912 KB |
Output is correct |
5 |
Correct |
8 ms |
17876 KB |
Output is correct |
6 |
Incorrect |
8 ms |
17876 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
17876 KB |
Output is correct |
2 |
Correct |
9 ms |
17940 KB |
Output is correct |
3 |
Correct |
7 ms |
17952 KB |
Output is correct |
4 |
Correct |
7 ms |
17912 KB |
Output is correct |
5 |
Correct |
8 ms |
17876 KB |
Output is correct |
6 |
Incorrect |
8 ms |
17876 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
18004 KB |
Output is correct |
2 |
Correct |
8 ms |
17896 KB |
Output is correct |
3 |
Correct |
8 ms |
17876 KB |
Output is correct |
4 |
Correct |
8 ms |
17876 KB |
Output is correct |
5 |
Correct |
7 ms |
17872 KB |
Output is correct |
6 |
Incorrect |
9 ms |
17876 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |