#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; 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(j < (int)used.size()-1 && 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 vector< P > dp; dp.push_back(P(0,0)); for(int i = 0;i < (int)tasks2.size();++i){ int j = (upper_bound(dp.begin(), dp.end(), P(tasks2[i].first, inf)) - dp.begin()) - 1; dp.push_back(P(tasks2[i].first + k, dp[j].second + tasks2[i].second)); /* for(int i = 0;i < (int)dp.size();++i){ printf("(%lld, %lld) ",dp[i].first,dp[i].second); } cout << endl; */ } ll res = 0; for(int i = 0;i < (int)dp.size();++i){ res = max(res,dp[i].second); } cout << dmax << endl; cout << tot - res << endl; /* for(int i = 0;i < (int)tasks2.size();++i){ cout << tasks2[i].first << " " << tasks2[i].second << endl; } */ return 0; }