Problem:
Score : 300 points

Problem Statement

Hearing that energy drinks increase rating in those sites, Takahashi decides to buy up M cans of energy drinks.

There are N stores that sell energy drinks. In the i-th store, he can buy at most B_i cans of energy drinks for A_i yen (the currency of Japan) each.

What is the minimum amount of money with which he can buy M cans of energy drinks?

It is guaranteed that, in the given inputs, a sufficient amount of money can always buy M cans of energy drinks.

Constraints

All values in input are integers.

1 \leq N, M \leq 10^5

1 \leq A_i \leq 10^9

1 \leq B_i \leq 10^5

B_1 + ... + B_N \geq M

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
A_2 B_2
\vdots
A_N B_N

Output

Print the minimum amount of money with which Takahashi can buy M cans of energy drinks.

Sample Input 1

2 5
4 9
2 4

Sample Output 1

12

With 12 yen, we can buy one drink at the first store and four drinks at the second store, for the total of five drinks. However, we cannot buy 5 drinks with 11 yen or less.

Sample Input 2

4 30
6 18
2 5
3 10
7 9

Sample Output 2

130

Sample Input 3

1 100000
1000000000 100000

Sample Output 3

100000000000000

The output may not fit into a 32-bit integer type.

Solution in C (GCC 5.4.1) (judge verdict: Runtime Error)

#include<stdio.h>
#define MAX_heap 1000

typedef struct _cell{
  int a;
  int b;
}cell;

typedef struct _heap{
  cell cells[MAX_heap+1];
  int length;
}heap;

heap H;

typedef char boolean;
typedef long long int  lli;
void init_heap_univ();
boolean compare_cell(cell x, cell y);
void put_heap_univ(cell x);
cell pull_heap_univ();
int flag_heap_univ(int itr,cell temp);
void sort_heap_univ(cell *array);





int main(void){
  int n,m;
  scanf("%d",&n);
  scanf("%d",&m);
  cell c;
  int x,y,i;
  for(i=0;i<n;i++){
    scanf("%d%d",&x,&y);
    c.a=x;c.b=y;
    put_heap_univ(c);
  }
  cell store[n];
  sort_heap_univ(store);

  //  for(i=0;i<n;i++) printf("a%d b%d\n",store[i].a,store[i].b);


  i=0;
  int w=m;
  lli ans=0;
  while(w>0){
    if(w<store[i].b)store[i].b=w;
    ans+=(lli)store[i].a*store[i].b;
    w-=store[i].b;
    i++;
  }
  printf("%lld",ans);
}

void init_heap_univ(){
  H.length=0;
}

boolean compare_cell(cell x, cell y){
  if(x.a<y.a)return 1;
  return 0;
}

void put_heap_univ(cell x){
  H.length++;
  int i=H.length;
  while(i>1 && compare_cell(x,H.cells[i/2])){
    H.cells[i]=H.cells[i/2];
    i/=2;
  }
  H.cells[i]=x;
  //  for(i=0; i<=heap[0];i++)
  //    printf("%lu ",heap[i]);
  //  printf("\n",heap[i]);

}

cell pull_heap_univ(){
  cell x=H.cells[1],temp=H.cells[H.length];
  int itr=1,itr2,flag;
  H.length--;
  flag=flag_heap_univ(itr,temp);
  while(flag){
    itr2=2*itr+flag-1;
    H.cells[itr]=H.cells[itr2];
    itr=itr2;
    flag=flag_heap_univ(itr,temp);
  }
  H.cells[itr]=temp;
  return x;
}

int flag_heap_univ(int itr,cell temp){
  int flag;
  if(H.length<itr*2) flag=0;
  else if(H.length==itr*2)
    flag=(compare_cell(temp,H.cells[itr*2])?0:1);
  else if(compare_cell(H.cells[itr*2],H.cells[itr*2+1]))
    flag=(compare_cell(temp,H.cells[itr*2])?0:1);
  else flag=(compare_cell(temp,H.cells[itr*2+1])?0:2);

  //  printf("%d ",flag);
  return flag;
}

void sort_heap_univ(cell *array){

  
  int i,length=H.length;


  for(i=0; i<length; i++)
    array[i]=pull_heap_univ();
}