N, K = gets.split.map(&:to_i)
sushi = Hash.new {[]}
N.times do
  t, d = gets.split.map(&:to_i)
  sushi[t] += [d]
end

p sushi
hash = Hash.new {[]}
sushi.each do |k, v|
  tmp = v.sort.reverse
  hash[1] += [tmp.first]
  hash[0] += tmp[1..-1]
end

hash[1] = hash[1].sort.reverse
hash[0] = hash[0].sort.reverse

max = 0
1.upto(sushi.keys.size) do |i|
  tmp = hash[1].take(i).reduce(:+).to_i + hash[0].take(K-i).reduce(:+).to_i + i**2
  if tmp > max
    max = tmp
  end
end

p max
