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
#include <bits/stdc++.h>
#define int long long

using namespace std;
const int MX = 5e5+9;
map<int, set<int>>M;
int dp[MX];
int32_t main(){
    cin.tie(0)->sync_with_stdio(0);
    int n, k; cin >> n >> k;
    for(auto i = 1; i <= n; ++i){
        int x, y; cin >> x >> y;
        M[x].insert(y);
    }
    int maxi = 0, res = 0;
    for(auto h = 1; h < MX; ++h){
        if(M[h].empty()) continue;
        for(auto x : M[h]){
            dp[x] = max(dp[x]+h, maxi+h-k);
        }
        for(auto x : M[h]){
            maxi = max(maxi, dp[x]);
        }
    }
    for(auto i = 1; i < MX; ++i) res = max(res, dp[i]);
    cout << res;
}