// yukicoder: No.165 四角で囲え! // 2019.6.21 bal4u #include #include #if 1 #define gc() getchar_unlocked() #else #define gc() getchar() #endif int in() // 整数の入力(負数対応) { int n = 0, c = gc(); if (c == '-') { c = gc(); do n = 10*n + (c & 0xf), c = gc(); while (c >= '0'); return -n; } do n = 10*n + (c & 0xf), c = gc(); while (c >= '0'); return n; } typedef struct { int x, y, p; } PP; PP t[405]; int x[405], xs; int y[405], ys; int map[405][405], no[405][405]; int sum[405][405], cnt[405][405]; int N, B; int cmp(const void *a, const void *b) { return *(int *)a - *(int *)b; } int uniq(int *a, int n) { int i, j; for (i = 0, j = 1; j < n; j++) { while (j < n && a[j] == a[i]) j++; if (j < n) if (++i != j) a[i] = a[j]; } return i+1; } int bsch(int *a, int x, int r) { int m, l = 0; while (l < r) { m = (l + r) >> 1; if (a[m] == x) return m; if (a[m] < x) l = m + 1; else r = m; } return l-1; } int main() { int i, a, r, c, rr, cc, ans; N = in(), B = in(); for (i = 0; i < N; i++) { t[i].x = x[i] = in(), t[i].y = y[i] = in(), t[i].p = in(); } qsort(x, N, sizeof(int), cmp); xs = uniq(x, N); qsort(y, N, sizeof(int), cmp); ys = uniq(y, N); for (i = 0; i < N; i++) { c = bsch(x, t[i].x, xs); r = bsch(y, t[i].y, ys); map[r][c] = t[i].p, no[r][c] = 1; } for (r = 0; r < ys; r++) for (c = 0; c < xs; c++) { sum[r+1][c+1] = sum[r][c+1] + sum[r+1][c] - sum[r][c] + map[r][c]; cnt[r+1][c+1] = cnt[r][c+1] + cnt[r+1][c] - cnt[r][c] + no[r][c]; } ans = 0; for (r = 0; r < ys; r++) for (rr = 1; rr <= ys; rr++) { c = 0, cc = 1; while (cc <= xs) { while (cc <= xs && sum[rr][cc] - sum[r][cc] - sum[rr][c] + sum[r][c] <= B) { a = cnt[rr][cc] - cnt[r][cc] - cnt[rr][c] + cnt[r][c]; if (a > ans) { ans = a; if (ans == N) goto done; } cc++; } while (c < cc && sum[rr][cc] - sum[r][cc] - sum[rr][c] + sum[r][c] > B) c++; } } done: printf("%d\n", ans); return 0; }