#define ATCODER #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using ll = long long; #define FOR(i, a, b) for(ll i=(a); i<(b);i++) #define REP(i, n) for(ll i=0; i<(n);i++) #define ROF(i, a, b) for(ll i=(b-1); i>=(a);i--) #define PER(i, n) for(ll i=n-1; i>=0;i--) #define VL vector #define VVL vector> #define VP vector< pair > #define VVP vector>> #define all(i) begin(i),end(i) #define SORT(i) sort(all(i)) #define EXISTBIT(x,i) (((x>>i) & 1) != 0) #define MP(a,b) make_pair(a,b) #ifdef ATCODER #include using namespace atcoder; using mint = modint1000000007; using mint2 = modint998244353; #endif template vector read(size_t n) { vector ts(n); for (size_t i = 0; i < n; i++) cin >> ts[i]; return ts; } template void read_tuple_impl(TV&) {} template void read_tuple_impl(TV& ts) { get(ts).emplace_back(*(istream_iterator(cin))); read_tuple_impl(ts); } template decltype(auto) read_tuple(size_t n) { tuple...> ts; for (size_t i = 0; i < n; i++) read_tuple_impl(ts); return ts; } template T det2(array ar) { return ar[0] * ar[3] - ar[1] * ar[2]; } template T det3(array ar) { return ar[0] * ar[4] * ar[8] + ar[1] * ar[5] * ar[6] + ar[2] * ar[3] * ar[7] - ar[0] * ar[5] * ar[7] - ar[1] * ar[3] * ar[8] - ar[2] * ar[4] * ar[6]; } template bool chmax(T& tar, T src) { return tar < src ? tar = src, true : false; } template bool chmin(T& tar, T src) { return tar > src ? tar = src, true : false; } template void inc(vector& ar) { for (auto& v : ar) v++; } template void dec(vector& ar) { for (auto& v : ar) v--; } template vector> id_sort(vector& a) { vector res(a.size()); for (int i = 0; i < a.size(); i++)res[i] = MP(a[i], i); SORT(res); return res; } using val = ll; using func = ll; val op(val a, val b) { return max(a, b); } val e() { return -1e18; } val mp(func f, val a) { return a + f; } func comp(func f, func g) { return f + g; } func id() { return 0; } // Rook ll dxr[4] = { 1,0,-1,0 }; ll dyr[4] = { 0,1,0,-1 }; // Bishop ll dxb[4] = { -1,-1,1,1 }; ll djb[4] = { -1,1,-1,1 }; // qween ll dxq[8] = { 0,-1,-1,-1,0,1,1,1 }; ll dyq[8] = { -1,-1,0,1,1,1,0,-1 }; // NekoTanuki // Ponnyaaaaaaaaaaaaaaaaaaaaaaaaaa void solve() { ll n, a; cin >> n >> a; auto [x, y, v] = read_tuple(n); a *= 2; VL xs, ys; vector> yx; REP(i, n) { x[i] *= 2; y[i] *= 2; xs.push_back(x[i]); xs.push_back(x[i] + a + 1); ys.push_back(y[i]); ys.push_back(y[i] + a + 1); yx.emplace_back(y[i], x[i], i); yx.emplace_back(y[i] + a + 1, x[i], - i - 1); } SORT(xs);; SORT(ys); SORT(yx); xs.erase(unique(all(xs)), xs.end()); ys.erase(unique(all(ys)), ys.end()); lazy_segtree st(xs.size()); REP(i, xs.size()) { st.set(i, 0LL); } ll ans = 0; ll revy = -1e18; for (auto& [yy, xx, ii] : yx) { if (yy != revy) { revy = yy; chmax(ans, st.all_prod()); } if (ii >= 0) { ll l = lower_bound(all(xs), xx) - xs.begin(); ll r = lower_bound(all(xs), xx + a + 1) - xs.begin(); st.apply(l, r, v[ii]); } else { ll l = lower_bound(all(xs), xx) - xs.begin(); ll r = lower_bound(all(xs), xx + a + 1) - xs.begin(); st.apply(l, r, -v[-ii-1]); } } chmax(ans, st.all_prod()); cout << ans; } int main() { ll t = 1; // cin >> t; while (t--) { solve(); } return 0; }