n, k = gets.strip.split.map(&:to_i)
td = readlines.map {|line| line.strip.split.map(&:to_i) }

grouped_td = td.group_by(&:first)

sushi_max = grouped_td.map {|t, arr| [t, arr.map(&:last)] }.map {|t, arr| [t, arr.max] }.sort_by(&:last).reverse

sushi_max.each do |t|
  td.delete_at(td.index(t))
end

top_sushis = sushi_max.map {|t, d| [t>0 ? 1: 0, d] }.sort_by(&:last).reverse
selected_sushis = top_sushis.take(k)

unselected_top_sushis = top_sushis[k..-1].to_a
unselected_sushis = (unselected_top_sushis + td.map {|t, d| [0 ,d] }.map {|t, d| [t>0 ? 1: 0, d] }).sort_by(&:last).reverse

all_sushis = selected_sushis + unselected_sushis

#p sushi_max
#p all_sushis
#p selected_sushis
#p unselected_sushis

def points(sushis)
  sushis.inject([0,0]) {|(t, d), (mt, md)| [mt+t, md+d] }
end

t, d = points(selected_sushis)
current_result = t**2 + d

if n==k
  p current_result
  exit
end

unselected_sushis.each do |t,d|
  selected_sushis.pop
  selected_sushis.push([t,d])
  tt, dd = points(selected_sushis)
  tmp = tt**2 + dd
  current_result = tmp if current_result < tmp
end

p current_result