import std.algorithm, std.conv, std.range, std.stdio, std.string; import std.container; // SList, DList, BinaryHeap import std.typecons; // Tuple, Nullable, BigFlags import std.math; // math functions import std.numeric; // gcd, fft import std.bigint; // BigInt import std.random; // random import std.bitmanip; // BitArray import core.bitop; // bit operation import std.regex; // RegEx import std.uni; // unicode void main() { auto rd = readln.split, n = rd[0].to!size_t, d = rd[1].to!int; auto t = new int[](n), k = new int[](n); foreach (i; 0..n) { auto rd2 = readln.split.to!(int[]); t[i] = rd2[0]; k[i] = rd2[1]; } auto dp1 = new int[](n), dp2 = new int[](n); dp1[0] = t[0]; dp2[0] = k[0] - d; foreach (i; 1..n) { dp1[i] = max(dp1[i-1]+t[i], dp2[i-1]+t[i]-d); dp2[i] = max(dp1[i-1]+k[i]-d, dp2[i-1]+k[i]); } writeln(max(dp1[$-1], dp2[$-1])); }