require 'pp'
require 'set'
N,K = readline.chomp.split.map(&:to_i)

all_sushi = N.times.each.map {|i| readline.chomp.split.map(&:to_i) }.sort_by {|t,d| -d}
#pp all_sushi

selected = (0...K).to_a.reverse

tmax = all_sushi.map {|t,d| t}.uniq.size
#pp tmax

selected_types = Hash.new { 0 }
selected.each do |i|
  t = all_sushi[i][0]
  selected_types[t] += 1
end
#pp selected, selected_types

dscore = selected.map {|i| all_sushi[i][1] }.inject(:+)
tscore = selected_types.size * selected_types.size

max_score = dscore + tscore

not_selected_top_idx = K
#pp max_score
while selected_types.size < [K,tmax].min
  rmv_idx = selected.index do |i|
    t = all_sushi[i][0]
    selected_types[t] > 1
  end

  rmv = selected[rmv_idx]
  removed_type = all_sushi[rmv][0]
  selected_types[removed_type] -= 1
  dscore -= all_sushi[rmv][1]

  aidx = (not_selected_top_idx...N).find do |i|
    t = all_sushi[i][0]
    selected_types[t] == 0
  end
  #pp "aidx : #{aidx}"
  not_selected_top_idx = aidx + 1
  selected[rmv_idx] = aidx
  selected_types[ all_sushi[aidx][0] ] += 1
  dscore += all_sushi[aidx][1]
  tscore = selected_types.size * selected_types.size

  if dscore + tscore > max_score
    max_score = dscore + tscore
  end
  #pp "dscore: #{dscore}, tscore : #{tscore}, selected_types : #{selected_types.inspect}"
end

puts max_score

# pp score( all_sushi[0..(K-1)] )

