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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <bits/stdc++.h>
using namespace std;
#define V vector
#define Q queue
#define PQ priority_queue
#define ST stack
#define UMAP unordered_map
typedef long long LL;
typedef pair<int, int> PII;
typedef V<int> VI;
typedef V<V<int>> VVI;
typedef V<LL> VLL;

constexpr int MAXW = 1'000'000;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    LL n, c;
    cin >> n >> c;
    VLL colors(MAXW, 0);
    LL best = 0;
    int last = -1;
    V<pair<LL,LL>> process;
    V<pair<LL,LL>> inp(n);
    for (int i = 0; i < n; ++i) {
        cin >> inp[i].first >> inp[i].second;
    }
    for (int i = n-1; i >= 0; --i) {
        LL a = inp[i].first, w = inp[i].second;
        if (last != a) {
            for (const auto& x : process) {
                colors[x.first] = max(colors[x.first], x.second);
                best = max(best, x.second);
            }
            process.clear();
        }
        process.push_back({w, max(colors[w]+a, best+a-c)});
        last = a;
    }
    for (const auto& x : process) {
        colors[x.first] = max(colors[x.first], x.second);
        best = max(best, x.second);
    }
    LL res = max(0LL, best);
    for (int i = 0; i < MAXW; ++i) {
        res = max(res, colors[i]);
    }

    cout << res << '\n';

    return 0;
}