using System; public class Hello { public static void Main() { string[] line = Console.ReadLine().Trim().Split(' '); var n = int.Parse(line[0]); var d = int.Parse(line[1]); var tk = new int[n, 2]; for (int i = 0; i < n; i++) { line = Console.ReadLine().Trim().Split(' '); tk[i, 0] = int.Parse(line[0]); tk[i, 1] = int.Parse(line[1]); } var dp = new int[n + 1, 2]; dp[0, 1] = -d; for (int i = 0; i < n; i++) for (int j = 0; j < 2; j++) { dp[i + 1, j] = Math.Max(dp[i + 1, j], dp[i, j] + tk[i, j]); dp[i + 1, 1 - j] = Math.Max(dp[i + 1, 1 - j], dp[i, j] + tk[i, 1 - j] - d); } Console.WriteLine(Math.Max(dp[n, 0], dp[n, 1])); } }