// tested by Hightail - https://github.com/dj3500/hightail import std.stdio, std.string, std.conv, std.algorithm; import std.range, std.array, std.math, std.typecons, std.container, core.bitop; immutable inf = 10^^9 + 7; void main() { int n, d; long[] t, k; scan(n, d); t = new long[](n); k = new long[](n); foreach (i ; 0 .. n) { scan(t[i], k[i]); } auto dp = new long[][](n + 1, 2); dp[0][1] = -inf; foreach (i ; 0 .. n) { foreach (j ; 0 .. 2) { if (j == 0) { dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + t[i]); dp[i + 1][j ^ 1] = max(dp[i + 1][j ^ 1], dp[i][j] - d + k[i]); } else { dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + k[i]); dp[i + 1][j ^ 1] = max(dp[i + 1][j ^ 1], dp[i][j] - d + t[i]); } } } writeln(max(dp[n][0], dp[n][1])); } void scan(T...)(ref T args) { string[] line = readln.split; foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }