def score(base,type)
  base + type.size**2
end
io = STDIN
td = []
n,k=io.gets.split.map(&:to_i)
td=(1..n).map{io.gets.split.map(&:to_i)}.sort_by{|t,d|-d}
base=0
exchange_candidate=[]
type={}
(0...k).each do |i|
  t,d=td[i]
  base+=d
  if type[t]
    exchange_candidate << d
  end
  type[t]=true
end
ans=score(base,type)
(k...n).each do |i|
  t,d=td[i]
  next if type[t]
  break if exchange_candidate.empty?
  remove=exchange_candidate.pop
  type[t]=true
  base+=(d-remove)
  ans = [ans,score(base,type)].max
end
puts ans
