# frozen_string_literal: true

n, w = gets.split.map(&:to_i)

table = [[0, 0]]
n.times do
  weight, value = gets.split.map(&:to_i)
  new_rows = table.select { |row| row[0] + weight <= w }.map { |row| [row[0] + weight, row[1] + value] }
  table.concat new_rows
end

puts table.max_by { |row| row[1] }[1]
