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:
N, K = map(int, input().split())
*A, = map(int, input().split())
*F, = map(int, input().split())
A, F = sorted(A), sorted(F, reverse=True)

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

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

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

hq = []

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

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

for i in range(k):
    s,a1,f1 = heapq.heappop(hq)
    if s == 0:
        print(0)
        exit()
    a1 -= 1
    heapq.heappush(hq, (-a1*f1, a1, f1))

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

Submission status: Time Limit Exceeded

Solution code:
import sys
import heapq

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

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


def solve():
    if N == 1:
        return max(A[0] - K, 0) * F[0]
    hp = []
    for i in range(N):
        heapq.heappush(hp, (-A[i] * F[i], F[i]))
    k = K
    while k > 0:
        t0, f0 = heapq.heappop(hp)
        if t0 == 0:
            break
        t0 = -t0
        t1, f1 = hp[0]
        t1 = -t1
        if t0 == t1:
            m = 1
        else:
            assert t0 > t1
            m = ((t0 - t1) + f0 - 1) // f0
        m = min(m, k)
        k -= m
        t0 -= m * f0
        heapq.heappush(hp, (-t0, f0))

    t0, _ = hp[0]
    return -t0


print(solve())

Submission status: Time Limit Exceeded

Solution code:
import sys
import heapq

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

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


def solve():
    if N == 1:
        return max(A[0] - K, 0) * F[0]
    hp = []
    for i in range(N):
        heapq.heappush(hp, (-A[i] * F[i], F[i]))
    k = K
    while k > 0:
        t0, f0 = heapq.heappop(hp)
        t0 = -t0
        t1, f1 = hp[0]
        t1 = -t1
        if t0 == t1:
            m = 1
        else:
            assert t0 > t1
            m = ((t0 - t1) + f0 - 1) // f0
        m = min(m, k)
        k -= m
        t0 -= m * f0
        heapq.heappush(hp, (-t0, f0))

    t0, _ = hp[0]
    return -t0


print(solve())

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()
F.sort(reverse = True)

