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



Sample input:
3 5
4 2 1
2 3 1


Sample output:
2

Submission status: Accepted

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys


def get_an(s,t):
  # sをtの倍数になるよう切り上げる
  return s//t

## input
N,K = map(int, input().split())
An = list(map(int, sys.stdin.readline().rstrip().split()))
An = sorted(An)
Fn = list(map(int, sys.stdin.readline().rstrip().split()))
Fn = sorted(Fn,reverse=True)
MX = An[0]*Fn[0]
for i in range(N):
  MX = max(MX,An[i]*Fn[i])

def check(ret):
  k = K
  for i in range(N):
    if An[i]*Fn[i] <= ret:
      continue
    else:
     k -= An[i]-get_an(ret,Fn[i])
  if k < 0:
    return False
  else:
    return True

l,r = 0,MX
while r-l > 1:
  c = (l+r)//2
  if check(c):
    r = c
  else:
    l = c
if check(l):
  print(l)
else:
  print(r)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10000000)
import heapq
import math
 
def main():
    N, K = map(int, input().split())
    A = [int(c) for c in input().split()]
    F = [int(c) for c in input().split()]
    
    A.sort()
    F.sort(reverse=True)
 
    num = 0
    sum_ = []
    for i in range(N):
      sum_.append(A[i] * F[i])
    left = 0
    right = 10 ** 12
    while(num < 90):
        number = 0
        mid = (left + right) // 2
        # print(mid)
        for i in range(len(A)):
            if sum_[i] <= mid:
                continue
            else:
                number += math.ceil((sum_[i] - mid) / F[i])
            # print(number)
        if number > K:
            # 最大値をmidより小さくできない -> 最大値はもっとでかい
            left = mid + 1
        else:
            right = mid
        num += 1
    print(mid)
                
        
 
 
 
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#"""
import sys
input = sys.stdin.readline
#"""

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

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

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

l = -1
r = 10**13
while l+1<r:
    i = (l+r)//2
    if check(i):
        r = i
    else:
        l = i

print(r)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
A = sorted([int(i) for i in input().split()])
F = sorted([int(i) for i in input().split()], reverse=True)

if sum(A) <= K:
    print(0)
    exit()
