This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
    IN THE NAME OF GOD
*/
#include <bits/stdc++.h>
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef long double ld;
#define F                                      first
#define S                                      second
#define Mp                                     make_pair
#define pb                                     push_back
#define pf                                     push_front
#define size(x)                                ((ll)x.size())
#define all(x)                                 (x).begin(),(x).end()
#define kill(x)		                           cout << x << '\n', exit(0);
#define fuck(x)                                cout << "(" << #x << " , " << x << ")" << endl
#define endl                                   '\n'
const int N = 3e5+23, lg = 18;
ll Mod = 1e9+7; //998244353;
inline ll MOD(ll a, ll mod=Mod) {a%=mod; (a<0)&&(a+=mod); return a;}
inline ll poww(ll a, ll b, ll mod=Mod) {
    ll ans = 1;
    a=MOD(a, mod);
    while (b) {
        if (b & 1) ans = MOD(ans*a, mod);
        b >>= 1;
        a = MOD(a*a, mod);
    }
    return ans;
}
int n, a[3][N], mx[2*N], mn[2*N], ans;
vector<int> comp[3], vec[N];
int get(int id, int x) {
    return lower_bound(all(comp[id]), x) - comp[id].begin() + 1;
}
void upd(int ind, int val) {
    mn[ind] = min(mn[ind], val), mx[ind] = max(mx[ind], val);
    ind/=2;
    while(ind > 0) {
        mx[ind] = max(mx[2*ind], mx[2*ind+1]);
        mn[ind] = min(mn[2*ind], mn[2*ind+1]);
        ind/=2;
    }
}
int qrymx(int r, int ind=1, int lc=1, int rc=(1<<lg)+1) {
    if(lc>=r) return 0;
    if(rc<=r) return mx[ind];
    int mid=(lc+rc)/2;
    return max(qrymx(r,2*ind,lc,mid), qrymx(r,2*ind+1,mid,rc));
}
int qry(int l, int val, int ind=1, int lc=1, int rc=(1<<lg)+1) {
    if(rc<=l) return 0;
    if(ind>=(1<<lg)) {
        if(mn[ind] < val) return comp[1][lc-1];
        return 0;
    }
    int mid = (lc+rc)/2;
    if(lc>=l) {
        if(mn[2*ind+1] < val) return qry(l,val,2*ind+1,mid,rc);
        return qry(l,val,2*ind,lc,mid);
    }
    if(mn[2*ind+1] < val) return qry(l,val,2*ind+1,mid,rc);
    return qry(l,val,2*ind,lc,mid);
}
void solve() {
    for(int i=1; i<=n; i++) {
        vec[get(0, a[0][i])].pb(i);
    }
    for(int i=1; i<=n; i++) {
    for(auto it : vec[i]) {
        int maxi=qrymx(get(1,a[1][it])+1);
        int res = qry(get(1,a[1][it])+1, maxi);
if(res<1e9 && res != 0 && maxi != 0) ans = max(ans,a[0][it]+maxi+res);
    }
    for(auto it : vec[i]) {
        upd(get(1,a[1][it])+(1<<lg)-1, a[2][it]);
    }
    }
}
void reset() {
    fill(mx, mx+2*N, 0);
    fill(mn, mn+2*N, 1e9);
    for(int i=1; i<=n; i++) vec[i].clear();
}
int main () {
	ios_base::sync_with_stdio(false), cin.tie(0);
	cin>>n;
    for(int i=1; i<=n; i++) {
        for(int j=0; j<3; j++) {
            cin>>a[j][i]; comp[j].pb(a[j][i]);
        }
    }
    for(int i=0; i<3; i++) {
        sort(all(comp[i])); 
        comp[i].resize(unique(all(comp[i]))-comp[i].begin());
    }
    fill(mn, mn+2*N, 1e9);
    solve();
    reset();
    
    for(int i=1; i<=n; i++) swap(a[1][i], a[2][i]);
    swap(comp[1], comp[2]);
    solve();
    reset();
    
    for(int i=1; i<=n; i++) swap(a[1][i], a[0][i]);
    swap(comp[1], comp[0]);
    solve();
    reset();
    
    for(int i=1; i<=n; i++) swap(a[1][i], a[2][i]);
    swap(comp[1], comp[2]);
    solve();
    reset();
    for(int i=1; i<=n; i++) swap(a[1][i], a[0][i]);
    swap(comp[1], comp[0]);
    solve();
    reset();
    
    for(int i=1; i<=n; i++) swap(a[1][i], a[2][i]);
    swap(comp[1], comp[2]);
    solve();
    cout<<(ans==0 ? -1 : ans)<<endl;
	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... |