#include #include #include #include #include #include using namespace std; int N; int B; struct Point { Point() {} Point(int x, int y, int p) : x(x), y(y), p(p) {} int x; int y; int p; int ix; int iy; }; vector points; void input(istream& in) { in >> N >> B; points.resize(N); for (int i = 0; i < N; i++) { in >> points[i].x >> points[i].y >> points[i].p; } } int resolve() { map> Xmap; map> Ymap; for (int i = 0; i < N; i++) { Point &pt = points[i]; Xmap[pt.x].push_back(&pt); Ymap[pt.y].push_back(&pt); } vector X; for (auto ite : Xmap) { X.push_back(ite.first); } map X_to_index; int ix = 0; for (auto ite : X) { X_to_index[ite] = ix++; } vector Y; for (auto ite : Ymap) { Y.push_back(ite.first); } map Y_to_index; int iy = 0; for (auto ite : Y) { Y_to_index[ite] = iy++; } vector>> sum_scores(Y.size()); for (size_t iy = 0; iy < Y.size(); iy++) { sum_scores[iy].resize(X.size()); } for (int i = 0; i < N; i++) { Point &pt = points[i]; pt.ix = X_to_index[pt.x]; pt.iy = Y_to_index[pt.y]; auto& sum_score = sum_scores[pt.iy]; for (size_t ix = pt.ix; ix < X.size(); ix++) { sum_score[ix].first++; sum_score[ix].second += pt.p; } } int best_count = 0; for (size_t ix1 = 0; ix1 < X.size()-1; ix1++) { for (size_t ix2 = ix1+1; ix2 < X.size(); ix2++) { size_t iy1 = 0; size_t iy2 = 0; int score = 0; int count = 0; while (iy1 < Y.size()) { while (iy2 < Y.size() && score <= B) { count += sum_scores[iy2][ix2].first - sum_scores[iy2][ix1].first; score += sum_scores[iy2][ix2].second - sum_scores[iy2][ix1].second; iy2++; } if (score <= B) { if (count > best_count) { best_count = count; } break; } count -= sum_scores[iy1][ix2].first - sum_scores[iy1][ix1].first; score -= sum_scores[iy1][ix2].second - sum_scores[iy1][ix1].second; iy1++; if (count > best_count) { best_count = count; } } } } return best_count; } int main(int argc, char **argv) { input(cin); cout << resolve() << endl; return 0; }