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
#include <bits/stdc++.h>
#define dbg(x) #x << " = " << x << " "
using namespace std;
using ll = long long;
constexpr int MAXN = 500'005;
constexpr ll INF = 1e18;

int a[MAXN], w[MAXN];

ll dp[MAXN], nowe_dp[MAXN];
int32_t main() {
    ios_base::sync_with_stdio(0);
    int n, c;
    cin >> n >> c;

    for (int i = 1; i <= n; i++) {
        cin >> a[i] >> w[i];
        dp[w[i]] = -INF;
        nowe_dp[w[i]] = -INF;
    }

    set<pair<ll, int>> maksy;
    vector<pair<ll, int>> to_insert;

    for (int i = 1; i <= n; i++) {
        if (a[i] != a[i-1]) {
            while (!to_insert.empty()) {
                auto [A, W_A] = to_insert.back();
                to_insert.pop_back();
                dp[W_A] = A;
                maksy.insert({-A, W_A});
            }
        }
        ll nowe = max((ll)a[i], dp[w[i]] + a[i]);
        int two = 2;
        for (auto [A, W_A] : maksy) {
            nowe = max(nowe, -A + a[i] - c);
            two--;
            if (!two) break;           
        }

        to_insert.emplace_back(nowe, w[i]);
    }

    while (!to_insert.empty()) {
        auto [A, W_A] = to_insert.back();
        to_insert.pop_back();
        dp[W_A] = A;
        maksy.insert({-A, W_A});
    }

    cout << -maksy.begin()->first << endl;
}