n, k    = gets.split.map(&:to_i)
#ushi   = Array.new(n).map{Array.new}
sushi = []

n.times do
  neta, taste = gets.split.map(&:to_i)
  sushi << [neta, taste]
  #sushi[neta - 1] << taste
end

sum     = 0
various = []

sushi = sushi.sort_by{|a| a[1]}.reverse

k.times do |i|
  sum     += sushi[i][1]
  various << sushi[i][0]
end

various = various.uniq.size
puts sum + various ** 2