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
#include<bits/stdc++.h>
using namespace std;
const int N = 5e5+3;
long long n,c,res = 0;
long long MAX_DP[N],DP[N];
unordered_set<int>M[N];
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    cin >> n >> c;
    vector<pair<int,int>>klo;
    for(int i = 0;i < n;i++){
        int h,w;
        cin >> h >> w;
        M[h].insert(w);
    }
    for(int i = (int)5e5;i >= 1;i--){
        for(auto w: M[i]){
            DP[w] = max({DP[w] + i,MAX_DP[i+1] + i - c,DP[w]});
            MAX_DP[i] = max(MAX_DP[i],DP[w]);
            res = max({res,DP[w],MAX_DP[i]});
        }
        MAX_DP[i] = max(MAX_DP[i+1],MAX_DP[i]);
        res = max(res,MAX_DP[i]);
    }
    cout << res;
}