#include using namespace std; #define int long long typedef long long int64; vector< int64 > vv; int64 N, K, T[200000], D[200000]; signed main() { cin >> N >> K; priority_queue< pair< int, int >, vector< pair< int, int > >, greater< pair< int, int > > > que; for(int i = 0; i < N; i++) { cin >> T[i] >> D[i]; que.emplace(T[i], i); } int low = 0, high = 1 << 30; while(high - low > 0) { int mid = (low + high) >> 1; int64 prev = -1145141919810LL; bool flag = true; for(int i = 0; i < N; i++) { if(mid < D[i]) { if(T[i] - prev < K) flag = false; prev = T[i]; } } if(flag) high = mid; else low = mid + 1; } int64 all = 0; vv.push_back(-1145141919810LL); for(int i = 0; i < N; i++) { if(low < D[i]) { vv.push_back(T[i]); all += D[i]; } } vv.push_back(1145141919810LL); for(int i = 1; i < vv.size(); i++) { vector< int > arrays; while(!que.empty() && que.top().first < vv[i - 1] + K) { que.pop(); } while(!que.empty() && que.top().first <= vv[i] - K) { arrays.push_back(que.top().second); que.pop(); } vector< int64 > dp(arrays.size() + 1, 0); for(int j = 0; j < arrays.size(); j++) { if(j > 0) dp[j] = max(dp[j], dp[j - 1]); int curr = lower_bound(arrays.begin(), arrays.end(), T[arrays[j]] + K) - arrays.begin(); dp[curr] = max(dp[curr], dp[j] + D[arrays[j]]); } all += *max_element(begin(dp), end(dp)); } cout << low << endl; cout << accumulate(D, D + N, 0LL) - all << endl; }