#include using namespace std; using ll = long long; const ll mod = 1e9 + 7; const int N = 200005; const int INF = 0x3f3f3f3f; struct SegTree { int n; struct Node { ll mx, tag; }; vector st; SegTree(int _n) : n(_n), st(4 * n + 4, {0, 0}) {} void gx(int u, ll v) { st[u].mx += v; st[u].tag += v; } void pushdown(int u) { if (st[u].tag) { gx(u << 1, st[u].tag); gx(u << 1 | 1, st[u].tag); st[u].tag = 0; } } void pushup(int u) { st[u].mx = max(st[u << 1].mx, st[u << 1 | 1].mx); } void upd(int u, int l, int r, int L, int R, ll v) { if (L > r || R < l) return; if (L <= l && r <= R) { gx(u, v); return; } int m = (l + r) >> 1; pushdown(u); upd(u << 1, l, m, L, R, v); upd(u << 1 | 1, m + 1, r, L, R, v); pushup(u); } void update(int L, int R, ll v) { if (L > R) return; upd(1, 0, n - 1, L, R, v); } ll query_max() const { return st[1].mx; } }; struct Point { ll x, y, v; }; struct Event { ll x; bool ok; int L, R; ll d; }; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; ll A; cin >> n >> A; vector pts(n); vector ys; ys.reserve(n); for (int i = 0; i < n; i++) { cin >> pts[i].x >> pts[i].y >> pts[i].v; ys.push_back(pts[i].y); } sort(ys.begin(), ys.end()); ys.erase(unique(ys.begin(), ys.end()), ys.end()); int tot = (int)ys.size(); vector ev; ev.reserve(2 * n); for (auto &p : pts) { int hi = lower_bound(ys.begin(), ys.end(), p.y) - ys.begin(); ll ylo = p.y - A; int lo = lower_bound(ys.begin(), ys.end(), ylo) - ys.begin(); ev.push_back({p.x, 0, lo, hi, -p.v}); ev.push_back({p.x - A, 1, lo, hi, +p.v}); } sort(ev.begin(), ev.end(), [&](auto &a, auto &b) { if (a.x != b.x) return a.x < b.x; return a.ok > b.ok; }); SegTree st(tot); ll ans = 0; int i = 0, E = ev.size(); while (i < E) { ll curx = ev[i].x; while (i < E && ev[i].x == curx && !ev[i].ok) { st.update(ev[i].L, ev[i].R, ev[i].d); i++; } while (i < E && ev[i].x == curx && ev[i].ok) { st.update(ev[i].L, ev[i].R, ev[i].d); i++; } ans = max(ans, st.query_max()); } cout << max(ans, 0ll) << "\n"; return 0; }