N, K = gets.split(' ').map(&:to_i)
sushi = []
N.times do
  t = gets.split(' ').map(&:to_i)
  sushi.push(t)
end
sushi.sort_by!{|s| -s[1]}

eat = []
kind = []
K.times do 
  n = sushi.shift
  if !kind.include?(n[0])
	eat.push(n + [0])
  	kind.push(n[0])
  else
    eat.push(n + [1])
  end
end
yum = eat.collect{|n| n[1]}.inject(:+)
cand = [yum + kind.size ** 2]

while !sushi.empty? && kind.size < K
  n = sushi.shift
  if !kind.include?(n[0])
    del = eat.rindex{|m| m[2] == 1}
    yum = yum - eat[del][1] + n[1]
    eat.delete_at(del)
    eat.push(n + [1])
    kind.push(n[0])
    cand.push(yum + kind.size ** 2)
  end
end

puts cand.max