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
sys.setrecursionlimit(10**7)
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return sys.stdin.readline().strip()
INF = 10 ** 18
MOD = 10 ** 9 + 7

debug = True
debug = False

def dprint(*objects):
    if debug == True:
        print(*objects)

def solve():
    N, K = LI()
    a_list = LI()
    f_list = LI()

    

    low = 0
    maxa = max(a_list)
    maxf = max(f_list)
    high = (maxa * maxf * N) + 1

    
    a_list = sorted(a_list)
    f_list = sorted(f_list, reverse=True)
    import math

    dprint(a_list)
    dprint(f_list)

    def judge(ans):
        
        cnt = 0
        for i in range(N):
            a = a_list[i]
            f = f_list[i]
            if a*f > ans:
                
                ki = math.ceil((a*f - ans) / f)
                cnt += ki
                if cnt > K:
                    dprint('False', ans, cnt)
                    return False
        dprint('True', ans, cnt)
        return True

    while low < high:
        mid = math.floor((low + high) / 2)

        guess = judge(mid)
        

        if guess == True:
            
            high = mid
        else:
            
            low = mid + 1
        
    print(low)


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)
ok = max(a)*max(f)
ng = -1

while ok-ng > 1:
    mid = (ok+ng)//2
    t = 0
    for i in range(n):
        t += max(0,a[i]-int(mid/f[i]))
    if t > k:
        ng = mid
    else:
        ok = mid

print(ok)

