#define _USE_MATH_DEFINES #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair i_i; typedef pair ll_i; typedef pair d_i; typedef pair ll_ll; typedef pair d_d; struct edge { int v, w; }; ll MOD = 1000000007; ll _MOD = 1000000009; double EPS = 1e-10; int compress(vector& X) { vector x = X; sort(x.begin(), x.end()); x.erase(unique(x.begin(), x.end()), x.end()); for (int i = 0; i < X.size(); i++) X[i] = lower_bound(x.begin(), x.end(), X[i]) - x.begin(); return x.size(); } int calc(int l, int r, int u, int d, vector >& sum) { return sum[d][r] - sum[d][l] - sum[u][r] + sum[u][l]; } int main() { int N, B; cin >> N >> B; vector X(N), Y(N), P(N); for (int i = 0; i < N; i++) cin >> X[i] >> Y[i] >> P[i]; int w = compress(X), h = compress(Y); vector > a(h, vector(w)), p(h, vector(w)); for (int i = 0; i < N; i++) { a[Y[i]][X[i]]++; p[Y[i]][X[i]] += P[i]; } vector > asum(h + 1, vector(w + 1)), psum(h + 1, vector(w + 1)); for (int y = 1; y <= h; y++) for (int x = 1; x <= w; x++) { asum[y][x] = asum[y][x - 1] + asum[y - 1][x] + asum[y - 1][x - 1] + a[y - 1][x - 1]; psum[y][x] = psum[y][x - 1] + psum[y - 1][x] + psum[y - 1][x - 1] + p[y - 1][x - 1]; } int maxi = 0; for (int l = 0; l <= w; l++) for (int r = l; r <= w; r++) { int d = 0; for (int u = 0; u <= h; u++) while (d <= h && calc(l, r, u, d, psum) <= B) { maxi = max(maxi, calc(l, r, u, d, asum)); d++; } } cout << maxi << endl; }