N, K = gets.split.map(&:to_i)
sushis = N.times.map { gets.split.map(&:to_i) }.sort_by! { |sushi| -sushi[1] }
current_sushis = sushis[0...K]
used = Hash.new(false)
tpt = 0
dup_sushis = []
current_sushis.each do |sushi|
  tpt += sushi[1]
  dup_sushis << sushi if used[sushi[0]]
  used[sushi[0]] = true
end
dup_sushis.sort_by! { |sushi| sushi[1] }
# p sushis, current_sushis, dup_sushis, used

types_count = used.size
max_points = types_count ** 2 + tpt

sushis[K..-1].each do |sushi|
  next if used[sushi[0]]

  outdated_sushi = dup_sushis.shift
  break unless outdated_sushi

  tpt = tpt - outdated_sushi[1] + sushi[1]
  types_count += 1

  max_points = [max_points, types_count ** 2 + tpt].max
  # p current_sushis, max_points
end

puts max_points