def parse_input(): try: N, D = map(int, input().split()) if N <= 0 or D < 0: raise ValueError("N must be greater than 0 and D must be non-negative.") T, K = [], [] for _ in range(N): t, k = map(int, input().split()) if t < 0 or k < 0: raise ValueError("Job incomes must be non-negative.") T.append(t) K.append(k) except ValueError as e: print(f"Invalid input: {e}") exit(1) return N, D, T, K def calculate_max_income(N, D, T, K): dp_tokyo = [0] * (N + 1) # Dynamic programming table for staying in Tokyo dp_kyoto = [0] * (N + 1) # Dynamic programming table for staying in Kyoto # Initialization: A's home is in Tokyo dp_tokyo[0] = 0 dp_kyoto[0] = -D # Subtract the cost of moving to Kyoto on the first day # Populate the DP tables for i in range(1, N + 1): dp_tokyo[i] = max(dp_tokyo[i - 1] + T[i - 1], dp_kyoto[i - 1] + K[i - 1] - D) dp_kyoto[i] = max(dp_kyoto[i - 1] + K[i - 1], dp_tokyo[i - 1] + T[i - 1] - D) return max(dp_tokyo[N], dp_kyoto[N]) # Main if __name__ == "__main__": N, D, T, K = parse_input() max_income = calculate_max_income(N, D, T, K) print(max_income)