N,K=gets.split.map(&:to_i)
S=N.times.map{gets.split.map(&:to_i)}

num=Hash.new{|h,k|h[k]=0}
ans=x=sum=k=0
stack=[]
S.sort{|l,r|r[1]<=>l[1]}.each do |t, d|
  if k<K
    k += 1
    stack << [t,d]
    sum += d
    sum -= x*x
    x += 1 if num[t].zero?
    sum += x*x
    num[t] += 1
  else
    if x != k
      until stack.empty?
        low = stack.pop
        next if num[low[0]] == 1
        if low[0] == t
          stack << low
          break
        end
        num[low[0]] -= 1
        sum -= low[1]
        stack << [t,d]
        sum += d
        sum -= x*x
        x += 1 if num[t].zero?
        sum += x*x
        num[t] += 1
        break
      end
    end
  end
  ans = [ans, sum].max
end
p ans