Solution (Python):
def main():
    n,k=map(int,input().split())
    a=sorted(list(map(int,input().split())))
    f=sorted(list(map(int,input().split())),reverse=True)
    def b_search(a,b,value,lim):
        med=(a+b)//2
        if a==b:return a
        elif lim=="upper":
            if a+1==b:
                if value(b):return b
                else:return a
            elif value(med):return b_search(med,b,value,lim)
            else:return b_search(a,med-1,value,lim)
        else:
            if a+1==b:
                if value(a):return a
                else:return b
            elif value(med):return b_search(a,med,value,lim)
            else:return b_search(med+1,b,value,lim)

    def count(val):
        cnt=0
        for i in range(n):
            cnt+=max(a[i]-val//f[i],0)
        if cnt>k:
            return False
        else:
            return True
    
    print(b_search(0,10**12+10,count,"lower"))
    
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()
ok=a[-1]*f[-1]
ng=0
if sum(a)<=k:print(0);exit()
ma=ok
while ng+1!=ok:
  mid=(ok+ng)//2
  b=[mid//i for i in f]
  b.sort()
  h=0
  for i in range(n):h+=max(0,a[i]-b[i])
  if h<=k:ok=mid
  else:ng=mid
print(ok)

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor, atan2, degrees
from operator import mul
from functools import reduce
sys.setrecursionlimit(10**8)

INF = float('inf')
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n, k = LI()
A = sorted(LI())
F = sorted(LI(), reverse=True)
ok = A[-1] * F[0]
ng = -1
while ok > ng + 1:
    mid = (ok + ng) // 2
    ret = 0
    for i in range(n):
        if A[i] * F[i] > mid:
            ret += (A[i] * F[i] - mid) // F[i] + bool((A[i] * F[i] - mid) % F[i])
    if ret > k:
        ng = mid
    else:
        ok = mid


print(ok)

Solution (Python):
N,K=map(int,input().split())
A,F=[list(map(int,input().split())) for i in range(2)]
A.sort()
F.sort()
l,r=-1,10**18
while r-l>1:
    h=(l+r)//2
    k=0
    for i in range(N):
        k+=max(0,A[i]-h//F[-i-1])
        if k>K:
            k=-1
            break
    if k>=0:
        r=h
    else:
        l=h
print(r)

Solution (Python):
import sys
import math

input = sys.stdin.readline

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

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

cost = []
for ai, fi in zip(A,F):
	cost.append([ai*fi,fi])
cost.sort(reverse=True)

thmin = -1
thmax = cost[0][0]+1

while thmax-thmin!=1:
	
	th = (thmin+thmax)//2
	ktmp = 0
	ind = 0
	judge = True
	while True:
		ci, fi = cost[ind]
		if ci<=th: break
		ktmp += math.ceil((ci-th)/fi)
		if ktmp>K:
			judge = False
			break
		ind += 1
		if ind==N: break
	if judge:
		thmax = th
	else:
		thmin = th

print(thmax)

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

def cnt(ans):
    rt=0
    for i in range(n):
        rt+=max(0,a[i]-int(ans//f[i]))
    return rt

lans,rans=0,max(a[i]*f[i] for i in range(n))
while 1:
    if rans-lans<=1:
        break
    mans=(rans+lans+1)//2
    if cnt(mans)<=k:
        rans=mans
    else:
        lans=mans
if rans==lans:
    print(rans)
else:
    if cnt(lans)>k:
        print(rans)
    else:
        print(lans)

Solution (Python):

n, k = map(int, input().split())
a = sorted(map(int, input().split()))[::-1]
f = sorted(map(int, input().split()))


def check(x):
    res = 0
    for i in range(n):
        res += (max(0, a[i] * f[i] - x) + f[i] - 1) // f[i]
    return res


ok = 10**12
ng = -1
while ok - ng > 1:
    m = (ok + ng) // 2
    res = check(m)
    if res <= k:
        ok = m
    else:
        ng = m

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)

max_time = 0
for i in range(N):
    if A[i]*F[i] > max_time:
        max_time = A[i]*F[i]


ans = max_time // 2
l, r = 0, max_time

while r - l > 1:
    counta = 0
    for i in range(N):
        if A[i] * F[i] > ans:
            counta += (A[i] - ans // F[i])

    if counta <= K:
        ans, r = (l + ans)//2, ans
    else:
        ans, l = (ans + r)//2, ans


ans = l
counta = 0
for i in range(N):
    if A[i] * F[i] > ans:
        counta += (A[i] - ans // F[i])
if counta > K:
    print(r)
else:
    print(ans)

Solution (Python):
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N,num):
    if N<=0:
        return [[]]*num
    elif num==1:
        return [I() for _ in range(N)]
    else:
        read_all = [tuple(II()) for _ in range(N)]
        return map(list, zip(*read_all))



import heapq
class Reverse:
    def __init__(self, val):
        self.val = val
        
    def __lt__(self, other):
        return self.val > other.val
        
    def __repr__(self):
        return repr(self.val)
    
class Heapq:
    def __init__(self, arr, desc = False):
        if desc:
            for i in range(len(arr)):
                arr[i] = Reverse(arr[i])
        self.desc = desc
        self.hq = arr
        heapq.heapify(self.hq)
 
    def pop(self):
        if self.desc: return heapq.heappop(self.hq).val
        else: return heapq.heappop(self.hq)
 
    def push(self, a):
        if self.desc: heapq.heappush(self.hq, Reverse(a))
        else: heapq.heappush(self.hq, a)
 
    def top(self):
        if self.desc: return self.hq[0].val
        else: return self.hq[0]

N,K = II()
A = III()
F = III()

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

ng = -1
ok = 10**12

while (abs(ok-ng) > 1):
    mid = (ok+ng)//2
    num = 0
    for i in range(N):
        a = math.floor(mid/F[i])
        if A[i]>a:
            num += A[i]-a
    if num<=K:
        ok = mid
    else:
        ng = mid

print(ok)

Solution (Python):
import sys

def wantmin(n):

    ret = 0

    for i in range(N):
        if n > A[i] * F[i]:
            ret += A[i]

        else:
            ret += n // F[i]

    return ret

    

N,K = map(int,input().split())

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

M = 0


S = 0
for i in range(N):
    M = max(M,A[i]*F[i])
    S += A[i]

A.sort()
F.sort()
F.reverse()

l = 0
r = M+1
lm = wantmin(l)
rm = wantmin(r)
mm = 0
amari = S - K


if amari <= 0:
    print (0)
    sys.exit()

while True:

    mid = (l + r) // 2

    mm = wantmin(mid)
    

    
     
      

    if mm < amari:
        l = mid
        lm = mm

    else:
        r = mid
        rm = mm

    if r - l == 1:
        print (r)
        sys.exit()

Solution (Python):
N,K = map(int,input().split())
A = [int(a) for a in input().split()]
F = [int(f) for f in input().split()]

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

isOK = max([a*f for a,f in zip(A,F)])
isNG = -1

while(isOK-isNG>1):
    mid = (isOK+isNG)//2
    cnt = 0
    for i in range(N):
        if A[i] * F[i] > mid:
            tmp = (mid//F[i])+2
            while(tmp*F[i] > mid):
                tmp -= 1
            cnt += A[i]-tmp

    if cnt>K:
        isNG = mid
    else:
        isOK = mid
print(isOK)

Solution (Python):
import math

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 = []
R = 0
for i in range(N):
    cost.append([A[i]*F[i],F[i]])
    R = max(R,A[i]*F[i])

def func(l,r):
    if l == r:
        return l
    t = (l+r)//2
    count = 0
    for a,b in cost:
        count += max(0,math.ceil((a-t)/b))
    if count <= K:
        return func(l,t)
    else:
        return func(t+1,r)

print(func(0,R))

Solution (Python):
import math
import random
from operator import itemgetter
import re
import sys
from itertools import accumulate
from collections import defaultdict
from collections import deque
from bisect import bisect_left,bisect
from heapq import heappop,heappush
from fractions import gcd
from copy import deepcopy
input = sys.stdin.readline
N,K = map(int,input().split())
A = [int(i) for i in input().split()]
F = [int(i) for i in input().split()]
A.sort()
F.sort(reverse=True)
maxi = 0
for i in range(N):
    maxi = max(maxi,A[i]*F[i])
start=0
end = maxi
while True:
    count = 0
    value = (start+end)//2
    for i in range(N):
        count += max(0,A[i]-value//F[i])
    if count > K :
        if 2 > end-start:
            value +=1
            break
        start = value
    else:
        if 2 > end-start:
            break
        end = value
print(value)

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 ok(v):
    return sum([max(0, (a[i] * f[i] - v + (f[i] - 1)) // f[i]) for i in range(n)]) <= k


if ok(0):
    print(0)
    exit()
mi = 0
ma = 10 ** 12
while mi+1 < ma:
    m = (mi + ma) // 2
    if ok(m):
        ma = m
    else:
        mi = m
print(ma)

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

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



B = [f*a for f,a in zip(F,A)]
mn = 0
mx = max(B)

ans = mx
m = (mn+mx)//2
while True:
    flag = True 
    k = 0
    for i in range(N):
        if B[i] >= m:
            k += -(-(B[i] - m) // F[i])
            if k > K:
                flag = False
                break
    if flag:
        if m == mx:
            break
        ans = m
        mx = m
        m = (mn+mx)//2
    else:
        if m == mn:
            break
        mn = m
        m = (mn+mx)//2
print(ans)

Solution (Python):
import math

N, K = [int(x) for x in input().split()]
As = sorted([int(x) for x in input().split()])
Fs = sorted([int(x) for x in input().split()], reverse=True)

def in_n_sec(n):
  total_training = sum(max(0, a-int(n/f)) for a, f in zip(As, Fs))
  return total_training <= K
  
current_worst_confirmed = 10**12
current_best_possible = 0
while current_worst_confirmed > current_best_possible:
  to_try = (current_worst_confirmed + current_best_possible ) // 2
  if in_n_sec(to_try):
    current_worst_confirmed = to_try
  else:
    current_best_possible = to_try + 1
print(current_worst_confirmed)

Solution (Python):
import heapq as hq
N, K = map(int, input().split())
A = list(map(int,input().split()))
A.sort()
F = list(map(int,input().split()))
F.sort()

if sum(A) <= K:
	print(0)
	exit()
ans = max([A[i]*F[N-1-i] for i in range(N)])

def achievable(score):
	cnt = 0
	for i in range(N):
		a,f = A[i], F[N-1-i]
		if a*f > score:
			n = score // f
			cnt += a-n
			if cnt > K:
				return False
	return True

l,r = 0, ans
while r - l > 1:
	p = (l + r) // 2
	if achievable(p):
		r = p
	else:
		l = p
print(r)

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline

def main():
    n, k = map(int, input().split())
    aa = list(map(int, input().split()))
    ff = list(map(int, input().split()))
    aa.sort(reverse=True)
    ff.sort()
    l = -1
    r = aa[0] * ff[-1]
    
    
    while l + 1 < r:
        
        m = (l + r) // 2
        s = 0
        for a, f in zip(aa, ff):
            b = m // f
            s += max(a - b, 0)
            if s > k:
                l = m
                break
        else:
            r = m
    print(r)

main()

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


def training(x):
    global a_list, f_list
    ret = 0
    for i in range(n):
        a = x // f_list[i]
        ret +=  max(0, a_list[i] - a)
    return ret

ans = -1

if training(0) <= k:
    ans = 0
else:
    ng = 0
    ok = 10**12
    while ok - ng > 1:

        mid = (ng+ok) // 2
        if training(mid) <= k:
            ok = mid
            mid = (ng+ok) // 2
        else:
            ng = mid
            mid = (ng+ok) // 2
    ans = ok
print(ans)

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

import numpy as np

N,K = map(int,readline().split())
A = np.array(readline().split(),np.int64)
F = np.array(readline().split(),np.int64)

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

Asum = A.sum()

def test(x):
    
    return Asum - np.minimum(A,x//F).sum() <= K

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

answer = right
print(answer)

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

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

def judge(n, a, f, x, k):
    req = 0
    for i in range(n):
        if a[i]*f[i] > x:
            req += -(-(a[i]*f[i] - x) // f[i])
    return True if req <= k else False

def binsearch(n, a, f, k):
    low = 0
    high = max(a) * max(f)
    while high - low > 1:
        mid = (low + high) // 2
        if judge(n, a, f, mid, k):
            high = mid
        else:
            low = mid

    return high

n, k = li()
a = list(li())
f = list(li())

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

if k >= sum(a):
    print(0)
else:
    print(binsearch(n, a, f, k))

Solution (Python):

import sys
INF = float("inf")
from bisect import bisect_left


def solve(N: int, K: int, A: "List[int]", F: "List[int]"):
    A.sort()
    F.sort(reverse=True)
    h = []
    for a, f in zip(A, F):
        h.append((a*f, a, f))
    h.sort()
    maxtime = h[-1][0]

    def isOK(y):
        i = bisect_left(h, (y+1, -1, -1))
        counter = 0
        
        for p, a, f in h[i:]:
            counter += a-y//f
        return counter <= K

    def binary_search(x):
        ng = -1
        ok = len(x)
        while abs(ok - ng) > 1:
            mid = (ok + ng)//2
            
            if isOK(mid):
                ok = mid
            else:
                ng = mid
        return ng, ok

    ng, ok = binary_search(range(maxtime))
    print(ok)

    
    
    
    
    
    
    
    
    
    
    
    

    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    K = int(next(tokens))  
    A = [int(next(tokens)) for _ in range(N)]  
    F = [int(next(tokens)) for _ in range(N)]  
    solve(N, K, A, F)


if __name__ == '__main__':
    main()

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

K = min(K, sum(A))
X = [[a, f] for a, f in zip(A, F)]

ok, ng = 10 ** 12 + 1, -1
while abs(ok - ng) > 1:
    m = (ok + ng) // 2
    k_cost = 0
    for a, f in X:
        if a * f <= m:
            continue
        else:
            k_cost += max(0, ceil(-(m / f - a)))

    if k_cost <= K:
        ok = m
    else:
        ng = m

print(ok)

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

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

M = 0
for i in range(N):
    M=max(F[i]*A[i],M)

def judge(n):
    score = 0
    for i in range(N):
        score += max(A[i] - n//F[i],0)
    return score<=K

def bisect(l,r):
    while r-l>1:
        if judge((l+r)//2):
            r = (l+r)//2
        else:
            l = (l+r)//2
    return r

if judge(0):
    print(0)
else:
    print(bisect(0,M))

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

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

M = 0
for i in range(N):
  M += A[i] * F[i]

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

def hersCode():
  ok = M
  ng = -1
  while abs(ok - ng) > 1:
    mid = abs(ok + ng) // 2
    if nasu(mid):
      ok = mid
    else:
      ng = mid
  return ok

print(hersCode())

Solution (Python):

import sys
input = sys.stdin.buffer.readline

INF = float("inf")

def getlist():
	return list(map(int, input().split()))


def Binary_Search(A, F, N, K):
	
	left = 0
	right = 10 ** 12
	ans = INF
	
	
	while left <= right:
		mid = (left + right) // 2
		cnt = 0
		for i in range(N):
			if A[i] * F[i] > mid:
				cnt += (A[i] - int(mid // F[i]))
		if cnt <= K:
			right = mid - 1
			ans = min(ans, mid)
		else:
			left = mid + 1

	return ans


def main():
	N, K = getlist()
	A = getlist()
	F = getlist()
	A = sorted(A)
	F = sorted(F)[::-1]
	ans = Binary_Search(A, F, N, K)
	print(ans)

if __name__ == '__main__':
	main()

Solution (Python):
import sys
from collections import Counter
input = sys.stdin.readline
sys.setrecursionlimit(100000)
def getN():
    return int(input())
def getList():
    return list(map(int, input().split()))

def check(mid, k, pairs):
    tmp = 0
    for pair in pairs:
        a,b = pair
        kari_a = mid // b
        tmp += max(a - kari_a, 0)

    if tmp <= k:
        return True
    else:
        return False


def solve():
    n, k = getList()
    cap = getList()
    foods = getList()
    cap.sort()
    foods.sort(reverse=True)
    h = []
    pairs = [(a, b) for a, b in zip(cap, foods)]
    ansmn = 0
    ansmx = 10**13
    
    
    
    while(ansmx - ansmn > 1):
        mid = (ansmn + ansmx) // 2
        res = check(mid, k, pairs)
        if res:
            ansmx = mid
        else:
            ansmn = mid


    if check(ansmn, k, pairs):
        print(ansmn)
    else:
        print(ansmx)

def main():
    solve()


if __name__ == "__main__":
    main()


"""
10 10
95 38 9 26 36 10 69 25 44 83
6 42 33 74 75 31 73 20 70 93
"""

Solution (Python):
def judge(n, k, a_list_s, f_list_s, v):
    res = 0
    
    for i in range(n):
        a_necessary = v // f_list_s[i]
        
        if a_list_s[i] > a_necessary:
            res += a_list_s[i] - a_necessary
    if res > k:
        return False
    else:
        return True


def solve_e(n, k, a_list, f_list):

    if sum(a_list) <= k:
        return 0
    
    a_list_s = sorted(a_list)
    f_list_s = sorted(f_list, reverse=True)

    left = 0
    right = 10 ** 12

    while right - left > 1:
        middle = (left + right) // 2
        if judge(n, k, a_list_s, f_list_s, middle):
            right = middle
        else:
            left = middle
    return right


def main():
    
    n, k = map(int, input().split())
    a_list = list(map(int, input().split()))
    f_list = list(map(int, input().split()))
    res = solve_e(n, k, a_list, f_list)
    print(res)


def test():
    assert solve_e(3, 5, [4, 2, 1], [2, 3, 1]) == 2
    assert solve_e(3, 8, [4, 2, 1], [2, 3, 1]) == 0
    assert solve_e(
        11, 14, [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5],
        [8, 9, 7, 9, 3, 2, 3, 8, 4, 6, 2]) == 12


if __name__ == "__main__":
    test()
    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)

H = 0
for i in range(n):
    H = max(H, a[i] * f[i])

if H == 0:
    print(0)
    exit()

H += 1
L = 0
while L < H:
    M = (L + H) // 2
    temp = k
    for i in range(n):
        if a[i] * f[i] > M:
            temp -= a[i] - (M // f[i])
            if temp < 0:
                break
    
    if temp < 0:
        L = M + 1
    else:
        H = M

print(L)

Solution (Python):
import numpy as np


def solve(n, k, aaa, fff):
    aaa = np.sort(aaa)
    fff = np.sort(fff)[::-1]

    l = 0
    r = 10 ** 12
    while l <= r:
        m = (l + r) // 2
        req = np.clip(aaa - m // fff, 0, None).sum()
        
        if req > k:
            l = m + 1
        else:
            r = m - 1
    return l


n, k = list(map(int, input().split()))
aaa = np.array(list(map(int, input().split())), dtype=np.int64)
fff = np.array(list(map(int, input().split())), dtype=np.int64)

print(solve(n, k, aaa, fff))

Solution (Python):
N, K = map(int, input().split())
A = sorted([int(a) for a in input().split()])
F = sorted([int(a) for a in input().split()])[::-1]
l = -1
r = 10**18
while r-l > 1:
    m = (l+r)//2
    if sum([max(0, A[i] - m//F[i]) for i in range(N)]) <= K:
        r = m
    else:
        l = m
print(r)

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

ori = [0] * n
for i in range(n):
    ori[i] = a[i]*f[i]

ok = max(ori)
ng = -1
mid = (ok+ng) // 2
while ok - ng > 1:
    cnt = 0
    flg = 0
    for i in range(n):
        if ori[i] > mid:
            cnt += (a[i] - mid // f[i])
        if cnt > k:
            flg = 1
            break
    if flg:
        ng = mid
    else:
        ok = mid
    mid = (ok + ng) // 2

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)
m = 0
for i in range(n):
    m += a[i] * f[i]
l, r = -1, m
while l + 1 != r:
    m = (l + r) // 2
    tmp = 0
    for i in range(n):
        tmp += max(a[i] - m // f[i], 0)
    if k < tmp:
        l = m
    else:
        r = m
print(r)

Solution (Python):
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def bisection(l,r,f,left=True,discrete=True):
    eps=1 if discrete else 10**-12
    if((not left)^f(r)): return r if left else r+1
    elif(left^f(l)): return l-1 if left else l
    while(r-l>eps):
        h=(l+r)//2 if discrete else (l+r)/2
        if((not left)^f(h)): l=h
        else: r=h
    return (l+r)/2 if not discrete else l if left else r

def resolve():
    n,k=map(int,input().split())
    A=sorted(map(int,input().split()))
    F=sorted(map(int,input().split()),reverse=1)
    def check(x):
        res=0
        for a,f in zip(A,F):
            if(a*f)<=x: continue
            res+=a-int(x/f)
        return res<=k
    print(bisection(0,10**12,check,left=False))
resolve()

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)

left = -1
right = 10**18+1


while right - left > 1:
    mid = left + (right - left) // 2


    train = k
    for i in range(n):
        req = mid // f[i]
        if req < a[i]:
            train -= a[i] - req


    if train >= 0:
        right = mid
    else:
        left = mid



print(right)

Solution (Python):
import sys
from heapq import heappop, heappush
from operator import itemgetter
from collections import deque, defaultdict, Counter
from bisect import bisect_left, bisect_right
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)
MOD = 10**9 + 7
INF = float('inf')

def sol():
    _, K = map(int, input().split())
    A = list(map(int, input().split()))
    F = list(map(int, input().split()))

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

    def isOk(t):
        cnt = 0
        for a, f in zip(A, F):
            ness = t // f
            if a <= ness:
                continue
            cnt += a - ness

        if cnt > K:
            return False
        else:
            return True

    ng = -1
    ok = max(A) * max(F) + 10

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


sol()

Solution (Python):
import math

n, k = (int(i) for i in input().split())
alist = list(int(i) for i in input().split())
flist = list(int(i) for i in input().split())

alist=sorted(alist)
flist=sorted(flist, reverse=True)
low = 0
high = 10**12
t = (low + high) // 2




while (low<high):
    kaisu=0
    for i in range(n):
        wari=math.floor(t/flist[i])
        shugyo=max(alist[i]-wari,0)
        kaisu+=shugyo
    
    
    if kaisu<k:
        high=t
    elif kaisu>k:
        low=t+1
    elif kaisu==k:
        high=t 
    if low<high:
        t=(low+high)//2
    
print(low)

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

As.sort()
Fs.sort(reverse=True)
maxAF = max([A*F for A, F in zip(As, Fs)])

def isOK(X):
    rest = K
    for A, F in zip(As, Fs):
        if A*F > X:
            A2 = X//F
            d = A-A2
            if d > rest:
                return False
            rest -= d
    return True

ng, ok = -1, maxAF
while abs(ok-ng) > 1:
    mid = (ng+ok) // 2
    if isOK(mid):
        ok = mid
    else:
        ng = mid

print(ok)

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

import numpy as np

N,K = map(int,readline().split())
A = np.array(readline().split(),np.int64)
F = np.array(readline().split(),np.int64)

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

def test(x):
    
    return np.maximum(0,A-x//F).sum() <= K

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

answer = right
print(answer)

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)

mx = max(a)*max(f)
mn = -1
while mx-mn>1:
    mid = (mx+mn)//2
    shugyo = 0
    for i in range(n):
        a_ = mid//f[i]
        shugyo += max(a[i]-a_,0)
    if shugyo<=k:
        mx = mid
    else:
        mn = mid

print(mx)

Solution (Python):
from math import ceil

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 check(m):
    used = 0
    for i in range(n):
        b = ceil(a[i]-m/f[i])
        if b < 0:
            b = 0
        if a[i]-b < 0:
            return False
        used += b
        if used > k:
            return False
    return True

left = -1
right = max(a)*max(f)
while right-left > 1:
    mid = (right+left)//2
    if check(mid):
        right = mid
    else:
        left = mid
print(right)

Solution (Python):

import sys
def input():
	return sys.stdin.readline().strip()

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

right = F[-1]*A[0]
left = 0

while left < right:
	mid = (left + right) // 2

	total = 0
	for i in range(N):
		if F[i]*A[i] > mid:
			add = F[i]*A[i] - mid
			if add % F[i] == 0:
				total += add//F[i]
			else:
				total += add//F[i] + 1
	
	if total <= K:
		judge = 1
	else:
		judge = 0

	if judge == 1: 
		right = mid
	else: 
		left = mid + 1
		mid += 1


print(mid)