#include #include #include #include #include #include #include #include #include static const int MOD = 1000000007; using ll = int64_t; using u32 = uint32_t; using namespace std; template constexpr T INF = ::numeric_limits::max()/32*15+208; template vector make_v(U size, const T& init){ return vector(static_cast(size), init); } template auto make_v(U size, Ts... rest) { return vector(static_cast(size), make_v(rest...)); } template void chmin(T &a, const T &b){ a = (a < b ? a : b); } template void chmax(T &a, const T &b){ a = (a > b ? a : b); } int main() { int n, d; cin >> n >> d; auto dp = make_v(n+1, 2, -INF); auto x = make_v(n, 2, 0); for (int i = 0; i < n; ++i) { for (int j = 0; j < 2; ++j) { scanf("%d", &x[i][j]); } } dp[0][0] = 0; dp[0][1] = -d; for (int i = 0; i < n; ++i) { for (int j = 0; j < 2; ++j) { for (int k = 0; k < 2; ++k) { chmax(dp[i+1][j], dp[i][k] - (j != k) * d+ x[i][j]); } } } cout << max(dp[n][0], dp[n][1]); return 0; }