#include <stdio.h>
#include <stdlib.h>
#include <values.h>
#define true 1
#define false 0
typedef struct Node{
int h,w;
struct Node *next;
unsigned char noOfElements;
} node;
typedef node* linkedList;
#define MinData -MAXINT
typedef struct gutuie {
int w;
int h;
} Gutuie;
typedef struct heapStruct {
int Capacity;
int Size;
Gutuie *Gutui;
} HeapStruct;
typedef HeapStruct* PriorityQueue;
void merge(int *h, int *w, int s, int m, int d) {
int i = s;
int j = m + 1;
int k = 0;
int h2[100000];
int w2[100000];
while (i <= m && j <= d) {
if (w[i] > w[j]) {
h2[k] = h[i];
w2[k++] = w[i++];
}
else {
h2[k] = h[j];
w2[k++] = w[j++];
}
}
for (; i <= m ; i++){
h2[k] = h[i];
w2[k++] = w[i];
}
for (;j <= d ; j++){
h2[k] = h[j];
w2[k++] = w[j];
}
for (i = 0 ; i <= (d-s) ; i++){
w[i+s] = w2[i];
h[i+s] = h2[i];
}
}
void mSort(int *h, int *w, int s, int d) {
if (s < d) {
int m = (s+d)/2;
mSort(h,w,s,m);
mSort(h,w,m+1,d);
merge(h,w,s,m,d);
}
}
PriorityQueue MakeQueue(int MaxCapacity) {
PriorityQueue Q = (PriorityQueue)malloc(sizeof(HeapStruct));
Q -> Capacity = MaxCapacity;
Q -> Size = 0;
Q -> Gutui = (Gutuie*)malloc(MaxCapacity*sizeof(Gutuie));
Q -> Gutui[0].w = MinData;
Q -> Gutui[0].h = MinData;
return Q;
}
/* H->Element[ 0 ] is a sentinel */
void Insert(Gutuie oGutuie, PriorityQueue Q) {
int i;
for (i = ++ Q -> Size ; Q -> Gutui[i / 2].w > oGutuie.w ; i /= 2)
Q -> Gutui [i] = Q -> Gutui [i / 2];
Q -> Gutui [i] = oGutuie;
}
Gutuie DeleteMin(PriorityQueue Q) {
int i, Child;
Gutuie MinElement, LastElement;
MinElement = Q -> Gutui[1];
LastElement = Q -> Gutui[Q -> Size --];
for (i = 1; i * 2 <= Q->Size; i = Child) {
Child = i * 2;
if (Child != Q -> Size && Q -> Gutui[Child + 1].w < Q -> Gutui[Child].w)
Child++;
if (LastElement.w > Q -> Gutui[Child].w)
Q -> Gutui[i] = Q -> Gutui[Child];
else break;
}
Q -> Gutui [i] = LastElement;
return MinElement;
}
int IsEmpty(PriorityQueue Q) {
if (Q -> Size == 0)
return 1;
return 0;
}
node* newNode (int h, int w, node *nextNode){
node *aNode = (node*)malloc(sizeof(node));
aNode -> h = h;
aNode -> w = w;
aNode -> next = nextNode;
aNode -> noOfElements = 0;
return aNode;
}
void setNext(node *aNode, node* nextNode){
aNode -> next = nextNode;
}
int listInsertAsc(linkedList aList, int maxEl, node *aNode) {
node * tempNode = aList;
while (tempNode -> next != NULL && tempNode -> next -> w < aNode -> w) {
tempNode = tempNode -> next;
}
setNext(aNode,tempNode->next);
setNext(tempNode,aNode);
return true;
}
int listInsertDesc(linkedList aList, int maxEl, node *aNode) {
node * tempNode = aList;
int listCount = 0;
while (tempNode -> next != NULL && tempNode -> next -> w > aNode -> w && listCount < maxEl) {
tempNode = tempNode -> next;
listCount ++;
}
if (listCount == maxEl)
return false;
setNext(aNode,tempNode->next);
setNext(tempNode,aNode);
return true;
}
int isEmpty(linkedList aList){
if (aList -> next == NULL)
return true;
return false;
}
node *getNextNode(linkedList aList){
node * tempNode = aList -> next;
aList -> next = aList -> next -> next;
return tempNode;
}
void printList(linkedList aList) {
node * tempNode = aList -> next;
while (tempNode != NULL){
printf("%d %d\n",tempNode -> h, tempNode -> w);
tempNode = tempNode ->next;
}
}
int main() {
FILE *fin, *fout;
int N, H, U;
fin = fopen("gutui.in","r");
fout = fopen("gutui.out","w");
fscanf(fin,"%d %d %d", &N, &H, &U);
int i;
int *h = (int*)malloc(N*sizeof(int));
int *w = (int*)malloc(N*sizeof(int));
// Read elements
for (i = 0 ; i < N ; i++) {
fscanf(fin,"%d %d", &h[i], &w[i]);
}
// Sort elements
mSort(h,w,0,N-1);
// Make list of lists
int noOfLists = H/U;
if (H % U) noOfLists++;
linkedList *lists = (linkedList*) malloc (noOfLists * sizeof(linkedList));
for (i = 0 ; i < noOfLists ; i++) {
lists[i] = (linkedList) malloc(sizeof(node));
lists[i] -> noOfElements = 0;
}
// Fill the lists
for (i = 0 ; i < N ; i++) {
int listNo;
listNo = (H - h[i])/U;
if (lists[listNo] -> noOfElements < (listNo + 1)) {
listInsertDesc(lists[listNo],N,newNode(h[i],w[i],NULL));
lists[listNo] -> noOfElements++;
}
}
/*
// print lists
for (i = 0 ; i < noOfLists ; i++) {
printf("lista %d:\n",i);
printList(lists[i]);
}
*/
//linkedList selected = (linkedList) malloc (sizeof(node));
free(h);
free(w);
PriorityQueue Q = MakeQueue(H / U + 1);
for (i = 0 ; i < noOfLists ; i++) {
while (isEmpty(lists[i]) == false) {
node *tempNode = getNextNode(lists[i]);
Gutuie oGutuie;
oGutuie.w = tempNode -> w;
oGutuie.h = tempNode -> h;
if (Q -> Size > 1 && Q -> Gutui[1].w > oGutuie.w)
Insert(oGutuie, Q);
if (Q -> Size > i + 1)
DeleteMin(Q);
}
}
unsigned long long suma = 0;
while (IsEmpty(Q) == 0) {
Gutuie x = DeleteMin(Q);
suma += x.w;
}
/*add(aBTree,newBNode(100,10));
add(aBTree,newBNode(200,20));
add(aBTree,newBNode(10,15));
add(aBTree,newBNode(250,25));
add(aBTree,newBNode(10,30));
add(aBTree,newBNode(250,24));
printBTree(aBTree -> nodes);
delMin(aBTree);
printf("\n");
printBTree(aBTree -> nodes);*/
// printBTree(aBTree -> nodes);
//printf("lista finala:\n");
//printList(selected);
fprintf(fout,"%lld\n",suma);
//printf("%lld\n",suma);
fclose(fin);
fclose(fout);
return 0;
}