#include using namespace std; int main() { int64_t N, K; cin >> N >> K; vector T(N), D(N); vector DD; for(int i = 0; i < N; i++) { cin >> T[i] >> D[i]; } int64_t low = 0, high = 1100000000; while(low + 1 < high) { int64_t mid = (low + high) / 2; int64_t last = -K - 1; bool found = true; for(int i = 0; i < N; i++) { if(D[i] >= mid) { if(T[i] - last < K) found = false; last = T[i]; } } if(found) high = mid; else low = mid; } int64_t answer = low; for(int i = 0; i < N; i++) { if(D[i] > answer) DD.push_back(T[i]); } vector dp(N + 1, 0); for(int i = 0; i < N; i++) { if(D[i] <= answer) { dp[i + 1] = max(dp[i + 1], dp[i]); } // それでも選ぶ場合 int next_id = lower_bound(T.begin(), T.end(), T[i] + K) - T.begin(); int idx = lower_bound(DD.begin(), DD.end(), T[i] + 1) - DD.begin(); if(idx >= DD.size() || DD[idx] >= T[i] + K) { dp[next_id] = max(dp[next_id], dp[i] + D[i]); } } cout << answer << endl; cout << std::accumulate(D.begin(), D.end(), 0LL) - dp[N] << endl; }