N,K=gets.split.map(&:to_i)
various_max = Hash.new(0) # key: 種類, val: 美味しさ最大値
sushi = Hash.new{|h,k| h[k] = []}
N.times do
  t,d = gets.split.map(&:to_i)
  various_max[t] = d if d > various_max[t]
  sushi[t] << d
end

# 美味しさ, 種類増えるか
list = []
sushi.each do |name, taste_list|
  taste_list.each do |taste|
    list << (various_max[name] == taste ? [taste, 1] : [taste, 0])
  end
end
add, nadd = list.partition{|x| x[1] == 1}
add.sort_by!{|x| -x[0]}
nadd.sort_by!{|x| -x[0]}
ans = 0
(1..K).each do |y|
  score = add[0...y].inject(0){|sum, e| sum + e[0]}
  score += nadd[0...(K-y)].inject(0){|sum, e| sum + e[0]}
  score += y**2
  ans = score if score > ans
end

p ans

