#include #include #include #include using namespace std; using namespace atcoder; typedef long long ll; typedef pair P; typedef pair PP; const ll INF = 1000000000000000; struct S{ ll x; }; S mx(S a, S b){ return S{max(a.x, b.x)}; } S e(){ return S{-INF}; } ll l[200005], r[200005]; ll v[200005]; ll dp[200005]; P pl[200005], pr[200005]; int re[200005]; void divide_conquer(int a, int b){ if(b - a <= 1){ for(int i = a; i < b; i++){ for(int j = a; j < i; j++){ if(l[i] <= l[j] && r[i] >= r[j]) dp[i] = max(dp[i], dp[j] + v[i]); } } return; } divide_conquer(a, (a + b) / 2); int m = b - a; for(int i = a; i < b; i++){ pl[i - a] = P(l[i], i); pr[i - a] = P(r[i], i); } sort(pl, pl + m); sort(pr, pr + m); for(int i = 0; i < m; i++) re[pl[i].second] = i; segtree seg(m); for(int e = 0; e < m; e++){ int i = pr[e].second; if(i < (a + b) / 2) seg.set(re[i], S{dp[i]}); else dp[i] = max(dp[i], seg.prod(re[i], m).x + v[i]); } divide_conquer((a + b) / 2, b); } int main() { int n; cin >> n; PP q[200005]; q[0] = PP(P(0, 0), 0); for(int i = 1; i <= n; i++){ ll t, x, v; cin >> t >> x >> v; q[i] = PP(P(t, x), v); } sort(q, q + n + 1); for(int i = 0; i <= n; i++){ v[i] = q[i].second; l[i] = q[i].first.second - q[i].first.first; r[i] = q[i].first.second + q[i].first.first; dp[i] = -INF; } dp[0] = 0; if(n <= 5000){ for(int i = 1; i <= n; i++){ for(int j = 0; j < i; j++){ if(l[i] <= l[j] && r[i] >= r[j]) dp[i] = max(dp[i], dp[j] + v[i]); } } } else{ divide_conquer(0, n + 1); } ll ans = 0; for(int i = 1; i <= n; i++) ans = max(ans, dp[i]); cout << ans << endl; }