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 sort_crh(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 sort_crh(var)
     ! input randam var(:,1)
     ! output sorting var(:,1) small->big
     ! beside var(:,2)
     implicit none
     integer(8),intent(inout) :: var(:,:) ! (:,2) 1.crh, 2.another
     integer(8) :: size_1
     integer(8) :: Location
     integer(8) :: i

     size_1= size(var,1)
     do i=1,size_1-1
        Location = FindMinimum(var(:,1),i,size_1)
        call Swap(var(i,:),var(Location,:))
     end do
     return
   end subroutine sort_crh

  SUBROUTINE  Swap(a, b)
      IMPLICIT  NONE
      integer(8), INTENT(INOUT) :: a(2), b(2)
      integer(8)                :: Temp(2)

      Temp = a
      a    = b
      b    = Temp
      return
    END SUBROUTINE  Swap

   INTEGER(8) FUNCTION  FindMinimum(x, Start, End)
      IMPLICIT  NONE
      integer(8), DIMENSION(1:), INTENT(IN) :: x
      integer(8), INTENT(IN)                :: Start, End
      integer(8)                           :: Minimum
      INTEGER(8)                            :: Location
      INTEGER(8)                            :: i

      Minimum  = x(Start)               ! assume the first is the min
      Location = Start                  ! record its position
      DO i = Start+1, End               ! start with next elements
         IF (x(i) < Minimum) THEN       !   if x(i) less than the min?
            Minimum  = x(i)             !      Yes, a new minimum found
            Location = i                !      record its position
         END IF
      END DO
      FindMinimum = Location            ! return the position
    END FUNCTION  FindMinimum
 end program main
