// Om Namah Shivaya
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define endl '\n'
#define sz(a) a.size()
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
#define conts continue
#define ceil2(x, y) ((x + y - 1) / (y))
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "Yes" << endl
#define no cout << "No" << endl
#define rep(i, n) for(int i = 0; i < n; ++i)
#define rep1(i, n) for(int i = 1; i <= n; ++i)
#define rev(i, s, e) for(int i = s; i >= e; --i)
#define trav(i, a) for(auto &i : a)
template<typename T>
void amin(T &a, T b) {
a = min(a, b);
}
template<typename T>
void amax(T &a, T b) {
a = max(a, b);
}
#ifdef LOCAL
#include "debug.h"
#else
#define debug(x) 42
#endif
/*
refs:
edi
https://github.com/koosaga/olympiad/blob/master/JOI/camp22_team.cpp
let's look at the initial array
when will some guy definitely not be in the best triplet?
=> if he dominates in >= 2 areas
so we can remove all guys who cant be there
repeat the process on the resulting array
repeat until we can no longer delete anyone
what does it mean if we can make no more deletions?
there is exactly 1 guy dominating each area
so we can pick the guy dominating each area and that would be our triplet
if < 3 guys are remaining, then not possible
naive simulation of this process = O(n^2)
we can efficiently simulate the process using multiset and map
*/
const int MOD = 1e9 + 7;
const int N = 1e5 + 5;
const int inf1 = int(1e9) + 5;
const ll inf2 = ll(1e18) + 5;
void solve(int test_case)
{
ll n; cin >> n;
vector<array<ll,3>> a(n);
rep(i,n) rep(j,3) cin >> a[i][j];
multiset<ll> ms[3];
rep(i,n){
rep(j,3){
ms[j].insert(a[i][j]);
}
}
array<ll,3> mxs;
rep(j,3) mxs[j] = *ms[j].rbegin();
vector<ll> cnt(n);
queue<ll> q;
rep(i,n){
rep(j,3){
if(a[i][j] == mxs[j]){
cnt[i]++;
}
}
if(cnt[i] >= 2){
q.push(i);
}
}
map<ll,vector<ll>> mp[3];
rep(i,n){
rep(j,3){
mp[j][a[i][j]].pb(i);
}
}
vector<bool> rem(n);
ll rem_cnt = 0;
while(!q.empty()){
ll i = q.front();
q.pop();
if(rem[i]) conts;
rem[i] = 1;
rem_cnt++;
if(rem_cnt == n){
cout << -1 << endl;
return;
}
rep(j,3){
ms[j].erase(ms[j].find(a[i][j]));
}
array<ll,3> mxs_new;
rep(j,3){
mxs_new[j] = *ms[j].rbegin();
}
rep(j,3){
if(mxs_new[j] != mxs[j]){
trav(i,mp[j][mxs_new[j]]){
if(rem[i]) conts;
cnt[i]++;
if(cnt[i] >= 2){
q.push(i);
}
}
}
}
mxs = mxs_new;
}
ll ans = 0;
rep(j,3) ans += mxs[j];
cout << ans << endl;
}
int main()
{
fastio;
int t = 1;
// cin >> t;
rep1(i, t) {
solve(i);
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
468 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 |
340 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 |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
468 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 |
340 KB |
Output is correct |
26 |
Correct |
7 ms |
2260 KB |
Output is correct |
27 |
Correct |
6 ms |
1692 KB |
Output is correct |
28 |
Correct |
7 ms |
1676 KB |
Output is correct |
29 |
Correct |
8 ms |
1640 KB |
Output is correct |
30 |
Correct |
4 ms |
1268 KB |
Output is correct |
31 |
Correct |
5 ms |
1364 KB |
Output is correct |
32 |
Correct |
6 ms |
1236 KB |
Output is correct |
33 |
Correct |
5 ms |
1388 KB |
Output is correct |
34 |
Correct |
8 ms |
2208 KB |
Output is correct |
35 |
Correct |
1 ms |
468 KB |
Output is correct |
36 |
Correct |
1 ms |
468 KB |
Output is correct |
37 |
Correct |
4 ms |
1492 KB |
Output is correct |
38 |
Correct |
7 ms |
1492 KB |
Output is correct |
39 |
Correct |
2 ms |
724 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 |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
182 ms |
30564 KB |
Output is correct |
12 |
Correct |
154 ms |
20716 KB |
Output is correct |
13 |
Correct |
108 ms |
24696 KB |
Output is correct |
14 |
Correct |
238 ms |
31060 KB |
Output is correct |
15 |
Correct |
153 ms |
31424 KB |
Output is correct |
16 |
Correct |
145 ms |
31592 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 |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
182 ms |
30564 KB |
Output is correct |
12 |
Correct |
154 ms |
20716 KB |
Output is correct |
13 |
Correct |
108 ms |
24696 KB |
Output is correct |
14 |
Correct |
238 ms |
31060 KB |
Output is correct |
15 |
Correct |
153 ms |
31424 KB |
Output is correct |
16 |
Correct |
145 ms |
31592 KB |
Output is correct |
17 |
Correct |
0 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 |
2 ms |
724 KB |
Output is correct |
22 |
Correct |
198 ms |
30424 KB |
Output is correct |
23 |
Correct |
198 ms |
29552 KB |
Output is correct |
24 |
Correct |
176 ms |
22456 KB |
Output is correct |
25 |
Correct |
279 ms |
30696 KB |
Output is correct |
26 |
Correct |
169 ms |
31152 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 |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
182 ms |
30564 KB |
Output is correct |
12 |
Correct |
154 ms |
20716 KB |
Output is correct |
13 |
Correct |
108 ms |
24696 KB |
Output is correct |
14 |
Correct |
238 ms |
31060 KB |
Output is correct |
15 |
Correct |
153 ms |
31424 KB |
Output is correct |
16 |
Correct |
145 ms |
31592 KB |
Output is correct |
17 |
Correct |
0 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 |
2 ms |
724 KB |
Output is correct |
22 |
Correct |
198 ms |
30424 KB |
Output is correct |
23 |
Correct |
198 ms |
29552 KB |
Output is correct |
24 |
Correct |
176 ms |
22456 KB |
Output is correct |
25 |
Correct |
279 ms |
30696 KB |
Output is correct |
26 |
Correct |
169 ms |
31152 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 |
6 ms |
1236 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
253 ms |
29336 KB |
Output is correct |
35 |
Correct |
274 ms |
29824 KB |
Output is correct |
36 |
Correct |
310 ms |
31760 KB |
Output is correct |
37 |
Correct |
387 ms |
31048 KB |
Output is correct |
38 |
Correct |
209 ms |
31044 KB |
Output is correct |
39 |
Correct |
129 ms |
18972 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 |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
182 ms |
30564 KB |
Output is correct |
12 |
Correct |
154 ms |
20716 KB |
Output is correct |
13 |
Correct |
108 ms |
24696 KB |
Output is correct |
14 |
Correct |
238 ms |
31060 KB |
Output is correct |
15 |
Correct |
153 ms |
31424 KB |
Output is correct |
16 |
Correct |
145 ms |
31592 KB |
Output is correct |
17 |
Correct |
0 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 |
2 ms |
724 KB |
Output is correct |
22 |
Correct |
198 ms |
30424 KB |
Output is correct |
23 |
Correct |
198 ms |
29552 KB |
Output is correct |
24 |
Correct |
176 ms |
22456 KB |
Output is correct |
25 |
Correct |
279 ms |
30696 KB |
Output is correct |
26 |
Correct |
169 ms |
31152 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 |
6 ms |
1236 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
253 ms |
29336 KB |
Output is correct |
35 |
Correct |
274 ms |
29824 KB |
Output is correct |
36 |
Correct |
310 ms |
31760 KB |
Output is correct |
37 |
Correct |
387 ms |
31048 KB |
Output is correct |
38 |
Correct |
209 ms |
31044 KB |
Output is correct |
39 |
Correct |
129 ms |
18972 KB |
Output is correct |
40 |
Correct |
4 ms |
1236 KB |
Output is correct |
41 |
Correct |
5 ms |
1476 KB |
Output is correct |
42 |
Correct |
4 ms |
1492 KB |
Output is correct |
43 |
Correct |
5 ms |
1492 KB |
Output is correct |
44 |
Correct |
380 ms |
33844 KB |
Output is correct |
45 |
Correct |
515 ms |
32768 KB |
Output is correct |
46 |
Correct |
499 ms |
35576 KB |
Output is correct |
47 |
Correct |
525 ms |
35192 KB |
Output is correct |
48 |
Correct |
253 ms |
34260 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 |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
468 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 |
340 KB |
Output is correct |
26 |
Correct |
7 ms |
2260 KB |
Output is correct |
27 |
Correct |
6 ms |
1692 KB |
Output is correct |
28 |
Correct |
7 ms |
1676 KB |
Output is correct |
29 |
Correct |
8 ms |
1640 KB |
Output is correct |
30 |
Correct |
4 ms |
1268 KB |
Output is correct |
31 |
Correct |
5 ms |
1364 KB |
Output is correct |
32 |
Correct |
6 ms |
1236 KB |
Output is correct |
33 |
Correct |
5 ms |
1388 KB |
Output is correct |
34 |
Correct |
8 ms |
2208 KB |
Output is correct |
35 |
Correct |
1 ms |
468 KB |
Output is correct |
36 |
Correct |
1 ms |
468 KB |
Output is correct |
37 |
Correct |
4 ms |
1492 KB |
Output is correct |
38 |
Correct |
7 ms |
1492 KB |
Output is correct |
39 |
Correct |
2 ms |
724 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 |
1 ms |
212 KB |
Output is correct |
44 |
Correct |
0 ms |
212 KB |
Output is correct |
45 |
Correct |
0 ms |
212 KB |
Output is correct |
46 |
Correct |
0 ms |
212 KB |
Output is correct |
47 |
Correct |
0 ms |
212 KB |
Output is correct |
48 |
Correct |
0 ms |
212 KB |
Output is correct |
49 |
Correct |
0 ms |
212 KB |
Output is correct |
50 |
Correct |
182 ms |
30564 KB |
Output is correct |
51 |
Correct |
154 ms |
20716 KB |
Output is correct |
52 |
Correct |
108 ms |
24696 KB |
Output is correct |
53 |
Correct |
238 ms |
31060 KB |
Output is correct |
54 |
Correct |
153 ms |
31424 KB |
Output is correct |
55 |
Correct |
145 ms |
31592 KB |
Output is correct |
56 |
Correct |
0 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 |
2 ms |
724 KB |
Output is correct |
61 |
Correct |
198 ms |
30424 KB |
Output is correct |
62 |
Correct |
198 ms |
29552 KB |
Output is correct |
63 |
Correct |
176 ms |
22456 KB |
Output is correct |
64 |
Correct |
279 ms |
30696 KB |
Output is correct |
65 |
Correct |
169 ms |
31152 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 |
6 ms |
1236 KB |
Output is correct |
71 |
Correct |
1 ms |
468 KB |
Output is correct |
72 |
Correct |
1 ms |
468 KB |
Output is correct |
73 |
Correct |
253 ms |
29336 KB |
Output is correct |
74 |
Correct |
274 ms |
29824 KB |
Output is correct |
75 |
Correct |
310 ms |
31760 KB |
Output is correct |
76 |
Correct |
387 ms |
31048 KB |
Output is correct |
77 |
Correct |
209 ms |
31044 KB |
Output is correct |
78 |
Correct |
129 ms |
18972 KB |
Output is correct |
79 |
Correct |
4 ms |
1236 KB |
Output is correct |
80 |
Correct |
5 ms |
1476 KB |
Output is correct |
81 |
Correct |
4 ms |
1492 KB |
Output is correct |
82 |
Correct |
5 ms |
1492 KB |
Output is correct |
83 |
Correct |
380 ms |
33844 KB |
Output is correct |
84 |
Correct |
515 ms |
32768 KB |
Output is correct |
85 |
Correct |
499 ms |
35576 KB |
Output is correct |
86 |
Correct |
525 ms |
35192 KB |
Output is correct |
87 |
Correct |
253 ms |
34260 KB |
Output is correct |
88 |
Correct |
686 ms |
79344 KB |
Output is correct |
89 |
Correct |
658 ms |
58192 KB |
Output is correct |
90 |
Correct |
606 ms |
61484 KB |
Output is correct |
91 |
Correct |
692 ms |
53836 KB |
Output is correct |
92 |
Correct |
518 ms |
42444 KB |
Output is correct |
93 |
Correct |
713 ms |
58696 KB |
Output is correct |
94 |
Correct |
635 ms |
49012 KB |
Output is correct |
95 |
Correct |
313 ms |
46120 KB |
Output is correct |
96 |
Correct |
465 ms |
79348 KB |
Output is correct |
97 |
Correct |
179 ms |
35544 KB |
Output is correct |
98 |
Correct |
197 ms |
47456 KB |
Output is correct |
99 |
Correct |
247 ms |
47516 KB |
Output is correct |