job_count,period = gets.split.map{|s|s.to_i}
jobs = [] 
job_count.times { 
  days,pay = gets.split.map{|s|s.to_i}
  jobs.push({days: days, pay: pay})
}

def jobs_to_pay(jobs,period)
  return 0 if jobs.empty?
  return 0 if period == 0
  return 0 if jobs.first[:days] > period
  jobs.first[:pay] + jobs_to_pay(jobs.drop(1),period - 1)
end

puts jobs
.permutation([period,jobs.size].min)
.map{|js|
  jobs_to_pay(js,period)
}.max