n,e = gets.split.map(&:to_i)
sushi = Array.new(n).map{Array.new(2, 0)}
for i in 0..n-1
  sushi[i] = gets.split.map(&:to_i)
end
eat1 = Array.new(n+1,0)
eat2 = []
base = 0
bonus = 0
ans = 0
sushi.sort_by! {|s| s[1]}
for i in 1..n
  if i <= e
    if eat1[sushi[-i][0]] == 0
      eat1[sushi[-i][0]] = 1
      base += sushi[-i][1]
      bonus += 1
    else
      eat2.push(sushi[-i][1])
      base += sushi[-i][1]
    end
    if ans < base+bonus**2
      ans = base+bonus**2
    end
  else
    if eat2.length > 0 && eat1[sushi[-i][0]] == 0
      base -= eat2[-1] - sushi[-i][1]
      bonus += 1
      eat2.delete_at(-1)
      eat1[sushi[-i][0]] = 1
      if ans < base+bonus**2
        ans = base+bonus**2
      end
    end
  end
end
puts ans
