This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |