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: Time Limit Exceeded

Solution code:
import heapq
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, a, f] for a, f in zip(A, F)]

heapq.heapify(X)
cnt = 0
while K:
    cost, a, f = heapq.heappop(X)

    if a == 0:
        heapq.heappush(X, [-a * f, new_a, f])
        continue

    new_a = a - 1
    heapq.heappush(X, [-new_a * f, new_a, f])
    K -= 1

X.sort()
print(-X[0][0])

Submission status: Time Limit Exceeded

Solution code:
import heapq
N,K=[int(s) for s in input().split()]
ls1=[int(s) for s in input().split()]
ls2=[int(s) for s in input().split()]
ls1.sort()
ls2.sort()
cost=[]

ls3=[ls1[i]*ls2[N-1-i] for i in range(N)]

for i in range(N):
  heapq.heappush(cost,(-ls1[i]*ls2[N-1-i],ls1[i],ls2[N-i-1]))
for i in range(K):
  xy, x, y = heapq.heappop(cost)
  if x==0:
    heapq.heappush(cost,(-1*x*y,x,y))
    break
  else:
    heapq.heappush(cost,(-1*(x-1)*y,x-1,y))
xy,x,y=heapq.heappop(cost)
print(-1*xy)

Submission status: Time Limit Exceeded

Solution code:
import heapq

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

pq = []
for i in range(n):
    heapq.heappush(pq, (-a[i]*f[i], a[i], f[i]))

for i in range(k):
    score, ai, fi = heapq.heappop(pq)
    if score == 0:
        heapq.heappush(pq, (0, 0, 0))
        break
    heapq.heappush(pq, (-((ai-1) * fi), ai-1, fi))

print(-heapq.heappop(pq)[0])

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin


def input():
    return stdin.readline()[:-1]


def intput():
    return int(input())


def sinput():
    return input().split()


def intsput():
    return map(int, sinput())


n, k = intsput()

a = list(intsput())
f = list(intsput())

a.sort()
f.sort(reverse=True)
p = [a[i] * f[i] for i in range(n)]
scrap = [0] * n

start = 0
end = max(p)
target = (start + end) // 2
while start != end:
    cnt = 0
    for i in range(n):
        fix = 0
        if p[i] > target:
            fix = (p[i] - target + f[i] - 1) // f[i]
            cnt += fix
        #scrap[i] = a[i] - fix
        if cnt > k:
            start = target + 1
            target = (start + end) // 2
            break
    else:
        end = target
        target = (start + end) // 2

print(start)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

A = np.array(input().split(), int)
F = np.array(input().split(), int)

A = np.sort(A)
F = np.sort(F)[::-1]
cost = A * F

for i in range(K):
  t = np.argmax(cost)
  cost[t] -= F[t]

print(max(0,max(cost)))

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(4100000)
import heapq
n, k = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
f = [int(i) for i in input().split()]
a.sort()
f.sort(reverse=True)

if sum(a) <= k:
    print(0)
else:
    heap = []
    for i in range(n):
        heapq.heappush(heap, (-a[i]*f[i], a[i], f[i]))
    for i in range(k):
        cur,x,y = heapq.heappop(heap)
        x -= 1
        cur = x*y
        heapq.heappush(heap,(-cur,x,y))
    ans = -float('inf')
    for i in range(len(heap)):
        ans = max(-heap[i][0], ans)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int, input().split())
