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
#include <iostream>
#include <algorithm>

using namespace std;

long long n, m, s, c;
pair <long long, long long> t[500002];
long long lewa, prawa, used[500002];

int main()
{
    cin.tie(0);
    std::ios_base::sync_with_stdio(0);
    cout.tie(0);
    
    
    cin >> n >> c;
    
    for (int i = 0; i < n; ++i)
    {
        cin >> t[i].first >> t[i].second;
    }
    
    sort (t,t+n);
    
    int p=0, l=0, wyn=0, akt=0;
    
    
    //cout << endl;
    
    while (p < n)
    {
        if (t[l].first == t[p].first && p > 0)
        {
            p++;
        }
        
        //cout << t[l].first << " " << t[l].second << " | " << t[p].first << " " << t[p].second << endl;
        
        
        akt = t[p].first;

        if (t[p].second != t[l].second)
        {
            akt -= c;
        }
        //cout << akt << "   ";
        
        if (akt > 0)
        {
            wyn += akt;
            p++;
            l = p - 1;
        }
        else
        {
            akt = 0;
            p++;
        }
        
        //cout << wyn << endl << endl;
    }
    
    cout << wyn;
    
    
    
    
    

    return 0;
}