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

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

sum1 = Array.new(hash[1].size) {0}
sum0 = Array.new(hash[0].size) {0}

hash[1].each_with_index do |a, i|
  sum1[i+1] = sum1[i] + a
end

hash[0].each_with_index do |a, i|
  sum0[i+1] = sum0[i] + a
end

max = 0
1.upto(sushi.keys.size) do |i|
  next if i >= sum1.size || (K-i) >= sum0.size
  tmp = sum1[i] + sum0[K-i] + i**2
  if tmp > max
    max = tmp
  end
end

p max
