#include using namespace std; int main() { int N, D; cin >> N >> D; long long DP[N + 1][2] = {}; for (size_t i = 1; i <= N; i++) { long long T, K; cin >> T >> K; if (i == 1) { DP[i][0] = T; DP[i][1] = K - D; continue; } DP[i][0] = max(DP[i - 1][0] + T, DP[i - 1][1] + K - D); DP[i][1] = max(DP[i - 1][0] + T - D, DP[i - 1][1] + K); } cout << max(DP[N][0], DP[N][1]) << endl; }