#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 p[200005]; void divide_conquer(int a, int b){ if(b - a <= 1) return; divide_conquer(a, (a + b) / 2); map mp; for(int i = a; i < b; i++) mp[l[i]]++; int m = b - a, k = 0; for(auto itr = mp.begin(); itr != mp.end(); itr++) mp[itr->first] = k++; for(int e = 0; e < m; e++){ int i = a + e; p[e] = P(r[i], i); } sort(p, p + m); segtree seg(k); for(int e = 0; e < m; e++){ int i = p[e].second; if(i < (a + b) / 2) seg.set(mp[l[i]], mx(seg.get(mp[l[i]]), S{dp[i]})); else dp[i] = max(dp[i], seg.prod(mp[l[i]], k).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; divide_conquer(0, n + 1); ll ans = 0; for(int i = 1; i <= n; i++) ans = max(ans, dp[i]); cout << ans << endl; }