require 'set'

n, k = gets.strip.split.map(&:to_i)
td = readlines.map {|line| line.strip.split.map(&:to_i) }.sort_by(&:last).reverse

selected = td.take(k)
unselected = td[k...n]

selected_kind = Set.new
swapable = []

selected.each do|t, d|
  unless selected_kind.include?(t)
    selected_kind.add(t)
  else
    swapable.push([t,d])
  end
end

score = selected.map(&:last).inject(:+)
bonus = selected_kind.size**2
result = score + bonus

unselected.each do|t, d|
  next if selected_kind.include?(t)

  swap = swapable.pop
  break if swap.nil?

  selected_kind.add(t)
#p [score, bonus, swap, t, d]
  score = score - swap.last + d

  bonus = selected_kind.size**2
  current_result = score + bonus
  result = current_result if current_result > result
end

#p td
p result
