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

int n,w;
long long a,c;
long long biggest_c[500001];
long long biggest;
//std::vector<std::pair<long long,int>> klo;
int main() {
    std::ios_base::sync_with_stdio(0);
    std::cin.tie(NULL);
    std::cin>>n>>c;
    long long last_a=0;
    long long biggest_t=0;
    std::set<long long> used_colors;
    for(int i=0;i<n;i++){
        std::cin>>a>>w;
        if(last_a!=a){
            used_colors.clear();
            biggest=biggest_t;
        }
        if(!used_colors.contains(w))
            biggest_c[w]=std::max(biggest_c[w]+a,biggest+a-c);
        used_colors.emplace(w);
        if(biggest_c[w]>biggest_t){
            biggest_t=biggest_c[w];
        }
        last_a=a;
    }
    std::cout<<biggest_t<<"\n";
    
    
}