Problem:
Score: 500 points

Problem Statement

Takahashi will take part in an eating contest. Teams of N members will compete in this contest, and Takahashi's team consists of N players numbered 1 through N from youngest to oldest. The consumption coefficient of Member i is A_i.

In the contest, N foods numbered 1 through N will be presented, and the difficulty of Food i is F_i. The details of the contest are as follows:

A team should assign one member to each food, and should not assign the same member to multiple foods.

It will take x \times y seconds for a member to finish the food, where x is the consumption coefficient of the member and y is the difficulty of the dish.

The score of a team is the longest time it takes for an individual member to finish the food.

Before the contest, Takahashi's team decided to do some training. In one set of training, a member can reduce his/her consumption coefficient by 1, as long as it does not go below 0. However, for financial reasons, the N members can do at most K sets of training in total.

What is the minimum possible score of the team, achieved by choosing the amounts of members' training and allocating the dishes optimally?

Constraints

All values in input are integers.

1 \leq N \leq 2 \times 10^5

0 \leq K \leq 10^{18}

1 \leq A_i \leq 10^6\ (1 \leq i \leq N)

1 \leq F_i \leq 10^6\ (1 \leq i \leq N)

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 ... A_N
F_1 F_2 ... F_N

Output

Print the minimum possible score of the team.

Sample Input 1

3 5
4 2 1
2 3 1

Sample Output 1

2

They can achieve the score of 2, as follows:

Member 1 does 4 sets of training and eats Food 2 in (4-4) \times 3 = 0 seconds.

Member 2 does 1 set of training and eats Food 3 in (2-1) \times 1 = 1 second.

Member 3 does 0 sets of training and eats Food 1 in (1-0) \times 2 = 2 seconds.

They cannot achieve a score of less than 2, so the answer is 2.

Sample Input 2

3 8
4 2 1
2 3 1

Sample Output 2

0

They can choose not to do exactly K sets of training.

Sample Input 3

11 14
3 1 4 1 5 9 2 6 5 3 5
8 9 7 9 3 2 3 8 4 6 2

Sample Output 3

12

Solution in Ruby (2.7.1) (judge verdict: Accepted)

# 全計算量 O(n * log(n))

class Heap
  def initialize(cs, &block)
    @que = []
    @size = 0
    @cs = cs
    @ds = []
    @cmp = block || lambda{ |a,b| a < b }
  end

  def top
    @que[0]
  end

  # O(log(n))
  def push(x)
    i = @size
    while i > 0 do
      j = (i - 1) / 2
      break unless @cmp.call(x, @que[j])
      @que[i] = @que[j]
      i = j
    end
    @que[i] = x
    @size += 1
    @ds[x] = @cs[x]
  end

  # O(log(n))
  def trim1(n)
    x = @que[0]
    m = [n, @ds[x]].min

    return m if ((@ds[x] -= m) > 0)

    @ds[x] = @cs[x]
    @update.call(x)
    i = 0
    while (ii = i * 2 + 1) < @size do
      j1 = ii
      j2 = ii + 1
      j1 = j2 if j2 < @size && @cmp.call(@que[j2], @que[j1])
      break unless @cmp.call(@que[j1], x)
      @que[i] = @que[j1]
      i = j1
    end
    @que[i] = x
    m
  end

  # O(n * log(n))
  def trim(n, &block)
    @update = block
    n -= trim1(n) while (n > 0)
  end
end

n, k = gets.split.map(&:to_i)
as = gets.split.map(&:to_i)
fs = gets.split.map(&:to_i)
 
l = as.inject(:+) - k

if (l <= 0)
  p 0
  exit
end

# O(n * log(n))
as.sort!
fs.sort_by! {|f| -f}

max_f = fs[0]

# O(n * log(n))
afs = as.zip(fs).sort_by do |a,f| a*f end

h = l * max_f * max_f
a_acc = 0
r_acc = 0

# O(n)
fs = afs.map do |a,f| f end
a_sums = afs.map do |a,f| a_acc += a end
rs = (n-1).downto(0).map do |i| r_acc += h / fs[i] end

t = nil
lm = 0

# O(log(n))
m = (0...n).bsearch do |i|
  next true if (i == n - 1)

  a_i, f_i = afs[i]
  af = a_i * f_i

  nn = n - i - 1
  ub = a_sums[i] + af * (rs[nn-1] + nn) / h
  lb = ub - nn

  next true if (lb >= l)

  if (ub < l)
    lm = i + 1
  else
    t = af
  end
  false
end

# O(n)
if (t)
  s = a_sums[m-1]
  (m...n).each do |i| s += t / fs[i] end
  t = nil if (s < l)
end

unless (t)
  nn = n - m
  ll = (m == 0) ? l: l - a_sums[m-1]
  t = [(ll + nn) * h / rs[nn-1], afs[m][0] * afs[m][1]].min
end

# O(n)
if (lm > 0)
  l -= a_sums[lm-1]
  n -= lm
  m -= lm
  fs = fs.drop(lm)
  afs = afs.drop(lm)
end

gs = []
# O(n)
(0...m).each do |i|
  a,f = afs[i]
  l -= a
  gs.push(a*f)
end
# O(n)
(m...n).each do |i|
  f = fs[i]
  a = t / f
  l -= a
  gs.push(a*f)
end

l = -l

raise unless (l >= 0 && l <= n)

if (l == 0)
  p gs.max
else
  # O(n * log(n))
  is = (0...m).to_a + (m...n).sort_by do |i| fs[i] end

  cs = []
  q = Heap.new(cs) do |i,j| gs[i] > gs[j] end

  i0 = is.shift
  cs[i0], prev_i, prev = 1, i0, [gs[i0], fs[i0]]

  # O(n * log(n))
  is.each do |i|
    cur = [gs[i], fs[i]]
    if (cur == prev)
      cs[prev_i] += 1
    else
      q.push(prev_i)
      cs[i], prev_i, prev = 1, i, cur
    end
  end
  q.push(prev_i)

  # O(n * log(n))
  q.trim(l) do |i| gs[i] -= fs[i] end
  p gs[q.top]
end