Node = Struct.new(:t, :d) do
  def <=>(node)
    d <=> node.d
  end
end

class PriorityQueue
  attr_reader :size

  def initialize(order: :asc)
    @size = 0
    @nodes = []
  end

  def empty?
    @size.zero?
  end

  def push(node)
    index = size
    @size += 1

    while index > 0
      parent = (index - 1) / 2

      break if (@nodes[parent] <=> node) <= 0

      @nodes[index] = @nodes[parent]
      index = parent
    end

    @nodes[index] = node
  end

  alias_method :<<, :push

  def pop
    node = @nodes.first
    root = @nodes[size - 1]
    @size -= 1
    index = 0

    while index * 2 + 1 < @size
      left = 2 * index + 1
      right = left + 1

      if right < size && (@nodes[right] <=> @nodes[left]) <= 0
        left = right
      end

      break if (@nodes[left] <=> root) > 0

      @nodes[index] = @nodes[left]
      index = left
    end

    @nodes[index] = root
    node
  end

  def top
    @nodes.first
  end
end

N, K = gets.split.map(&:to_i)

sushi = []

N.times do 
  t, d = gets.split.map(&:to_i)

  sushi << [t, d]
end

sushi.sort_by! { |_t, d| -d }

current_list = sushi.take(K)
sum = 0
kinds = Hash.new(0)
pque = PriorityQueue.new

current_list.each do |t, d|
  sum += d
  kinds[t] += 1
  pque.push(Node.new(t, d))
end

k = kinds.size
ans = sum + k ** 2

sushi[K..-1].each do |t, d|
  next if kinds[t] > 0

  while kinds[pque.top.t] <= 1
    pque.pop
  end

  s = sum - pque.top.d + d
  x = (k + 1) ** 2
  a = s + x

  sum = s
  ans = a if ans < a
  k += 1
  kinds[pque.top.t] -= 1
  kinds[t] += 1
  pque.pop

  break if k == K
  break if pque.empty?
end

puts ans
