1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#pragma GCC optimize ("Ofast")
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define FOR(i, a, b) for (auto i=(a); i<(b); i++)
#define FORD(i, a, b) for (auto i=(a); i>(b); i--)
#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(), (x).end()
#ifdef DEBUG
#include "debug.h"
#else
#define dbg(...) 0
#endif

template <typename T1, typename T2> inline void remax(T1& a, T2 b) { a = std::max(a, (T1)b);	}

const int maxN = 1 << 19;

long long dp[maxN], prop[maxN];

std::vector <int> pattsOnH[maxN];

void solve()
{
	std::fill(dp + 1, dp + maxN, -(1ll << 50));
	long long best = 0;

	int n, c;
	scanf ("%d%d", &n, &c);
	FOR(i, 0, n)
	{
		int h, w;
		scanf ("%d%d", &h, &w);
		pattsOnH[h].push_back(w);
	}

	FOR(h, 1, maxN)
	{
		for (int w : pattsOnH[h])
			prop[w] = std::max(best + h - c, dp[w] + h);
		for (int w : pattsOnH[h])
			remax(best, dp[w] = prop[w]);
	}

	long long res = *std::max_element(dp + 1, dp + maxN) + c;
	remax(res, 0);
	printf("%lld\n", res);
}

int main()
{
	int tc = 1;
//	scanf ("%d", &tc);	
	FOR(cid, 1, tc+1)
		solve();
	return 0;
}