using System; using System.Linq; using System.Collections.Generic; namespace yukicoder { class Programs { static void Main(string[] args) { var line1 = Console.ReadLine().Split(' ').Select(c => int.Parse(c)).ToArray(); int N = line1[0]; int D = line1[1]; int[,] pay = new int[N,2]; for(int i = 0;i < N;i++) { var line2 = Console.ReadLine().Split(' ').Select(c => int.Parse(c)).ToArray(); pay[i,0] = line2[0];//東京 pay[i,1] = line2[1];//京都 } int[,] dp = new int[N,2]; dp[0,0] = pay[0,0]; dp[0,1] = pay[0,1] - D; for(int i = 1;i < N;i++)//i日目dp { for(int p = 0;p < 2;p++)//今日の東京、京都更新 { int maxPay = int.MinValue; for(int q = 0;q < 2;q++) { int todayPay = pay[i,p] + dp[i - 1,q]; if(p != q)todayPay -= D; if(maxPay <= todayPay)maxPay = todayPay; } dp[i,p] = maxPay; } } Console.WriteLine(Math.Max(dp[N-1,0],dp[N-1,1])); } } }