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

using namespace std;

typedef long long ll;

int cnt[5001];

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, c;
    cin >> n >> c;
    vector<ll> mn(500001);
    vector<ll> lsz(500001);
    vector<pair<int, int>> a(n);
    ll best = 0;
    for (int i = 0; i < n; i++) {
        cin >> a[i].first >> a[i].second;
    }
    int p = 0;
    while (p < n) {
        int p0 = p;
        int sz = a[p0].first;
        ll newBest = best;
        while (p < n && a[p].first == sz) {
            int w = a[p].second;
            if (lsz[w] < sz) {
                lsz[w] = sz; 
                mn[w] = max(mn[w] + sz, best + sz - c);
                newBest = max(newBest, mn[w]);
            }
            p++;
        }
        best = newBest;        
    }
    cout << best << "\n";
    return 0;
}