#include "bits/stdc++.h" using namespace std; #define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i)) #define rep(i,j) FOR(i,0,j) #define each(x,y) for(auto &(x):(y)) #define mp make_pair #define mt make_tuple #define all(x) (x).begin(),(x).end() #define debug(x) cout<<#x<<": "<<(x)< pii; typedef vector vi; typedef vector vll; template> class SparseTableRMQ{ public: SparseTableRMQ(){ } SparseTableRMQ(int *ar, int n): n(n), data(n){ for(int i = 0; i < n;++i) data[i] = ar[i]; table = buildRMQ(); } int queryPos(int l, int r){ int k = lgInt(r - l - 1); int lid = table[l + n*k], rid = table[r + n*k - (1 << k)]; return (cmp(data[lid], data[rid])) ? lid : rid; } int queryValue(int l, int r){ return data[queryPos(l, r)]; } private: int n; vector data, table; Cmp cmp; int lgInt(int v){ static const unsigned b[] = {0x2, 0xC, 0xF0, 0xFF00, 0xFFFF0000}; static const unsigned S[] = {1, 2, 4, 8, 16}; int res = 0; for (int i = 4; i >= 0; i--) { if (v & b[i]) { v >>= S[i]; res |= S[i]; } } return res; } vector buildRMQ() { int logn = 1; for (int k = 1; k < n; k *= 2) ++logn; vector b(n*logn); for(int i = 0; i < n; ++i)b[i] = i; int cur = 0; for(int k = 1; k < n; k *= 2){ copy(b.begin() + cur, b.begin() + cur + n, b.begin() + cur + n); cur += n; for(int i = 0; i < n - k; ++i){ int idl = b[cur + i], idr = b[cur + i + k]; if(cmp(data[idl], data[idr])){ b[cur + i] = idl; } else{ b[cur + i] = idr; } } } return b; } }; typedef SparseTableRMQ> RMinQ; typedef SparseTableRMQ> RMaxQ; int D[200001]; int main(){ ios::sync_with_stdio(false); cin.tie(0); int N, K; const ll INF = LLONG_MAX; while(cin >> N >> K) { vi T(N + 1); rep(i, N) cin >> T[i] >> D[i]; vector> dp(N + 1, mp(INT_MAX, INF)); dp[0] = mp(0, 0); vll sumD(N + 1); rep(i, N) { sumD[i + 1] = sumD[i] + D[i]; } RMaxQ rmq(D, N); T[N] = (int)2e9 + 1; rep(i, N) { smin(dp[i + 1], mp(max(dp[i].first, D[i]), dp[i].second + D[i])); int lb = i, ub = N, mid; while(ub - lb > 1) { mid = (lb + ub) / 2; (T[mid] - T[i] >= K ? ub : lb) = mid; } int x = i + 1 == ub ? 0 : rmq.queryValue(i + 1, ub); ll y = sumD[ub] - sumD[i + 1]; smin(dp[ub], mp(max(dp[i].first, x), dp[i].second + y)); } cout << dp[N].first << endl << dp[N].second << endl; } }