def max(a,b); a > b ? a : b; end N,D = gets.split.map(&:to_i) TK = N.times.map{gets.split.map(&:to_i) } # TK[i][0] : tokyo, TK[i][1] : kyoto dp = Array.new(N + 1){ Array.new(2, 0) }; dp[0][1] -= (1 << 30) (1 .. N).each do |i| t,k = TK[i - 1] dp[i][0] = max(dp[i - 1][0] + t, dp[i - 1][1] - D + t) dp[i][1] = max(dp[i - 1][0] - D + k, dp[i - 1][1] + k) end puts dp[N].max