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]); } 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; } preTime = T[i]; } } right = middle; } max = right; System.out.println(max); // SegTree seg = new SegTree(T[N - 1] + 1); SegTree seg = new SegTree(N); int preTime = -K; for (int i = 0; i < N; ++i) { if (D[i] <= max) { long opt = Math.max(seg.query(binarySearchUpper(T, preTime + K), binarySearchLower(T, T[i] - K) + 1), seg.query(binarySearchUpper(T, preTime), binarySearchUpper(T, preTime) + 1)); seg.setVal(i, opt + D[i]); } else { seg.setVal(i, seg.query(binarySearchUpper(T, preTime + K), binarySearchLower(T, T[i] - K) + 1)); preTime = T[i]; } } long sum = 0; for (int i = 0; i < N; ++i) { if (D[i] <= max) sum += D[i]; } System.out.println(sum - Math.max(seg.query(binarySearchUpper(T, preTime), binarySearchLower(T, preTime) + 1), seg.query(binarySearchUpper(T, preTime + K), N))); } static int binarySearchLower(int[] a, int key) { int left = -1; int right = a.length; while (right - left > 1) { int middle = (right + left) / 2; if (key >= a[middle]) { left = middle; } else { right = middle; } } return left; } static int binarySearchUpper(int[] a, int key) { int left = -1; int right = a.length; while (right - left > 1) { int middle = (right + left) / 2; if (key <= a[middle]) { right =middle; } else { left = middle; } } return right; } static class SegTree { int n = 1; long[] max; public SegTree(int n_) { while (n < n_) { n *= 2; } max = new long[2 * n - 1]; } void setVal(int k, long val) { k += n - 1; max[k] = val; while (k > 0) { k = (k - 1) / 2; max[k] = Math.max(max[2 * k + 1], max[2 * k + 2]); } } long query(int a, int b) { return query(a, b, 0, n, 0); } long query(int a, int b, int l, int r, int k) { if (a >= r || b <= l) { return 0; } else if (a <= l && r <= b) { return max[k]; } else { long vl = query(a, b, l, (l + r) / 2, 2 * k + 1); long vr = query(a, b, (l + r) / 2, r, 2 * k + 2); return Math.max(vl, vr); } } } static void tr(Object... objects) { System.out.println(Arrays.deepToString(objects)); } }