def calc(x): #全てx以下にするために何回変える必要があるか
    tmp = 0
    for a, f in zip(A, F):
        tmp += max(0, a - (x // f))
    return tmp

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

print (ok)

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()
f.sort(reverse = True)
cost = max([a[i] * f[i] for i in range(n)])
cost2 = 0
while True:
    cost3 = (cost + cost2) // 2
    count = 0
    for i in range(n):
        count += max((a[i] * f[i] - cost3 - 1) // f[i] + 1, 0)
    if count <= k:
        cost = cost3
    else:
        cost2 = cost3 + 1
    if cost == cost2:
        break
print(cost)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# hello worldと表示する
#float型を許すな
#numpyはpythonで
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi,factorial,sqrt
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7

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

Submission status: Time Limit Exceeded

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

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

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

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

Submission status: Time Limit Exceeded

Solution code:
import heapq

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

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

heapq.heapify(heap_list)    
for i in range(k):
    max_v, f = heapq.heappop(heap_list)
    if max_v < 0:
        max_v = min(max_v + f, 0)
    heapq.heappush(heap_list, (max_v, f))
max_v, f = heapq.heappop(heap_list)
print(-max_v)

Submission status: Time Limit Exceeded

Solution code:
import math

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

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

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=sorted(A, reverse=False)
F=sorted(F, reverse=True)

C=[[-A[i]*F[i], A[i], F[i]] for i in range(N)]

heapq.heapify(C)

for _ in range(K):
  tmp=heapq.heappop(C)
  if tmp[1]==0:
    print(0)
    exit()
    
  a=tmp[1]-1
  heapq.heappush(C, [-a*tmp[2], a, tmp[2]])
  
tmp=heapq.heappop(C)
print(-tmp[0])

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=sorted(A, reverse=False)
F=sorted(F, reverse=True)

C=[[-A[i]*F[i], A[i], F[i]] for i in range(N)]

heapq.heapify(C)

for _ in range(K):
  tmp=heapq.heappop(C)
  a=max(tmp[1]-1, 0)
  heapq.heappush(C, [-a*tmp[2], a, tmp[2]])
  
tmp=heapq.heappop(C)
print(-tmp[0])

Submission status: Time Limit Exceeded

Solution code:
# 2020年6月17日

# 修行という概念がない場合、消化コストを大から小に並べて食べにくさを小から大に並べて順にマッチングさせれば良い。
# そうでない状態から2つを交換したときに最大値が下がる方向にしか行かないため。

# 修行をするなら最大値を引き下げるところに修行をするのが最適なので、
# 無修行状態の結果を優先度付きキューに入れる→K回最大値を取り出してコストを引いて再投入→を繰り返す。
# 本当か? やってるうちに最適な組み合わせが変わらないか? →変わらない。食べにくさを昇順に並べたときに消化コストが降順になるのが最善手なので。
# あとKが大きいのでこれだと間に合わない。

# 決め打ち二分探索か?
# あ、それだわ。

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

# 食べにくさは昇順（小から大）、人間は降順（大から小）
foods.sort()
people.sort(reverse=True)

def is_valid(time):
    # k回以下の修行で実現可能か

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


time_ok = 2 * 10 ** 12
time_ng = -1  # 計算上、is_valid(-1) = Trueになることはあるが、二分探索の方法から言ってこれで正しいはず

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

print(time_ok)

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()
f.sort(reverse=True)

def is_ok(arg):
    return sum(max(0, a[i] - arg // f[i]) for i in range(n)) <= 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

print(meguru_bisect(-1, 10 ** 18 + 1))

Submission status: Time Limit Exceeded

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

lo, hi = -1, 10 ** 19
while hi - lo > 1:
    mid = (lo + hi) // 2
    if sum(max(0, a - mid // f) for a, f in zip(a, f)) <= k:
        hi = mid
    else:
        lo = mid

print(hi)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import sys
# sys.setrecursionlimit(10**6)
# buff_readline = sys.stdin.buffer.readline
buff_readline = sys.stdin.readline
readline = sys.stdin.readline

INF = 2**62-1


def read_int():
    return int(buff_readline())


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


def read_float():
    return float(buff_readline())


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


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


def read_str_n():
    return readline().strip().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 Mod:
    def __init__(self, m):
        self.m = m

    def add(self, a, b):
        return (a + b) % self.m

    def sub(self, a, b):
        return (a - b) % self.m

    def mul(self, a, b):
        return ((a % self.m) * (b % self.m)) % self.m

    def div(self, a, b):
        return self.mul(a, pow(b, self.m-2, self.m))

    def pow(self, a, b):
        return pow(a, b, self.m)


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

def f(n):
    r = 1
    for i in range(1, n+1):
        r *= i
    return r

@mt
def slv(N, K, A, F):
    from heapq import heappop, heappush, heapify
    A.sort()
    F.sort(reverse=True)
    q = []
    for a, f in zip(A, F):
        q.append((-(a * f), a, f))
    heapify(q)

    for _ in range(K):
        _, a, f = heappop(q)
        if a > 0:
            a -= 1
        heappush(q, (-a*f, a, f))

    return -min(q, key=lambda x: x[0])[0]




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

Solution code:
import sys

input = lambda: sys.stdin.readline().rstrip()

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


def binary_search(min_n, max_n):

    while max_n - min_n != 1:
        tn = (min_n + max_n) // 2
        if judge(tn):
            max_n = tn
        else:
            min_n = tn

    return max_n


def judge(tn):

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

    return True


def solve():

    ans = binary_search(-1, 10**12)
    print(ans)


if __name__ == '__main__':
    solve()

Submission status: Time Limit Exceeded

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


def binary_search(min_n, max_n):

    while max_n - min_n != 1:
        tn = (min_n + max_n) // 2
        if judge(tn):
            max_n = tn
        else:
            min_n = tn

    return max_n


def judge(tn):

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

    return True


def solve():

    ans = binary_search(-1, 10**12)
    print(ans)


if __name__ == '__main__':
    solve()

Submission status: Time Limit Exceeded

Solution code:
import sys

input = lambda: sys.stdin.readline().rstrip()


class SegTree:

    def __init__(self, init_val, segfunc, ide_ele):

        n = len(init_val)
        self.ide_ele = ide_ele
        self.segfunc = segfunc
        self.num = 2**(n - 1).bit_length()
        self.seg = [self.ide_ele] * 2 * self.num

        for i in range(n):
            self.seg[i + self.num - 1] = init_val[i]

        for i in range(self.num - 2, -1, -1):
            self.seg[i] = self.segfunc(self.seg[2 * i + 1], self.seg[2 * i + 2])

    def update(self, k, x):
        k += self.num - 1
        self.seg[k] = x
        while k:
            k = (k - 1) // 2
            self.seg[k] = self.segfunc(self.seg[k * 2 + 1], self.seg[k * 2 + 2])

    def max_index(self):

        i = 0

        while True:
            l, r = i * 2 + 1, i * 2 + 2
            if self.seg[l] < self.seg[r]:
                i = r
            else:
                i = l
            if i * 2 + 2 >= len(self.seg):
                break
        return (i - (self.num - 1), self.seg[0])


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


def solve():

    seg = [A[i] * F[i] for i in range(N)]
    segtree = SegTree(seg, segfunc=max, ide_ele=0)

    for _ in range(K):
        i, num = segtree.max_index()
        if num == 0:
            break
        else:
            A[i] -= 1
            segtree.update(i, A[i] * F[i])

    i, num = segtree.max_index()
    print(num)


if __name__ == '__main__':
    solve()

Submission status: Time Limit Exceeded

Solution code:
N,K=[int(s) for s in input().split()]
A=[int(s) for s in input().split()]
F=[int(s) for s in input().split()]
A.sort()
F.sort(reverse=1)


def success(x):
  suc_sum=0
  for i in range(N):
    a=x//F[i]
    if A[i]>a:
      suc_sum+=A[i]-a
  if suc_sum<=K:
    return True
  else:
    return False

def bisearch():
  bi_max=10**12
  bi_a=0
  bi_b=bi_max
  while bi_b-bi_a>1:
    c=(bi_a+bi_b)//2
    if success(c):
      bi_a,bi_b=[bi_a,c]
    else:
      bi_a,bi_b=[c,bi_b]
  return [bi_a,bi_b]

a,b=bisearch()
if success(a):
  print(a)
else:
  print(b)

Submission status: Time Limit Exceeded

Solution code:
def binary_search(*, ok, ng, func):
    while abs(ok - ng) > 1:
        mid = (ok + ng) // 2
        if func(mid):
            ok = mid
        else:
            ng = mid
    return ok


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

    def is_ok(score):
        rest = K
        for a, f in zip(A, F):
            exceed = max(0, a * f - score)
            if exceed:
                need = (exceed + f - 1) // f
                if need > a or need > rest:
                    return False
                rest -= need
        return True

    ans = binary_search(ok=10 ** 12, ng=-1, func=is_ok)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    def meguru_bisect(is_ok, left, right):
        """
        :param is_ok: 定義して下さい
        :param left: 取りうる最小の値-1
        :param right: 取りうる最大の値+1
        :return: is_okを満たす最小(もしくは最大)の値
        """
        while abs(left - right) > 1:
            mid = (left + right) // 2
            if is_ok(mid):
                right = mid
            else:
                left = mid
        return right

    def is_ok(x):
        total = 0
        for i in range(n):
            total += max(0, A[i] - x // F[i])
        return total <= k

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

    left = -1
    right = 10 ** 12 + 1
    res = meguru_bisect(is_ok, left, right)
    print(res)


if __name__ == '__main__':
    resolve()

Submission status: Time Limit Exceeded

Solution code:
import math
import os,io
input=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
n,k=map(int,input().split())
a=list(map(int,input().split()))
f=list(map(int,input().split()))
a.sort()
a.reverse()
f.sort()
totalnum=sum(a)
maxproduct=0
for i in range(n):
  maxproduct=max(f[i]*a[i],maxproduct)
l1=0
r1=maxproduct
while True:
  l2=l1
  r2=(l1+r1)//2
  if l2==r2:
    break
  counter=0
  for i in range(n):
    c=math.floor(r2/f[i])
    while (c+1)*f[i]<=r2:
      c+=1
    counter+=max(0,a[i]-c)
  if counter<=k:
    r1=r2
  else:
    l1=r2
if l1==r1:
  print(r2)
else:
  counter=0
  r2=l1
  for i in range(n):
    c=math.floor(r2/f[i])
    while (c+1)*f[i]<=r2:
      c+=1
    counter+=max(0,a[i]-c)
  if counter<=k:
    print(l1)
  else:
    print(r1)

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

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

L = []
for i in range(N):
  heapq.heappush(L, (-A[i]*F[i],A[i],F[i]))

for i in range(K):
  temp = heapq.heappop(L)
  rep = (-(temp[1]-1)*temp[2], (temp[1]-1), temp[2])
  heapq.heappush(L, rep)
ans = heapq.heappop(L)
print(-1*ans[0])

Submission status: Time Limit Exceeded

Solution code:
import math
n,k=map(int,input().split())
a=list(map(int,input().split()))
f=list(map(int,input().split()))
a.sort()
a.reverse()
f.sort()
totalnum=sum(a)
maxproduct=0
for i in range(n):
  maxproduct=max(f[i]*a[i],maxproduct)
l1=0
r1=maxproduct
while True:
  l2=l1
  r2=(l1+r1)//2
  if l2==r2:
    break
  counter=0
  for i in range(n):
    c=math.floor(r2/f[i])
    while (c+1)*f[i]<=r2:
      c+=1
    counter+=max(0,a[i]-c)
  if counter<=k:
    r1=r2
  else:
    l1=r2
if l1==r1:
  print(r2)
else:
  counter=0
  r2=l1
  for i in range(n):
    c=math.floor(r2/f[i])
    while (c+1)*f[i]<=r2:
      c+=1
    counter+=max(0,a[i]-c)
  if counter<=k:
    print(l1)
  else:
    print(r1)

Submission status: Time Limit Exceeded

Solution code:
import sys
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()))
    F.sort(reverse=True)
    A.sort()
    right=10**12
    left=-1
    while right-left>1:#区間(left,right]に解Xがある
        x=(right+left)//2
        k_sum=0
        for i in range(n):
            k_sum+=max(0,A[i]-x//F[i])
        if k_sum<=k:
            right=x
        else:
            left=x
    print(right)
    

if __name__=='__main__':
    main()

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

L = []
for i in range(N):
  heapq.heappush(L, (-A[i]*F[i],A[i],F[i]))

for i in range(K):
  temp = heapq.heappop(L)
  rep = (-(temp[1]-1)*temp[2], (temp[1]-1), temp[2])
  heapq.heappush(L, rep)
ans = heapq.heappop(L)
print(-1*ans[0])

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int,input().split())
A = [int(i) for i in input().split()]
A.sort()
F = [int(i) for i in input().split()]
F.sort(reverse=True)

left = -1
right = 10**12
def f(x):
    cnt = 0
    for i in range(n): cnt += max(A[i]-x//F[i],0)
    return cnt <= k
    
while right - left > 1:
    mid = (left+right)//2
    if f(mid): right = mid
    else: left = mid
print(right)

Submission status: Time Limit Exceeded

Solution code:
import math

N,K,*AF = map(int, open(0).read().split())
A = sorted(AF[:N])
F = sorted(AF[N:],reverse=True)
left = 0
right = max(A) * max(F)
while left != right:
    mid = (left+right) // 2
    if sum([max(A[i]-math.floor(mid/F[i]),0) for i in range(N)]) <= K:
        right = mid
    else:
        left = mid + 1
print(left)

Submission status: Time Limit Exceeded

Solution code:
import math

N,K,*AF = map(int, open(0).read().split())
A = sorted(AF[:N])
F = sorted(AF[N:],reverse=True)
init = [A[i] * F[i] for i in range(N)]
left = 0
right = max(init)
while left != right:
    mid = (left+right) // 2
    temp = [max(A[i]-math.floor(mid/F[i]),0) for i in range(N)]
    if sum(temp) <= K:
        right = mid
    else:
        left = mid + 1
print(left)

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()
F.sort(reverse=True)

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

Submission status: Time Limit Exceeded

Solution code:
import bisect
n,k=map(int,input().split())
a=sorted(list(map(int,input().split())))[::-1]
b=sorted(list(map(int,input().split())))
ma=a[0]*b[-1]
mi=0
if sum(a)<k+1:
  ma=0
for i in range(n):
  a[i]=a[i]*b[i]
for i in range(50):
  now=0
  for i in range(n):
    now+=max(0,(a[i]-(mi+ma)//2+b[i]-1)//b[i])
  if now>k:
    mi=(ma+mi)//2
  else:
    ma=(ma+mi)//2
print(ma)

Submission status: Time Limit Exceeded

Solution code:
import bisect
n,k=map(int,input().split())
a=sorted(list(map(int,input().split())))[::-1]
b=sorted(list(map(int,input().split())))
ma=10**13
mi=0
if sum(a)<k+1:
  print(0)
  quit()
while abs(ma-mi)>1:
  now=0
  for i in range(n):
    now+=max(0,(a[i]*b[i]-(mi+ma)//2+b[i]-1)//b[i])
  if now>k:
    mi=(ma+mi)//2
  else:
    ma=(ma+mi)//2
print(ma)

Submission status: Time Limit Exceeded

Solution code:

def Z(): return int(input())
def ZZ(): return [int(_) for _ in input().split()]

def main():
    N, K = ZZ()
    A = sorted(ZZ(), reverse=True)
    F = sorted(ZZ())

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

    cost = list()
    for a, f in zip(A, F): cost.append(a*f)

    ok = 10 ** 12
    ng = -1

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

    return

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10 ** 8)

def Z(): return int(input())
def ZZ(): return [int(_) for _ in input().split()]

def main():
    N, K = ZZ()
    A = sorted(ZZ(), reverse=True)
    F = sorted(ZZ())

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

    cost = list()
    for a, f in zip(A, F): cost.append(a*f)

    ok = 10 ** 12
    ng = -1

    for _ in range(100):
        mid = (ok+ng)//2
        cc = 0
        for i in range(N): cc += max(0, (cost[i]-mid+F[i]-1)//F[i])
        if cc <= K: ok = mid
        else: ng = mid
    print(ok)

    return

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 19
MOD = 10 ** 9 + 7

def bisearch_min(mn, mx, func):
    """ 条件を満たす最小値を見つける二分探索 """

    ok = mx
    ng = mn
    while ng+1 < ok:
        mid = (ok+ng) // 2
        if func(mid):
            ok = mid
        else:
            ng = mid
    return ok

N, K = MAP()
A = LIST()
B = LIST()
A.sort()
B.sort(reverse=1)

def check(m):
    C = [0] * N
    for i in range(N):
        C[i] = m // B[i]
    res = 0
    for i in range(N):
        res += max(A[i] - C[i], 0)
    return res <= K

res = bisearch_min(-1, INF, check)
print(res)

Submission status: Time Limit Exceeded

Solution code:
import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 19
MOD = 10 ** 9 + 7

def bisearch_min(mn, mx, func):
    """ 条件を満たす最小値を見つける二分探索 """

    ok = mx
    ng = mn
    while ng+1 < ok:
        mid = (ok+ng) // 2
        if func(mid):
            ok = mid
        else:
            ng = mid
    return ok

N, K = MAP()
A = LIST()
B = LIST()
A.sort()

def check(m):
    C = [0] * N
    for i in range(N):
        C[i] = m // B[i]
    C.sort()
    res = 0
    for i in range(N):
        res += max(A[i] - C[i], 0)
    return res <= K

res = bisearch_min(-1, INF, check)
print(res)

Submission status: Time Limit Exceeded

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 ** 13
    while high - low > 1:
        mid = (low + high) // 2
        count = 0
        for i in range(N):
            counterF = mid // F[i] 
            if counterF < A[i]: count += A[i] - counterF
        if count <= K: high = mid
        else: low = mid
    print(high)
         
    return 0

if __name__ == "__main__":
    solve()

Submission status: Time Limit Exceeded

Solution code:
import sys
input = lambda : sys.stdin.readline().rstrip()

n, k = map(int, input().split())
a = list(map(int, input().split()))
f = list(map(int, input().split()))
a.sort()
f = sorted(f, reverse=True)
cost = []
for i in range(n):
    cost.append(a[i]*f[i])
left = -1
right = max(cost)
def solve(x):
    cnt = 0
    for i in range(n):
        d = cost[i] - x
        if d <= 0:
            continue
        cnt += d//f[i] + int(d % f[i] != 0)
    if cnt <= k:
        return True
    else:
        return False
while right - left > 1:
    mid = (right + left) // 2
    if solve(mid):
        right = mid
    else:
        left = mid
print(right)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.buffer.readline

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

    AF = [(a, f) for a, f in zip(sorted(A), sorted(F, reverse=True))]

    def isOk(T):
        ret = 0
        for a, f in AF:
            m = T // f
            if a > m:
                ret += a - T // f
        return ret <= K

    ok = max(A) * max(F)
    ng = -1
    while ok - ng > 1:
        mid = (ok + ng) // 2
        if isOk(mid):
            ok = mid
        else:
            ng = mid

    print(ok)

sol()

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.buffer.readline

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

    AF = [(a, f) for a, f in zip(sorted(A), sorted(F, reverse=True))]

    def isOk(T):
        ret = 0
        for a, f in AF:
            m = T // f
            if a > m:
                ret += a - T // f
        return ret <= K

    ok = max(a * f for a, f in AF)
    ng = -1
    while ok - ng > 1:
        mid = (ok + ng) // 2
        if isOk(mid):
            ok = mid
        else:
            ng = mid

    print(ok)

sol()