N,K = gets.split.map(&:to_i)

greedy = (0..N-1).map{gets.split.map(&:to_i)}.sort_by{|e| -e[1]}

kiso = 0
set = []
neta = Hash.new{|h,k| h[k] = 0}

K.times do |i|
  kiso += greedy[i][1]
  set << greedy[i]
  neta[greedy[i][0]] += 1
end

deletable = -1

syurui = neta.count{|k,v| v > 0}
ans = kiso + syurui*syurui

K.upto(N-1) do |i|
  t, d = greedy[i]
  next if neta[t] > 0

  (set.length-1).downto(0) do |j|
    if neta[set[j][0]] > 1
      deletable = j
      break
    else
      if j==0
        deletable = -1
      else
        next
      end
    end
  end

  break if deletable == -1

  kiso += d - set[deletable][1]
  syurui += 1
  tmp = kiso + syurui**2

  set.delete_at(deletable)
  set << [t,d]

  ans = tmp if tmp > ans
end

puts ans
