N, K = gets.split.map(&:to_i)
X = []
Y = []

N.times.map{ gets.split.map(&:to_i) }
  .sort_by(&:last)
  .reverse_each
  .with_index(0)
  .with_object({}) do |((k, v), i), used|
    if i < K && used[k]
      X.push(v)
    elsif !used[k]
      Y.push(v)
      used[k] = 1
      break if used.size == K
    end
  end

sumX = Array.new(X.size + 1, 0)
sumY = Array.new(Y.size + 1, 0)

X.each_with_index {|v,i| sumX[i + 1] = sumX[i] + v }
Y.each_with_index {|v,i| sumY[i + 1] = sumY[i] + v }

puts (K - X.size .. Y.size).map{|k| sumY[k] + sumX[K - k] + k ** 2 }.max