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 (Python):






import sys
input = sys.stdin.readline

import math

import bisect
import heapq
from collections import defaultdict
from collections import deque
from collections import Counter
from functools import lru_cache





MOD = 10**9+7
INF = float('inf')
AZ = "abcdefghijklmnopqrstuvwxyz"






def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]


def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()


def DD(arg): return defaultdict(arg)


def inv(n): return pow(n, MOD-2, MOD)


kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n): return kaijo_memo[n]
  if(len(kaijo_memo) == 0): kaijo_memo.append(1)
  while(len(kaijo_memo) <= n): kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n): return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0): gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n): gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if n == r: return 1
  if n < r or r < 0: return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret


def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1: arr.append([temp, 1])
  if arr==[]: arr.append([n, 1])
  return arr


def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors


def make_primes(N):
  max = int(math.sqrt(N))
  seachList = [i for i in range(2,N+1)]
  primeNum = []
  while seachList[0] <= max:
    primeNum.append(seachList[0])
    tmp = seachList[0]
    seachList = [i for i in seachList if i % tmp != 0]
  primeNum.extend(seachList)
  return primeNum


def gcd(a, b):
    while b: a, b = b, a % b
    return a


def lcm(a, b):
    return a * b // gcd (a, b)


def count_bit(n):
  count = 0
  while n:
    n &= n-1
    count += 1
  return count


def base_10_to_n(X, n):
  if X//n: return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i-1])*n**i for i in range(len(str(X))))

def base_10_to_n_without_0(X, n):
  X -= 1
  if X//n: return base_10_to_n_without_0(X//n, n)+[X%n]
  return [X%n]


def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count




N,K = IL()
A = IL()
F = IL()
A.sort()
F.sort(reverse=True)

