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

strong = {}
yowai_sushi = []
n.times do |i|
  t, d = gets.split.map(&:to_i)
  if strong.include?(t)
    if strong[t] < d
      yowai_sushi << strong[t]
      strong[t] = d
    else
      yowai_sushi << d
    end
  else
    strong[t] = d
  end
end

strong = strong.values.sort!.reverse!
yowai_sushi.sort!.reverse!

intstrong = [0]
intyowai = [0]

accum = 0
strong.each do |x|
  accum += x
  intstrong << accum
end

accum = 0
yowai_sushi.each do |x|
  accum += x
  intyowai << accum
end

max = 0
([k, strong.size].min+1).times do |t|
  if k - t < intyowai.size
    tmp = intstrong[t] + intyowai[k - t] + t*t
    max = tmp if max < tmp
  end
end

p max
