/* imports all std modules {{{*/
import
  std.algorithm,
  std.array,
  std.ascii,
  std.base64,
  std.bigint,
  std.bitmanip,
  std.compiler,
  std.complex,
  std.concurrency,
  std.container,
  std.conv,
  std.csv,
  std.datetime,
  std.demangle,
  std.encoding,
  std.exception,
  std.file,
  std.format,
  std.functional,
  std.getopt,
  std.json,
  std.math,
  std.mathspecial,
  std.meta,
  std.mmfile,
  std.net.curl,
  std.net.isemail,
  std.numeric,
  std.parallelism,
  std.path,
  std.process,
  std.random,
  std.range,
  std.regex,
  std.signals,
  std.socket,
  std.stdint,
  std.stdio,
  std.string,
  std.system,
  std.traits,
  std.typecons,
  std.uni,
  std.uri,
  std.utf,
  std.uuid,
  std.variant,
  std.zip,
  std.zlib;
/*}}}*/

/+---test
3 8
3 30
4 50
5 60
---+/
/+---test
5 5
1 1000000000
1 1000000000
1 1000000000
1 1000000000
1 1000000000
---+/
/+---test
6 15
6 5
5 6
6 4
6 6
3 5
7 2
---+/

void main(string[] args) {
  long N, W;
  readf(" %d %d ", &N, &W);

  struct Item { long w, v; }
  auto items = new Item[N];
  foreach (ref item; items) readf(" %d %d ", &item.w, &item.v);

  long[][] dp;
  dp.length = N+1;
  dp.each!((ref x) => x.length = W+1);

  foreach (n; 0..N) foreach (w; 0..W+1) {
    if (w >= items[n].w) {
      dp[n+1][w] = max(dp[n][w-items[n].w] + items[n].v, dp[n][w]);
    } else {
      dp[n+1][w] = dp[n][w];
    }
  }
  long ans;
  dp[$-1].each!(x => ans = ans.max(x));
  ans.writeln;
}
