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
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

const int N = 5e5 + 7;
long long dp[N];
long long maxi;
vector<pair<int, long long>> updates;

void update() {
    for (auto it : updates) {
        dp[it.first] = max(dp[it.first], it.second);
        maxi = max(maxi, it.second);
    }
    updates.clear();
}

int main() {
    int n, c, h, kol;
    cin >> n >> c;
    int prevh = 0;
    for (int i = 1; i <= n; i++) {
        cin >> h >> kol;
        if (h != prevh) {
            update();
            prevh = h;
        }
        updates.emplace_back(kol, max(dp[kol] + h, maxi + h - c));
    }
    update();
    cout << maxi;
    return 0;
}