#pragma GCC optimize("Ofast") #include #include #include #include #include #include #include #include #include #include using namespace std; using ll = long long int; #define REP(i, n) for(int i = 0; i < (int)(n); i++) #define YES(n) std::cout << ((n) ? "YES" : "NO" ) << "\n"; #define Yes(n) std::cout << ((n) ? "Yes" : "No" ) << "\n"; #define ALL_INCLUDED_ENVIRONMENT template class C> std::ostream& operator<<(std::ostream& o, const C& v) { o << "{"; bool init = 1; for (auto e : v) { if (init) { init = 0; } else { o << ", "; } o << e; } o << "}"; return o; } int n, b, sup, res; vector xs, ys, ps, table; vector > cumuls, exists; template inline T get_2dcumul(const vector >& cumuls, int y1, int x1, int y2, int x2) { return cumuls[y2][x2] - cumuls[y2][x1] - cumuls[y1][x2] + cumuls[y1][x1]; } int main() { // cin.tie(0); // ios::sync_with_stdio(false); cin >> n >> b; REP (i, n) { int x, y, p; cin >> x >> y >> p; xs.push_back(x); ys.push_back(y); ps.push_back(p); table.push_back(x); table.push_back(y); } sort(table.begin(), table.end(), std::less()); table.erase(unique(table.begin(), table.end()), table.end()); REP (i, n) { xs[i] = lower_bound(table.begin(), table.end(), xs[i]) - table.begin(); ys[i] = lower_bound(table.begin(), table.end(), ys[i]) - table.begin(); } sup = table.size(); cumuls = vector >(sup+1, vector(sup+1, 0)); exists = vector >(sup+1, vector(sup+1, 0)); REP (i, n) { int x = xs[i]; int y = ys[i]; cumuls[y+1][x+1] += ps[i]; exists[y+1][x+1] += 1; } REP (i, sup+1) { REP (j, sup) { cumuls[i][j+1] += cumuls[i][j]; exists[i][j+1] += exists[i][j]; } } REP (j, sup+1) { REP (i, sup) { cumuls[i+1][j] += cumuls[i][j]; exists[i+1][j] += exists[i][j]; } } res = 0; for (int y1 = 0; y1 <= sup; y1++) { for (int y2 = y1+1; y2 <= sup; y2++) { int x1 = 0; for (int x2 = 0; x2 <= sup; x2++) { while(get_2dcumul(cumuls, y1, x1, y2, x2) > b) { x1++; } res = max(res, get_2dcumul(exists, y1, x1, y2, x2)); } } } cout << res << "\n"; return 0; }