module mod_priority_queue
  implicit none
  type t_node
    integer(8) :: a, f
  end type
  type t_priority_queue
    private
    integer :: num = 0
    type(t_node), pointer :: heap(:) => null()
  contains
    procedure :: offer => offer
    procedure :: clear => clear
    procedure :: poll => poll
    procedure :: peek => peek
    procedure :: size => size_of
  end type
  interface node
    module procedure :: newn
  end interface node
contains
  function newn(a,f) result(ret)
    integer(8), intent(in) :: a, f
    type(t_node) :: ret
    ret%a = a
    ret%f = f
  end
  function val(this) result(ret)
    class(t_node), intent(in) :: this
    integer(8) :: ret
    ret = this%a*this%f
  end
  function compare(this,other) result(ret)
    class(t_node), intent(in) :: this
    type(t_node), intent(in) :: other
    integer(8) :: ret, x, y
    x = val(this)
    y = val(other)
    if (x == y) then
      ret = this%f-other%f
    else
      ret = y-x
    end if
  end
  subroutine offer(this,item)
    class(t_priority_queue), intent(inout) :: this
    type(t_node), intent(in) :: item
    integer :: n, i
    type(t_node) :: t
    type(t_node), allocatable :: tmp(:)
    if (.not.associated(this%heap)) allocate(this%heap(1))
    if (this%num == size(this%heap)) then
      allocate(tmp(this%num))
      tmp = this%heap
      deallocate(this%heap)
      allocate(this%heap(2*this%num))
      this%heap(1:this%num) = tmp
      deallocate(tmp)
    end if
    this%num = this%num+1
    this%heap(this%num) = item
    n = this%num
    do while (n > 1)
      i = n/2
      if (compare(this%heap(n),this%heap(i)) < 0_8) then
        t = this%heap(n)
        this%heap(n) = this%heap(i)
        this%heap(i) = t
      end if
      n = i
    end do
  end
  subroutine clear(this)
    class(t_priority_queue), intent(inout) :: this
    if (associated(this%heap)) deallocate(this%heap)
    this%num = 0
  end
  function poll(this) result(item)
    class(t_priority_queue), intent(inout) :: this
    type(t_node) :: item, tmp
    integer :: n, i, j
    n = this%num
    item = this%heap(1)
    this%heap(1) = this%heap(n)
    this%num = this%num-1
    i = 1
    do while (2*i < n)
      j = 2*i
      if (j+1 < n .and. compare(this%heap(j+1),this%heap(j)) < 0_8) j = j+1
      if (compare(this%heap(j),this%heap(i)) < 0_8) then
        tmp = this%heap(j)
        this%heap(j) = this%heap(i)
        this%heap(i) = tmp
      end if
      i = j
    end do
  end
  function peek(this) result(item)
    class(t_priority_queue), intent(in) :: this
    type(t_node) :: item
    item = this%heap(1)
  end
  integer function size_of(this)
    class(t_priority_queue), intent(in) :: this
    size_of = this%num
  end
end module mod_priority_queue
program gluttony
  use mod_priority_queue
  implicit none
  integer :: n, i
  integer(8) :: k, dv, t
  integer(8), dimension(200000) :: a = 0_8, f = 0_8
  type(t_priority_queue) :: pq
  type(t_node) :: nd1, nd2
  read(*,*) n, k
  read(*,*) a(1:n)
  if (sum(a(1:n)) <= k) then
    write(*,'(i0)') 0
    stop
  end if
  read(*,*) f(1:n)
  call bitonic_sort(a(1:n),.true.)
  call bitonic_sort(f(1:n),.false.)
  do i = 1, n
    call pq%offer(node(a(i),f(i)))
  end do
  do
    nd1 = pq%poll()
    nd2 = pq%peek()
    dv = val(nd1)-val(nd2)
    t = min(dv/nd1%f+1_8,nd1%a)
    if (t < k) then
      k = k-t
      call pq%offer(node(nd1%a-t,nd1%f))
    else
      call pq%offer(node(nd1%a-k,nd1%f))
      exit
    end if
  end do
  write(*,'(i0)') val(pq%peek())
contains
  recursive subroutine bitonic_sort(a,ascend)
    integer(8), intent(inout) :: a(:)
    logical, intent(in) :: ascend
    integer :: n
    n = size(a)
    if (n == 1) return
    call bitonic_sort(a(1:n/2),.not.ascend)
    call bitonic_sort(a(n/2+1:n),ascend)
    call bitonic_merge(a,ascend)
  end
  recursive subroutine bitonic_merge(a,ascend)
    integer(8), intent(inout) :: a(:)
    logical, intent(in) :: ascend
    integer :: n, m, i
    n = size(a)
    if (n == 1) return
    m = 1
    do while (2*m < n)
      m = 2*m
    end do
    do i = 1, n-m
      if ((a(i) > a(i+m)) .eqv. ascend) then
        a(i) = xor(a(i),a(i+m))
        a(i+m) = xor(a(i),a(i+m))
        a(i) = xor(a(i),a(i+m))
      end if
    end do
    call bitonic_merge(a(1:m),ascend)
    call bitonic_merge(a(m+1:n),ascend)
  end
end program gluttony