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

N.times
  .map{ gets.split.map(&:to_i) }
  .sort_by(&:last)
  .reverse
  .each_with_index.with_object(Hash.new(0)) do |((k, v), i), used|
    if used[k] > 0 && i < K
      X.push(v)
    elsif used[k] == 0
      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 do |v,i|
  sumX[i + 1] = sumX[i] + v
end
Y.each_with_index do |v,i|
  sumY[i + 1] = sumY[i] + v
end

points = (1 .. K).map do |k|
    K - k < sumX.size && k < sumY.size ? sumY[k] + sumX[K - k] + k ** 2 : nil
  end

puts points.compact.max
