#include <bits/stdc++.h>
using namespace std;
int main() {
// step #1. read input
cin.tie(0);
ios_base::sync_with_stdio(false);
int N;
cin >> N;
vector<int> X(N), Y(N), Z(N);
for (int i = 0; i < N; i++) {
cin >> X[i] >> Y[i] >> Z[i];
}
// step #2. erase from the top
vector<int> px(N), py(N), pz(N);
for (int i = 0; i < N; i++) {
px[i] = i;
py[i] = i;
pz[i] = i;
}
sort(px.begin(), px.end(), [&](int i, int j) { return X[i] < X[j]; });
sort(py.begin(), py.end(), [&](int i, int j) { return Y[i] < Y[j]; });
sort(pz.begin(), pz.end(), [&](int i, int j) { return Z[i] < Z[j]; });
vector<bool> used(N, false);
int tx = N - 1, ty = N - 1, tz = N - 1;
bool found = false;
while (tx != -1 && ty != -1 && tz != -1) {
int mx = X[px[tx]], my = Y[py[ty]], mz = Z[pz[tz]];
int cntx = 1 + (Y[px[tx]] == my ? 1 : 0) + (Z[px[tx]] == mz ? 1 : 0);
int cnty = 1 + (Z[py[ty]] == mz ? 1 : 0) + (X[py[ty]] == mx ? 1 : 0);
int cntz = 1 + (X[pz[tz]] == mx ? 1 : 0) + (Y[pz[tz]] == my ? 1 : 0);
if (cntx >= 2) {
used[px[tx]] = true;
tx--;
} else if (cnty >= 2) {
used[py[ty]] = true;
ty--;
} else if (cntz >= 2) {
used[pz[tz]] = true;
tz--;
} else {
cout << mx + my + mz << endl;
found = true;
break;
}
while (tx != -1 && used[px[tx]])
tx--;
while (ty != -1 && used[py[ty]])
ty--;
while (tz != -1 && used[pz[tz]])
tz--;
}
if (!found) {
cout << -1 << endl;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
600 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
600 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
2 ms |
604 KB |
Output is correct |
27 |
Correct |
2 ms |
604 KB |
Output is correct |
28 |
Correct |
2 ms |
604 KB |
Output is correct |
29 |
Correct |
3 ms |
604 KB |
Output is correct |
30 |
Correct |
1 ms |
344 KB |
Output is correct |
31 |
Correct |
2 ms |
348 KB |
Output is correct |
32 |
Correct |
2 ms |
348 KB |
Output is correct |
33 |
Correct |
2 ms |
604 KB |
Output is correct |
34 |
Correct |
2 ms |
604 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
2 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
456 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
600 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
29 ms |
4884 KB |
Output is correct |
12 |
Correct |
21 ms |
3292 KB |
Output is correct |
13 |
Correct |
22 ms |
3932 KB |
Output is correct |
14 |
Correct |
30 ms |
4884 KB |
Output is correct |
15 |
Correct |
27 ms |
4700 KB |
Output is correct |
16 |
Correct |
24 ms |
4696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
456 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
600 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
29 ms |
4884 KB |
Output is correct |
12 |
Correct |
21 ms |
3292 KB |
Output is correct |
13 |
Correct |
22 ms |
3932 KB |
Output is correct |
14 |
Correct |
30 ms |
4884 KB |
Output is correct |
15 |
Correct |
27 ms |
4700 KB |
Output is correct |
16 |
Correct |
24 ms |
4696 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
660 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
37 ms |
4872 KB |
Output is correct |
23 |
Correct |
34 ms |
4700 KB |
Output is correct |
24 |
Correct |
27 ms |
3664 KB |
Output is correct |
25 |
Correct |
39 ms |
4908 KB |
Output is correct |
26 |
Correct |
29 ms |
4944 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
456 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
600 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
29 ms |
4884 KB |
Output is correct |
12 |
Correct |
21 ms |
3292 KB |
Output is correct |
13 |
Correct |
22 ms |
3932 KB |
Output is correct |
14 |
Correct |
30 ms |
4884 KB |
Output is correct |
15 |
Correct |
27 ms |
4700 KB |
Output is correct |
16 |
Correct |
24 ms |
4696 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
660 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
37 ms |
4872 KB |
Output is correct |
23 |
Correct |
34 ms |
4700 KB |
Output is correct |
24 |
Correct |
27 ms |
3664 KB |
Output is correct |
25 |
Correct |
39 ms |
4908 KB |
Output is correct |
26 |
Correct |
29 ms |
4944 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
2 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
344 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
43 ms |
5340 KB |
Output is correct |
35 |
Correct |
43 ms |
4956 KB |
Output is correct |
36 |
Correct |
45 ms |
5468 KB |
Output is correct |
37 |
Correct |
51 ms |
5712 KB |
Output is correct |
38 |
Correct |
37 ms |
5468 KB |
Output is correct |
39 |
Correct |
19 ms |
3420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
456 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
600 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
29 ms |
4884 KB |
Output is correct |
12 |
Correct |
21 ms |
3292 KB |
Output is correct |
13 |
Correct |
22 ms |
3932 KB |
Output is correct |
14 |
Correct |
30 ms |
4884 KB |
Output is correct |
15 |
Correct |
27 ms |
4700 KB |
Output is correct |
16 |
Correct |
24 ms |
4696 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
660 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
37 ms |
4872 KB |
Output is correct |
23 |
Correct |
34 ms |
4700 KB |
Output is correct |
24 |
Correct |
27 ms |
3664 KB |
Output is correct |
25 |
Correct |
39 ms |
4908 KB |
Output is correct |
26 |
Correct |
29 ms |
4944 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
2 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
344 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
43 ms |
5340 KB |
Output is correct |
35 |
Correct |
43 ms |
4956 KB |
Output is correct |
36 |
Correct |
45 ms |
5468 KB |
Output is correct |
37 |
Correct |
51 ms |
5712 KB |
Output is correct |
38 |
Correct |
37 ms |
5468 KB |
Output is correct |
39 |
Correct |
19 ms |
3420 KB |
Output is correct |
40 |
Correct |
2 ms |
344 KB |
Output is correct |
41 |
Correct |
2 ms |
344 KB |
Output is correct |
42 |
Correct |
2 ms |
604 KB |
Output is correct |
43 |
Correct |
2 ms |
604 KB |
Output is correct |
44 |
Correct |
54 ms |
6056 KB |
Output is correct |
45 |
Correct |
57 ms |
6064 KB |
Output is correct |
46 |
Correct |
57 ms |
6168 KB |
Output is correct |
47 |
Correct |
58 ms |
5972 KB |
Output is correct |
48 |
Correct |
39 ms |
5772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
600 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
2 ms |
604 KB |
Output is correct |
27 |
Correct |
2 ms |
604 KB |
Output is correct |
28 |
Correct |
2 ms |
604 KB |
Output is correct |
29 |
Correct |
3 ms |
604 KB |
Output is correct |
30 |
Correct |
1 ms |
344 KB |
Output is correct |
31 |
Correct |
2 ms |
348 KB |
Output is correct |
32 |
Correct |
2 ms |
348 KB |
Output is correct |
33 |
Correct |
2 ms |
604 KB |
Output is correct |
34 |
Correct |
2 ms |
604 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
2 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |
42 |
Correct |
1 ms |
456 KB |
Output is correct |
43 |
Correct |
1 ms |
348 KB |
Output is correct |
44 |
Correct |
0 ms |
600 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
1 ms |
348 KB |
Output is correct |
48 |
Correct |
1 ms |
344 KB |
Output is correct |
49 |
Correct |
1 ms |
348 KB |
Output is correct |
50 |
Correct |
29 ms |
4884 KB |
Output is correct |
51 |
Correct |
21 ms |
3292 KB |
Output is correct |
52 |
Correct |
22 ms |
3932 KB |
Output is correct |
53 |
Correct |
30 ms |
4884 KB |
Output is correct |
54 |
Correct |
27 ms |
4700 KB |
Output is correct |
55 |
Correct |
24 ms |
4696 KB |
Output is correct |
56 |
Correct |
1 ms |
348 KB |
Output is correct |
57 |
Correct |
0 ms |
348 KB |
Output is correct |
58 |
Correct |
1 ms |
660 KB |
Output is correct |
59 |
Correct |
1 ms |
348 KB |
Output is correct |
60 |
Correct |
1 ms |
344 KB |
Output is correct |
61 |
Correct |
37 ms |
4872 KB |
Output is correct |
62 |
Correct |
34 ms |
4700 KB |
Output is correct |
63 |
Correct |
27 ms |
3664 KB |
Output is correct |
64 |
Correct |
39 ms |
4908 KB |
Output is correct |
65 |
Correct |
29 ms |
4944 KB |
Output is correct |
66 |
Correct |
1 ms |
348 KB |
Output is correct |
67 |
Correct |
0 ms |
348 KB |
Output is correct |
68 |
Correct |
0 ms |
348 KB |
Output is correct |
69 |
Correct |
1 ms |
348 KB |
Output is correct |
70 |
Correct |
2 ms |
468 KB |
Output is correct |
71 |
Correct |
1 ms |
344 KB |
Output is correct |
72 |
Correct |
1 ms |
348 KB |
Output is correct |
73 |
Correct |
43 ms |
5340 KB |
Output is correct |
74 |
Correct |
43 ms |
4956 KB |
Output is correct |
75 |
Correct |
45 ms |
5468 KB |
Output is correct |
76 |
Correct |
51 ms |
5712 KB |
Output is correct |
77 |
Correct |
37 ms |
5468 KB |
Output is correct |
78 |
Correct |
19 ms |
3420 KB |
Output is correct |
79 |
Correct |
2 ms |
344 KB |
Output is correct |
80 |
Correct |
2 ms |
344 KB |
Output is correct |
81 |
Correct |
2 ms |
604 KB |
Output is correct |
82 |
Correct |
2 ms |
604 KB |
Output is correct |
83 |
Correct |
54 ms |
6056 KB |
Output is correct |
84 |
Correct |
57 ms |
6064 KB |
Output is correct |
85 |
Correct |
57 ms |
6168 KB |
Output is correct |
86 |
Correct |
58 ms |
5972 KB |
Output is correct |
87 |
Correct |
39 ms |
5772 KB |
Output is correct |
88 |
Correct |
70 ms |
7760 KB |
Output is correct |
89 |
Correct |
76 ms |
7768 KB |
Output is correct |
90 |
Correct |
70 ms |
7764 KB |
Output is correct |
91 |
Correct |
75 ms |
7856 KB |
Output is correct |
92 |
Correct |
64 ms |
6232 KB |
Output is correct |
93 |
Correct |
74 ms |
6676 KB |
Output is correct |
94 |
Correct |
69 ms |
6480 KB |
Output is correct |
95 |
Correct |
51 ms |
7720 KB |
Output is correct |
96 |
Correct |
72 ms |
7880 KB |
Output is correct |
97 |
Correct |
40 ms |
7764 KB |
Output is correct |
98 |
Correct |
34 ms |
6300 KB |
Output is correct |
99 |
Correct |
79 ms |
6484 KB |
Output is correct |