#include <bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
using namespace std;
typedef long long ll;
#define pll pair<ll,ll>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
struct trio{
	ll x, g, e;
};
ll ans=0;
vector<trio> vect;
bool custom(trio a, trio b){
	return a.x<b.x;
}
void dnc(ll l, ll r){
	if(l>r)return;
	ll mid=(l+r)/2, mx=0;
	vector<pll> left, right;
	for (ll i=mid+1, sume=0, sumg=0; i<=r; ++i)sume+=vect[i].e, sumg+=vect[i].g, right.pb(mp(sume-vect[i].x+vect[mid].x, sumg));
	for (ll i=mid-1, sume=0, sumg=0; i>=l; --i)sume+=vect[i].e, sumg+=vect[i].g, left.pb(mp(sume+vect[i].x-vect[mid].x, sumg));
	for (auto a:right)if (vect[mid].e+a.fi>=0)mx=max(mx, a.se);
	for (auto a:left)if (vect[mid].e+a.fi>=0)mx=max(mx, a.se);
	for (ll i=right.size()-2; i>=0; --i)right[i].fi=max(right[i].fi, right[i+1].fi);
	reverse(right.begin(), right.end());
	for (ll i=0; i<left.size(); ++i){
		auto it = lower_bound(right.begin(), right.end(), mp(-left[i].fi-vect[mid].e, 0ll));
		if (it!=right.end())mx=max(mx, left[i].se+it->se);
	}
	ans=max(ans, vect[mid].g+mx);
	if (l==r)return;
	dnc(l, mid-1);
	dnc(mid+1, r);
}
signed main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int n;
	cin>>n;
	vect.resize(n);
	for (int i=0; i<n; ++i)cin>>vect[i].x>>vect[i].g>>vect[i].e;
	sort(vect.begin(), vect.end(),custom);
	dnc(0, n-1);
	cout<<ans;
    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... |