N, K = gets.split.map(&:to_i)
data = []
eaten_kind = Hash.new(false)
N.times do
  t, d = gets.split.map(&:to_i)
  data << { t: t, d: d }
end

data.sort_by! { |d| d[:d] }.reverse!

dup = []

lp = 0
data[0...K].each do |d|
  lp += d[:d]
  dup << d if eaten_kind[d[:t]]
  eaten_kind[d[:t]] = true
end

dup.sort_by! do |d|
  d[:d]
end

point = eaten_kind.size ** 2 + lp

data[K..-1].each do |d|
  next if eaten_kind[d[:t]]

  os = dup.shift
  break unless os

  lp = lp - os[:d] + d[:d]

  eaten_kind[d[:t]] = true

  point = [point, eaten_kind.size ** 2 + lp].max
end

p point
