package yukicoder; import java.util.*; public class Q448 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int K = sc.nextInt(); int[] T = new int[N]; int[] D = new int[N]; int max = -1; for (int i = 0; i < N; ++i) { T[i] = sc.nextInt(); D[i] = sc.nextInt(); max = Math.max(max, D[i]); } { assert 1 <= N && N <= 2 * 1_000_00; assert 1 <= K && K <= 1_000_000_000; for (int i = 0; i < N; ++i) { assert 1 <= T[i] && T[i] <= 1_000_000_000; assert 1 <= D[i] && D[i] <= 1_000_000_000; if (i + 1 < N) assert T[i] < T[i + 1]; } } int left = -1; int right = max; outer: while (right - left > 1) { int middle = (left + right) / 2; int preTime = -K; for (int i = 0; i < N; ++i) { if (D[i] > middle) { if (T[i] - preTime < K) { left = middle; continue outer; } else preTime = T[i]; } } right = middle; } max = right; System.out.println(max); int[] count = new int[N]; int preTime = -K; for (int i = 0; i < N; ++i) { if (D[i] > max) { preTime = T[i]; } else { if (preTime + K <= T[i]) { ++count[i]; } } } preTime = T[N - 1] + K; for (int i = N - 1; i >= 0; --i) { if (D[i] > max) { preTime = T[i]; } else { if (preTime >= T[i] + K) { ++count[i]; } } } int[] pendingIndex = new int[N]; int pos = 0; for (int i = 0; i < N; ++i) { if (count[i] == 2) { pendingIndex[pos++] = i; } } pendingIndex = Arrays.copyOf(pendingIndex, pos); long[] dp = new long[pos]; int pre = -1; for (int i = 0; i < pos; ++i) { while (pre + 1 < pos && T[pendingIndex[i]] >= T[pendingIndex[pre + 1]] + K) { ++pre; } if (i > 0) dp[i] = dp[i - 1]; dp[i] = Math.max(dp[i], D[pendingIndex[i]] + (pre >= 0 ? dp[pre] : 0)); } long sum = 0; for (int i = 0; i < N; ++i) { if (D[i] <= max) { sum += D[i]; } } System.out.println(sum - (pos > 0 ? dp[pos - 1] : 0)); sc.close(); } static void tr(Object... objects) { System.out.println(Arrays.deepToString(objects)); } }