else:
    OK = 10**12
    NG = 0

    while OK-NG > 1:
        mid = (OK+NG)//2
        cnt = 0

        for i, a in enumerate(A):
            cnt += max(0, a-mid//F[i])
        if cnt <= K:
            OK = mid
        else:
            NG = mid

print(OK)

Submission status: Accepted

Solution code:
from heapq import heappop, heappush
N, K = map(int, input().split())
A = list(map(int, input().split()))
F = list(map(int, input().split()))

def ok(x):
    cnt = 0
    for i in range(N):
        current = A[i] * F[i]
        if current > x:
            cnt += -(-(current - x) // F[i])
            
    return cnt <= K

A.sort()
F.sort(reverse=True)
lb = 0
ub = 10 ** 12 + 1

while ub - lb > 1:
    mid = (lb + ub) // 2
    
    if ok(mid):
        ub = mid
    else:
        lb = mid
        
if sum(A) <= K:
    print(0)
else:
    print(ub)

Submission status: Accepted

Solution code:
import sys

def solve():
    input = sys.stdin.readline
    N, K = map(int, input().split())
    A = [int(a) for a in input().split()]
    F = [int(f) for f in input().split()]
    A.sort(reverse = True)
    F.sort()
    low, high = -1, 10 ** 12 + 1
    while high - low > 1:
        mid = (low + high) // 2
        count = 0
        for i in range(N):
            if A[i] * F[i] > mid: count += A[i] - (mid // F[i])
        if count > K: low = mid
        else: high = mid
    print(high)

    return 0

if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
n,k=map(int,input().split())
a=list(map(int,input().split()))
a=sorted(a)
f=list(map(int,input().split()))
f=sorted(f,reverse=True)

s=sum(a)
if k>=s:
    print(0)
    exit()

def is_ok(b):
    m=0
    for p,q in zip(a,f):
        if p>b//q:
            m+=p-(b//q)
            if m>k:
                return False
    return True

ok=10**12+1
ng=0
while ok-ng>1:
    m=(ok+ng)//2
    if is_ok(m):
        ok=m
    else:
        ng=m

print(ok)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
a_ls = list(map(int,input().split()))
f_ls = list(map(int,input().split()))

a_ls.sort(reverse=True)
f_ls.sort()

l=-1
r=a_ls[0] * f_ls[-1]
while(l+1<r):
    c=(l+r)//2
    total = 0
    for i in range(n):
        a = max(a_ls[i] * f_ls[i] - c, 0)
        b = f_ls[i]
        total += (a+b-1)//b
    if total > k:
        l=c
    else:
        r=c
print(r)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline
INF = 2**62-1

def read_int():
    return int(input())


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


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


class Bisect:
    def __init__(self, func):
        self.__func = func

    def bisect_left(self, x, lo, hi):
        while lo < hi:
            mid = (lo+hi)//2
            if self.__func(mid) < x:
                lo = mid+1
            else:
                hi = mid
        return lo

    def bisect_right(self, x, lo, hi):
        while lo < hi:
            mid = (lo+hi)//2
            if x < self.__func(mid):
                hi = mid
            else:
                lo = mid+1
        return lo

@mt
def slv(N, K, A, F):
    A.sort(reverse=True)
    F.sort()

    def possible(c):
        k = K
        for a, f in zip(A, F):
            d = c//f
            if d - a < 0:
                k += d - a
        if k >= 0:
            return 1
        return 0
    
    b = Bisect(possible)
    return b.bisect_left(1, 0, 10**18)


def main():
    N, K = read_int_n()
    A = read_int_n()
    F = read_int_n()
    print(slv(N, K, A, F))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
A = sorted([int(i) for i in input().split()])
F = sorted([int(i) for i in input().split()], reverse=True)

if sum(A) <= K:
    print(0)
    exit()
else:
    OK = 10**12
    NG = -1

    while OK-NG > 1:
        mid = (OK+NG)//2
        cnt = 0

        for i, a in enumerate(A):
            cnt += max(0, a-mid//F[i])
        if cnt <= K:
            OK = mid
        else:
            NG = mid

print(OK)

Submission status: Accepted

Solution code:
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()

def chk(x):
  ct=0
  for i in range(n):
    a,f=A[i],F[-i-1]
    ct+=max(0,a-x//f)
  if ct<=k:
    return True
  else:
    return False


l,r=0,A[-1]*F[-1]
while r-l>1:
  m=(l+r)//2
  if chk(m):
    r=m
  else:
    l=m

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

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
a.sort()
b.sort(reverse=True)
#print(a)
#print(b)
big = 44
stand = 2 ** big
width = 2 ** (big-1)
if sum(a) <= k:
  print(0)
else:
  while True:
    tmp = []
    for i in range(n):
       tmp.append(max(0,a[i]-stand//b[i]))
    if width == 0:
      if sum(tmp) <= k:
        print(stand)
      else:
        print(stand+1)
      break
    if sum(tmp) <= k:
      stand -= width
      width //= 2
    else:
      stand += width
      width //= 2
    #print(stand,tmp)

Submission status: Accepted

Solution code:
import numpy as np
N, K = map(int, input().split())
A = np.array(list(map(int, input().split())))
F = np.array(list(map(int, input().split())))
A = np.sort(A)[::-1]
F = np.sort(F)
# 二部探索法　Aの最大値をどこまで減らせるか
if A.sum() <= K:
    print(0)
    exit()
if K == 0:
    print((A * F).max())
up = (A * F).max() # 可能 mid は完食にかかる時間
low = 0 # 不可能
while up > low + 1:
    mid = (up + low) // 2
    if (A - np.minimum(A, (mid // F))).sum() <= K: #可能
        up = mid
    else:
        low = mid
print(up)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
A = [int(i) for i in input().split()]
F = [int(i) for i in input().split()]

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

ng = -1
ok = 10**12
while ok - ng > 1 :
    mid = (ok + ng) // 2
    k = 0
    for i in range(N) :
        k += max(0, A[i] - mid // F[i])
    if k <= K :
        ok = mid
    else :
        ng = mid
print(ok)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
F = list(map(int, input().split()))
A.sort()
F.sort()
F.reverse()
#print('A = {}'.format(A))
#print('F = {}'.format(F))
M = [0] * N
for i in range(N):
    M[i] = A[i] * F[i]

def Below(X, K):
    for i in range(N):
        if A[i]*F[i] <= X:
            continue
        else:
            cost = X // F[i]
            K -= (A[i] - cost)
    if K >= 0:
        return True
    else:
        return False

left = 0
right = max(M)
while left != right:
    mid = (left + right) // 2
    #print('For mid={}, {}'.format(mid, Below(mid, K)))
    if Below(mid, K):
        right = mid
    else:
        left = mid + 1

print(left)

Submission status: Accepted

Solution code:

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

import copy
A.sort(reverse = True)
DA = copy.copy(A)
F.sort()

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

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

def judge(val):
    cnt = 0
    for i in range(N):
        if (values[i] > val):
            cnt += A[i]-val//F[i]
    
    return cnt<=K

s = 0
e = max(A)*max(F)
while (s<e):
    c = (s+e)//2
    if judge(c):
        e = c-1
        
    else:
        s = c+1

while(judge(c)):
    c-=1
while (not(judge(c))):
    c+=1
print(c)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
f=list(map(int,input().split()))
a.sort()
f.sort(reverse=1)
su=ok=ng=0
for i in range(n):
  ok=max(ok,a[i]*f[i])
  su+=a[i]
if su<=k:print(0);exit()
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)

Submission status: Accepted

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

As.sort()
As.reverse()
Fs.sort()
arr = []
M = 0
for a, f in zip(As, Fs):
    af = a*f
    arr.append((af, a, f))
    M = af if af > M else M

f = -1
t = M
while f + 1 < t:
    m = f + math.ceil((t-f) / 2)
    k = K
    for AF, A, F in arr:
        d = A - m // F
        d = d if d >= 0 else 0
        if d > k:
            f = m
            break
        k -= d
    else:
        t = m

print(t)

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline  #危険！基本オフにしろ！
 
def main():
    n,k=map(int,input().split())
    a=sorted(map(int,input().split()))
    b=sorted(map(int,input().split()),reverse=1)
    #print(a,b)
    ok=max(a)*max(b)
    ng=-1
    
    def check(a,b,mid,k):
        if sum(a[i]-mid//b[i]for i in range(n) if a[i]*b[i]>mid)<=k:
            return 1
        else:
            return 0
            
    while abs(ok-ng)>1:
        mid=(ok+ng)//2
        if check(a,b,mid,k):
            ok=mid
        else:
            ng=mid
    print(ok)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import heapq
n,k=map(int,input().split())
a=list(map(int,input().split()))
f=list(map(int,input().split()))
a.sort()
f.sort()
f.reverse()
l=0;r=max(a)*max(f)
sum=sum(a)
while abs(l-r)>1:
  x=(l+r)//2
  ct=0
  for i in range(n):
    ct+=max(a[i]-(x//f[i]),0)
  if ct<=k:
    r=x
  else:
    l=x+1
x=l
ct=0
for i in range(n):
  ct+=max(a[i]-(x//f[i]),0)
if ct<=k:
  print(l)
else:
  print(r)

Submission status: Accepted

Solution code:
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):
        # 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)
        # dprint(high, low, mid, guess)

        if guess == True:
            # できるので、これより1少ないのが最大
            high = mid
        else:
            # できないので、これより大きい
            low = mid + 1
        # dprint(high, low)
    print(low)


solve()

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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:
	# print(thmin,thmax)
	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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
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)

Submission status: Accepted

Solution code:
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 = 18
ans = max_time // 2
l, r = 0, max_time
#print(l,ans,r)
while r - l > 1:
    counta = 0
    for i in range(N):
        if A[i] * F[i] > ans:
            counta += (A[i] - ans // F[i])
#        print(counta)
    if counta <= K:
        ans, r = (l + ans)//2, ans
    else:
        ans, l = (ans + r)//2, ans
#    print(l,ans,r)

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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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
#print (amari)

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

while True:

    mid = (l + r) // 2

    mm = wantmin(mid)
    #print (l,mid,r,mm)

    #if mm == amari:
     #   print (mid)
      #  sys.exit()

    if mm < amari:
        l = mid
        lm = mm

    else:
        r = mid
        rm = mm

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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

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

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

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)

Submission status: Accepted

Solution code:
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]
    # print(aa)
    # print(ff)
    while l + 1 < r:
        # print(l,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()

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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):
    # 全員がx秒以下に収めるための必要コストがK以下である
    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)

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
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
        # print(h[i:])
        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
            # print("mid: {}, in ({}, {})".format(mid, ng, ok))
            if isOK(mid):
                ok = mid
            else:
                ng = mid
        return ng, ok

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

    # while k > 0:
    #     p1, a1, f1 = heapq.heappop(h)
    #     p2, a2, f2 = heapq.heappop(h)
    #     p1, p2 = -p1, -p2
    #     sub = min(a1 - math.ceil((p2/f1)-1), k)
    #     a1 -= sub
    #     k -= sub
    #     heapq.heappush(h, (-a1*f1, a1, f1))
    #     heapq.heappush(h, (-a2*f2, a2, f2))
    # # print(h)
    # p, a, f = heapq.heappop(h)
    # print(-p)

    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))  # type: int
    K = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    F = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, K, A, F)


if __name__ == '__main__':
    main()