#include #include #include #include #include #include #include #include #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; constexpr int INF = 1001001001; constexpr int mod = 1000000007; // constexpr int mod = 998244353; template inline bool chmax(T& x, T y){ if(x < y){ x = y; return true; } return false; } template inline bool chmin(T& x, T y){ if(x > y){ x = y; return true; } return false; } template struct CumulativeSum2D{ vector> data; CumulativeSum2D(int W, int H) : data(W + 1, vector(H + 1, 0)) {} void add(int x, int y, T z){ ++x, ++y; if(x >= data.size() || y >= data[0].size()) return; data[x][y] += z; } void build(){ for(int i = 1; i < (int)data.size(); ++i){ for(int j = 1; j < (int)data[i].size(); ++j){ data[i][j] += data[i][j - 1] + data[i - 1][j] - data[i - 1][j - 1]; } } } T query(int sx, int sy, int gx, int gy){ return (data[gx][gy] - data[sx][gy] - data[gx][sy] + data[sx][sy]); } }; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int N, B; cin >> N >> B; vector X(N), Y(N), P(N), xs, ys; for(int i = 0; i < N; ++i){ cin >> X[i] >> Y[i] >> P[i]; xs.emplace_back(X[i]); ys.emplace_back(Y[i]); } sort(begin(xs), end(xs)); sort(begin(ys), end(ys)); xs.erase(unique(begin(xs), end(xs)), end(xs)); ys.erase(unique(begin(ys), end(ys)), end(ys)); int H = xs.size(), W = ys.size(); CumulativeSum2D score(H, W), point(H, W); for(int i = 0; i < N; ++i){ int x = lower_bound(begin(xs), end(xs), X[i]) - begin(xs); int y = lower_bound(begin(ys), end(ys), Y[i]) - begin(ys); score.add(x, y, P[i]); point.add(x, y, 1); } score.build(); point.build(); int ans = 0; for(int lx = 0; lx < H; ++lx){ for(int rx = lx + 1; rx <= H; ++rx){ for(int ly = 0; ly < W; ++ly){ int low = ly, high = W + 1; while(high - low > 1){ int mid = (low + high) / 2; if(score.query(lx, ly, rx, mid) <= B) low = mid; else high = mid; } chmax(ans, point.query(lx, ly, rx, low)); } } } cout << ans << endl; return 0; }