#include<bits/stdc++.h>
using namespace std;
#define task "a"
#define se second
#define fi first
#define ll long long
#define ii pair<ll, ll>
const long mxN = 2e5 + 7;
ii a[mxN], b[mxN], c[mxN];
int ctr[4], vt[mxN][2], n;
bool dd[mxN][2];
bool cmp(ii u, ii v)
{
return u.fi > v.fi || (u.fi == v.fi && vt[u.se][0] < vt[v.se][0]);
}
bool cmpp(ii u, ii v)
{
return u.fi > v.fi || (u.fi == v.fi && c[u.se].fi > c[v.se].fi);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
//freopen(task".INP", "r", stdin);
//freopen(task".OUT", "w", stdout);
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> a[i].fi >> b[i].fi >> c[i].fi;
a[i].se = b[i].se = c[i].se = i;
}
sort(a + 1, a + n + 1, greater<ii>());
sort(b + 1, b + n + 1, cmpp);
for (int i = 1; i <= n; i++)
vt[b[i].se][0] = i;
sort(c + 1, c + n + 1, cmp);
for (int i = 1; i <= n; i++)
vt[c[i].se][1] = i;
ctr[2] = ctr[0] = ctr[1] = 1;
for (int i = 1; i <= n; i++)
{
while (a[ctr[2]].fi == a[i].fi)
{
int id = a[ctr[2]].se;
dd[vt[id][0]][0] = dd[vt[id][1]][1] = 1;
ctr[2]++;
}
while (1)
{
for (int id = 0; id <= 1; id++)
{
while (dd[ctr[id]][id])
ctr[id]++;
if (ctr[id] > n)
{
cout << -1;
return 0;
}
}
if (b[ctr[0]].se != c[ctr[1]].se)
break;
dd[ctr[0]][0] = dd[ctr[1]][1] = 1;
}
if (b[ctr[0]].fi > b[a[i].se].fi && c[ctr[1]].fi > c[a[i].se].fi)
{
cout << a[i].fi + b[ctr[0]].fi + c[ctr[1]].fi;
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... |