def inpf() a=gets.chomp.split(" ").map(&:to_f)end

def inps() a=gets.chomp.split(" ")end

def copy(a) Marshal.load(Marshal.dump(a)) end

def kaijo(n,r = 10**9+7)(n < 2)? 1 : (2..n).inject{|memo,u|memo=(memo*u)%r} end

def na(n,d=0) Array.new(n,d)end

def na2(n,m,d=0) Array.new(n){Array.new(m,d)}end

def na3(n,m,l,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end

def inp() a=gets.chomp.split(" ").map(&:to_i)end

def r_up(a, b) (a+b-1)/b end

def sum(a) a.inject(:+) end

n,k = inp
x = []
n.times{x << inp}
t = []
h = {}
x.sort_by!{|u|-1*u[1]}
ft = na(n,false)
x.each.with_index do |a, i|
  if(h[a[0]] == nil)
    h[a[0]] = i
    ft[i]    = true
    t.push(a[1])
  end
end
t = t[0...([t.size,k].min)]

ans = t.size ** 2 + sum(t)
ind = 0
g = []
  g = 0
now = ans
while(ind < n)
  break if(t.size == 0)
  if(ft[ind])
    ind += 1
    next
  end
  if(g + t.size < k)
    now += x[ind][1]
    ans = [now,ans].max
    ind += 1
    g += 1
    next
  end
  now -= t.size**2
  now -= t.pop
  g += 1
  now += t.size**2
  now += x[ind][1]
  ans = [now,ans].max
  ind += 1
end
p ans