import logging import sys from inspect import currentframe sys.setrecursionlimit(10 ** 6) input = sys.stdin.readline logging.basicConfig(level=logging.DEBUG) def dbg(*args): id2names = {id(v): k for k, v in currentframe().f_back.f_locals.items()} logging.debug( ", ".join(id2names.get(id(arg), "???") + " = " + repr(arg) for arg in args) ) def main(): n, d = map(int, input().split()) t = [0] * n k = [0] * n for i in range(n): t[i], k[i] = map(int, input().split()) # dp = [[0] * (n + 1) for _ in range(2)] dp = [[0] * 2 for _ in range(n + 1)] dp[0][1] = -float("inf") for i in range(n): dp[i + 1][0] = max(dp[i][0] + t[i], dp[i][1] + t[i] - d) dp[i + 1][1] = max(dp[i][1] + k[i], dp[i][0] + k[i] - d) ans = max(dp[n][0], dp[n][1]) dbg(dp) print(ans) if __name__ == "__main__": main()