#include using namespace std; using ll = long long; bool chmin(auto &a, auto b) { return a > b ? a = b, true : false; } bool chmax(auto &a, auto b) { return a < b ? a = b, true : false; } #include template vector merge_and_unique(const vector &a, const Ts &...z) { vector res = a; (res.insert(res.end(), z.begin(), z.end()), ...); sort(res.begin(), res.end()); res.erase(unique(res.begin(), res.end()), res.end()); return res; } template struct fenwick_tree { using M = Abelian_Group; using S = typename M::S; fenwick_tree() = default; fenwick_tree(int _n) : fenwick_tree(vector(_n, M::e())) {} fenwick_tree(const vector &_v) : n(_v.size()) { d = vector(n, M::e()); v = vector(n, M::e()); for (int i = 1; i <= n; i++) { v[i - 1] = _v[i - 1]; d[i - 1] += v[i - 1]; int j = i + (i & -i); if (j <= n) d[j - 1] += d[i - 1]; } } void add(int p, S x) { assert(0 <= p && p < n); v[p] = M::op(v[p], x); for (p++; p <= n; p += p & -p) { d[p - 1] = M::op(d[p - 1], x); } } void set(int p, S x) { assert(0 <= p && p < n); add(p, M::op(x, M::inv(v[p]))); } S prod(int l, int r) const { assert(0 <= l && l <= r && r <= n); return prod(r) - prod(l); } private: int n; vector d, v; S prod(int p) const { S res = M::e(); for (; p > 0; p -= p & -p) { res = M::op(res, d[p - 1]); } return res; } }; template struct static_point_add_rectangle_sum { using M = Abelian_Group; using S = typename M::S; static_point_add_rectangle_sum() = default; static_point_add_rectangle_sum(int n, int q) { xs.reserve(n); ys.reserve(n); ws.reserve(n); xls.reserve(q); yls.reserve(q); xrs.reserve(q); yrs.reserve(q); } void add_point(T x, T y, S w) { xs.emplace_back(x); ys.emplace_back(y); ws.emplace_back(w); } void add_query(T xl, T yl, T xr, T yr) { xls.emplace_back(xl); yls.emplace_back(yl); xrs.emplace_back(xr); yrs.emplace_back(yr); } vector solve() { int n = xs.size(), q = xls.size(); if (n == 0 || q == 0) return vector(q, M::e()); auto yb = merge_and_unique(ys); for (T &y : ys) { y = lower_bound(yb.begin(), yb.end(), y) - yb.begin(); } vector ord_pts(n), ord_query(2 * q); iota(ord_pts.begin(), ord_pts.end(), 0); iota(ord_query.begin(), ord_query.end(), 0); ranges::sort(ord_pts, {}, [&](int i) { return xs[i]; }); ranges::sort(ord_query, {}, [&](int i) { return i < q ? xls[i] : xrs[i - q]; }); fenwick_tree fw(yb.size()); vector res(q); int j = 0; for (int i : ord_query) { T x = (i < q ? xls[i] : xrs[i - q]); while (j < n && xs[ord_pts[j]] < x) { fw.add(ys[ord_pts[j]], ws[ord_pts[j]]); j++; } if (i < q) { int yl = lower_bound(yb.begin(), yb.end(), yls[i]) - yb.begin(); int yr = lower_bound(yb.begin(), yb.end(), yrs[i]) - yb.begin(); res[i] = M::op(res[i], M::inv(fw.prod(yl, yr))); } else { i -= q; int yl = lower_bound(yb.begin(), yb.end(), yls[i]) - yb.begin(); int yr = lower_bound(yb.begin(), yb.end(), yrs[i]) - yb.begin(); res[i] = M::op(res[i], fw.prod(yl, yr)); } } return res; } private: vector xs, ys, xls, yls, xrs, yrs; vector ws; }; template struct add_monoid { using S = T; static S op(const S &a, const S &b) { return a + b; } static S e() { return 0; } static S inv(const S &x) { return -x; } static S pow(const S &x, long long k) { return x * k; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N, L; cin >> N >> L; L = 2 * L + 1; static_point_add_rectangle_sum> RS(N, 4 * N); for (ll X, Y, V; N--; ) { cin >> X >> Y >> V; X = 2 * X, Y = 2 * X; RS.add_point(X, Y, V); RS.add_query(X, Y, X + L, Y + L); RS.add_query(X, Y - L + 1, X + L, Y + 1); RS.add_query(X - L + 1, Y, X + 1, Y + L); RS.add_query(X - L + 1, Y - L + 1, X + 1, Y + 1); RS.add_query(X + 1, Y + 1, X + L + 1, Y + L + 1); RS.add_query(X + 1, Y - L, X + L + 1, Y); RS.add_query(X - L, Y + 1, X, Y + L + 1); RS.add_query(X - L, Y - L, X, Y); } ll ans = 0; for (auto v : RS.solve()) chmax(ans, v); cout << ans << endl; }