import strutils, sequtils, algorithm, future, math type int = int64 when isMainModule: var input = stdin.readline.split.map(parseint) (n, d) = (input[0], input[1]) dp = newSeqWith(n+1, newSeq[int](2)) dp[0][1] = -d for i in 1..n: var input = stdin.readline.split.map(parseint) (a, b) = (input[0], input[1]) dp[i][0] = max(dp[i-1][0] + a, dp[i-1][1] + b - d) dp[i][1] = max(dp[i-1][0] + a - d, dp[i-1][1] + b) echo max(dp[n][0], dp[n][1])