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
/* -----------------------
Autor: Tomasz Boguslawski
-------------------------- */
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
#include<sstream>
#include<cstring>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include <fstream>
#include<math.h>
#include <random>

#define LL long long
#define FOR(x, b, e) for(LL x = b; x <= (e); x++)
#define FORS(x, b, e, s) for(LL x = b; x <= (e); x+=s)
#define FORD(x, b, e) for(LL x = b; x >= (e); x--)
#define VAR(v, n) __typeof(n) v = (n)
#define ALL(c) (c).begin(), (c).end()
#define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define DEBUG if (debug)
#define MIN(a,b) ((a>b)?b:a)
#define MAX(a,b) ((a>b)?a:b)

using namespace std;

class Node
{
public:
    LL shape;
    LL value;
    Node() { shape=0; value=0; }
    Node(LL p_shape, LL p_value) { shape=p_shape; value=p_value; }
};


class Heap
{
    public:
    Node* arr;
    LL* pos;
    Heap(LL n)
    {
        arr = new Node[n+1];
        pos = new LL[n+1];
        FOR(i,1,n) { arr[i].shape=i; pos[i]=i; }
    }
    void increaseValueForShape(LL shape, LL newValue)
    {
        arr[pos[shape]].value = newValue;
        LL curr = pos[shape];
        LL parent = curr/2;
        while (parent>0 && arr[parent].value<arr[curr].value)
        {
            //cout << "switching curr=" << curr << " with parent=" << parent << "\n";
            Node pom = arr[parent];
            arr[parent] = arr[curr];
            arr[curr] = pom;
            pos[arr[curr].shape] = curr;
            pos[arr[parent].shape] = parent;
            //cout << " position of " << arr[curr].shape << " is " << curr << "\n";
            //cout << " position of " << arr[parent].shape << " is " << parent << "\n";

            curr = parent;
            parent = curr/2;
        }
    }
    LL bestValue() { return arr[1].value; }
    LL valueOf(LL shape) { return arr[pos[shape]].value; }
};

LL n, c;
LL a[510000];
LL w[510000];

Node newValue[510000];

void handleSet(Heap& heap, set<LL> &puzzles, LL height)
{
    //cout << "Handling set, height=" << height << " shapes: ";
    //FOREACH(p, puzzles) cout << *p << " ";
    //cout << "\n";

    LL count = 0;

    FOREACH(p, puzzles)
    {
        LL shape = *p;
        LL prop1 = heap.bestValue() + height - c;
        LL prop2 = heap.valueOf(shape) + height;
        count++;
        newValue[count].shape = shape;
        newValue[count].value = MAX(prop1, prop2);
    }

    FOR(i, 1, count)
    {
        heap.increaseValueForShape(newValue[i].shape, newValue[i].value);
        //cout << " for shape " << newValue[i].shape << " new value is " << newValue[i].value << "\n";
    }
}

Heap heap(501000);

LL handleWisely()
{
    set<LL> puzzles;
    LL height = 0;
    FORD(i,n,1)
    {
        height = a[i];
        puzzles.insert(w[i]);
        if (i==1 || a[i-1]!=a[i])
        {
            handleSet(heap, puzzles, height);
            puzzles.clear();
        }
    }
    return heap.bestValue();

}

/// MAIN
int main(int argc, char* argv[])
{
    // magic formula, which makes streams work faster:
	ios_base::sync_with_stdio(0);
    cin >> n; cin >> c;
    FOR(i,1,n) { cin >> a[i]; cin >> w[i]; }
    cout << handleWisely() << "\n";

	return 0;
}