#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const ll inf=2e18+5;
struct Seg{
	ll n,shift=0;
	vector<ll>tree,lazy;
	vector<int>left_child;
	int add(){
		tree.push_back(-inf);
		lazy.push_back(0);
		left_child.push_back(-1);
		tree.push_back(-inf);
		lazy.push_back(0);
		left_child.push_back(-1);
		return tree.size()-2;
	}
	int lef(int node){
		if(left_child[node]==-1)left_child[node]=add();
		return left_child[node];
	}
	void init(ll N){
		n=N;
		tree.push_back(-inf);
		lazy.push_back(0);
		left_child.push_back(-1);
	}
	void push(int node,ll left,ll right){
		if(lazy[node]==0)return;
		tree[node]+=lazy[node];
		if(left!=right){
			lazy[lef(node)]+=lazy[node];
			lazy[lef(node)+1]+=lazy[node];
		}
		lazy[node]=0;
	}
	ll l,r,x;
	void up(int node=0,ll left=-inf,ll right=-inf){
		if(right==-inf){
			left=-n;
			right=n;
		}
		if(left>=l&&right<=r){
			lazy[node]+=x;
			push(node,left,right);
			return;
		}
		push(node,left,right);
		if(left>r||right<l)return;
		ll mid=(left+right)>>1;
		up(lef(node),left,mid);
		up(lef(node)+1,mid+1,right);
		tree[node]=max(tree[lef(node)],tree[lef(node)+1]);
	}
	void update(ll left,ll right,ll val){
		l=left-shift;r=right-shift;x=val;
		l=max(l,-n);
		r=min(r,n);
		up();
	}
	ll qu(int node=0,ll left=-inf,ll right=-inf){
		if(right==-inf){
			left=-n;
			right=n;
		}
		if(left>r||right<l)return -inf;
		push(node,left,right);
		if(left>=l&&right<=r)return tree[node];
		ll mid=(left+right)>>1;
		return max(qu(lef(node),left,mid),qu(lef(node)+1,mid+1,right));
	}
	ll query(ll left,ll right){
		l=left-shift;r=right-shift;
		l=max(l,-n);
		r=min(r,n);
		return qu();
	}
};
int n;
ll ans=0;
Seg seg;
const ll lim=1e15;
int main(){
	ios_base::sync_with_stdio(false);cin.tie(NULL);
	seg.init(lim);
	cin>>n;
	for(int i=0;i<n;i++){
		ll x,g,d;cin>>x>>g>>d;
		ans=max(ans,max(0ll,seg.query(x-d,lim))+g);
		seg.update(x,x,max(0ll,-seg.query(x,x)));
		seg.update(-lim,lim,g);
		seg.shift+=d;
	}
	cout<<ans;
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |