N, K = gets.split.map(&:to_i)
D = (1..N).map{gets.split.map(&:to_i)}.sort_by{|ar| -ar[1]}

group = {}
taste = 0
pool = []
D[0...K].each do |t, d|
  taste += d 
  if group[t]
    pool << d
  else  
    group[t] = true 
  end
end
kind = group.size
pool.sort!

g2 = Hash.new(0)
D[K..-1].each do |t, d|
  if d > g2[t] && group[t].nil?
    g2[t] = d 
  end
end
other = g2.values.sort!.reverse

ans = taste + kind ** 2
(0...[pool.size, other.size].min).each do |i|
  taste += (other[i] - pool[i])
  kind += 1
  score = taste + kind ** 2
  ans = score if score > ans
end 

puts ans