n, k = gets.chomp.split(' ').map(&:to_i)
t, d = [], []
n.times { |i| t[i], d[i] = gets.chomp.split(' ').map(&:to_i) }

res = []
t.uniq.size.times do |i|
  tmp = []
  sum = 0
  t.uniq.each do |t_val|
    max = [0, 0]
    d.map.with_index { |d, index| d = t[index] == t_val ? d : 0 }.each_with_index do |d_val, index2|
      max = [d_val, index2] if d_val > max[0]
    end
    tmp << max
  end
  used = []
  tmp.sort_by { |a,b| a }.reverse.each_with_index do |tm, index|
    break if index > i
    sum += tm[0]
    used << tm[1]
  end
  d.map.with_index { |d, index| d = used.include?(index) ? 0 : d }.sort.reverse.each_with_index do |d_val, index|
    break if index >= k - (i + 1)
    sum += d_val
  end

  sum += (i + 1) * (i + 1)
  res << sum
end

puts res.max