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;
#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;
}
# | 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... |