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
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
ll n,c;
ll a[500500],w[500500],f[500500];
map<ll,ll> dp;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>c;
	for(int i=1;i<=n;i++)
		cin>>a[i]>>w[i];
	ll mx=0;
	for(int i=1;i<=n;i++)
	{
		int j=i;
		while(j<n&&a[j+1]==a[i]) j++;
		for(int x=i;x<=j;x++)
			f[x]=max(mx-c+a[x],dp[w[x]]+a[x]);
		for(int x=i;x<=j;x++)
		{
			dp[w[x]]=max(dp[w[x]],f[x]);
			mx=max(mx,f[x]);
		}
		i=j;
	}
	cout<<mx<<'\n';
	return 0;
}