#include using namespace std; const int N = 2e5; template void chmin(T1 &a, T2 b) { if (b < a) a = b; } const int M = 1 << 18; int seg[M * 2]; int maximum(int l, int r) { int res = -2e9; for (l += M, r += M; l < r; l >>= 1, r >>= 1) { if (l & 1) res = max(res, seg[l++]); if (r & 1) res = max(res, seg[--r]); } return res; } int main() { int n, K; cin >> n >> K; vector T(n), D(n); for (int i = 0; i < n; i++) scanf("%d %d", &T[i], &D[i]); static int dp[N + 1]; fill_n(dp, N + 1, INT_MAX); fill_n(seg, M * 2, -2e9); dp[0] = 0; for (int i = 0; i < n; i++) seg[i + M] = D[i]; for (int i = M - 1; i >= 1; i--) seg[i] = max(seg[i * 2], seg[i * 2 + 1]); for (int i = 0; i < n; i++) { int j = lower_bound(T.begin(), T.end(), T[i] + K) - T.begin(); chmin(dp[j], max(dp[i], maximum(i + 1, j))); chmin(dp[i + 1], max(dp[i], D[i])); } vector sum(n + 1); for (int i = 0; i < n; i++) sum[i + 1] = sum[i] + (D[i] <= dp[n] ? D[i] : 2e9); static int64_t dp2[N + 1]; fill_n(dp2, N + 1, 1e18); dp2[0] = 0; for (int i = 0; i < n; i++) { int j = lower_bound(T.begin(), T.end(), T[i] + K) - T.begin(); chmin(dp2[j], dp2[i] + sum[j] - sum[i + 1]); if (D[i] <= dp[n]) chmin(dp2[i + 1], dp2[i] + D[i]); } cout << dp[n] << endl; cout << dp2[n] << endl; }