function pint(s) { return parseInt(s); }
function pfloat(s) { return parseFloat(s); }
Array.prototype.sortNumAsc  = function () { return this.sort((a, b) => (a - b)); }
Array.prototype.sortNumDesc = function () { return this.sort((a, b) => (b - a)); }
Array.prototype.sortStrAsc  = function () { return this.sort();                  }
Array.prototype.sortStrDesc = function () { return this.sort().reverse();        }
Array.prototype.without = function (itm) { return this.filter((elem) => elem !== itm); }
var input = '';
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', chunk => input += chunk);
process.stdin.on('end', () => {
  var lines = input.split('\n').map(l => l.trim());
  if (lines[lines.length-1] === '')
    lines = lines.slice(0, -1);
  main(lines);
});
function main(lines) {
  var arr = lines[0].split(' ').map(pint);
  var N = arr[0];
  var W = arr[1];
  lines = lines.slice(1).map(l => l.split(' ').map(pint));
  var t = 0;
  var dp = [{}, {}];
  dp[t][0] = 0;
  for (var i = 0; i < N; i++) {
    t ^= 1;
    dp[t] = {};
    var w = lines[i][0];
    var v = lines[i][1];
    for (var n in dp[t ^ 1]) {
      var wt = pint(n);
      var va = dp[t ^ 1][n];
      dp[t][wt] = Math.max(dp[t][wt] || 0, va);
      if (wt + w <= W)
        dp[t][wt+w] = Math.max(dp[t][wt+w] || 0, va+v);
    }
  }
  var ans = 0;
  for (var n in dp[t]) {
    var v = dp[t][n];
    ans = Math.max(ans, v);
  }
  console.log(ans);
}