n,k = gets.split.map(&:to_i)
s = []
n.times do
  s << gets.split.map(&:to_i)
end

#上からk個
slc = []
cnt = [0]*(n+1)
s.sort_by!{|a| a[1]}.reverse!
k.times do |i|
  slc << s[i]
  cnt[s[i][0]] += 1
end

kind = 0
1.upto(n) do |i|
  kind += 1 if cnt[i] > 0
end
slc.sort_by!{|a| [a[0],a[1]]}.reverse!


ext = []
prev = 0
sum = 0
slc.each do |a|
  ext << a[1] if prev == a[0]
  sum += a[1]
  prev = a[0]
end
ans = sum + kind ** 2
ext.sort!

k.upto(n-1) do |i|
  if ext.empty?
    break
  end
  if cnt[s[i][0]] == 0
    newVal = sum - ext[0] + s[i][1] + (kind+1)**2
    if ans <= newVal
      ans = newVal
      sum = sum - ext[0] + s[i][1]
      ext.shift
      kind += 1
      cnt[s[i][0]] += 1
    end
  end
end
p ans
