n, k = gets.split.map(&:to_i)
sushi = n.times.map { gets.split.map(&:to_i) }
sushi.sort!
ans = 0
sum = 0
r = 0
used = Hash.new(0)
(n - 1).downto(0) do |i|
  if i >= (n - 1) - k + 1
    used[sushi[i][0]] += 1
    sum += sushi[i][1]
    if i == (n - 1) - k + 1
      x = used.keys.select { |key| used[key] != 0 }.size
      ans = sum + x * x
      r = (n - 1) - k + 1
    end
  else
    if used[sushi[i][0]] == 0 # 使ってない寿司か？
      used[sushi[i][0]] += 1
      while r < n && used[sushi[r][0]] == 1
        r += 1
      end
      break if r == n
      sum += sushi[i][1] - sushi[r][1]
      used[sushi[r][0]] -= 1
      r += 1
      x = used.keys.select { |key| used[key] != 0 }.size
      ans = [ans, sum + x * x].max
    end
  end
end

p ans
