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
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;


LL w[500009];
LL w2[500009];
LL res[500009];

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	LL n, c;
	cin>>n>>c;
	
	LL p = 0;
	for(int i=0; i<n; i++){
		LL a, b;
		cin>>a>>b;
		while(a > p+1){
			res[p+1] = res[p];
			p++;
		}
		if(w2[b] != a){
			w2[b] = a;
			w[b] = max(w[b] + a, res[a-1] + a - c);
			res[a] = max(res[a], max(w[b], res[a-1]));
			p=a;
		}
	}
	cout<<res[p]<<"\n";
	return 0;
}