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
#include <cstdio>
#include <vector>
#include <cmath>

std::pair<long long int, int> bestInColor[500007];

int main()
{
    int n;
    long long int c;
    scanf("%d %lld", &n, &c);

    int a,w;
    std::pair<long long int, int> smallerBestTower={0,-1};
    std::pair<long long int, int> bestTower={0,0};
    long long int bestForCube;
    long long int tempBestTower;

    for(int i=0; i<n; i++)
    {
        scanf("%d %d", &a, &w);
        if(bestInColor[w].second == a) continue;
        if(a > bestTower.second) tempBestTower=bestTower.first;
        else tempBestTower=smallerBestTower.first;

        bestForCube = std::max(tempBestTower-c, bestInColor[w].first)+(long long int)a;
        bestInColor[w].first = bestForCube;
        bestInColor[w].second = a;

        if(bestTower.first < bestForCube)
        {
            if(bestTower.second < a)
            {
                smallerBestTower=bestTower;
                bestTower.second = a;
            }
            bestTower.first = bestForCube;
        }
    }

    printf("%lld", bestTower.first);

    return 0;
}