#include using namespace std; #define rep(i,n) for(int i = 0; i < (n);i++) #define sz(x) int(x.size()) typedef long long ll; typedef pair P; int main(){ int n; ll D; cin >> n >> D; vector t(n), k(n); rep(i,n) cin >> t[i] >> k[i]; vector> dp(n+1,vector(2,0)); rep(i,n) { dp[i+1][0] = max(dp[i+1][0], max(dp[i][0] + t[i], dp[i][1] + t[i] - D)); if (i > 0) dp[i+1][1] = max(dp[i+1][1], max(dp[i][1] + k[i], dp[i][0] + k[i] - D)); else dp[i+1][1] = dp[i][1] + k[i] - D; } cout << max(dp[n][0], dp[n][1]) << endl; return 0; }