#include #include #include #include #include #include #include using namespace std; typedef long long ll; int N, K; ll T[200002]; ll D[200002]; ll dp[200002]; bool ok(ll d){ ll pre = T[0]; for(int i = 1; i <= N; i++){ if(D[i] > d) { if(T[i]-pre < K) return false; else pre = T[i]; } } return true; } set st; int main(){ ios::sync_with_stdio(false); cin.tie(0); cout << setprecision(10) << fixed; cin >> N >> K; ll sum = 0; for(int i = 1; i <= N; i++) { cin >> T[i] >> D[i]; sum += D[i]; } T[0] = -1e12, T[N+1] = 1e12; if(ok(0)){ cout << 0 << endl; cout << 0 << endl; return 0; } st.insert(-1e12); st.insert(1e12); ll l = 0, r = 1e9; while(r-l > 1){ ll c = (l+r)/2; if(ok(c)) r = c; else l = c; } ll d = r; for(int i = 1; i <= N; i++){ if(D[i] > d) st.insert(T[i]); } for(int i = 1; i <= N; i++){ auto p = upper_bound(T, T+N+2, T[i]-K); p--; int dif = p-T; if(D[i] > d){ dp[i] = dp[dif]+D[i]; }else{ auto ptl = st.lower_bound(T[i]); ptl--; auto ptr = st.upper_bound(T[i]); if(T[i]-(*ptl) < K || *ptr-T[i] < K) dp[i] = dp[i-1]; else dp[i] = max(dp[i-1], dp[dif]+D[i]); } } cout << d << endl; cout << sum-dp[N] << endl; }