#include <bits/stdc++.h>
using namespace std;
int n, m[3];
bool in[150001];
array<int, 3> a[150001];
multiset<int> st[3];
map<int, vector<int>> mp[3];
queue<int> q;
int gm(multiset<int>& sus) {
if (sus.empty()) {
cout << "-1\n";
exit(0);
}
return *sus.rbegin();
}
int32_t main() {
ios::sync_with_stdio(0); cin.tie(0);
cin >> n;
for (int i = 0; i < n; i++) {
in[i] = 1;
for (int j = 0; j < 3; j++) {
cin >> a[i][j];
st[j].insert(a[i][j]);
mp[j][a[i][j]].push_back(i);
m[j] = max(m[j], a[i][j]);
}
}
for (int i = 0; i < n; i++) {
int cnt = 0;
for (int j = 0; j < 3; j++) if (a[i][j] == m[j]) cnt++;
if (cnt >= 2) q.push(i);
}
while (!q.empty()) {
int i = q.front();
q.pop();
if (!in[i]) continue;
in[i] = 0;
for (int j = 0; j < 3; j++) {
st[j].erase(st[j].find(a[i][j]));
if (gm(st[j]) < m[j]) {
m[j] = gm(st[j]);
for (int x : mp[j][m[j]]) {
if (!in[x]) continue;
int cnt = 0;
for (int k = 0; k < 3; k++) if (a[x][k] == m[k]) cnt++;
if (cnt >= 2) q.push(x);
}
}
}
}
if ((int) st[0].size() < 3) cout << "-1\n";
else cout << m[0] + m[1] + m[2] << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 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 |
0 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 |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
604 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
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 |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 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 |
0 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 |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
604 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
6 ms |
2396 KB |
Output is correct |
27 |
Correct |
6 ms |
1940 KB |
Output is correct |
28 |
Correct |
6 ms |
1884 KB |
Output is correct |
29 |
Correct |
6 ms |
1624 KB |
Output is correct |
30 |
Correct |
4 ms |
1204 KB |
Output is correct |
31 |
Correct |
5 ms |
1372 KB |
Output is correct |
32 |
Correct |
4 ms |
1116 KB |
Output is correct |
33 |
Correct |
6 ms |
1500 KB |
Output is correct |
34 |
Correct |
5 ms |
2396 KB |
Output is correct |
35 |
Correct |
1 ms |
604 KB |
Output is correct |
36 |
Correct |
1 ms |
604 KB |
Output is correct |
37 |
Correct |
3 ms |
1372 KB |
Output is correct |
38 |
Correct |
4 ms |
1588 KB |
Output is correct |
39 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 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 |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
129 ms |
26216 KB |
Output is correct |
12 |
Correct |
95 ms |
17476 KB |
Output is correct |
13 |
Correct |
75 ms |
21588 KB |
Output is correct |
14 |
Correct |
150 ms |
26300 KB |
Output is correct |
15 |
Correct |
129 ms |
26556 KB |
Output is correct |
16 |
Correct |
120 ms |
26572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 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 |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
129 ms |
26216 KB |
Output is correct |
12 |
Correct |
95 ms |
17476 KB |
Output is correct |
13 |
Correct |
75 ms |
21588 KB |
Output is correct |
14 |
Correct |
150 ms |
26300 KB |
Output is correct |
15 |
Correct |
129 ms |
26556 KB |
Output is correct |
16 |
Correct |
120 ms |
26572 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 |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
732 KB |
Output is correct |
22 |
Correct |
145 ms |
26448 KB |
Output is correct |
23 |
Correct |
142 ms |
25536 KB |
Output is correct |
24 |
Correct |
136 ms |
19272 KB |
Output is correct |
25 |
Correct |
213 ms |
26444 KB |
Output is correct |
26 |
Correct |
120 ms |
26752 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 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 |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
129 ms |
26216 KB |
Output is correct |
12 |
Correct |
95 ms |
17476 KB |
Output is correct |
13 |
Correct |
75 ms |
21588 KB |
Output is correct |
14 |
Correct |
150 ms |
26300 KB |
Output is correct |
15 |
Correct |
129 ms |
26556 KB |
Output is correct |
16 |
Correct |
120 ms |
26572 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 |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
732 KB |
Output is correct |
22 |
Correct |
145 ms |
26448 KB |
Output is correct |
23 |
Correct |
142 ms |
25536 KB |
Output is correct |
24 |
Correct |
136 ms |
19272 KB |
Output is correct |
25 |
Correct |
213 ms |
26444 KB |
Output is correct |
26 |
Correct |
120 ms |
26752 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 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 |
6 ms |
1120 KB |
Output is correct |
32 |
Correct |
1 ms |
600 KB |
Output is correct |
33 |
Correct |
1 ms |
604 KB |
Output is correct |
34 |
Correct |
236 ms |
26000 KB |
Output is correct |
35 |
Correct |
235 ms |
25332 KB |
Output is correct |
36 |
Correct |
276 ms |
27476 KB |
Output is correct |
37 |
Correct |
284 ms |
27300 KB |
Output is correct |
38 |
Correct |
165 ms |
27356 KB |
Output is correct |
39 |
Correct |
79 ms |
16840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 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 |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
129 ms |
26216 KB |
Output is correct |
12 |
Correct |
95 ms |
17476 KB |
Output is correct |
13 |
Correct |
75 ms |
21588 KB |
Output is correct |
14 |
Correct |
150 ms |
26300 KB |
Output is correct |
15 |
Correct |
129 ms |
26556 KB |
Output is correct |
16 |
Correct |
120 ms |
26572 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 |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
732 KB |
Output is correct |
22 |
Correct |
145 ms |
26448 KB |
Output is correct |
23 |
Correct |
142 ms |
25536 KB |
Output is correct |
24 |
Correct |
136 ms |
19272 KB |
Output is correct |
25 |
Correct |
213 ms |
26444 KB |
Output is correct |
26 |
Correct |
120 ms |
26752 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 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 |
6 ms |
1120 KB |
Output is correct |
32 |
Correct |
1 ms |
600 KB |
Output is correct |
33 |
Correct |
1 ms |
604 KB |
Output is correct |
34 |
Correct |
236 ms |
26000 KB |
Output is correct |
35 |
Correct |
235 ms |
25332 KB |
Output is correct |
36 |
Correct |
276 ms |
27476 KB |
Output is correct |
37 |
Correct |
284 ms |
27300 KB |
Output is correct |
38 |
Correct |
165 ms |
27356 KB |
Output is correct |
39 |
Correct |
79 ms |
16840 KB |
Output is correct |
40 |
Correct |
4 ms |
1368 KB |
Output is correct |
41 |
Correct |
4 ms |
1372 KB |
Output is correct |
42 |
Correct |
3 ms |
1368 KB |
Output is correct |
43 |
Correct |
4 ms |
1372 KB |
Output is correct |
44 |
Correct |
240 ms |
29704 KB |
Output is correct |
45 |
Correct |
360 ms |
29492 KB |
Output is correct |
46 |
Correct |
335 ms |
29520 KB |
Output is correct |
47 |
Correct |
343 ms |
29584 KB |
Output is correct |
48 |
Correct |
190 ms |
28604 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 |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 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 |
0 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 |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
604 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
6 ms |
2396 KB |
Output is correct |
27 |
Correct |
6 ms |
1940 KB |
Output is correct |
28 |
Correct |
6 ms |
1884 KB |
Output is correct |
29 |
Correct |
6 ms |
1624 KB |
Output is correct |
30 |
Correct |
4 ms |
1204 KB |
Output is correct |
31 |
Correct |
5 ms |
1372 KB |
Output is correct |
32 |
Correct |
4 ms |
1116 KB |
Output is correct |
33 |
Correct |
6 ms |
1500 KB |
Output is correct |
34 |
Correct |
5 ms |
2396 KB |
Output is correct |
35 |
Correct |
1 ms |
604 KB |
Output is correct |
36 |
Correct |
1 ms |
604 KB |
Output is correct |
37 |
Correct |
3 ms |
1372 KB |
Output is correct |
38 |
Correct |
4 ms |
1588 KB |
Output is correct |
39 |
Correct |
1 ms |
604 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
1 ms |
348 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
0 ms |
344 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
0 ms |
344 KB |
Output is correct |
50 |
Correct |
129 ms |
26216 KB |
Output is correct |
51 |
Correct |
95 ms |
17476 KB |
Output is correct |
52 |
Correct |
75 ms |
21588 KB |
Output is correct |
53 |
Correct |
150 ms |
26300 KB |
Output is correct |
54 |
Correct |
129 ms |
26556 KB |
Output is correct |
55 |
Correct |
120 ms |
26572 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 |
348 KB |
Output is correct |
59 |
Correct |
0 ms |
348 KB |
Output is correct |
60 |
Correct |
1 ms |
732 KB |
Output is correct |
61 |
Correct |
145 ms |
26448 KB |
Output is correct |
62 |
Correct |
142 ms |
25536 KB |
Output is correct |
63 |
Correct |
136 ms |
19272 KB |
Output is correct |
64 |
Correct |
213 ms |
26444 KB |
Output is correct |
65 |
Correct |
120 ms |
26752 KB |
Output is correct |
66 |
Correct |
1 ms |
348 KB |
Output is correct |
67 |
Correct |
1 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 |
6 ms |
1120 KB |
Output is correct |
71 |
Correct |
1 ms |
600 KB |
Output is correct |
72 |
Correct |
1 ms |
604 KB |
Output is correct |
73 |
Correct |
236 ms |
26000 KB |
Output is correct |
74 |
Correct |
235 ms |
25332 KB |
Output is correct |
75 |
Correct |
276 ms |
27476 KB |
Output is correct |
76 |
Correct |
284 ms |
27300 KB |
Output is correct |
77 |
Correct |
165 ms |
27356 KB |
Output is correct |
78 |
Correct |
79 ms |
16840 KB |
Output is correct |
79 |
Correct |
4 ms |
1368 KB |
Output is correct |
80 |
Correct |
4 ms |
1372 KB |
Output is correct |
81 |
Correct |
3 ms |
1368 KB |
Output is correct |
82 |
Correct |
4 ms |
1372 KB |
Output is correct |
83 |
Correct |
240 ms |
29704 KB |
Output is correct |
84 |
Correct |
360 ms |
29492 KB |
Output is correct |
85 |
Correct |
335 ms |
29520 KB |
Output is correct |
86 |
Correct |
343 ms |
29584 KB |
Output is correct |
87 |
Correct |
190 ms |
28604 KB |
Output is correct |
88 |
Correct |
447 ms |
76544 KB |
Output is correct |
89 |
Correct |
507 ms |
54576 KB |
Output is correct |
90 |
Correct |
430 ms |
57940 KB |
Output is correct |
91 |
Correct |
521 ms |
49760 KB |
Output is correct |
92 |
Correct |
405 ms |
37288 KB |
Output is correct |
93 |
Correct |
630 ms |
54960 KB |
Output is correct |
94 |
Correct |
599 ms |
44448 KB |
Output is correct |
95 |
Correct |
284 ms |
41400 KB |
Output is correct |
96 |
Correct |
368 ms |
76556 KB |
Output is correct |
97 |
Correct |
133 ms |
29616 KB |
Output is correct |
98 |
Correct |
200 ms |
43092 KB |
Output is correct |
99 |
Correct |
208 ms |
43124 KB |
Output is correct |