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 Fortran (gfortran v4.8.4) (judge verdict: Accepted)

program main
  implicit none
  integer(8) :: N,M
  integer(8),allocatable :: A(:),B(:)
  integer(8),allocatable :: A2(:,:)
  integer(8) :: i
  integer(8) :: nout
  integer(8) :: out
  read(*,*) N,M
  allocate(A(N),B(N))
  do i = 1,N
     read(*,*) A(i),B(i)
  end do
  allocate(A2(N,2) )
  A2(:,1) = A(:)
  A2(:,2) = B(:)

  call hsort(N,A2)
  out=0
  nout=0
  do i = 1,N
     nout = nout + A2(i,2)
     out = out +A2(i,1) * A2(i,2)
     if ( nout >= M ) THEn
        out = out - A2(i,1)*(nout-M)
        exit
     end if
  end do
  write(*,'(i0)') out
  stop
contains
  subroutine hsort(n, x)
    implicit none
    integer(8), intent(IN) :: n
    integer(8), intent(INOUT) :: x(n,2)
    ! local
    integer(8) k
    integer(8) :: tmp,tmp2
    ! external subroutine
    !external mkheap

    ! 配列 x 全体をヒープにする。
    do k = n/2, 1, -1
       tmp = x(k,1)
       call mkheap(n, x, k, n, tmp)
    enddo

    ! ヒープの根と右下の葉を交換してソートする。
    do k = n, 2, -1
       tmp = x(k,1)
       x(k,1) = x(1,1)
       tmp2 = x(1,2)
       x(1,2) =x(k,2) 
       x(k,2) = tmp2
       
       call mkheap(n, x, int(1,8), k-1, tmp)
    end do

    return
  end subroutine hsort

  subroutine mkheap(n, x, root, leaf, val)
    implicit none
    integer(8), intent(IN)    :: n, root, leaf
    integer(8), intent(INOUT) :: x(1:n,2)
    integer(8), intent(IN)    :: val
    ! local
    integer(8) :: i, j
    integer(8) :: tmp

    i = root
    j = i * 2

    do
       if (j > leaf) exit

       if (j < leaf) then
          if (x(j,1) < x(j+1,1)) j = j + 1
       end if

       if (x(j,1) > val) then
          x(i,1) = x(j,1)
          tmp = x(j,2)
          x(j,2) = x(i,2)
          x(i,2) = tmp
          i = j
          j = i * 2
       else
          j = leaf + 1
       endif
    end do

    x(i,1) = val

    return
  end subroutine mkheap
  
 end program main