#include using namespace std; using ll = long long; using P = pair; const ll inf = numeric_limits::max()/3; int n; ll k; vector< P > tasks; ll tree[1000000]; void init(int k,int l,int r){ if(r - l == 1){ tree[k] = -inf; // init leaf }else{ int chl = k*2+1; int chr = k*2+2; tree[k] = -inf; // init internal-node init(chl, l, (l+r)/2); init(chr, (l+r)/2, r); } } void update(int i,int x,int k,int l, int r){ if(l <= i && i < r){ if(r - l == 1){ tree[k] = x; }else{ int chl = k*2+1; int chr = k*2+2; update(i,x,chl,l,(l+r)/2); update(i,x,chr,(l+r)/2,r); tree[k] = max(tree[chl],tree[chr]); } } } ll find(int a,int b,int k,int l,int r){ // not cross if(r <= a || b <= l)return -inf; // [a,b) contain [l,r) else if(a <= l && r <= b)return tree[k]; // otherwise else{ int chl = k*2+1; int chr = k*2+2; ll vl = find(a,b,chl,l,(l+r)/2); ll vr = find(a,b,chr,(l+r)/2,r); return max(vl,vr); } } bool detect(ll m){ ll last = -inf; for(int i = 0;i < n;++i){ if(tasks[i].second > m){ if(tasks[i].first - last < k) return false; last = tasks[i].first; } } return true; } int main(void){ cin >> n >> k; tasks.clear(); // find dmax (by bsearch) ll tot = 0; set< ll > diff_set; vector< ll > diff_vec; diff_set.insert(0LL); for(int i = 0;i < n;++i){ ll t,d; cin >> t >> d; tasks.push_back(P(t,d)); diff_set.insert(d); tot += d; } for(auto d : diff_set){ diff_vec.push_back(d); } int l = -1,r = (int)diff_vec.size()-1; while(r - l > 1){ int mid = (l+r)/2; //cout << diff_vec[mid] << " " << detect(diff_vec[mid]) << endl; if(detect(diff_vec[mid])) r = mid; else l = mid; } ll dmax = diff_vec[r]; // remove unavalilable tasks vector< ll > used; used.push_back(-inf); for(int i = 0;i < n;++i){ if(tasks[i].second > dmax){ used.push_back(tasks[i].first); tot -= tasks[i].second; } } used.push_back(inf); vector< P > tasks2; int j = 1; for(int i = 0;i < n;++i){ while(used[j] < tasks[i].first) ++j; if(used[j-1] + k <= tasks[i].first && tasks[i].first <= used[j] - k){ tasks2.push_back(tasks[i]); } } // find optimal sum int size = (int)tasks2.size(); init(0,0,size+1); vector< ll > tms; tms.push_back(0); update(0,0,0,0,size+1); for(int i = 0;i < size;++i){ int j = (upper_bound(tms.begin(), tms.end(), tasks2[i].first - 1) - tms.begin()); ll calc = find(0,j,0,0,size+1); update(i+1,calc+tasks2[i].second,0,0,size+1); tms.push_back(tasks2[i].first + k); } cout << dmax << endl; cout << tot - find(0,size+1,0,0,size+1) << endl; return 0; }