#include <bits/stdc++.h>
#define int long long
using namespace std;
struct Val {
int val, id;
};
const int N = 2e5 + 42, INF = 1e18 + 42;
bool b[N];
deque<Val> v[3];
int n, val[N][3];
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for(int i = 0; i < n; i++)
for(int j = 0; j < 3; j++) {
cin >> val[i][j];
v[j].push_back({val[i][j], i});
}
for(int i = 0; i < 3; i++)
sort(v[i].begin(), v[i].end(),
[](Val a, Val b) {
return a.val < b.val;
});
while(!v[0].empty() && !v[1].empty() && !v[2].empty()) {
bool continuer = false;
for(int i = 0; i < 3; i++)
for(int j = i+1; j < 3; j++) {
if(val[v[i].back().id][j] == val[v[j].back().id][j]) {
continuer = true;
b[v[i].back().id] = true;
}
if(val[v[i].back().id][i] == val[v[j].back().id][i]) {
continuer = true;
b[v[j].back().id] = true;
}
}
if(!continuer)
break;
for(int i = 0; i < 3; i++)
while(!v[i].empty() && b[v[i].back().id])
v[i].pop_back();
}
if(v[0].empty() || v[1].empty() || v[2].empty())
cout << -1;
else
cout << v[0].back().val + v[1].back().val + v[2].back().val;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
3 ms |
724 KB |
Output is correct |
27 |
Correct |
3 ms |
736 KB |
Output is correct |
28 |
Correct |
3 ms |
724 KB |
Output is correct |
29 |
Correct |
3 ms |
732 KB |
Output is correct |
30 |
Correct |
2 ms |
468 KB |
Output is correct |
31 |
Correct |
3 ms |
596 KB |
Output is correct |
32 |
Correct |
3 ms |
596 KB |
Output is correct |
33 |
Correct |
3 ms |
732 KB |
Output is correct |
34 |
Correct |
3 ms |
732 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
2 ms |
348 KB |
Output is correct |
37 |
Correct |
2 ms |
576 KB |
Output is correct |
38 |
Correct |
3 ms |
572 KB |
Output is correct |
39 |
Correct |
1 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
47 ms |
12128 KB |
Output is correct |
12 |
Correct |
32 ms |
8276 KB |
Output is correct |
13 |
Correct |
34 ms |
10060 KB |
Output is correct |
14 |
Correct |
57 ms |
12196 KB |
Output is correct |
15 |
Correct |
46 ms |
12236 KB |
Output is correct |
16 |
Correct |
65 ms |
12180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
47 ms |
12128 KB |
Output is correct |
12 |
Correct |
32 ms |
8276 KB |
Output is correct |
13 |
Correct |
34 ms |
10060 KB |
Output is correct |
14 |
Correct |
57 ms |
12196 KB |
Output is correct |
15 |
Correct |
46 ms |
12236 KB |
Output is correct |
16 |
Correct |
65 ms |
12180 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
328 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
54 ms |
12256 KB |
Output is correct |
23 |
Correct |
57 ms |
11976 KB |
Output is correct |
24 |
Correct |
52 ms |
9028 KB |
Output is correct |
25 |
Correct |
67 ms |
12484 KB |
Output is correct |
26 |
Correct |
54 ms |
12516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
47 ms |
12128 KB |
Output is correct |
12 |
Correct |
32 ms |
8276 KB |
Output is correct |
13 |
Correct |
34 ms |
10060 KB |
Output is correct |
14 |
Correct |
57 ms |
12196 KB |
Output is correct |
15 |
Correct |
46 ms |
12236 KB |
Output is correct |
16 |
Correct |
65 ms |
12180 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
328 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
54 ms |
12256 KB |
Output is correct |
23 |
Correct |
57 ms |
11976 KB |
Output is correct |
24 |
Correct |
52 ms |
9028 KB |
Output is correct |
25 |
Correct |
67 ms |
12484 KB |
Output is correct |
26 |
Correct |
54 ms |
12516 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
2 ms |
604 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
59 ms |
12236 KB |
Output is correct |
35 |
Correct |
62 ms |
11888 KB |
Output is correct |
36 |
Correct |
69 ms |
12920 KB |
Output is correct |
37 |
Correct |
92 ms |
13044 KB |
Output is correct |
38 |
Correct |
70 ms |
12688 KB |
Output is correct |
39 |
Correct |
32 ms |
7860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
47 ms |
12128 KB |
Output is correct |
12 |
Correct |
32 ms |
8276 KB |
Output is correct |
13 |
Correct |
34 ms |
10060 KB |
Output is correct |
14 |
Correct |
57 ms |
12196 KB |
Output is correct |
15 |
Correct |
46 ms |
12236 KB |
Output is correct |
16 |
Correct |
65 ms |
12180 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
328 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
54 ms |
12256 KB |
Output is correct |
23 |
Correct |
57 ms |
11976 KB |
Output is correct |
24 |
Correct |
52 ms |
9028 KB |
Output is correct |
25 |
Correct |
67 ms |
12484 KB |
Output is correct |
26 |
Correct |
54 ms |
12516 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
2 ms |
604 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
59 ms |
12236 KB |
Output is correct |
35 |
Correct |
62 ms |
11888 KB |
Output is correct |
36 |
Correct |
69 ms |
12920 KB |
Output is correct |
37 |
Correct |
92 ms |
13044 KB |
Output is correct |
38 |
Correct |
70 ms |
12688 KB |
Output is correct |
39 |
Correct |
32 ms |
7860 KB |
Output is correct |
40 |
Correct |
2 ms |
468 KB |
Output is correct |
41 |
Correct |
2 ms |
600 KB |
Output is correct |
42 |
Correct |
2 ms |
596 KB |
Output is correct |
43 |
Correct |
3 ms |
596 KB |
Output is correct |
44 |
Correct |
79 ms |
13260 KB |
Output is correct |
45 |
Correct |
86 ms |
13456 KB |
Output is correct |
46 |
Correct |
83 ms |
13460 KB |
Output is correct |
47 |
Correct |
90 ms |
13456 KB |
Output is correct |
48 |
Correct |
73 ms |
13388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
3 ms |
724 KB |
Output is correct |
27 |
Correct |
3 ms |
736 KB |
Output is correct |
28 |
Correct |
3 ms |
724 KB |
Output is correct |
29 |
Correct |
3 ms |
732 KB |
Output is correct |
30 |
Correct |
2 ms |
468 KB |
Output is correct |
31 |
Correct |
3 ms |
596 KB |
Output is correct |
32 |
Correct |
3 ms |
596 KB |
Output is correct |
33 |
Correct |
3 ms |
732 KB |
Output is correct |
34 |
Correct |
3 ms |
732 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
2 ms |
348 KB |
Output is correct |
37 |
Correct |
2 ms |
576 KB |
Output is correct |
38 |
Correct |
3 ms |
572 KB |
Output is correct |
39 |
Correct |
1 ms |
596 KB |
Output is correct |
40 |
Correct |
0 ms |
212 KB |
Output is correct |
41 |
Correct |
0 ms |
212 KB |
Output is correct |
42 |
Correct |
0 ms |
212 KB |
Output is correct |
43 |
Correct |
0 ms |
212 KB |
Output is correct |
44 |
Correct |
1 ms |
212 KB |
Output is correct |
45 |
Correct |
1 ms |
212 KB |
Output is correct |
46 |
Correct |
1 ms |
212 KB |
Output is correct |
47 |
Correct |
1 ms |
212 KB |
Output is correct |
48 |
Correct |
1 ms |
212 KB |
Output is correct |
49 |
Correct |
0 ms |
212 KB |
Output is correct |
50 |
Correct |
47 ms |
12128 KB |
Output is correct |
51 |
Correct |
32 ms |
8276 KB |
Output is correct |
52 |
Correct |
34 ms |
10060 KB |
Output is correct |
53 |
Correct |
57 ms |
12196 KB |
Output is correct |
54 |
Correct |
46 ms |
12236 KB |
Output is correct |
55 |
Correct |
65 ms |
12180 KB |
Output is correct |
56 |
Correct |
1 ms |
212 KB |
Output is correct |
57 |
Correct |
1 ms |
212 KB |
Output is correct |
58 |
Correct |
1 ms |
328 KB |
Output is correct |
59 |
Correct |
1 ms |
340 KB |
Output is correct |
60 |
Correct |
1 ms |
468 KB |
Output is correct |
61 |
Correct |
54 ms |
12256 KB |
Output is correct |
62 |
Correct |
57 ms |
11976 KB |
Output is correct |
63 |
Correct |
52 ms |
9028 KB |
Output is correct |
64 |
Correct |
67 ms |
12484 KB |
Output is correct |
65 |
Correct |
54 ms |
12516 KB |
Output is correct |
66 |
Correct |
1 ms |
340 KB |
Output is correct |
67 |
Correct |
1 ms |
340 KB |
Output is correct |
68 |
Correct |
1 ms |
340 KB |
Output is correct |
69 |
Correct |
1 ms |
340 KB |
Output is correct |
70 |
Correct |
2 ms |
604 KB |
Output is correct |
71 |
Correct |
1 ms |
340 KB |
Output is correct |
72 |
Correct |
1 ms |
340 KB |
Output is correct |
73 |
Correct |
59 ms |
12236 KB |
Output is correct |
74 |
Correct |
62 ms |
11888 KB |
Output is correct |
75 |
Correct |
69 ms |
12920 KB |
Output is correct |
76 |
Correct |
92 ms |
13044 KB |
Output is correct |
77 |
Correct |
70 ms |
12688 KB |
Output is correct |
78 |
Correct |
32 ms |
7860 KB |
Output is correct |
79 |
Correct |
2 ms |
468 KB |
Output is correct |
80 |
Correct |
2 ms |
600 KB |
Output is correct |
81 |
Correct |
2 ms |
596 KB |
Output is correct |
82 |
Correct |
3 ms |
596 KB |
Output is correct |
83 |
Correct |
79 ms |
13260 KB |
Output is correct |
84 |
Correct |
86 ms |
13456 KB |
Output is correct |
85 |
Correct |
83 ms |
13460 KB |
Output is correct |
86 |
Correct |
90 ms |
13456 KB |
Output is correct |
87 |
Correct |
73 ms |
13388 KB |
Output is correct |
88 |
Correct |
97 ms |
15160 KB |
Output is correct |
89 |
Correct |
99 ms |
15236 KB |
Output is correct |
90 |
Correct |
101 ms |
15204 KB |
Output is correct |
91 |
Correct |
125 ms |
15180 KB |
Output is correct |
92 |
Correct |
89 ms |
13744 KB |
Output is correct |
93 |
Correct |
108 ms |
14148 KB |
Output is correct |
94 |
Correct |
107 ms |
13984 KB |
Output is correct |
95 |
Correct |
76 ms |
15096 KB |
Output is correct |
96 |
Correct |
94 ms |
15304 KB |
Output is correct |
97 |
Correct |
73 ms |
15340 KB |
Output is correct |
98 |
Correct |
60 ms |
13792 KB |
Output is correct |
99 |
Correct |
138 ms |
13916 KB |
Output is correct |