gets A=$<.map{_1.split.map &:to_i}.sort_by{-_2} d=0,*[1e12]*M=$.*2/3 A.map{|a,b|(i=M).times{d[i]=[d[i],d[i-=1]+a+b*i].min}} p d[M]