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,l,r) for(int i = (l); i < (r); i++)
#define per(i,r,l) for(int i = (r); i >= (l); i--)
#define all(x) begin(x), end(x)
#define sz(x) (int)size(x)
#define pb push_back
#define ff first
#define ss second
typedef long long ll;
typedef pair<int, int> pp;
void dbg(){
     cerr << endl;
}
template<typename H, typename... T> void dbg(H h, T... t){
     cerr << h << ", ";
     dbg(t...);
}
void IOS(){
     cin.tie(0) -> sync_with_stdio(0);
     #ifndef ONLINE_JUDGE
          // freopen("inp.txt", "r", stdin);
          // freopen("out.txt", "w", stdout);
          #define er(...) cerr << __LINE__ << " <" << #__VA_ARGS__ << ">: ", dbg(__VA_ARGS__)
     #else
          #define er(...) 0
     #endif
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll mod = 998244353, maxn = 5e5 + 5, lg = 21, inf = ll(1e9) + 5;
int X = -1, Y = -1;
multiset<pp> s;
void add(int x, int y){
     vector<pp> del;
     auto it = s.lower_bound(pp(x, -1));
     while(it != begin(s)){
          it--;
          if(it->ss <= y) break;
          del.pb(*it);
     }
     auto it2 = s.upper_bound(pp(x, inf));
     while(it2 != end(s)){
          if(it2->ss >= y) break;
          del.pb(*it2);
          it2++;
     }
     if(del.empty()){
          s.insert({x, y});
     } else{
          X = max(X, x), Y = max(Y, y);
          for(auto p: del){
               auto it = s.find(p);
               s.erase(it);
               X = max(X, p.ff), Y = max(Y, p.ss);
          }
     }
}
int main(){ IOS();
     int n; cin >> n;
     map<int, vector<pp>> mp;
     rep(i,0,n){
          int x, y, z; cin >> x >> y >> z;
          mp[z].pb({x, y});
     }
     int ans = -1;
     for(auto[Z, v]: mp){
          for(auto[x, y]: v){
               if(X > x && Y > y){
                    ans = max(ans, Z + X + Y);
               }
          }
          for(auto[x, y]: v){
               add(x, y);
          }
     }
     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... |