n,k=gets.split.map &:to_i
a=$<.map{|e|e.split.map &:to_i}.sort_by{|e|-e[1]}
s=r=f=0
c=[0]*-~n
f=0
l=[]
i=k-1
a.each{|m,v|
	if l.size>=k
        next if c[m]>0
		i=i.downto(0).find{|j|c[l[j][0]]>1}
		break if !i
		s-=l[i][1]
		c[l[i][0]]-=1
		l.delete_at(i)
	end
	s+=v
    f+=1 if c[m]==0
	c[m]+=1
	l<< [m,v]
	r=[r,s+f**2].max
}
p r