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 numpy as np
n,k=list(map(int,input().split()))
a=sorted(list(map(int,input().split())))
f=sorted(list(map(int,input().split())),reverse=True)
b = [a[i]*f[i] for i in range(n)]

def judgement(x):
    judge = -1
    cnt = 0
    for i in range(n):
        if b[i]>x:
            cnt += -((x-b[i])//f[i])
        if cnt > k:
            judge = False
            break
    if cnt <= k:
        judge = True
    return judge

low=0
high=np.amax(b)
while low<high:
    mid = (low + high)//2
    if judgement(mid) == False:
        low=mid+1
    else:
        high=mid
print(low)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
n, k = (int(x) for x in input().split())
a = list(map(int, input().split()))
f = list(map(int, input().split()))
a.sort()
f.sort(reverse=True)
res = [a[i]*f[i] for i in range(n)]
for i in range(min([k,sum(a)])):
  num = res.index(max(res))
  res[num] -= f[num]
print(max(res))

Submission status: Time Limit Exceeded

Solution code:
n, k = (int(x) for x in input().split())
a = list(map(int, input().split()))
f = list(map(int, input().split()))
a.sort()
f.sort(reverse=True)
res = [a[i]*f[i] for i in range(0,n)]
for i in range(min([k,sum(a)])):
  num = res.index(max(res))
  res[num] -= f[num]
print(max(res))

Submission status: Time Limit Exceeded

Solution code:
n, k = (int(x) for x in input().split())
a = list(map(int, input().split()))
f = list(map(int, input().split()))
a.sort()
f.sort(reverse=True)
res = [a[i]*f[i] for i in range(0,n)]
for i in range(k):
  num = res.index(max(res))
  if a[num]<=0:
    break
  a[num] -= 1
  res[num] = (a[num])*f[num]
print(max(res))

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())))[::-1]
C = [(-A[i]*F[i], F[i], A[i]) for i in range(N)]
heapq.heapify(C)
x1, y1, z1 = heapq.heappop(C)
while z1 > 0 and K > 0:
    #print(x1, y1)
    x2, y2, z2 = heapq.heappop(C)
    a = min((x2 - x1)//z1 + 1, K, z1)
    K -= a
    heapq.heappush(C, (x1 + a*y1, y1, z1 - a))
    x1, y1, z1 = x2, y2, z2
    #print(y2)
print(- x1)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
#import math
N, K = map(int, input().split())
A = np.array(sorted(list(map(int, input().split()))))
F = np.array(sorted(list(map(int, input().split())), reverse=True))



left = - 1
right = 10**12

def f(x):
    return sum(np.maximum(0, A - x//F)) <= 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 numpy as np
#import math
N, K = map(int, input().split())
A = np.array(sorted(list(map(int, input().split()))))
F = np.array(sorted(list(map(int, input().split())), reverse=True))

C = A*F

left = - 1
right = 10**12

def f(x):
    return sum(np.maximum(0, A - x//F)) <= 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
a,b=map(int,input().split())
c=list(map(int,input().split()))
d=list(map(int,input().split()))
c.sort()
d.sort(reverse=True)
e=[0 for i in range(a)]
for i in range(a):
  e[i]=c[i]*d[i]
def s(e,x):
  k=0
  for i in range(a):
    if e[i]<x:
      continue
    else:
      k+=math.ceil((e[i]-x)/d[i])
  if k>b:
    return False
  else:
    return True
    
left=0
right=max(e)
while left!=right:
  mid=(left+right)//2
  if s(e,mid):
    right=mid
  else:
    left=mid+1
print(left)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-

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 ** 7)
INF = 10 ** 18
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(reverse=True)
B.sort()

def check(m):
    k = 0
    for i, a in enumerate(A):
        b = B[i]
        k += max(ceil(a - m/b), 0)
        if k > K:
            return False
    return True

print(bisearch_min(-1, 10**12, check))

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)
Amax = A[-1]
Fmax = F[0]
As = sum(a for a in A)

x=0
if As > K:
    x1 = 0
    x2 = Amax*Fmax
    while True:
        Asum1 = Asum2 = 0
        for i in range(0,N):
            if x1//F[i] > A[i]:
                if x1<x2:
                    x1 += 1
                    break
            else:
                Asum1 += x1//F[i]
                Asum2 += x2//F[i]
        else:
            if As-Asum1 > K and As-Asum2 <= K:  #x1-NG and x2-OK
                if x1+1==x2:    #0,1,...,x1,x2,...  Xmin=x2
                    x = x2
                    break
                else:       #0,1,...x1,x1+1,...,x2-1,x2,... 
                    x2b = x2
                    x2 = int((x1+x2)//2)
                    continue
            elif As-Asum1 > K and As-Asum2 > K:   #x1-NG and x2-NG
                x1 = x2
                x2 = int((x2+x2b)//2)
                if x1 == x2:
                    x2 += 1
                    continue
print(x)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
#import math
N, K = map(int, input().split())
A = np.array(sorted(list(map(int, input().split()))))
F = np.array(sorted(list(map(int, input().split())), reverse=True))

C = A*F

left = - 1
right = max(C)

def f(x):
    cnt = 0
    for i in range(N):
        if C[i] > x:
            #cnt += math.ceil((C[i] - x)/F[i])
            cnt += (C[i] - x + F[i] - 1)//F[i]
            if cnt > K:
                return False
    return True

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 sys
input = sys.stdin.readline

n,k = map(int,input().split())
a = list(map(int,input().split()))
f = list(map(int,input().split()))
a.sort(reverse=True)
f.sort()
z = list(zip(a,f))
ok = max(x*y for x,y in z)
ng = -1
while ok-ng>1:
  mid = (ok+ng)//2
  if sum(x-mid//y for x,y in z if x*y > 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(reverse=True)
f.sort()
z = list(zip(a,f))
ok = max(x*y for x,y in z)
ng = -1
while ok-ng>1:
  mid = (ok+ng)//2
  if sum(x-mid//y for x,y in z if x*y > mid) <= k:
    ok = mid
  else:
    ng = mid
print(ok)

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)
C = [(-A[i]*F[i], F[i], A[i]) for i in range(N)]
heapq.heapify(C)
x1, y1, z1 = heapq.heappop(C)
while z1 > 0 and K > 0:
    #print(x1, y1)
    x2, y2, z2 = heapq.heappop(C)
    a = min((x2 - x1)//z1 + 1, K, z1)
    K -= a
    heapq.heappush(C, (x1 + a*y1, y1, z1 - a))
    x1, y1, z1 = x2, y2, z2
    #print(y2)
print(- x1)

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 = max(x*y for x,y in zip(a,f))
ng = -1

def judge(v):
  tot = 0
  for x,y in zip(a,f):
    if x*y > v:
      tot += x - v//y
  return tot <= k

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

Submission status: Time Limit Exceeded

Solution code:
from heapq import heappop, heappush, heapify
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)
H = []
for i in range(N):
    H.append((-A[i]*F[i],F[i]))
heapify(H)

while K>0:
    K -= 1
    x,y = heappop(H)
    x = min(0,x+y)
    heappush(H,(x,y))
print(-H[0][0])

Submission status: Time Limit Exceeded

Solution code:
from heapq import heappop, heappush
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)
H = []
for i in range(N):
    heappush(H,(-A[i]*F[i],F[i]))

while K>0:
    K -= 1
    x,y = heappop(H)
    x = min(0,x+y)
    heappush(H,(x,y))
print(-H[0][0])

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import numpy as np
N, K = map(int, input().split())
A = list(map(int, input().split()))
F = list(map(int, input().split()))

A.sort()
F.sort(reverse = True)
sa = sum(A) 
A = np.array(A)
F = np.array(F)

l = -1 
r = A[N-1] * F[0]
while l+1 < r: 
    m = (l + r) //2
    S = sa - sum(np.minimum(m // F, A)) 
    if S <= K:
        r = m
    else:
        l = m
         
print(r)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
N, K = map(int, input().split())
A = list(map(int, input().split()))
F = list(map(int, input().split()))

A.sort()
F.sort(reverse = True)
sa = sum(A) 
l = -1 
r = A[N-1] * F[0]
while l+1 < r: 
    m = (l + r) //2
    S = sa
    for i in range(N):
        S -= min(m // F[i], A[i])
    if S <= K:
        r = m
    else:
        l = m
         
print(r)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
N, K = map(int, input().split())
A = list(map(int, input().split()))
F = list(map(int, input().split()))

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

l = -1 
r = A[N-1] * F[0]
while l+1 < r: 
    m = (l + r) //2
    S = 0
    for i in range(N):
        S += A[i] - min((m // F[i]), A[i])
    if S <= K:
        r = m
    else:
        l = m
         
print(r)

Submission status: Time Limit Exceeded

Solution code:
def is_ok(x, A, F):
    result = 0
    for i in range(N):
        a = A[i]
        t = x // F[i]
        if t < a:
            result += a - t
    return result <= K


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

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

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

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)

a[int(a.index(max(a)))] -= 1
ans = []
for z in range(k):
    ans.clear()
    for x, y in zip(a, f):
        ans.append(x * y)
    a[int(ans.index(max(ans)))] -= 1
if (max(ans) < 0):
    print(0)
    exit()
print(max(ans))

Submission status: Time Limit Exceeded

Solution code:
def f(x, A, F):
    result = 0
    for i in range(N):
        t = x // F[i]
        if t < A[i]:
            result += A[i] - t
    return result


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

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

lo = 0
hi = A[-1] * F[0]
while lo < hi:
    mid = (lo+hi)//2
    if K - f(mid, A, F) < 0:
        lo = mid+1
    else:
        hi = mid
print(lo)

Submission status: Time Limit Exceeded

Solution code:
def f(x):
    result = 0
    for i in range(N):
        t = x // F[i]
        if t < A[i]:
            result += A[i] - t
    return result


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

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

lo = 0
hi = A[-1] * F[0]
while lo < hi:
    mid = (lo+hi)//2
    if K - f(mid) < 0:
        lo = mid+1
    else:
        hi = mid
print(lo)

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 judge(t):
    global a,F,N,K
    K_t=K
    for i in range(N):
        target=t//F[i]
        if a[i]<=target:
            continue
        if K_t>=a[i]-target:#引けるかどうか
            K_t-=a[i]-target
        else:
            return False
    return True

t_max=a[0]*F[-1] #絶対True
t_min=-1 #絶対False
while t_max>t_min+1:
    t_mid=(t_max+t_min)//2
    if judge(t_mid):
        t_max=t_mid
    else:
        t_min=t_mid
print(t_max)

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 judge(t):
    global a,F,N,K
    K_t=K
    for i in range(N):
        target=t//F[i]
        if a[i]<=target:
            continue
        if K_t>=a[i]-target:#引けるかどうか
            K_t-=a[i]-target
        else:
            return False
    return True

t_max=10**12+1 #絶対True
t_min=-1 #絶対False
while t_max>t_min+1:
    t_mid=(t_max+t_min)//2
    if judge(t_mid):
        t_max=t_mid
    else:
        t_min=t_mid
print(t_max)

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

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

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)

K_list = [0] * N
K_sum = 0

A_sum = 0

for i in range(N):
    A_sum += A[i]

if A_sum <= K:
    print(0)

else:
    left = 0
    right = A[N-1] * F[0]

    while right - left > 1:
        K_sum = 0
        mid = (right + left) // 2
        for i in range(N):
            if A[i] * F[i] > mid:
                K_list[i] = A[i] - mid // F[i]
                K_sum += K_list[i]

        if K_sum > K:
            left = mid
        else:
            right = mid
    print(right)

Submission status: Time Limit Exceeded

Solution code:
import bisect

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)

af = [[0 for i in range(3)] for j in range(n)]

for i in range(n):
    af[i][0] = a[i]*f[i]
    af[i][1] = a[i]
    af[i][2] = f[i]
    
af.sort(reverse=True)

if k == 0:
    print(af[0][0])
    exit()

low = -1
high = af[0][0]+1
tmp = 0

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

Submission status: Time Limit Exceeded

Solution code:
import sys
from io import StringIO
import unittest
import math


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


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

    # binary search
    l = 0
    r = max(A) * max(F)
    result = None
    while l <= r:
        # print('l {}, r {}'.format(l, r))
        if l == r:
            result = l
            break
        mid = (l + r) // 2
        if check(A, F, mid, N, K):
            r = mid
        else:
            l = mid + 1

    print(result)


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """3 5
4 2 1
2 3 1"""
        output = """2"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """3 8
4 2 1
2 3 1"""
        output = """0"""
        self.assertIO(input, output)

    def test_入力例_3(self):
        input = """11 14
3 1 4 1 5 9 2 6 5 3 5
8 9 7 9 3 2 3 8 4 6 2"""
        output = """12"""
        self.assertIO(input, output)


if __name__ == "__main__":
    # unittest.main()
    resolve()

Submission status: Time Limit Exceeded

Solution code:
import sys
n, k = map(int, input().split())
l=list(map(int, input().split()))
f=list(map(int, input().split()))
if sum(l)<=k:
    print(0)
    sys.exit()
ans=int(0)
l.sort()
f.sort()
f.reverse()


ok=int(l[n-1]*f[0])
ng=int(0)
while True:
    c=int(0)
    mid=int((ok + ng)//2)
    if ok-ng<=1:
        break

    for i in range(n):
        a=mid//f[i]
        b=l[i]
        if a<b:
            c +=b-a

    if c<=k:
        ok=mid

    else:
        ng=mid

print(ok)

Submission status: Time Limit Exceeded

Solution code:
import math

n,k=map(int,input().split())
a=sorted([int(i) for i in input().split()],reverse=True)
f=sorted([int(i) for i in input().split()])
c=[[a[i],f[i]] for i in range(n)]
c.sort(key=lambda x:x[0]*x[1],reverse=True)

if n==1:
    print(max(0,(c[0][0]-k)*c[0][1]))
else:
    while True:
        #print(c)
        if k==0 or c[0][0]==0:
            break
        elif c[0][0]*c[0][1]-c[1][0]*c[1][1]==0:
            k-=1
            c[0][0]-=1
        elif c[0][0]*c[0][1]-c[1][0]*c[1][1] < c[0][1]:
            k-=1
            c[0][0]-=1
        else:
            l=math.ceil((c[0][0]*c[0][1]-c[1][0]*c[1][1])/c[0][1])
            m=min(l,c[0][0])
            k-=m
            c[0][0]-=m
        for i in range(n-1):
            if c[i][0]*c[i][1]<=c[i+1][0]*c[i+1][1]:
                c[i],c[i+1]=c[i+1],c[i]
            else:
                break

    #print(c)
    print(c[0][0]*c[0][1])

Submission status: Time Limit Exceeded

Solution code:
import sys
n, k = map(int, input().split())
l=list(map(int, input().split()))
f=list(map(int, input().split()))
if sum(l)<=k:
    print(0)
    sys.exit()
ans=int(0)
l.sort()
f.sort(reverse=True)



ok=int(l[n-1]*f[0])
ng=int(0)
while ok-ng>1:
    c=int(0)
    mid=int((ok + ng)//2)


    for i in range(n):
        if mid//f[i]<l[i]:
            c +=l[i]-mid//f[i]

    if c<=k:
        ok=mid

    else:
        ng=mid

print(ok)

Submission status: Time Limit Exceeded

Solution code:
def main():
	n, k = map(int, input().split())
	*a, = map(int, input().split())
	*f, = map(int, input().split())
	a.sort()
	f.sort(reverse=1)
	prod = [(y, x * y) for x, y in zip(a, f)]
	l = -1
	r = 10 ** 12
	while r - l > 1:
		m = (l + r) // 2
		s = sum(max(0, 0 - (m - p) // y) for y, p in prod)
		if s <= k:
			r = m
		else:
			l = m
	print(r)


if __name__ == "__main__":
	main()

Submission status: Time Limit Exceeded

Solution code:
import sys
n, k = map(int, input().split())
l=list(map(int, input().split()))
f=list(map(int, input().split()))
if sum(l)<=k:
    print(0)
    sys.exit()
ans=int(0)
l.sort()
f.sort()
f.reverse()


ok=int(l[n-1]*f[0])
ng=int(0)
while ok-ng>1:
    c=int(0)
    mid=int((ok + ng)//2)


    for i in range(n):
        if mid//f[i]<l[i]:
            c +=l[i]-mid//f[i]

    if c<=k:
        ok=mid

    else:
        ng=mid

print(ok)

Submission status: Time Limit Exceeded

Solution code:
import sys
n, k = map(int, input().split())
l=list(map(int, input().split()))
f=list(map(int, input().split()))
if sum(l)<=k:
    print(0)
    sys.exit()
ans=int(0)
l.sort()
f.sort()
f.reverse()


ok=int(l[n-1]*f[0])
ng=int(0)
while True:
    c=int(0)
    mid=int((ok + ng)//2)
    if ok-ng<=1:
        break

    for i in range(n):
        if mid//f[i]<l[i]:
            c +=l[i]-mid//f[i]


    if c<=k:
        ok=mid

    else:
        ng=mid

print(ok)

Submission status: Time Limit Exceeded

Solution code:
import sys
n, k = map(int, input().split())
l=list(map(int, input().split()))
f=list(map(int, input().split()))
if sum(l)<=k:
    print(0)
    sys.exit()
ans=int(0)
l.sort()
f.sort()
f.reverse()


ok=int(l[n-1]*f[0])
ng=int(0)
while True:
    a=int(0)
    mid=int((ok + ng)//2)
    if ok-ng<=1:
        break

    for i in range(n):
        if mid//f[i]<l[i]:
            a +=mid//f[i]
        else:
            a += l[i]

    if a>=sum(l)-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)

start = -1
end = A[-1] * F[0]

def check(now):
    cnt = 0
    for a, f in zip(A, F):
        # cnt += max(0, a-now//f)
        eatable = now//f
        if eatable < a:
            cnt += a - eatable
    return cnt <= K
    # if cnt > K:
    #     return False
    # return True

while end > start + 1:
    now = (start + end) // 2
    if check(now):
        end = now
    else:
        start = now

print(end)

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)

start = -1
end = A[-1] * F[0]

def check(now):
    cnt = 0
    for a, f in zip(A, F):
        cnt += max(0, a-now//f)
        # eatable = now//f
        # if eatable < a:
        #     cnt += a - eatable
    return cnt <= K
    # if cnt > K:
    #     return False
    # return True

while end > start + 1:
    now = (start + end) // 2
    if check(now):
        end = now
    else:
        start = now

print(end)

Submission status: Time Limit Exceeded

Solution code:
# E - Gluttony
def isOK(c, N, K, A, F):
    s = 0
    for i in range(N):
        s += max(0, A[i] - c // F[i])
    return s <= K
    
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 = -1
r = 10 ** 12
while l + 1 < r:
    c = (l + r) // 2
    ok = isOK(c, N, K, A, F)
    if ok:
        r = c
    else:
        l = c
    
print(r)

Submission status: Time Limit Exceeded

Solution code:
# E - Gluttony
def isOK(c, N, K, A, F):
    s = 0
    for i in range(N):
        s += max(0, A[i] - c // F[i])
    return s <= K
    
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 = -1
r = A[-1] * F[0] + 1
while l + 1 < r:
    c = (l + r) // 2
    ok = isOK(c, N, K, A, F)
    if ok:
        r = c
    else:
        l = c
    
print(r)

Submission status: Time Limit Exceeded

Solution code:
# E - Gluttony
def isOK(c, N, K, A, F):
    s = 0
    for i in range(N):
        s += max(0, A[i] - int(c / F[i]))
    return s <= K
    
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 = -1
r = A[-1] * F[0] + 1
while l + 1 < r:
    c = int((l + r) / 2)
    ok = isOK(c, N, K, A, F)
    if ok:
        r = c
    else:
        l = c
    
print(r)

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

import numpy as np
f.sort(reverse=True)

while K > 0:
  if max(a) <= 0:
    break
  else:
    a.sort()
    i = np.argmax(np.array(a)*np.array(f))
    a[i] = a[i] -1
    K = K-1
import numpy as np
print(max(np.array(a)*np.array(f)))

Submission status: Time Limit Exceeded

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

lft = -1
rgt = 10**12
while rgt-lft > 1:
    mid = ( rgt+lft ) // 2
    cnt = 0

    for i, e in enumerate(a):
        cnt += max( 0, e-mid//f[i] )

    if cnt <= k:
        rgt = mid
    else:
        lft = mid

print(rgt)

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)

H=10**12
L=0

while abs(H-L)>4:
    Mid=(H+L)//2
    #print(H,Mid,L)
    cnt=0
    for i in range(N):
        f=0
        if A[i]*F[i]>Mid:
            cnt+=(A[i]-int(Mid/F[i]))
            if cnt>K:
                f=1
                break
    if f==1:
        L=Mid
    else:
        H=Mid
for j in range(5):
    M=H-j
    cnt=0
    for i in range(N):
        if A[i]*F[i]>M:
            cnt+=(A[i]-int(M/F[i]))
            if cnt>K:
                print(M+1)
                exit()
print(0)

Submission status: Time Limit Exceeded

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

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

left = -1
right = 10**12

while right-left > 1:
  mid = (left + right)//2
  ans = 0
  for j in range(N): 
    tmp = A[j] - int(mid/L[j])
    if tmp > 0: ans += tmp
  if K >= ans:
    right = mid
  else:
    left = mid
print(right)

Submission status: Time Limit Exceeded

Solution code:

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

ans=int(0)
l.sort()
f.sort()
f.reverse()
t=[]

for i in range(n):

    t.append(l[i]*f[i])

for i in range(k):

    c=t.index(max(t))
    l[c] -=1
    t[c]=l[c]*f[c]


ans=max(t)

if ans>=0:
    print(ans)
else:
    print(0)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N,K = input().split()
A = input().split()
F = input().split()

N,K = int(N),int(K)
A = sorted([int(a) for a in A])
F = sorted([int(f) for f in F])

for i in range(K):

    mal_list = []
    for n in range(N):
        mal_list.append(A[n]*F[N-n-1])
        
    ind = np.argmax(mal_list)
    
    if(A[ind]>0):
        A[ind] -= 1
    else:
        break
    A = sorted(A)
    #print(A)

mal_list = []
for i in range(N):
    mal_list.append(A[i]*F[N-i-1])
print(max(mal_list))

Submission status: Time Limit Exceeded

Solution code:
#coding:utf-8
import sys
import bisect
import math
sys.setrecursionlimit(10**6)
write = sys.stdout.write
dbg = lambda *something : print(*something) if DEBUG else 0
DEBUG = True
def main(given = sys.stdin.readline):
    input = lambda : given().rstrip()
    LMIIS = lambda : list(map(int,input().split()))
    II = lambda : int(input())
    XLMIIS = lambda x : [LMIIS() for _ in range(x)]

    n, k = LMIIS()
    aa = LMIIS()
    ff = LMIIS()
    aa.sort(reverse=True)
    ff.sort()
    a = tuple(aa)
    f = tuple(ff)


    max_ = 10**12
    min_ = 0

    while max_ != min_ + 1:
        x = (max_ + min_) // 2
        shugyo = 0
        for i in range(n):
            shugyo += max(0, a[i] - x // f[i])

        if shugyo <= k:
            max_ = x
        else:
            min_ = x


    if x == min_:
        x += 1
    if sum(a) <= k:
        x = 0
    print(x)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def binary_search(a, f, target, left, right):
    ans = a[-1] * f[-1]
    while left + 1 < right:
        mid = (left+right)//2
        cost = 0
        for aa,ff in zip(a,f):
            if ff > mid:
                cost += aa
            elif aa - mid // ff > 0:
                cost += aa - mid // ff
        if cost > target:
            left = mid
        else:
            right = mid
    return right

n,k = map(int,input().split())
a = sorted(map(int, input().split()), reverse=True)
f = sorted(map(int, input().split()))
target = binary_search(a,f,k,-1,max(a) * max(f))
mx = 0
cost = 0
for aa, ff in zip(a,f):
    if ff > target:
        cost += aa
        aa = 0
    elif aa - target // ff > 0:
        cost += aa - target // ff
        aa = (target // ff)
    if mx < aa * ff:
        mx = aa * ff
print(mx)

Submission status: Time Limit Exceeded

Solution code:
#coding:utf-8
import sys
import bisect
import math
sys.setrecursionlimit(10**6)
write = sys.stdout.write
dbg = lambda *something : print(*something) if DEBUG else 0
DEBUG = True
def main(given = sys.stdin.readline):
    input = lambda : given().rstrip()
    LMIIS = lambda : list(map(int,input().split()))
    II = lambda : int(input())
    XLMIIS = lambda x : [LMIIS() for _ in range(x)]

    n, k = LMIIS()
    a = LMIIS()
    f = LMIIS()
    a.sort(reverse=True)
    f.sort()


    max_ = 10**12
    min_ = 0

    while True:
        x = (max_ + min_) // 2
        shugyo = 0
        for i in range(n):
            shugyo += max(0, a[i] - x // f[i])

        if shugyo <= k:
            max_ = x
        else:
            min_ = x


        if max_ == min_ + 1:
            if x == min_:
                x += 1
            if sum(a) <= k:
                x = 0
            break
    print(x)

if __name__ == '__main__':
    main()