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

sushis.sort!{|a,b| a[1] <=> b[1]}.reverse!

used = Hash.new(false)
fixed = []
sw = []
sushis[0...k].each do |sushi|
  if used[sushi[0]]
    sw << sushi
  else
    fixed << sushi
    used[sushi[0]] = true
  end
end

sw.sort_by!{|s| s[1]}

cands = []
sushis[k..-1].each do |sushi|
  break if cands.size == sw.size
  if !used[sushi[0]]
    cands << sushi
    used[sushi[0]] = true
  end
end
cands.sort_by!{|s| s[1]}.reverse!

fixed_size = fixed.size
fixed_point = fixed.reduce(0){|a,e|a+=e[1]}
ans = fixed_point + fixed_size**2 + sw.reduce(0){|a,e| a+=e[1]}

cands.size.times do |i|
  use_cands = cands[0..i]
  tmp = fixed_point + use_cands.reduce(0){|a,e|a+=e[1]} + sw[i+1..-1].reduce(
0){|a,e|a+=e[1]}
  tmp += (fixed_size + use_cands.size)**2
  ans = [ans, tmp].max
end

puts ans