#!/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=a[i][0] elsif a[i][0]-tailma tail=a[i][0] elsif tail-a[i][0]ma a[i][1]=0 else s+=a[i][1] a[i][1]=0 if ng[i] end } dp=[0] x=0 n.times{|i| x+=1 while a[i][0]-a[x][0]>=k dp[i+1]=[dp[i],dp[x]+a[i][1]].max } p s-dp[n]