def is_ok(x):
  count = 0
  for i in range(N):
    count += max(0,A[i]-(x//F[i]))
  if count<=K:
    return True
  else:
    return False

def meguru_bisect(ng, ok):
  while (abs(ok - ng) > 1):
    mid = (ok + ng) // 2
    if is_ok(mid):
      ok = mid
    else:
      ng = mid
  return ok

print(meguru_bisect(-1,A[-1]*F[0]))

Solution (Python):
import sys

import numba as nb
import numpy as np

input = sys.stdin.readline


@nb.njit("i8(i8,i8[:],i8[:])", cache=True)
def binary_search(K, A, F):
    """Meguru type binary search"""
    ng = -1
    ok = A[-1] * F[0]

    def is_ok(A, F, K, x):
        count = 0
        for a, f in zip(A, F):
            count += max(0, a - x // f)
        return count <= K

    while (ok - ng) > 1:
        mid = (ok + ng) // 2
        if is_ok(A, F, K, mid):
            ok = mid
        else:
            ng = mid

    return ok


def main():
    N, K = map(int, input().split())
    A = np.array(input().split(), dtype=np.int64)
    F = np.array(input().split(), dtype=np.int64)

    A.sort()
    F[::-1].sort()

    ans = binary_search(K, A, F)
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import sys

import numba as nb
import numpy as np

input = sys.stdin.readline


@nb.njit("i8(i8,i8[:],i8[:])", cache=True)
def binary_search(K, A, F):
    """Meguru type binary search"""
    ng = -1
    ok = A[-1] * F[0]

    def is_ok(A, F, K, x):
        count = 0
        for a, f in zip(A, F):
            count += max(0, a - x // f)
        return count <= K

    while (abs(ok - ng) > 1):
        mid = (ok + ng) // 2
        if is_ok(A, F, K, mid):
            ok = mid
        else:
            ng = mid

    return ok


def main():
    N, K = map(int, input().split())
    A = np.array(input().split(), dtype=np.int64)
    F = np.array(input().split(), dtype=np.int64)

    A.sort()
    F[::-1].sort()

    ans = binary_search(K, A, F)
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import sys

import numba as nb
import numpy as np

input = sys.stdin.readline


@nb.njit("i8(i8,i8[:],i8[:])", cache=True)
def binary_search(K, A, F):
    """Meguru type binary search"""
    ng = -1
    ok = 10 ** 12

    def is_ok(A, F, K, x):
        count = 0
        for a, f in zip(A, F):
            count += max(0, a - x // f)
        return count <= K

    while (abs(ok - ng) > 1):
        mid = (ok + ng) // 2
        if is_ok(A, F, K, mid):
            ok = mid
        else:
            ng = mid

    return ok


def main():
    N, K = map(int, input().split())
    A = np.array(input().split(), dtype=np.int64)
    F = np.array(input().split(), dtype=np.int64)

    A.sort()
    F[::-1].sort()

    ans = binary_search(K, A, F)
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
N, K = map(int, input().split())
A = list(map(int, input().split()))
F = list(map(int, input().split()))
A.sort()
F.sort(reverse=True)
ok = 10**12
ng = -1


def check(time):
    shugyou = 0
    for i in range(N):
        if A[i]*F[i] > time:
            shugyou += A[i] - (time//F[i])
    return shugyou <= K


while ok - ng > 1:
    mid = (ok + ng)//2
    if check(mid):
        ok = mid
    else:
        ng = mid
print(ok)

Solution (Python):
n,k = map(int, input().split())
al = list(map(int, input().split()))
fl = list(map(int, input().split()))

al.sort()
fl.sort(reverse=True)
afl = []
for a,f in zip(al,fl):
    afl.append((a,f))

ok, ng = 10**12, -1
while abs(ok-ng) > 1:
    mid = (ok+ng) // 2
    cnt = 0
    for a,f in afl:
        val = a*f
        train_cnt = (val-mid-1)//f + 1
        cnt += max(train_cnt, 0)

    if cnt <= k:
        ok = mid
    else:
    	ng = mid
print(ok)

Solution (Python):
n, k = (int(x) for x in input().split())
A = list(int(x) for x in input().split())
F = list(int(x) for x in input().split())
A.sort()
F.sort(reverse=True)

def meguru_bisect(ng, ok):
    """
    初期値のng,okを受け取り, check=>Trueを満たす最大値(最小値)を返す
    条件を満たす最大値を求める場合、初期値は(ng:上限の外, ok:下限の外)   例) 10**9+1, -1
    条件を満たす最小値を求める場合、初期値は(ng:下限の外, ok:上限の外)   例) -1, 10**9+1
    上限の外や下限の外が返ってくる場合は条件を満たす値が範囲内になかったことを示すので、例外処理が必要
    """
    while abs(ok - ng) > 1:
        mid = (ok + ng) // 2
        if check(mid):
            ok = mid
        else:
            ng = mid
    return ok

def check(x):
    tmp = 0
    for i in range(n):
        if A[i] * F[i] > x:
            tmp += A[i] - (x // F[i])
    return tmp <= k

ans = meguru_bisect(-1, 10**12 + 1)
print(ans)

Solution (Python):


N, K = map(int, input().split())
A = list(map(int, input().split()))
F = list(map(int, input().split()))
F.sort()
A.sort(reverse=True)


def func(X):
    s = 0
    for i in range(N):
        a, f = A[i], F[i]
        s += max(0, a-X//f)
    return s <= K


R = 10**12
L = -1
while R-L > 1:
    m = (R+L)//2
    if func(m):
        R = m
    else:
        L = m
print(R)

Solution (Python):
def main():
    import copy
    from heapq import heappop, heapify, heappush
    import math
    N, K = map(int, input().split())
    A = [int(i) for i in input().split()]
    F = [int(i) for i in input().split()]
    ans = 0
    A.sort(reverse=True)
    F.sort()
    s = []
    for i in range(N):
        s.append([A[i]*F[i], A[i], F[i]])
    s.sort(reverse=True, key=lambda x: x[0])

    def f(S, T):
        cur = 0
        num = 0
        while cur <= K:
            if num == N:
                break
            a, b, c = S[num]
            if a <= T:
                break
            cur += math.ceil((a - T)/c)
            num += 1
        if cur <= K:
            return True
        else:
            return False

    ok, ng = s[0][0], -1
    while abs(ok-ng) > 1:
        mid = (ok+ng)//2
        if f(s, mid) == True:
            ok = mid
        else:
            ng = mid
    print(ok)

if __name__ == '__main__':
    main()

Solution (Python):


import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10**6)
INF = float("inf")

import math

def solve(x, N, K, A, F):
    total = 0
    for i in range(N):
        total += max(0, math.ceil((A[i]*F[i] - x) / F[i]))
    if total <= K:
        return True
    else:
        return False

def main():
    N,K = map(int,input().split())
    A = list(map(int,input().split()))
    F = list(map(int,input().split()))

    A.sort()
    F.sort(reverse=True)

    ok = 10**13+1
    ng = -1
    while ok - ng > 1:
        mid = (ng + ok) // 2
        if solve(mid, N, K, A, F):
            ok = mid
        else:
            ng = mid
    print(ok)

if __name__ == "__main__":
    main()

Solution (Python):
N, K = map(int, input().split())
A = list(map(int, input().split()))
F = list(map(int, input().split()))

A.sort()
F.sort(reverse = True)

def count_train(x):
    tmp = 0
    for a, f in zip(A, F):
        tmp += max(0, a - (x // f))
        if tmp > K:
            return tmp
    return tmp

ng = -1
ok = 10 ** 18
while ok - ng > 1:
    mid = (ok + ng) // 2
    if count_train(mid) <= K:
        ok = mid
    else:
        ng = mid

print(ok)

Solution (Python):
import heapq
n, k = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
f = list(map(int, input().split()))
f.sort(reverse=True)

asum = sum(a)

def test(x):
    t = [min(a[i], x//f[i]) for i in range(n)]
    return asum - sum(t)


ng = -1
ok = 10**13
while abs(ok-ng)>1:
    mid = (ok+ng)//2
    if test(mid) <= k:
        ok = mid
    else:
        ng = mid

print(ok)

Solution (Python):
N, K = map(int, input().split())
*A, = map(int, input().split())
*F, = map(int, input().split())
A, F = sorted(A), sorted(F, reverse=True)

def cond(x):
    k = 0
    for a, f in zip(A, F): k += max(a - (x // f), 0)
    return True if k <= K else False 

MIN = -1
MAX = 10**12 + 1
left, right = MIN, MAX 
while right - left > 1:
    mid = (left + right) // 2
    if cond(mid): right = mid
    else: left = mid
print(right)

Solution (Python):
from heapq import heappush, heappop

N, K = map(int, input().split())
A = list(map(int, input().split()))
F = list(map(int, input().split()))

F.sort()
x = [a * b for a, b in zip(F, sorted(A, reverse=True))]

ub = max(x)
lb = 0
while ub - lb > 1:
    mid = (ub + lb) // 2
    res = 0
    for i in range(N):
        tmp = max(0, x[i] - mid)
        res += -(-tmp // F[i])

    if res <= K:
        ub = mid
    else:
        lb = mid

if sum(A) <= K:
    print(0)
else:
    print(ub)

Solution (Python):
N,K = map(int,input().split())

import numpy as np

A = np.array(input().split(),np.int64)
B = np.array(input().split(),np.int64)

A.sort() ; B.sort() 
B=B[::-1]

right = max(A*B)   
left = -1           

def test(t):
    
    C = A-t//B
    D= np.where(C<0,0,C)
    return D.sum()<=K

while left+1<right:
    mid = (left+right)//2
    if test(mid):
        right=mid
    else:
        left = mid

print(right)

Solution (Python):
n,k = map(int,input().split())
a = list(map(int,input().split()))
f = list(map(int,input().split()))

def hantei(c):
    tmp = 0
    for i in range(n):
        if af[i] > c:
            a1 = c // f[i]
            tmp += a[i]- a1
    if tmp <= k:
        return True
    else:
        return False

hq = []

a.sort()
f.sort(reverse = True)

af = []
for i in range(n):
    af.append(a[i] * f[i])

l = 0
r = 10**12+10

while r - l > 1:
    c = (l + r) // 2
    if hantei(c):
        r = c
    else:
        l = c

if hantei(l):
    print(l)
else:
    print(r)

Solution (Python):
import sys
import os,io
input=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
n,k=map(int,input().split())
a=list(map(int,input().split()))
f=list(map(int,input().split()))
a.sort()
f.sort()
f.reverse()
if sum(a)<=k:
  print(0)
  sys.exit()
pointer=0
l=0
r=0
for i in range(n):
  r=max(r,a[i]*f[i])
while l+1<r:
  try1=(l+r)//2
  required=0
  for i in range(n):
    required+=(max(0,a[i]-try1//f[i]))
  if required>k:
    l=try1
  else:
    r=try1
required=0
for i in range(n):
  required+=(max(0,a[i]-l//f[i]))
if required>k:
  print(r)
else:
  print(l)

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 5 + 10)
def input(): return sys.stdin.readline().strip()

def resolve():
    
    n,k=map(int,input().split())
    A=sorted(list(map(int,input().split())))
    F=sorted(list(map(int,input().split())),reverse=True)

    
    def is_ok(ind):
        cnt=0
        for i in range(n):
            target=ind//F[i]
            if target<A[i]:
                cnt+=A[i]-target
        if cnt<=k:
            return True
        else: return False

    def bin_search_meguru():
        """
        初期値のng,okを受け取り,is_okを満たす最小(最大)のokを返す
        まずis_okを定義すべし
        ng, okは,とり得る最小の値-1,とり得る最大の値+1
        """
    
        
        ng = -1  
        ok = A[-1]*F[0]+1  
    
        
        
        while (abs(ok - ng) > 1):
            mid = (ok + ng) // 2
            if is_ok(mid):
                ok = mid
            else:
                ng = mid
        return ok
    
    val=bin_search_meguru()
    print(val)



    
    
resolve()

Solution (Python):
N,K=list(map(int,input().split()))
A=sorted(list(map(int,input().split())))
F=sorted(list(map(int,input().split())),reverse=True)

ok=10**12
ng=0

while abs(ok-ng)>0:
    center=(ok+ng)//2
    cnt=0

    for i in range(N):
        ap=center//F[i]
        cnt+=max(0,A[i]-ap)
        if K<cnt:
            ng=center+1
            break
    else:
        ok=center

print(ok)

Solution (Python):
def check(T, K, A, F):
    
    counts = [max(0, (a * f - T + f - 1) // f) for a, f in zip(A, F)]  
    return sum(counts) <= K


def main():
    N, K = list(map(int, input().split(' ')))
    A = list(map(int, input().split(' ')))
    A.sort()
    F = list(map(int, input().split(' ')))
    F.sort(reverse=True)
    ok = 10 ** 12
    ng = - 1
    while ok - ng > 1:
        mid = (ok + ng) // 2
        if check(mid, K, A, F):
            ok = mid
        else:
            ng = mid
    print(ok)


if __name__ == '__main__':
    main()

Solution (Python):
import sys

read = sys.stdin.buffer.read


def main():
    N, K, *AF = map(int, read().split())
    A = AF[:N]
    F = AF[N:]

    A.sort()
    F.sort(reverse=True)

    ok = pow(10, 12)
    ng = -1
    while ok - ng > 1:
        mid = (ok + ng) // 2

        k = 0
        for i in range(N):
            if A[i] * F[i] > mid:
                k += A[i] - mid // F[i]

        if k <= K:
            ok = mid
        else:
            ng = mid

    print(ok)
    return


if __name__ == '__main__':
    main()

Solution (Python):
import sys

read = sys.stdin.buffer.read


def main():
    N, K, *AF = map(int, read().split())
    A = AF[:N]
    F = AF[N:]

    A.sort()
    F.sort(reverse=True)

    def is_ok(cost):
        k = 0
        for i in range(N):
            if A[i] * F[i] > cost:
                k += A[i] - cost // F[i]
        return k <= K

    ok = pow(10, 12)
    ng = -1
    while ok - ng > 1:
        mid = (ok + ng) // 2
        if is_ok(mid):
            ok = mid
        else:
            ng = mid

    print(ok)
    return


if __name__ == '__main__':
    main()

Solution (Python):
import sys

read = sys.stdin.buffer.read


def main():
    N, K, *AF = map(int, read().split())
    A = AF[:N]
    F = AF[N:]

    A.sort()
    F.sort(reverse=True)

    def is_ok(cost):
        k = 0
        for i in range(N):
            if A[i] * F[i] > cost:
                k += (A[i] * F[i] - cost + F[i] - 1) // F[i]
        return k <= K

    ok = pow(10, 12)
    ng = -1
    while ok - ng > 1:
        mid = (ok + ng) // 2
        if is_ok(mid):
            ok = mid
        else:
            ng = mid

    print(ok)
    return


if __name__ == '__main__':
    main()

Solution (Python):
import sys

read = sys.stdin.buffer.read


def main():
    N, K, *AF = map(int, read().split())
    A = AF[:N]
    F = AF[N:]

    A.sort()
    F.sort(reverse=True)
    C = [a * f for a, f in zip(A, F)]

    def is_ok(cost):
        k = 0
        for i in range(N):
            if C[i] > cost:
                k += (C[i] - cost + F[i] - 1) // F[i]
        return k <= K

    ok = pow(10, 12)
    ng = -1
    while ok - ng > 1:
        mid = (ok + ng) // 2
        if is_ok(mid):
            ok = mid
        else:
            ng = mid

    print(ok)
    return


if __name__ == '__main__':
    main()

Solution (Python):
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, K, *AF = map(int, read().split())
    A = AF[:N]
    F = AF[N:]

    A.sort()
    F.sort(reverse=True)
    C = [a * f for a, f in zip(A, F)]

    def is_ok(cost):
        k = 0
        for i in range(N):
            if C[i] > cost:
                k += (C[i] - cost + F[i] - 1) // F[i]
        return k <= K

    ok = max(C)
    ng = -1
    while ok - ng > 1:
        mid = (ok + ng) // 2
        if is_ok(mid):
            ok = mid
        else:
            ng = mid

    print(ok)
    return


if __name__ == '__main__':
    main()

Solution (Python):
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, K, *AF = map(int, read().split())
    A = AF[:N]
    F = AF[N:]

    A.sort()
    F.sort(reverse=True)
    C = [a * f for a, f in zip(A, F)]

    def is_ok(cost):
        k = 0
        for c, f in zip(C, F):
            if c > cost:
                k += (c - cost + f - 1) // f
        return k <= K

    ok = max(C)
    ng = -1
    while ok - ng > 1:
        mid = (ok + ng) // 2
        if is_ok(mid):
            ok = mid
        else:
            ng = mid

    print(ok)
    return


if __name__ == '__main__':
    main()

Solution (Python):
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, K, *AF = map(int, read().split())
    A = AF[:N]
    F = AF[N:]

    A.sort()
    F.sort(reverse=True)

    def is_ok(cost):
        k = 0
        for a, f in zip(A, F):
            if a * f > cost:
                k += (a * f - cost + f - 1) // f
        return k <= K

    ok = max(a * f for a, f in zip(A, F))
    ng = -1
    while ok - ng > 1:
        mid = (ok + ng) // 2
        if is_ok(mid):
            ok = mid
        else:
            ng = mid

    print(ok)
    return


if __name__ == '__main__':
    main()

Solution (Python):
n, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
a.sort()
b.sort(reverse=True)

high = 10**18+1
low = -1

while high-low>1:
    res = k
    mid = (low+high)//2
    for i in range(n):
        res -= max(0, a[i]-(mid//b[i]))
    if res < 0: low = mid
    else: high = mid
print(high)

Solution (Python):
import sys
import heapq

sys.setrecursionlimit(10 ** 8)
ini = lambda: int(sys.stdin.readline())
inm = lambda: map(int, sys.stdin.readline().split())
inl = lambda: list(inm())
ins = lambda: sys.stdin.readline().rstrip()
debug = lambda *a, **kw: print("\033[33m", *a, "\033[0m", **dict(file=sys.stderr, **kw))

N, K = inm()
A = inl()
F = inl()
A.sort()
F.sort(reverse=True)


def check(val):
    k = K
    for i in range(N):
        tmp = A[i] * F[i]
        if tmp <= val:
            continue
        dec = ((tmp - val) + F[i] - 1) // F[i]
        assert A[i] >= dec
        assert (A[i] - dec) * F[i] <= val
        k -= dec
        if k < 0:
            return False
    return True


def solve():
    ng, ok = -1, max([A[i] * F[i] for i in range(N)])
    while ok - ng > 1:
        mid = (ok + ng) // 2
        if check(mid):
            ok = mid
        else:
            ng = mid
    return ok


print(solve())

Solution (Python):
N, K = map(int, input().split())

A = list(map(int, input().split()))
F = list(map(int, input().split()))

A.sort()
F.sort(reverse = True)

def calc(x): 
    tmp = 0
    for a, f in zip(A, F):
        tmp += max(0, a - (x // f))
        if tmp > K:
            return tmp
    return tmp

ng = -1
ok = 10 ** 18
while ok - ng > 1:
    mid = (ok + ng) // 2
    if calc(mid) <= K:
        ok = mid
    else:
        ng = mid

print (ok)

Solution (Python):
N, K = map(int, input().split())

A = list(map(int, input().split()))
F = list(map(int, input().split()))

A.sort()
F.sort(reverse = True)

def calc(x): 
    tmp = 0
    for a, f in zip(A, F):
        tmp += max(0, a - (x // f))
    return tmp

ng = -1
ok = 10 ** 18
while ok - ng > 1:
    mid = (ok + ng) // 2
    if calc(mid) <= K:
        ok = mid
    else:
        ng = mid

print (ok)

Solution (Python):
n, k = map(int, input().split())
a = list(map(int, input().split()))
f = list(map(int, input().split()))
a.sort()
f.sort(reverse = True)
cost = max([a[i] * f[i] for i in range(n)])
cost2 = 0
while True:
    cost3 = (cost + cost2) // 2
    count = 0
    for i in range(n):
        count += max((a[i] * f[i] - cost3 - 1) // f[i] + 1, 0)
    if count <= k:
        cost = cost3
    else:
        cost2 = cost3 + 1
    if cost == cost2:
        break
print(cost)

Solution (Python):




import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi,factorial,sqrt
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7

def ceiler(a,b):
    return -((-a)//b)
n,k=MI()
costs=LI()
difs=LI()
costs.sort()
difs.sort(reverse=True)
lis=[0 for i in range(n)]


for i in range(n):
    lis[i]=costs[i]*difs[i]

def judge(x):
    times=0
    for i in range(n):
        u=ceiler(lis[i]-x,difs[i])
        times+=max(0,u)
        
    
    return times<=k
l,r=-1,10**13
while l+1<r:
    mid=(l+r)//2
    if judge(mid):
        r=mid
    else:
        l=mid
print(r)

Solution (Python):
N, K = map(int, input().split())
A = list(map(int, input().split()))
F = list(map(int, input().split()))

A = sorted(A)
F = sorted(F, reverse=True)

l = -1
r = 10**12+5

while (l+1<r):
  c = (l+r)//2
  tmp = 0
  for i in range(N):
    tmp += max(0, A[i]-(c//F[i]))
  
  if tmp <= K:
    r = c
  else:
    l = c

print(r)

Solution (Python):
n, k = map(int, input().split())
a_list = [int(i) for i in input().split()]
f_list = [int(i) for i in input().split()]
a_list.sort(reverse=True)
f_list.sort()

if k >= sum(a_list):
    print(0)
    exit()

point = []
for i in range(n):
    point.append(a_list[i] * f_list[i])

import math
def binary_search(point, k):
    left = 0
    right = 10 ** 12
    while left + 1< right:
        ans = 0
        center = (left + right) // 2
        for i in range(n):
            if point[i] > center:
                ans += a_list[i] - (center // f_list[i])
        if ans <= k:
            right = center
        else:
            left = center
    return left
    
value = binary_search(point, k)
print(value + 1)

Solution (Python):
n, k = map(int, input().split())
a_list = [int(i) for i in input().split()]
f_list = [int(i) for i in input().split()]
a_list.sort(reverse=True)
f_list.sort()

if k >= sum(a_list):
    print(0)
    exit()

point = []
for i in range(n):
    point.append(a_list[i] * f_list[i])

import math
def binary_search(point, k):
    left = 0
    right = 10 ** 12
    while left + 1< right:
        ans = 0
        center = (left + right) // 2
        for i in range(n):
            if point[i] > center:
                ans += a_list[i] - (center // f_list[i])
        if ans <= k:
            right = center
        else:
            left = center
    return right    
    
value = binary_search(point, k)
print(value)

Solution (Python):
import sys
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))  
def S(): return sys.stdin.readline().rstrip()

N,K = map(int,S().split())
A = LI()
F = LI()
A = sorted(A)
F = sorted(F,reverse=True)

if sum(A) <= K:
    print(0)
    exit()

left = 0
right = 10**12
while left + 1 < right:
    mid = (left + right) // 2
    if sum(max(0,A[i]-mid//F[i]) for i in range(N)) <= K:
        right = mid
    else:
        left = mid

print(right)

Solution (Python):
import math

N, K=map(int, input().split())
A=list(map(int, input().split()))
F=list(map(int, input().split()))
A=sorted(A, reverse=False)
F=sorted(F, reverse=True)

a=-1
b=max([A[i]*F[-i-1] for i in range(N)])
while b-a>1:
  tmp=(a+b)//2
  c=sum([max(0, math.ceil(A[i]-tmp/F[i])) for i in range(N)])
  if c<=K:
    b=tmp
  else:
    a=tmp
print(b)

Solution (Python):
















n, k = map(int, input().split())
people = list(map(int, input().split()))
foods = list(map(int, input().split()))


foods.sort()
people.sort(reverse=True)

def is_valid(time):
    

    n_train = 0
    for i in range(n):
        if foods[i] * people[i] <= time:
            pass
        else:
            n_train += people[i] - time // foods[i]
    
    return n_train <= k


time_ok = 2 * 10 ** 12
time_ng = -1  

while abs(time_ok - time_ng) > 1:
    mid = (time_ok + time_ng) // 2
    if is_valid(mid):
        time_ok = mid
    else:
        time_ng = mid

print(time_ok)

Solution (Python):













n, k = map(int, input().split())
people = list(map(int, input().split()))
foods = list(map(int, input().split()))


foods.sort()
people.sort(reverse=True)

def is_valid(time):
    

    n_train = 0
    for i in range(n):
        if foods[i] * people[i] <= time:
            pass
        else:
            n_train += people[i] - time // foods[i]
    
    return n_train <= k


time_ok = 2 * 10 ** 12
time_ng = -1  

while (abs(time_ok - time_ng > 1)):
    mid = (time_ok + time_ng) // 2
    if is_valid(mid):
        time_ok = mid
    else:
        time_ng = mid

print(time_ok)

Solution (Python):
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N, K = mapint()
As = sorted(list(mapint()))
Fs = sorted(list(mapint()))[::-1]
from math import ceil

L, R = -1, 10**12
while L+1<R:
    half = (L+R)//2
    k = 0
    for a, f in zip(As, Fs):
        k += max(0, ceil(((a*f)-half)/f))
        if k>K:
            L = half
            break
    else:
        R = half

print(R)

Solution (Python):
import sys
import os,io
input=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
n,k=map(int,input().split())
a=list(map(int,input().split()))
f=list(map(int,input().split()))
a.sort()
f.sort()
f.reverse()
if sum(a)<=k:
  print(0)
  sys.exit()
pointer=0
l=0
r=0
for i in range(n):
  r=max(r,a[i]*f[i])
while l+1<r:
  try1=(r+l)//2
  required=0
  for i in range(n):
    required+=(max(0,a[i]-try1//f[i]))
  if required>k:
    l=try1
  else:
    r=try1
required=0
for i in range(n):
  required+=(max(0,a[i]-l//f[i]))
if required>k:
  print(r)
else:
  print(l)

Solution (Python):
import sys
readline = sys.stdin.readline

import numpy as np

N,K = map(int,readline().split())
A = np.sort(np.array(readline().split(), dtype = int))
F = np.sort(np.array(readline().split(), dtype = int))[::-1]



ng = -1
ok = F[0] * A[-1]

def isOk(x):
  return A.sum() - np.minimum(A, x // F).sum() <= K
  
while abs(ok - ng) > 1:
  mid = abs(ok + ng) // 2
  if isOk(mid):
    ok = mid
  else:
    ng = mid
    
print(ok)

Solution (Python):
n, k = map(int, input().split())
a = sorted(map(int, input().split()))
f = sorted(map(int, input().split()), reverse=True)

lo, hi = -1, 10 ** 18 + 1
while hi - lo > 1:
    mid = (lo + hi) // 2
    if sum(max(0, a[i] - mid // f[i]) for i in range(n)) <= k:
        hi = mid
    else:
        lo = mid

print(hi)