import std.algorithm, std.array, std.container, std.range, std.bitmanip; import std.numeric, std.math, std.bigint, std.random, core.bitop; import std.string, std.regex, std.conv, std.stdio, std.typecons; class BiTree(T) { T[] b; // buffer size_t s; // size this(size_t t) { s = t; b = new int[](s + 1); } int get(size_t i) { if (!i) return 0; return b[i] + get(i - (i & -i)); } void add(size_t i, T v) { if (i > s) return; b[i] += v; add(i + (i & -i), v); } } void main() { auto rd = readln.split; auto n = rd[0].to!size_t, b = rd[1].to!int; auto pi = iota(n).map!(_ => readln.split.map!(to!int)).array; auto xi = pi.map!("a[0]").array; auto hx = compressMap(xi); auto w = hx.length; auto yi = pi.map!("a[1]").array; auto hy = compressMap(yi); auto h = hy.length; auto bitCi = new BiTree!int[](h); auto bitQi = new BiTree!int[](h); foreach (i; 0..h) { bitCi[i] = new BiTree!int(w); bitQi[i] = new BiTree!int(w); } foreach (p; pi) { bitCi[hy[p[1]]].add(hx[p[0]] + 1, 1); bitQi[hy[p[1]]].add(hx[p[0]] + 1, p[2]); } auto maxC = 0; foreach (i; 0..w-1) foreach (j; i..w) { auto ci = iota(h).map!(k => bitCi[k].get(j + 1) - bitCi[k].get(i)); auto qi = iota(h).map!(k => bitQi[k].get(j + 1) - bitQi[k].get(i)); auto k = 0, l = 0, sumC = 0, sumQ = 0; while (true) { ++l; if (l > h) break; sumC += ci[l - 1]; sumQ += qi[l - 1]; if (sumQ <= b) maxC = max(maxC, sumC); while (sumQ > b) { ++k; sumC -= ci[k - 1]; sumQ -= qi[k - 1]; } } } writeln(maxC); } int[int] compressMap(int[] pi) { pi = pi.sort().array.uniq().array; int[int] h; foreach (i, p; pi) h[p] = i.to!int; return h; }