void main(){ import std.stdio, std.string, std.conv, std.algorithm; import std.typecons; int n; long wmax; rd(n, wmax); auto v=new long[](n), w=new long[](n); foreach(i; 0..n) rd(v[i], w[i]); size_t B=50000; alias T=Tuple!(long, "vsum", long, "wsum"); T[] cur; cur~=T(0, 0); foreach(i; 0..n){ T[] nex; foreach(e; cur){ nex~=e; if(e.wsum+w[i]<=wmax) nex~=T(e.vsum+v[i], e.wsum+w[i]); } sort!((x, y)=>(x.vsum>y.vsum))(nex); if(nex.length>B) cur=nex[0..B]; else cur=nex; } writeln(cur[0].vsum); } void rd(T...)(ref T x){ import std.stdio, std.string, std.conv; auto l=readln.split; assert(l.length==x.length); foreach(i, ref e; x){ e=l[i].to!(typeof(e)); } } void wr(T...)(T x){ import std.stdio; foreach(e; x) stderr.write(e, " "); stderr.writeln(); }