require 'set'
N,K = gets.split.map(&:to_i)
tds = N.times.map{ gets.split.map(&:to_i) }

# とりあえず価値が最大になるように取ってみると、
# この種類より少ない状態で、最大値になることはないことが分かる
# なのであとは、今まで入っていなかった種類のうち最大のものから順番に置き換えていって、
# その中で、最大だったものを出力するだけ
tds = tds.sort_by{|td| td[1]*-1 }
selected = tds[0, K]
not_selected = tds[K, N]

current_kinds = Set.new(selected.map{|td| td[0] })
current_score = selected.map{|td| td[1] }.inject(:+)
max = current_score + current_kinds.size ** 2

dups_set = Set[]
dups_in_selected = []
selected.each do |td|
  t,d = td
  if dups_set.include? t
    dups_in_selected << td
  else
    dups_set << t
  end
end

if not_selected.nil?
  puts max
  exit
end

not_selected.each do |td|
  t,d = td
  next if current_kinds.include? t
  # remove duplicates, insert new kind
  removed = dups_in_selected.pop
  break if removed.nil?
  current_score -= removed[1]
  current_score += td[1]
  current_kinds << td[0]
  max = [max, current_score + current_kinds.size ** 2].max
end
puts max
