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 = []
del = []
kind = Array.new(N+1, 0)
ks = 0
yum = 0
K.times do 
  n = sushi.shift
  yum += n[1]
  if kind[n[0]] == 0
	eat.push(n + [0])
    kind[n[0]] = 1
    ks += 1
  else
    del.push(n + [1])
  end
end
cand = [yum + ks ** 2]

while !sushi.empty? && !del.empty?
  n = sushi.shift
  if kind[n[0]] == 0
    yum = yum - del[-1][1] + n[1]
    del.pop
    eat.push(n + [1])
    kind[n[0]] = 1
    ks += 1
    cand.push(yum + ks ** 2)
  end
end

puts cand.max