#!/usr/bin/ruby n,k=gets.split.map(&:to_i) a=$<.map{|e|e.split.map(&:to_i)} p ma=(0..10**9).bsearch{|e| tail=-k !!a.each{|t,d| if d>e if t-tailma tail=t[i] elsif tail-t[i]ma tail=t[i] elsif t[i]-tailma d[i]=0 else s+=d[i] d[i]=0 if ng[i] end j+=1 while t[i]-t[j]>=k m[i+1]=[m[i],m[j]+d[i]].max } p s-m[n]