import std.conv, std.functional, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.container, std.math, std.numeric, std.range, std.regex, std.typecons; import core.bitop; class EOFException : Throwable { this() { super("EOF"); } } string[] tokens; string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; } int readInt() { return readToken.to!int; } long readLong() { return readToken.to!long; } real readReal() { return readToken.to!real; } bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } } bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } } int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; } int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); } int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); } enum INF = 10L^^18; int N; long K; long[] T, D; long solve(long dLim) { debug { writeln("solve ", dLim); } int[] xs; xs ~= 0; foreach (i; 1 .. N + 1) { if (D[i] > dLim) { xs ~= i; } } xs ~= N + 1; const len = cast(int)(xs.length) - 1; debug { writeln(" xs = ", xs); } long ans = D.sum; auto dp = new long[N + 2]; foreach (j; 0 .. len) { if (T[xs[j + 1]] - T[xs[j]] < K) { return INF; } dp[xs[j]] = 0; long dpMax = -1; for (int l = xs[j], r = xs[j] + 1; r <= xs[j + 1]; ++r) { for (; T[r] - T[l] >= K; ++l) { chmax(dpMax, dp[l]); } dp[r] = (dpMax >= 0) ? (dpMax + D[r]) : -1; } debug { writefln(" dp[%s, %s] = %s", xs[j], xs[j + 1], dp[xs[j] .. xs[j + 1] + 1]); } assert(dp[xs[j + 1]] >= 0); ans -= dp[xs[j + 1]]; } return ans; } void main() { try { for (; ; ) { N = readInt(); K = readLong(); T = new long[N + 2]; D = new long[N + 2]; foreach (i; 1 .. N + 1) { T[i] = readLong(); D[i] = readLong(); } T[0] = T[1] - K; T[N + 1] = T[N] + K; D[0] = D[N + 1] = 0; auto ds = D.dup; ds = ds.sort.uniq.array; int lo = -1, hi = cast(int)(ds.length) - 1; for (; lo + 1 < hi; ) { const mid = (lo + hi) / 2; (solve(ds[mid]) < INF) ? (hi = mid) : (lo = mid); } const ans = solve(ds[hi]); writeln(ds[hi]); writeln(ans); } } catch (EOFException e) { } }