As = list(map(int, input().split()))
Fs = list(map(int, input().split()))
As.sort()
Fs.sort(reverse=True)
def check(x):
  k = 0
  for i in range(N):
    k += max(0, As[i]-x//Fs[i])
  return k <= K
a = 0
b = As[-1]*Fs[0]
while a < b:
  i = (a + b)//2
  if check(i):
    b = i
  else:
    if a == i:
      break
    a = i
r = b
print(r)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
import heapq as hp

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

A.sort()
l = -1
r = max(A)*max(F)+1
while r-l > 1:
    m = (r+l)//2
    q = []
    for f in F:
        hp.heappush(q, m//f)
    cost = 0
    for a in A:
        f = hp.heappop(q)
        cost += max(0, a-f)
    if cost <= K:
        r = m
    else:
        l = m

print(r)

Submission status: Time Limit Exceeded

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)

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

z = Heapq(z,True)
num = 0
for i in range(K):
    if num<N:
        z1 = z.pop()
        if z1[1] > 1:
            z.push(((z1[1]-1)*z1[2],z1[1]-1,z1[2]))
        else:
            num += 1
if num<N:
    print(z.top()[0])
else:
    print(0)

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
F = list(map(int, input().split()))
A.sort(reverse=True)
F.sort()
ok = 10**12
ng = -1
while ng+1 < ok:
    c = (ok+ng) // 2
    k = K
    for a, f in zip(A, F):
        k -= max(0, a - c//f)
    if k >= 0:
        ok = c
    else:
        ng = c
print(ok)

Submission status: Time Limit Exceeded

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

import heapq
pop = heapq.heappop
push = heapq.heappush
q  = []

for i in range(N):
  push(q, [- A[i] * F[i], A[i], F[i]])

for i in range(K):
  x, y, z = pop(q)
  if y == 0:
    print(0)
    exit()
  push(q, [- (y - 1) * z, y - 1, z])

t = pop(q)
print(-t[0])

Submission status: Time Limit Exceeded

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: Time Limit Exceeded

Solution code:
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 possible(x):
    k = K
    for i in range(N):
        if A[i]*F[i]>x:
            k -= ceil(A[i]-x/F[i])
        if k < 0:
            break
    return k >= 0


l = 0
r = 10**12
for i in range(100):
    m = (l+r)/2
    if possible(m):
        r = m
    else:
        l = m

print(round(m))

Submission status: Time Limit Exceeded

Solution code:
import math

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

A_list = list(map(int, input().strip().split()))
F_list = list(map(int, input().strip().split()))

A_list.sort()
F_list.sort()
F_list.reverse()

def check(score):
	k = K
	for a, f in zip(A_list, F_list):
		if a * f > score:
			t = math.floor(score / f)
			k -= a - t
			if k < 0:
				return False
	return True

score_min = 0
score_max = (10 ** 6) * (10 ** 6) * (10 ** 5)
while score_min < score_max:
	score = (score_max + score_min) // 2
	if check(score):
		score_max = score
	else:
		score_min = score + 1

print(score_min)

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
import heapq
input = sys.stdin.readline
sys.setrecursionlimit(100000)
def getN():
    return int(input())
def getList():
    return list(map(int, input().split()))

def solve():

    return

def main():
    n, k = getList()
    cap = getList()
    foods = getList()
    cap.sort()
    foods.sort(reverse=True)
    h = []
    pairs = [(-a*b, a, b) for a, b in zip(cap, foods)]
    for pair in pairs:
        heapq.heappush(h, pair)
    for _ in range(k):
        tmp = heapq.heappop(h)
        if tmp[0] != 0:
            _,na,nb = tmp
            na -= 1
            heapq.heappush(h, (-na*nb, na, nb))

    ans = heapq.heappop(h)
    print(-ans[0])

if __name__ =="__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

A = np.array(input().split(), int)
F = np.array(input().split(), int)

F = np.sort(F)[::-1]

for i in range(K):
  A.sort()
  cost = A * F
  t = np.argmax(cost)
  A[t] -= 1

print(max(0,max(A * F)))

Submission status: Time Limit Exceeded

Solution code:
get = lambda: int(raw_input().strip())
gets = lambda: map(int, raw_input().strip().split())

# fi = open('sample2.in', 'r')
# raw_input = fi.readline

N, K = gets()
A = sorted(gets())
F = sorted(gets())[::-1]

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


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

Submission status: Time Limit Exceeded

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)

def shugyo(x):
    need = 0
    for i in range(n):
        need += max(0, a[i] - (x // b[i]))
    return need


bottom = 0
top = (10 ** 12) + 1

while top - bottom > 1:
    mid = (top + bottom) // 2
    if shugyo(mid) <= k:
        top = mid
    else:
        bottom = mid

if shugyo(0) <= k:
    top = 0
        
print(top)

Submission status: Time Limit Exceeded

Solution code:
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

n, k = na()
a = na()
f = na()
a.sort()
f.sort()

def ok(h, a, f, k):
    n = len(a)
    s = 0
    for i in range(n):
        s += max(0, a[i]-h//f[n-1-i])
    return s <= k


low = -1; high = 2000000000000
while high - low > 1:
    h = high+low>>1
    if ok(h, a, f, k):
        high = h
    else:
        low = h
print(high)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
MI=lambda:map(int,input().split())
LI=lambda:list(map(int,input().split()))

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

def check(x):
    # x分以内に食べきれるための修行回数はK回以下か
    k=0
    for i in range(N):
        k+=max(int(0--(A[i]-x//F[i])//1),0)
    return k<=K

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

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
costs = list(map(int, input().split()))
foods = list(map(int, input().split()))
foods.sort()
costs.sort(reverse=True)

def can_eat(x):
  count = 0
  for i in range(N):
    count += max(0, costs[i]-x//foods[i])
  return count <= K

l, r = -1, 10**12+1
while r-l > 1:
  mid = (l+r)//2
  if can_eat(mid):
    r = mid
  else:
    l = mid
print(r)

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(reverse=True)
F.sort()
import math
def isok(n):
  sum=0
  for i in range(N):
    sum+=max(0,A[i]-n//F[i])
  return sum<=K
ma=0
for i in range(N):
  ma=max(ma,A[i]*F[i])
start,end=-1,ma
while end-start>1:
  mid=(start+end)//2
  if isok(mid):
    end=mid
  else:
    start=mid
print(end)

Submission status: Accepted

Solution code:
import numpy as np


def is_good(mid, key):
    return A_sum - np.minimum(A, mid // F).sum() <= key


def binary_search(bad, good, key):
    while good - bad > 1:
        mid = (bad + good) // 2
        if is_good(mid, key):
            good = mid
        else:
            bad = mid
    return good


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()
A_sum = A.sum()
ans = binary_search(-1, np.max(A * F), K)
print(ans)

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 = A[-1] * F[0]
ng = 0

while ok-ng>1:
    mid = (ok+ng)//2
    trainings = 0
    for i in range(N):
        t = A[i]-mid//F[i]
        if t > 0:
            trainings += t
    if trainings <= K:
        ok = mid
    else:
        ng = mid
trainings = 0
for i in range(N):
    t = A[i]-ng//F[i]
    if t>0:
        trainings += t
if trainings<=K:
    print(ng)
else:
    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)

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

while ok-ng>1:
    mid = ng + (ok-ng)//2
    trainings = 0
    for i in range(N):
        t = A[i]-mid//F[i]
        if t > 0:
            trainings += t
    if trainings <= K:
        ok = mid
    else:
        ng = mid
trainings = 0
for i in range(N):
    t = A[i]-ng//F[i]
    if t>0:
        trainings += t
if trainings<=K:
    print(ng)
else:
    print(ok)

Submission status: Accepted

Solution code:
# でつoO(YOU PLAY WITH THE CARDS YOU'RE DEALT..)
import sys
def main(N, K, A, F):
    A.sort()
    F.sort(reverse=True)

    def k(max_t):
        res = 0
        for a, f in zip(A, F):
            t = a * f
            if t > max_t:
                r = t - max_t
                res += (r + f - 1) // f
        return res

    l = -1
    r = 10**12
    while r - l > 1:
        m = (l + r) // 2
        if k(m) <= K: r = m
        else: l = m
    print(r)

if __name__ == '__main__':
    input = sys.stdin.readline
    N, K = map(int, input().split())
    *A, = map(int, input().split())
    *F, = map(int, input().split())
    main(N, K, A, F)

Submission status: Accepted

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

# 成績　Σ{i=1~n}a[i]*f[i]
# a[i]小さいの、f[i]でかいの組み合わせるとよい(交換しても悪化しない)
def c(x):
    need = 0
    for i in range(n):
        if a[i] * f[i] > x:
            # f[i]を何回減らしてx以下にできるか
            diff = a[i] * f[i] - x
            need += 0 - - diff // f[i]
    return need <= k


l = 0
r = 1 << 60

while r != l:
    mid = (l + r) >> 1
    if c(mid):
        r = mid
    else:
        l = mid + 1
print(l)

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 is_ok(x):
    c = 0
    for a, f in zip(A, F):
        c += max(0, a-x//f)
    if c <= k:
        return True
    else:
        return False

ng = -1
ok = 10**18
while ng+1 < ok:
    c = (ng+ok)//2
    if is_ok(c):
        ok = c
    else:
        ng = c
print(ok)

Submission status: Accepted

Solution code:
from collections import Counter
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 judge(x):
    y = k
    for i in range(n):
        goal = x//f[i]
        if a[i] > goal:
            y -= a[i] - goal
            if y < 0:
                return False
    return y >= 0


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

print(right)

Submission status: Accepted

Solution code:
from collections import Counter
n, k = map(int, input().split())
a = list(map(int, input().split()))
f = list(map(int, input().split()))
a.sort()
f.sort(reverse=True)

la = 10**6+1
cnt = [0] * la
for u in a:
    cnt[u] += 1
for i in range(1, la):
    cnt[i] += cnt[i-1]


def judge(x):
    y = k
    for i in range(n):
        goal = x//f[i]
        if a[i] > goal:
            y -= a[i] - goal
            if y < 0:
                return False
    return y >= 0


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

print(right)

Submission status: Accepted

Solution code:
import sys
import numpy as np
readline = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
#mod = 998244353
INF = 10**18
eps = 10**-7

n,k=map(int,input().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

l=-1
r=INF
while l+1<r:
  mid=(l+r)//2
  if test(mid):
    r=mid
  else:
    l=mid
    
print(r)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
N, K = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
a.sort()
b.sort(reverse = True)

def check(x):
  k = K
  for i in range(N):
    if a[i] * b[i] > x: k += (x - a[i] * b[i]) // b[i]
  return k >= 0

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

Submission status: Accepted

Solution code:
import sys
import itertools
# import numpy as np
import time
import math
from heapq import heappop, heappush
from collections import defaultdict
from collections import Counter
from collections import deque
from itertools import permutations
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())
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)

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

l = -1
r = 10 ** 18
while r - l > 1:
    m = (r + l) // 2
    if check(m):
        r = m
    else:
        l = m
print(r)

Submission status: Accepted

Solution code:
from math import ceil, cos
N,K=map(int,input().split())
A=list(map(int,input().split()))
F=list(map(int,input().split()))
A.sort()
F.sort(reverse=True)
sum_A = sum(A)
if K >= sum_A:
    print(0)
    exit()
AFC = [(A[i], F[i], A[i]*F[i]) for i in range(N)]
AFC.sort(key=lambda x: x[2])

def is_ok(x):
    rest = K
    #最大コストをx以下にできるか
    for i in range(N):
        a, f, cost = AFC[i]
        if cost <= x:
            continue
        need = ceil((cost - x)/f)
        rest -= need
    if rest >= 0:
        return True
    else:
        return False

left = 0
right = 10**12 + 100

while right-left > 1:
    mid = (left + right)//2
    res = is_ok(mid)
    if res:
        right = mid
    else:
        left = mid
print(right)

Submission status: Accepted

Solution code:
'''
A[i]<A[j]
F[i]<F[j]
となる組み合わせが存在したと仮定

max(A[i]*F[i],A[j]*F[j])=A[j]*F[j]
>A[i]*F[j]
>A[j]*F[i]
より、この場合はiとjを入れ替えたほうが最適となる。

結局
Aを昇順ソート
Fを降順ソート
するのが最適となる。
問題は修行の割当である。
順序を保ったまま修行していっても問題ない
3,2->1,2と
3.2->2,1は同じとみなせるため
二分探索
成績をmidにするために必要な修行コスト
lowならK回より真に大
highならK回以下
'''
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)
low=-1
high=max(A)*max(F)+1
while(high-low>1):
    mid=(high+low)//2
    tmp=0
    for i in range(N):
        #A[i]*F[i]-mid<=X*F[i]
        x=(A[i]*F[i]-mid+F[i]-1)//F[i]
        if x<0:
            continue
        tmp+=x
    if tmp<=K:
        high=mid
    else:
        low=mid
print(high)

Submission status: Accepted

Solution code:
from math import ceil
N, K = map(int, input().split())
A = list(map(int, input().split()))
F = list(map(int, input().split()))
if K >= sum(A):
  print(0)
  exit()
A.sort(reverse=True)
F.sort()
lot = []
for k in range(N):
  lot.append((A[k]*F[k], k))
lot.sort(key =  lambda x:x[0],reverse=True)
high = lot[0][0]
#print('A:', A)
#print('F:', F)
#rint(lot)
def binary_search(high):
  low = 0
  mid = (low + high) //2
  while low < high:
    final = False
    mid = (low + high) //2
    #print(low, high, mid)
    if low == high-1:
      final = True
    #print(low, high, mid)
    res = K
    flag = True
    for k in range(N):
      if res < 0:
        break
      else:
        if lot[k][0] <= mid:
          break
        else:
          res -= ceil((lot[k][0]-mid)/F[lot[k][1]])
          continue
    if res < 0:
      flag = False
    #print(flag)
    if flag:
      high = mid
    else:
      if final:
        mid += 1
      low = mid
    #print(low, high, mid)
  #return None
  return mid

print(binary_search(high))

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
F = list(map(int, input().split()))
# 小さいAと大きいF, 大きいAと小さいFの組を作りたい
A.sort()
F.sort(reverse=True)

# 最大値の最小化 -> 二分探索
# 目標値Xを達成できるか？

# ありうる最小値-1
left = -1
# ありうる最大値+1
right = 10**12 + 1
while right - left > 1:
    mid = (left + right) // 2
    cnt = 0
    for i in range(N):
        # スコアをmidにするために必要な「修行の回数」
        # 負になった場合は0にする（修行は必要ない）
        tmp = max(A[i] - mid // F[i], 0)
        cnt += tmp
    if cnt <= K:
        right = mid
    else:
        left = mid

print(right)

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)

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

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)
time = []
for i in range(n):
    time.append(a[i]*f[i])

left = -1
right = 10**12 + 1
while right - left > 1:
    mid = (left + right)//2
    count = 0
    flag = True
    for i in range(n):
        if time[i] <= mid:
            continue
        sa = time[i] - mid
        if sa % f[i] == 0:
            count += sa // f[i]
        else:
            count += sa // f[i] + 1
        if count > k:
            flag = False
            break
    if flag:
        right = mid
    else:
        left = mid
print(right)

Submission status: Accepted

Solution code:
import sys


# \n
def input():
    return sys.stdin.readline().rstrip()

def train(X, Y, T):  # O(N)    ans: 回数
    ans = 0
    for i in range(len(X)):
        ans += max(0, X[i] - T // Y[i])
    return ans


def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    F = list(map(int, input().split()))
    A.sort(reverse=True)  # 0(NlogN)
    F.sort()  # O(NlogN)

    t =A[0]*(10**6)

    if K>=t:
        print(0)
        exit()

    ok = t+1 #時間
    ng = -1

    # O(Nlog(maxK)?)
    while ok - ng > 1:
        mid = (ok + ng) // 2 #時間
        ans = train(A,F,mid)  #kaisuu
        if ans >K:
            ng =mid
        else:
            ok =mid

    print(ok)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys


# \n
def input():
    return sys.stdin.readline().rstrip()

def train(X, Y, T):  # O(N)    ans: 回数
    ans = 0
    for i in range(len(X)):
        ans += max(0, X[i] - T // Y[i])
    return ans


def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    F = list(map(int, input().split()))
    A.sort(reverse=True)  # 0(NlogN)
    F.sort()  # O(NlogN)



    if K>=10**12:
        print(0)
        exit()

    ok = 10**12+1 #時間
    ng = -1

    # O(Nlog(maxK)?)
    while ok - ng > 1:
        mid = (ok + ng) // 2 #時間
        ans = train(A,F,mid)  #kaisuu
        if ans >K:
            ng =mid
        else:
            ok =mid

    print(ok)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys


# \n
def input():
    return sys.stdin.readline().rstrip()


def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    F = list(map(int, input().split()))
    A.sort(reverse=True)  # 0(NlogN)
    F.sort()  # O(NlogN)

    def train(X, Y, T):  # O(N)    ans: 回数
        ans = 0
        for i in range(len(X)):
            ans += max(0, X[i] - T // Y[i])
        return ans

    if K>=10**12:
        print(0)
        exit()

    ok = 10**12+1 #時間
    ng = -1

    # O(Nlog(maxK)?)
    while ok - ng > 1:
        mid = (ok + ng) // 2 #時間
        ans = train(A,F,mid)  #kaisuu
        if ans >K:
            ng =mid
        else:
            ok =mid

    print(ok)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys


# \n
def input():
    return sys.stdin.readline().rstrip()


def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    F = list(map(int, input().split()))
    A.sort(reverse=True)  # 0(NlogN)
    F.sort()  # O(NlogN)

    def train(X, Y, T):  # O(N)    ans: 回数
        ans = 0
        for i in range(len(X)):
            ans += max(0, X[i] - T // Y[i])
        return ans

    ok = 10**18+1 #時間
    ng = -1

    while ok - ng > 1:
        mid = (ok + ng) // 2 #時間
        ans = train(A,F,mid)  #kaisuu
        if ans >K:
            ng =mid
        else:
            ok =mid

    print(ok)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def binary_search(key):
    """二分探索法 O(logN)

    Args:
        key (int): 基準値

    Vars:
        ok (int): 条件を満たすindexの上限値/下限値
        ng (int): 条件を満たさないindexの下限値-1/上限値+1
    
    Returns:
        int: 条件を満たす最小値/最大値
    """
    ok = 10 ** 12
    ng = -1
    while abs(ok - ng) > 1:
        mid = (ok + ng) // 2

        if isOK(mid, key):
            ok = mid
        else:
            ng = mid
    
    return ok

def isOK(i, key):
    """条件判定

    Args:
        i   (int): 判定対象の値
        key (int): 基準値

    Returns:
        bool: iが条件を満たすか否か
    """
    cnt = 0
    for v, d in l:
        if v > i:
            cnt += (v - i + d - 1) // d

    return cnt <= key


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

a.sort(reverse=True)
f.sort()
l = []
for i in range(n):
    l.append([a[i]*f[i], f[i]])

print(binary_search(k))

Submission status: Accepted

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


def is_lessthanK(X):
  ans=0
  for A,F in zip(a,f):
    if A*F>X:
      ans+=A-X//F
    if ans > k:
      return False
  return True


ng=-1
ok=a[-1]*f[0]
while ok-ng>1:
  mid=(ok+ng)//2
  if is_lessthanK(mid):
    ok=mid
  else:
    ng=mid
print(ok)

Submission status: Accepted

Solution code:
import math

N, K = map(int, input().split())
A = sorted(list(map(int, input().split())))
F = sorted(list(map(int, input().split())))[::-1]


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


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


ans = meguru_bisect(-1, max([a * f for a, f in zip(A, F)]))
print(ans)

Submission status: Accepted

Solution code:
import sys
import numpy as np

if sys.argv[-1] == 'ONLINE_JUDGE':
    from numba.pycc import CC
    cc = CC('my_module')

    @cc.export('is_ok', '(i8[:],i8[:],i8,i8)')
    def is_ok(A, F, x, k):
        tmp = 0
        for a, f in zip(A, F):
            y = a * f
            if y > x:
                tmp += a - x // f
        return tmp <= k

    cc.compile()
    sys.exit()
from my_module import is_ok

n, k = map(int, input().split())
A = np.array(input().split(), dtype="i8")
F = np.array(input().split(), dtype="i8")

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

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

print(ok)

Submission status: Accepted

Solution code:
import sys
import numpy as np

if sys.argv[-1] == 'ONLINE_JUDGE':
    from numba.pycc import CC
    cc = CC('my_module')

    @cc.export('is_ok', '(u8[:],u8[:],u8,u8)')
    def is_ok(A, F, x, k):
        tmp = 0
        for a, f in zip(A, F):
            y = a * f
            if y > x:
                tmp += a - x // f
        return tmp <= k

    cc.compile()
    sys.exit()
from my_module import is_ok

n, k = map(int, input().split())
A = np.array(input().split(), dtype="u8")
F = np.array(input().split(), dtype="u8")

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

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

print(ok)