#include #define rep(i,n,m) for(int i = (n); i <(m); i++) #define rrep(i,n,m) for(int i = (n) - 1; i >=(m); i--) #define chmax(a, b) a = max((a), (b)) using namespace std; using ll = long long; const int INF = 1000000000; int main() { int n, d; cin >> n >> d; vector tokyo(n), kyoto(n); rep(i, 0, n) cin >> tokyo[i] >> kyoto[i]; vector> dp(n+1, vector(2, -INF)); dp[0][0] = 0; // 0 -> tokyo , 1 -> kyoto rep(i, 0, n) { // from tokyo chmax(dp[i+1][0], dp[i][0]+tokyo[i]); chmax(dp[i+1][1], dp[i][0]+kyoto[i]-d); // from kyoto chmax(dp[i+1][0], dp[i][1]+tokyo[i]-d); chmax(dp[i+1][1], dp[i][1]+kyoto[i]); } cout << max(dp[n][0], dp[n][1]) << endl; return 0; }