#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define mp make_pair #define pb push_back #define all(x) (x).begin(),(x).end() #define rep(i,n) for(int i=0;i<(n);i++) using namespace std; typedef long long ll; typedef unsigned long long ull; typedef vector vb; typedef vector vi; typedef vector vvb; typedef vector vvi; typedef pair pii; const int INF=1<<29; const double EPS=1e-9; const int dx[]={1,0,-1,0,1,1,-1,-1},dy[]={0,-1,0,1,1,-1,-1,1}; map, double> dp; double dfs(double M, int N) { if (dp.find(mp(M,N)) != dp.end()) { return dp[mp(M,N)]; } if (N == 0) { return dp[mp(M,N)] = M; } else { return dp[mp(M,N)] = 1.0 / 3 * dfs(M * 2, N - 1) + 1.0 / 3 * dfs(M + 1, N - 1) + 1.0 / 3 * dfs(0, N - 1); } } int main() { int N, D; cin >> N >> D; vector> dp(N + 1, vector(2, -INF)); dp[0][0] = 0; for(int i = 1; i <= N; i++) { int a, b; cin >> a >> b; dp[i][0] = max((dp[i - 1][0] + a), (dp[i - 1][1] + a - D)); dp[i][1] = max((dp[i - 1][0] + b - D), (dp[i - 1][1] + b)); } cout << max(dp[N][0], dp[N][1]) << endl; return 0; }