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
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <bits/stdc++.h>

using namespace std;

const int N = 500004;

vector<pair<int, int>> adj[2*N];
pair<int,int> szesciany[N];
vector<int> rozne_wymiary;
vector<int> wzorek[N];  // kolejne wymiary od najwiekszego
int nextt[N];

bool visited[2*N];
vector<int> topological;

long long dist[2*N];

void dfs(int s) {
    if(visited[s]) return;
    visited[s] = true;

    for(auto [u, d] : adj[s]) {
        dfs(u);
    }
    topological.push_back(s);
}

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

    int n, c;
    cin >> n >> c;

    for(int i = 0; i < n; i++) {
        int a, w;
        cin >> a >> w;
        szesciany[n-1-i] = {a, w};
    }


    //
    // cout << "szesciany: \n";
    // for (int i = 0; i < n; i++) {
    //     cout << "a: " << szesciany[i].first << ", w: " << szesciany[i].second << "\n";
    // }
    //

    int count_dif = 1;
    rozne_wymiary.push_back(szesciany[0].first);
    for(int i = 0; i < n-1; i++) {
        if(szesciany[i].first != szesciany[i+1].first) {
            rozne_wymiary.push_back(szesciany[i+1].first);
            count_dif++;
        }
    }

    //
    // cout << "rozne_wymiary: \n";
    // for (const auto& rw : rozne_wymiary) {
    //     cout << rw << " ";
    // }
    // cout << "\n";
    //

    nextt[0] = rozne_wymiary[0];
    for(int i = 1; i < count_dif; i++) {
        nextt[rozne_wymiary[i-1]] = rozne_wymiary[i];
    }

    for(int i = 0; i < n; i++) {
        int a = szesciany[i].first;
        int w = szesciany[i].second;
        if(wzorek[w].empty()  ||  wzorek[w].back() != a) wzorek[w].push_back(a);     // rowne trzeba usunac
    }

    //
    // cout << "wzorek: \n";
    // for (int i = 1; i < N; i++) {
    //     if (!wzorek[i].empty()) {
    //         cout << "wzorek[" << i << "]: ";
    //         for (const auto& val : wzorek[i]) {
    //             cout << val << " ";
    //         }
    //         cout << "\n";
    //     }
    // }
    //

    for(int i = 0; i < count_dif-1; i++) {
        int M1 = rozne_wymiary[i];
        int M2 = rozne_wymiary[i+1];
        adj[M1].push_back({M2, M2-M1});
        adj[M1].push_back({M2, M2-c});
    }

    int start = N;
    adj[start].push_back({nextt[0], nextt[0]});
    int sztuczny = N+1;
    nextt[rozne_wymiary[count_dif-1]] = sztuczny;
    int free = N+2;

    for(int w = 1; w < N; w++) {
        if(wzorek[w].empty()) continue;

        if(wzorek[w][0] == nextt[0]) {
            adj[start].push_back({free, nextt[0]});
        }

        for(int i = 0; i < wzorek[w].size(); i++) {
            int a1 = wzorek[w][i];
            //
            // cout << "VVVwzorek " << w << " -wierzcholek: " << free << "\n";
            //
            if(i < wzorek[w].size()-1) {
                int a2 = wzorek[w][i+1];
                adj[free].push_back({free+1, a2});
                adj[free].push_back({nextt[a1], nextt[a1] - c});
                adj[a1].push_back({free, 0});
            }
            else {
                if(nextt[a1] != sztuczny) adj[free].push_back({nextt[a1], nextt[a1] - c});
                // else adj[free].push_back({nextt[a1], 0});  // niepotrzebne
            }
            free++;
        }
    }

    // cout << "adj: \n";
    // for (int i = 0; i < 2 * N; i++) {
    //     if (!adj[i].empty()) {
    //         cout << "adj[" << i << "]: ";
    //         for (const auto& p : adj[i]) {
    //             cout << "(" << p.first << ", " << p.second << ") ";
    //         }
    //         cout << "\n";
    //     }
    // }


    for(int i = 0; i < 2*N; i++) visited[i] = false;
    dfs(start);
    reverse(topological.begin(), topological.end());

    // cout << "topsort: \n";
    // for(auto u : topological) {
    //     cout << u << " ";
    // }
    // cout << "\n";

    for(int i = 0; i < 2*N; i++) dist[i] = 0;

    long long max_dist = 0;
    for(int i = 0; i < topological.size(); i++) {
        int v = topological[i];

        for(auto x : adj[v]) {
            int u = x.first;
            int waga = x.second;
            dist[u] = max(dist[u], dist[v] + waga);
            max_dist = max(max_dist, dist[u]);
        }
    }

    cout << max_dist << "\n";

    return 0;
}