#include <bits/stdc++.h>
using namespace std;
#define rep(i,s,e) for (int i = s; i <= e; ++i)
#define rrep(i,s,e) for (int i = s; i >= e; --i)
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define all(a) a.begin(), a.end()
#define len(a) (int)a.size()
typedef long long ll;
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<ll> vll;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
map<int, vi> with[3];
map<int, int> cnt[3];
queue<int> rem;
int n; cin >> n;
int have[n+1] = {}, d[n+1][3], dun[n+1] = {};
vi b[3];
rep (i,1,n) {
rep (j,0,2) {
cin >> d[i][j];
with[j][d[i][j]].pb(i);
if (!cnt[j][d[i][j]]++) b[j].pb(d[i][j]);
}
}
rep (i,0,2) {
sort(all(b[i]));
for (int el : with[i][b[i].back()])
if (++have[el]>1 && !dun[el]) {
rem.push(el);
dun[el] = 1;
}
}
while (len(rem)) {
int u = rem.front();
rem.pop();
rep (i,0,2) {
--cnt[i][d[u][i]];
bool c = 0;
while (len(b[i]) && !cnt[i][b[i].back()]) {
b[i].pop_back();
c = 1;
}
if (!len(b[i])) {
cout << "-1\n";
return 0;
}
if (c) {
for (int el : with[i][b[i].back()])
if (++have[el]>1 && !dun[el]) {
rem.push(el);
dun[el] = 1;
}
}
}
}
cout << b[0].back() + b[1].back() + b[2].back() << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
324 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
320 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
320 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 |
468 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 |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
324 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
320 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
320 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 |
468 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 |
8 ms |
2384 KB |
Output is correct |
27 |
Correct |
8 ms |
1620 KB |
Output is correct |
28 |
Correct |
8 ms |
1612 KB |
Output is correct |
29 |
Correct |
9 ms |
1364 KB |
Output is correct |
30 |
Correct |
6 ms |
1108 KB |
Output is correct |
31 |
Correct |
5 ms |
948 KB |
Output is correct |
32 |
Correct |
4 ms |
592 KB |
Output is correct |
33 |
Correct |
4 ms |
980 KB |
Output is correct |
34 |
Correct |
9 ms |
2376 KB |
Output is correct |
35 |
Correct |
1 ms |
456 KB |
Output is correct |
36 |
Correct |
2 ms |
468 KB |
Output is correct |
37 |
Correct |
5 ms |
1092 KB |
Output is correct |
38 |
Correct |
5 ms |
1108 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 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 |
1 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 |
37 ms |
6288 KB |
Output is correct |
12 |
Correct |
27 ms |
4560 KB |
Output is correct |
13 |
Correct |
26 ms |
5040 KB |
Output is correct |
14 |
Correct |
47 ms |
6624 KB |
Output is correct |
15 |
Correct |
51 ms |
6616 KB |
Output is correct |
16 |
Correct |
33 ms |
6460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 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 |
1 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 |
37 ms |
6288 KB |
Output is correct |
12 |
Correct |
27 ms |
4560 KB |
Output is correct |
13 |
Correct |
26 ms |
5040 KB |
Output is correct |
14 |
Correct |
47 ms |
6624 KB |
Output is correct |
15 |
Correct |
51 ms |
6616 KB |
Output is correct |
16 |
Correct |
33 ms |
6460 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
45 ms |
6376 KB |
Output is correct |
23 |
Correct |
54 ms |
6124 KB |
Output is correct |
24 |
Correct |
45 ms |
4940 KB |
Output is correct |
25 |
Correct |
59 ms |
6736 KB |
Output is correct |
26 |
Correct |
45 ms |
6648 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 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 |
1 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 |
37 ms |
6288 KB |
Output is correct |
12 |
Correct |
27 ms |
4560 KB |
Output is correct |
13 |
Correct |
26 ms |
5040 KB |
Output is correct |
14 |
Correct |
47 ms |
6624 KB |
Output is correct |
15 |
Correct |
51 ms |
6616 KB |
Output is correct |
16 |
Correct |
33 ms |
6460 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
45 ms |
6376 KB |
Output is correct |
23 |
Correct |
54 ms |
6124 KB |
Output is correct |
24 |
Correct |
45 ms |
4940 KB |
Output is correct |
25 |
Correct |
59 ms |
6736 KB |
Output is correct |
26 |
Correct |
45 ms |
6648 KB |
Output is correct |
27 |
Correct |
1 ms |
320 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 |
4 ms |
596 KB |
Output is correct |
32 |
Correct |
1 ms |
452 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
82 ms |
6960 KB |
Output is correct |
35 |
Correct |
78 ms |
7488 KB |
Output is correct |
36 |
Correct |
85 ms |
7672 KB |
Output is correct |
37 |
Correct |
102 ms |
7752 KB |
Output is correct |
38 |
Correct |
65 ms |
7424 KB |
Output is correct |
39 |
Correct |
37 ms |
4652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 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 |
1 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 |
37 ms |
6288 KB |
Output is correct |
12 |
Correct |
27 ms |
4560 KB |
Output is correct |
13 |
Correct |
26 ms |
5040 KB |
Output is correct |
14 |
Correct |
47 ms |
6624 KB |
Output is correct |
15 |
Correct |
51 ms |
6616 KB |
Output is correct |
16 |
Correct |
33 ms |
6460 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
45 ms |
6376 KB |
Output is correct |
23 |
Correct |
54 ms |
6124 KB |
Output is correct |
24 |
Correct |
45 ms |
4940 KB |
Output is correct |
25 |
Correct |
59 ms |
6736 KB |
Output is correct |
26 |
Correct |
45 ms |
6648 KB |
Output is correct |
27 |
Correct |
1 ms |
320 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 |
4 ms |
596 KB |
Output is correct |
32 |
Correct |
1 ms |
452 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
82 ms |
6960 KB |
Output is correct |
35 |
Correct |
78 ms |
7488 KB |
Output is correct |
36 |
Correct |
85 ms |
7672 KB |
Output is correct |
37 |
Correct |
102 ms |
7752 KB |
Output is correct |
38 |
Correct |
65 ms |
7424 KB |
Output is correct |
39 |
Correct |
37 ms |
4652 KB |
Output is correct |
40 |
Correct |
6 ms |
1048 KB |
Output is correct |
41 |
Correct |
5 ms |
980 KB |
Output is correct |
42 |
Correct |
6 ms |
1108 KB |
Output is correct |
43 |
Correct |
5 ms |
1124 KB |
Output is correct |
44 |
Correct |
150 ms |
10556 KB |
Output is correct |
45 |
Correct |
179 ms |
9904 KB |
Output is correct |
46 |
Correct |
187 ms |
10316 KB |
Output is correct |
47 |
Correct |
197 ms |
10124 KB |
Output is correct |
48 |
Correct |
97 ms |
9124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
324 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
320 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
320 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 |
468 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 |
8 ms |
2384 KB |
Output is correct |
27 |
Correct |
8 ms |
1620 KB |
Output is correct |
28 |
Correct |
8 ms |
1612 KB |
Output is correct |
29 |
Correct |
9 ms |
1364 KB |
Output is correct |
30 |
Correct |
6 ms |
1108 KB |
Output is correct |
31 |
Correct |
5 ms |
948 KB |
Output is correct |
32 |
Correct |
4 ms |
592 KB |
Output is correct |
33 |
Correct |
4 ms |
980 KB |
Output is correct |
34 |
Correct |
9 ms |
2376 KB |
Output is correct |
35 |
Correct |
1 ms |
456 KB |
Output is correct |
36 |
Correct |
2 ms |
468 KB |
Output is correct |
37 |
Correct |
5 ms |
1092 KB |
Output is correct |
38 |
Correct |
5 ms |
1108 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
212 KB |
Output is correct |
41 |
Correct |
1 ms |
212 KB |
Output is correct |
42 |
Correct |
0 ms |
320 KB |
Output is correct |
43 |
Correct |
1 ms |
212 KB |
Output is correct |
44 |
Correct |
0 ms |
212 KB |
Output is correct |
45 |
Correct |
1 ms |
212 KB |
Output is correct |
46 |
Correct |
0 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 |
1 ms |
212 KB |
Output is correct |
50 |
Correct |
37 ms |
6288 KB |
Output is correct |
51 |
Correct |
27 ms |
4560 KB |
Output is correct |
52 |
Correct |
26 ms |
5040 KB |
Output is correct |
53 |
Correct |
47 ms |
6624 KB |
Output is correct |
54 |
Correct |
51 ms |
6616 KB |
Output is correct |
55 |
Correct |
33 ms |
6460 KB |
Output is correct |
56 |
Correct |
1 ms |
212 KB |
Output is correct |
57 |
Correct |
0 ms |
212 KB |
Output is correct |
58 |
Correct |
0 ms |
212 KB |
Output is correct |
59 |
Correct |
1 ms |
340 KB |
Output is correct |
60 |
Correct |
1 ms |
340 KB |
Output is correct |
61 |
Correct |
45 ms |
6376 KB |
Output is correct |
62 |
Correct |
54 ms |
6124 KB |
Output is correct |
63 |
Correct |
45 ms |
4940 KB |
Output is correct |
64 |
Correct |
59 ms |
6736 KB |
Output is correct |
65 |
Correct |
45 ms |
6648 KB |
Output is correct |
66 |
Correct |
1 ms |
320 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 |
4 ms |
596 KB |
Output is correct |
71 |
Correct |
1 ms |
452 KB |
Output is correct |
72 |
Correct |
1 ms |
468 KB |
Output is correct |
73 |
Correct |
82 ms |
6960 KB |
Output is correct |
74 |
Correct |
78 ms |
7488 KB |
Output is correct |
75 |
Correct |
85 ms |
7672 KB |
Output is correct |
76 |
Correct |
102 ms |
7752 KB |
Output is correct |
77 |
Correct |
65 ms |
7424 KB |
Output is correct |
78 |
Correct |
37 ms |
4652 KB |
Output is correct |
79 |
Correct |
6 ms |
1048 KB |
Output is correct |
80 |
Correct |
5 ms |
980 KB |
Output is correct |
81 |
Correct |
6 ms |
1108 KB |
Output is correct |
82 |
Correct |
5 ms |
1124 KB |
Output is correct |
83 |
Correct |
150 ms |
10556 KB |
Output is correct |
84 |
Correct |
179 ms |
9904 KB |
Output is correct |
85 |
Correct |
187 ms |
10316 KB |
Output is correct |
86 |
Correct |
197 ms |
10124 KB |
Output is correct |
87 |
Correct |
97 ms |
9124 KB |
Output is correct |
88 |
Correct |
680 ms |
79428 KB |
Output is correct |
89 |
Correct |
691 ms |
47148 KB |
Output is correct |
90 |
Correct |
620 ms |
52064 KB |
Output is correct |
91 |
Correct |
697 ms |
40088 KB |
Output is correct |
92 |
Correct |
424 ms |
21412 KB |
Output is correct |
93 |
Correct |
784 ms |
48308 KB |
Output is correct |
94 |
Correct |
640 ms |
32732 KB |
Output is correct |
95 |
Correct |
316 ms |
26904 KB |
Output is correct |
96 |
Correct |
604 ms |
79316 KB |
Output is correct |
97 |
Correct |
52 ms |
9512 KB |
Output is correct |
98 |
Correct |
193 ms |
30480 KB |
Output is correct |
99 |
Correct |
281 ms |
30524 KB |
Output is correct |