#include <bits/stdc++.h>
using namespace std;
#define REP(i, j, k) for (int i = j; i < k; i++)
#define RREP(i, j, k) for (int i = j; i >= k; i--)
template <class T>
bool mnto(T &a, const T b) {return a > b ? a = b, 1 : 0;}
template <class T>
bool mxto(T &a, const T b) {return a < b ? a = b, 1 : 0;}
typedef long long ll;
#define FI first
#define SE second
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> iii;
#define ALL(x) x.begin(), x.end()
#define SZ(x) (int) x.size()
#define pb push_back
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ll> vll;
#ifndef DEBUG
#define cerr if(0) cerr
#endif
const int MAXN = 150005;
const int INF = 1000000005;
const ll LINF = 1000000000000000005;
int n;
int g[MAXN][3];
vi id[3];
ll ans;
bool used[MAXN];
int main() {
#ifndef DEBUG
ios::sync_with_stdio(0), cin.tie(0);
#endif
cin >> n;
REP (i, 0, n) {
REP (j, 0, 3) {
cin >> g[i][j];
}
}
REP (i, 0, 3) {
id[i] = vi(n, 0);
iota(ALL(id[i]), 0);
sort(ALL(id[i]), [&] (int l, int r) {
return g[l][i] < g[r][i];
});
}
ans = -1;
while (1) {
bool pos = 1;
bool nopop = 1;
REP (i, 0, 3) {
while (!id[i].empty() && used[id[i].back()]) {
id[i].pop_back();
}
if (id[i].empty()) {
pos = 0;
break;
}
REP (j, 0, 3) {
if (i == j) continue;
while (!id[j].empty() && used[id[j].back()]) {
id[j].pop_back();
}
if (id[j].empty()) {
pos = 0;
break;
}
int u = id[i].back(), v = id[j].back();
while (g[u][i] == g[v][i]) {
nopop = 0;
id[j].pop_back();
used[v] = 1;
if (id[j].empty()) {
pos = 0;
break;
}
v = id[j].back();
}
if (!pos) {
break;
}
}
if (!pos) {
break;
}
}
if (nopop && pos) {
ans = 0;
REP (i, 0, 3) {
ans += g[id[i].back()][i];
}
}
if (!pos || ans != -1) {
break;
}
}
cout << ans << '\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 |
0 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 |
336 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
280 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 |
340 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 |
344 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 |
0 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 |
336 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
280 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 |
340 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 |
344 KB |
Output is correct |
26 |
Correct |
3 ms |
476 KB |
Output is correct |
27 |
Correct |
3 ms |
352 KB |
Output is correct |
28 |
Correct |
3 ms |
468 KB |
Output is correct |
29 |
Correct |
4 ms |
468 KB |
Output is correct |
30 |
Correct |
2 ms |
424 KB |
Output is correct |
31 |
Correct |
3 ms |
404 KB |
Output is correct |
32 |
Correct |
2 ms |
340 KB |
Output is correct |
33 |
Correct |
2 ms |
436 KB |
Output is correct |
34 |
Correct |
3 ms |
472 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
344 KB |
Output is correct |
37 |
Correct |
3 ms |
392 KB |
Output is correct |
38 |
Correct |
3 ms |
476 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
296 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 |
332 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 |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
56 ms |
3764 KB |
Output is correct |
12 |
Correct |
39 ms |
3176 KB |
Output is correct |
13 |
Correct |
40 ms |
4000 KB |
Output is correct |
14 |
Correct |
46 ms |
4632 KB |
Output is correct |
15 |
Correct |
43 ms |
4408 KB |
Output is correct |
16 |
Correct |
36 ms |
4588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
296 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 |
332 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 |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
56 ms |
3764 KB |
Output is correct |
12 |
Correct |
39 ms |
3176 KB |
Output is correct |
13 |
Correct |
40 ms |
4000 KB |
Output is correct |
14 |
Correct |
46 ms |
4632 KB |
Output is correct |
15 |
Correct |
43 ms |
4408 KB |
Output is correct |
16 |
Correct |
36 ms |
4588 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 |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
2 ms |
340 KB |
Output is correct |
22 |
Correct |
51 ms |
4568 KB |
Output is correct |
23 |
Correct |
47 ms |
4660 KB |
Output is correct |
24 |
Correct |
44 ms |
3532 KB |
Output is correct |
25 |
Correct |
56 ms |
5016 KB |
Output is correct |
26 |
Correct |
44 ms |
4856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
296 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 |
332 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 |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
56 ms |
3764 KB |
Output is correct |
12 |
Correct |
39 ms |
3176 KB |
Output is correct |
13 |
Correct |
40 ms |
4000 KB |
Output is correct |
14 |
Correct |
46 ms |
4632 KB |
Output is correct |
15 |
Correct |
43 ms |
4408 KB |
Output is correct |
16 |
Correct |
36 ms |
4588 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 |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
2 ms |
340 KB |
Output is correct |
22 |
Correct |
51 ms |
4568 KB |
Output is correct |
23 |
Correct |
47 ms |
4660 KB |
Output is correct |
24 |
Correct |
44 ms |
3532 KB |
Output is correct |
25 |
Correct |
56 ms |
5016 KB |
Output is correct |
26 |
Correct |
44 ms |
4856 KB |
Output is correct |
27 |
Correct |
1 ms |
332 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 |
3 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
344 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
61 ms |
4988 KB |
Output is correct |
35 |
Correct |
62 ms |
4580 KB |
Output is correct |
36 |
Correct |
74 ms |
5028 KB |
Output is correct |
37 |
Correct |
68 ms |
5580 KB |
Output is correct |
38 |
Correct |
51 ms |
5068 KB |
Output is correct |
39 |
Correct |
26 ms |
3152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
296 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 |
332 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 |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
56 ms |
3764 KB |
Output is correct |
12 |
Correct |
39 ms |
3176 KB |
Output is correct |
13 |
Correct |
40 ms |
4000 KB |
Output is correct |
14 |
Correct |
46 ms |
4632 KB |
Output is correct |
15 |
Correct |
43 ms |
4408 KB |
Output is correct |
16 |
Correct |
36 ms |
4588 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 |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
2 ms |
340 KB |
Output is correct |
22 |
Correct |
51 ms |
4568 KB |
Output is correct |
23 |
Correct |
47 ms |
4660 KB |
Output is correct |
24 |
Correct |
44 ms |
3532 KB |
Output is correct |
25 |
Correct |
56 ms |
5016 KB |
Output is correct |
26 |
Correct |
44 ms |
4856 KB |
Output is correct |
27 |
Correct |
1 ms |
332 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 |
3 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
344 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
61 ms |
4988 KB |
Output is correct |
35 |
Correct |
62 ms |
4580 KB |
Output is correct |
36 |
Correct |
74 ms |
5028 KB |
Output is correct |
37 |
Correct |
68 ms |
5580 KB |
Output is correct |
38 |
Correct |
51 ms |
5068 KB |
Output is correct |
39 |
Correct |
26 ms |
3152 KB |
Output is correct |
40 |
Correct |
2 ms |
340 KB |
Output is correct |
41 |
Correct |
3 ms |
476 KB |
Output is correct |
42 |
Correct |
3 ms |
340 KB |
Output is correct |
43 |
Correct |
3 ms |
468 KB |
Output is correct |
44 |
Correct |
82 ms |
5792 KB |
Output is correct |
45 |
Correct |
79 ms |
5944 KB |
Output is correct |
46 |
Correct |
87 ms |
5492 KB |
Output is correct |
47 |
Correct |
96 ms |
6072 KB |
Output is correct |
48 |
Correct |
59 ms |
5952 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 |
0 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 |
336 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
280 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 |
340 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 |
344 KB |
Output is correct |
26 |
Correct |
3 ms |
476 KB |
Output is correct |
27 |
Correct |
3 ms |
352 KB |
Output is correct |
28 |
Correct |
3 ms |
468 KB |
Output is correct |
29 |
Correct |
4 ms |
468 KB |
Output is correct |
30 |
Correct |
2 ms |
424 KB |
Output is correct |
31 |
Correct |
3 ms |
404 KB |
Output is correct |
32 |
Correct |
2 ms |
340 KB |
Output is correct |
33 |
Correct |
2 ms |
436 KB |
Output is correct |
34 |
Correct |
3 ms |
472 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
344 KB |
Output is correct |
37 |
Correct |
3 ms |
392 KB |
Output is correct |
38 |
Correct |
3 ms |
476 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
296 KB |
Output is correct |
41 |
Correct |
0 ms |
212 KB |
Output is correct |
42 |
Correct |
1 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 |
332 KB |
Output is correct |
46 |
Correct |
1 ms |
212 KB |
Output is correct |
47 |
Correct |
0 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 |
56 ms |
3764 KB |
Output is correct |
51 |
Correct |
39 ms |
3176 KB |
Output is correct |
52 |
Correct |
40 ms |
4000 KB |
Output is correct |
53 |
Correct |
46 ms |
4632 KB |
Output is correct |
54 |
Correct |
43 ms |
4408 KB |
Output is correct |
55 |
Correct |
36 ms |
4588 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 |
332 KB |
Output is correct |
59 |
Correct |
1 ms |
340 KB |
Output is correct |
60 |
Correct |
2 ms |
340 KB |
Output is correct |
61 |
Correct |
51 ms |
4568 KB |
Output is correct |
62 |
Correct |
47 ms |
4660 KB |
Output is correct |
63 |
Correct |
44 ms |
3532 KB |
Output is correct |
64 |
Correct |
56 ms |
5016 KB |
Output is correct |
65 |
Correct |
44 ms |
4856 KB |
Output is correct |
66 |
Correct |
1 ms |
332 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 |
3 ms |
468 KB |
Output is correct |
71 |
Correct |
1 ms |
344 KB |
Output is correct |
72 |
Correct |
1 ms |
340 KB |
Output is correct |
73 |
Correct |
61 ms |
4988 KB |
Output is correct |
74 |
Correct |
62 ms |
4580 KB |
Output is correct |
75 |
Correct |
74 ms |
5028 KB |
Output is correct |
76 |
Correct |
68 ms |
5580 KB |
Output is correct |
77 |
Correct |
51 ms |
5068 KB |
Output is correct |
78 |
Correct |
26 ms |
3152 KB |
Output is correct |
79 |
Correct |
2 ms |
340 KB |
Output is correct |
80 |
Correct |
3 ms |
476 KB |
Output is correct |
81 |
Correct |
3 ms |
340 KB |
Output is correct |
82 |
Correct |
3 ms |
468 KB |
Output is correct |
83 |
Correct |
82 ms |
5792 KB |
Output is correct |
84 |
Correct |
79 ms |
5944 KB |
Output is correct |
85 |
Correct |
87 ms |
5492 KB |
Output is correct |
86 |
Correct |
96 ms |
6072 KB |
Output is correct |
87 |
Correct |
59 ms |
5952 KB |
Output is correct |
88 |
Correct |
102 ms |
6028 KB |
Output is correct |
89 |
Correct |
111 ms |
6252 KB |
Output is correct |
90 |
Correct |
102 ms |
6284 KB |
Output is correct |
91 |
Correct |
125 ms |
5456 KB |
Output is correct |
92 |
Correct |
97 ms |
5648 KB |
Output is correct |
93 |
Correct |
113 ms |
6200 KB |
Output is correct |
94 |
Correct |
113 ms |
6204 KB |
Output is correct |
95 |
Correct |
76 ms |
6252 KB |
Output is correct |
96 |
Correct |
98 ms |
6164 KB |
Output is correct |
97 |
Correct |
57 ms |
6276 KB |
Output is correct |
98 |
Correct |
50 ms |
6124 KB |
Output is correct |
99 |
Correct |
137 ms |
6256 KB |
Output is correct |