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

group = Array.new(N)
taste = 0
kind = 0
pool = []
for i in 0...K
  t, d = D[i]
  taste += d 
  if group[t]
    pool << d
  else  
    group[t] = true 
    kind += 1
  end
end

ans = taste + kind ** 2
for i in K...D.size
  t, d = D[i]
  next if group[t]
  break if pool.empty?
  group[t] = true
  taste += d - pool.pop
  kind += 1
  tmp = taste + kind ** 2
  ans = tmp if tmp > ans
end

puts ans
