#include #include #include #include using namespace std; int main() { int n, d; cin >> n >> d; int dpL = 0; int dpR = -1.01e9; for (int i = 0; i < n; i++) { int l, r; cin >> l >> r; int tmpL = dpL; int tmpR = dpR; dpL = max(tmpL + l, tmpR + l - d); dpR = max(tmpL - d + r, tmpR + r); } cout << max(dpL, dpR) << endl; }