// header {{{ #include using namespace std; // {U}{INT,LONG,LLONG}_{MAX,MIN} #define INF INT_MAX/3 #define LLINF LLONG_MAX/3 #define MOD (1000000007LL) #define MODA(a, b) a=((a)+(b))%MOD #define MODP(a, b) a=((a)*(b))%MOD #define inc(i, l, r) for(long long i=(l);i<(r);i++) #define dec(i, l, r) for(long long i=(r)-1;i>=(l);i--) #define pb push_back #define se second #define fi first #define mset(a, b) memset(a, b, sizeof(a)) using LL = long long; using G = vector>; int di[] = {0, -1, 0, 1}; int dj[] = {1, 0, -1, 0}; // }}} struct BIT { vector bit; int n; BIT(int n) { bit.resize(n+1, 0); this->n = n; } LL query(int i) { LL mmax = 0; while(i > 0){ mmax = max(mmax, bit[i]); // i & -i はiの最後の1のビット i -= i & -i; } return mmax; } void set(int i, LL x) { while(i <= n){ bit[i] = max(bit[i], x); i += i & -i; } } }; int main() { std::ios::sync_with_stdio(false); int n, k;cin >> n >> k; vector t(n), d(n), v; priority_queue> pq; LL sum = 0; inc(i, 0, n){ cin >> t[i] >> d[i]; sum += d[i]; pq.push({d[i], i}); } v = d; int ansmax = 0; while(!pq.empty()){ int i = pq.top().second; pq.pop(); if(!d[i]) continue; bool flag = false; inc(j, i+1, n){ if(d[j] == 0 || t[j]-t[i] >= k) break; if(d[j] == d[i]) flag = true; } dec(j, 0, i){ if(d[j] == 0 || t[i]-t[j] >= k) break; if(d[j] == d[i]) flag = true; } if(flag) break; //cout << d[i] << endl; sum -= d[i]; d[i] = 0; v[i] = 0; inc(j, i+1, n){ if(d[j] == 0 || t[j]-t[i] >= k) break; //ansmax = max(ansmax, d[j]); d[j] = 0; } dec(j, 0, i){ if(d[j] == 0 || t[i]-t[j] >= k) break; //ansmax = max(ansmax, d[j]); d[j] = 0; } } BIT bit(n); LL ans = 0; inc(i, 0, n){ int ng = i; int ok = n; while(ok-ng>1){ int m = (ng+ok)/2; if(t[m]-t[i] >= k){ ok = m; }else{ ng = m; } } //cout << i << " " << d[i] << " " << ok << endl; if(ok == n){ ans = max(ans, bit.query(i)+d[i]); }else{ bit.set(ok, bit.query(i)+d[i]); } } //cout << ans << endl; cout << *max_element(v.begin(), v.end()) << endl; cout << sum-ans << endl; return 0; }