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:
#!/usr/bin/pypy
N, K =map(int, raw_input().split())
AN = map(int, raw_input().split())
FN = map(int, raw_input().split())

AN.sort()
FN.sort(reverse=True)

CN=[]
for i in range(N):
    CN.append([i,AN[i] * FN[i]])

for i in range(K):
    CN.sort(key=lambda x: x[1], reverse=True)

    if AN[CN[0][0]] < 0:
        break
    AN[CN[0][0]] -= 1
    CN[0][1] = AN[CN[0][0]] * FN[CN[0][0]]

CN.sort(key=lambda x: x[1], reverse=True)
print (str(CN[0][1]))

Submission status: Time Limit Exceeded

Solution code:
def resolve():
    N,K = map(int,input().split())
    A = list(map(int,input().split()))
    F = list(map(int,input().split()))
    
    A.sort()
    F.sort(reverse=True)
    import heapq
    q = [((-1)*A[i]*F[i],A[i],F[i]) for i in range(N)]
    heapq.heapify(q)
    for _ in range(K):
        s,a,f = q[0]
        if s == 0:
            break
        heapq.heappushpop(q,((a-1)*f*(-1),a-1,f))
    
    print(q[0][0]*(-1))
resolve()

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

# base case
if sum(A) <= K: print(0)
else:
    h = []
    A.sort()
    F.sort(reverse=True)
    for i in range(N):
        heapq.heappush(h, (- A[i] * F[i], A[i], F[i]))

    for k in range(K):
        val, cost, food = heapq.heappop(h)
        cost -= 1
        heapq.heappush(h, (- cost*food, cost, food))
    
    val, cost, food = heapq.heappop(h)
    print(-val)

Submission status: Time Limit Exceeded

Solution code:
from heapq import heappush, heappop
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
N, K = map(int, input().split())
A = list(map(int, input().split()))
F = list(map(int, input().split()))

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

q = []
for a, f in zip(A, F):
    v = -1 * a * f
    heappush(q, (v, a, f))

for _ in range(K):
    val = heappop(q)
    a = val[1]
    f = val[2]
    newv = -1 * (a-1) * f
    heappush(q, (newv, a-1, f))

ans = heappop(q)[0]

if ans >= 0:
    print(0)
else:
    print(-1 * ans)

Submission status: Time Limit Exceeded

Solution code:
import heapq

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)
if sum(A) <= K:
    print(0)
else:
    X = [(-A[i]*F[i], A[i], F[i]) for i in range(N)]
    heapq.heapify(X)
    x = heapq.heappop(X)
    for i in range(K-1):
        x = heapq.heappushpop(X, (-(x[1]-1)*x[2], x[1]-1, x[2]))
    heapq.heappush(X, (-(x[1]-1)*x[2], x[1]-1, x[2]))
    print(max(map(lambda x: -x[0], X)))

Submission status: Time Limit Exceeded

Solution code:
import heapq

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

    
if K >= sum(A):
    ans = 0
else:
    A.sort(reverse=True)
    F.sort()
    h = []
    for i in range(N):
        heapq.heappush(h, [-A[i]*F[i], A[i], F[i]])
    while K > 0:
        temp = heapq.heappop(h)
        temp[0] += temp[2]
        temp[1] -= 1
        heapq.heappush(h, temp)
        K -= 1
    L = heapq.heappop(h)
    ans = -L[0]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N, K  = map(int, input().split())
As = sorted(list(map(int, input().split())))
Fs = sorted(list(map(int, input().split())),reverse=True)
Xs = sorted([(a*f,i) for i,(a,f) in enumerate(zip(As,Fs))],reverse=True)
# Xs =[a*f for a,f in zip(As,Fs)]
# print(Xs)
# print(As)
# print(Fs)
import heapq
q = []
INF = 10**6
heapq.heapify(q)
for i,(a,f) in enumerate(zip(As,Fs)):
    num = -(a*f*INF+i)
    heapq.heappush(q,num)
if K >= sum(As):
    print(0)
    exit()
for k in range(K):
    num = heapq.heappop(q)
    i = (-num)%INF
    n = (-num)//INF
    # print(i,n,As[i],Fs[i])
    As[i] -= 1
    upd = -(As[i]*Fs[i]*INF+i)
    heapq.heappush(q,upd)
num = heapq.heappop(q)
ans = (-num)//INF
print(ans)


# x_min = min(Xs)
# ns = [max(0,A-x_min//F) for A,F in zip(As,Fs)]
# print(ns)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3

import numpy as np


def solve(N, K, A, F):
    a = np.array(A, dtype=np.int64)
    f = np.array(F, dtype=np.int64)
    a = np.sort(a)
    f = np.sort(f)[::-1]
    if np.sum(a) <= K:
        return 0
    for _ in range(K):
        i = np.argmax(a*f)
        a[i] = a[i] - 1
        a = np.sort(a)
        if a[0] == 0:
            a = a[1:]
            f = f[1:]
    return np.max(a*f)


if __name__ == '__main__':
    N, K = map(int, input().split())
    A = [int(item) for item in input().split()]
    F = [int(item) for item in input().split()]

    ans = solve(N, K, A, F)

    print(ans)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import sys
from heapq import heappush, heappop, heapify

N,K=map(int, sys.stdin.readline().split())
A=map(int, sys.stdin.readline().split())
F=map(int, sys.stdin.readline().split())

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

hq=[]

for x,y in zip(A,F):
    heappush( hq,(-1*x*y,x,y) )


while 0<K:
    v=heappop(hq)
    x=v[1]
    y=v[2]
    x-=1
    K-=1
    heappush( hq,(-1*x*y,x,y) )

print hq[0][0]*-1

Submission status: Time Limit Exceeded

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

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
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)
A = np.array(A)
F = np.array(F)

af = A*F


while k > 0:
    maxIndex = [i for i, x in enumerate(af) if x == max(af)]
    if k >= len(maxIndex):
        af[maxIndex] -= F[maxIndex]
        k -= len(maxIndex)

    else:
        break

print(np.max(af))

Submission status: Time Limit Exceeded

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

debug = True
debug = False

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

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

    # 答えで二分探索

    low = 0
    high = (10**6)*(10**6)*2*(10**5)

    # 妥当な割り当て
    a_list = sorted(a_list)
    f_list = sorted(f_list, reverse=True)
    import math

    dprint(a_list)
    dprint(f_list)

    def judge(ans):
        # ans を答えにできるか
        cnt = 0
        for i in range(N):
            a = a_list[i]
            f = f_list[i]
            if a*f > ans:
                # 差分を訓練で埋める
                ki = math.ceil((a*f - ans) / f)
                cnt += ki
                if cnt > K:
                    dprint('False', ans, cnt)
                    return False
        dprint('True', ans, cnt)
        return True

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

        guess = judge(mid)
        # dprint(high, low, mid, guess)

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


solve()

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
from heapq import heappush, heappop, heapreplace

def solve():
    N, K = map(int, input().split())
    A = sorted(map(int, input().split()))
    F = sorted(map(int, input().split()), reverse=1)
    level = {}
    hq = []
    hqv = set()
    for a,f in zip(A,F):
        if -a*f not in hqv:
            heappush(hq, -a*f)
            hqv.add(-a*f)
        level[-a*f] = level.get(-a*f, list())+[f]
    while 1:
        #print(K, sorted(level.items()))
        c = heappop(hq)
        if c == 0:
            return '0'
        hqv -= {c}
        if K >= len(level[c]):
            for f in level[c]:
                level[c+f] = level.get(c+f, list())+[f]
                if c+f not in hqv:
                    heappush(hq, c+f)
                    hqv.add(c+f)
            K -= len(level[c])
            del level[c]
        else:
            return str(-c)

        
if __name__ == '__main__':
    print(solve())

Submission status: Time Limit Exceeded

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

    def count(val):
        cnt=0
        for i in range(n):
            cnt+=max(a[i]-val//f[i],0)
        if cnt>k:
            return False
        else:
            return True
    
    print(b_search(0,10**12+10,count,"lower"))
    
main()

Submission status: Time Limit Exceeded

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

  rest = sum(A) - K
  if rest <= 0: return 0

  _min = min(F)
  cost = 0

  while True:
    _sum = sum([cost // f for f in F])
    if _sum >= rest: return cost
    cost += _min

print(solve())

Submission status: Time Limit Exceeded

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

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

for i in range(0, K):
    max_value=0
    max_index=-1
    mul=[a*b for a,b in zip(A,F)]
    for j, value in enumerate(mul):
        if value>max_value:
            max_value=value
            max_index=j
    if A[max_index]>0:
        A[max_index]-=1
        if A[max_index]<A[max_index-1]:
            A.sort()


max_value=0
max_index=-1
for j in range(len(A)):
    temp=A[j]*F[j]
    if temp>max_value:
        max_value=temp
        max_index=j

print(max_value)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

N,K=map(int,input().split())
A=np.array(list(map(int,input().split())))
B=np.array(list(map(int,input().split())))

AA=np.sort(A)
BB=np.sort(B)[::-1]

CC=np.array([AA[i]*BB[i] for i in range(N)])

for j in range(K):
    if np.max(AA)==0:
        break
    m=np.argmax(CC)
    AA[m]-=1
    CC[m]=AA[m]*BB[m]
print(np.max(CC))

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()))
X =list(map(lambda x: 1/x, F))
judge=0
p=sum(X)
c=sum(A)
sss=sum(A)-k
score=0
j=0
k=1
if sss<=0:
    print(0)
else:
    while judge==0:
        for i in range(len(X)):
            score+=((k*X[i])//1)
        if score >=sss:
            j=k
            judge=1

        k+=1
        score=0
    print(j)

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

import heapq
heapq.heapify(time)

while k > 0:
    m1 = heapq.heappop(time)
    val1 = m1[0] * (-1)
    j1 = m1[1]

    m2 = heapq.heappop(time)
    val2 = m2[0] * (-1)
    j2 = m2[1]
    while val1 >= val2:
        if a[j1] == 0 or k == 0:
            k = 0
            break
        else:
            a[j1] -= 1
            k -= 1
            val1 = a[j1] * f[j1]
    heapq.heappush(time, [val1*(-1), j1])
    heapq.heappush(time, [val2*(-1), j2])
ans = heapq.heappop(time)
print( ans[0] * (-1) )

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

if N==1:
    ans=max(A[0]-K,0)*F[0]
else:
    F.sort(reverse=True) # 降順で固定
    A.sort()

#    print(f'F={F}')
#    print(A)

    while K>0 and A[-1]>0:
#        print(f'A={A}')
        S=sorted([(A[i]*F[i],i) for i in range(N)],reverse=True)
#        print(f'S={S}') #DB
        xym=S[0][0];xys=S[1][0]
        m=S[0][1];s=S[1][1]
        if xym==xys>0:
            A[m]-=1
            K-=1
        else:
            d=xym-xys
            p=min((d+(F[m]-1))//F[m],K,A[m])
            A[m]-=p # 負にならない
            K-=p
        A.sort()
    S=sorted([(A[i]*F[i],i) for i in range(N)],reverse=True)
    ans=max(S)[0]

print(ans)

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)

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

for i in range(K):
    total, pair = heapq.heappop(h)
    heapq.heappush(h, ((pair[0]-1)*pair[1]*(-1), (pair[0]-1, pair[1])))

res, _ = heapq.heappop(h)
print(-res)

Submission status: Time Limit Exceeded

Solution code:
import sys
import heapq

N,K=map(int,input().split())
alist=list(map(int,input().split()))
flist=list(map(int,input().split()))

alist.sort()
flist.sort(reverse=True)
#print(alist)
#print(flist)

if sum(alist)<=K:
  print(0)
  sys.exit(0)
  
hq=[]
for i in range(N):
  heapq.heappush(hq,(-alist[i]*flist[i],alist[i],flist[i]))
  
for i in range(K):
  naf,a,f=heapq.heappop(hq)
  naf+=f
  a-=1
  if a>0:
    heapq.heappush(hq,(naf,a,f))
    
if len(hq)==0:
  print(0)
else:
  naf,a,f=heapq.heappop(hq)
  print(-naf)

Submission status: Time Limit Exceeded

Solution code:
import heapq
import sys

input = sys.stdin.readline


class AtCoder:
    def __lt__(self, other):
        return self[0] < other[0]

    def main(self):
        N, K = map(int, input().split())
        A = list(map(int, input().split()))
        F = list(map(int, input().split()))

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

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

        while K > 0:
            score = heapq.heappop(hq)
            if score[1][1] == 0:
                print(0)
                exit()
            else:
                K -= 1
                new_A = score[1][1] - 1
                heapq.heappush(hq, (-1 * new_A * score[1][0], (score[1][0], new_A)))

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


# Run main
if __name__ == '__main__':
    AtCoder().main()

Submission status: Time Limit Exceeded

Solution code:
import heapq
import math
a,b=map(int,input().split())
c=list(map(int,input().split()))
d=list(map(int,input().split()))
if b>=sum(c):
  print(0)
else:
  c.sort()
  d.sort(reverse=True)
  e=[c[i]*d[i] for i in range(a)]
  f=[]
  for i in range(a):
    heapq.heappush(f,-e[i])
  k=0
  while k<b or f[0]==0:
    x=-heapq.heappop(f)
    y=e.index(x)
    z=min(b-k,(x+f[0])//d[y]+1)
    k+=z
    c[y]=c[y]-z
    e[y]=c[y]*d[y]
    heapq.heappush(f,-e[y])
  print(-f[0])

Submission status: Time Limit Exceeded

Solution code:
from heapq import heappush, heappop
def main():
    n,k = map(int,input().split())
    a = list(map(int,input().split()))
    a = sorted(a)
    f = list(map(int,input().split()))
    f = sorted(f,reverse=True)
    cost = [[-i*j,i,j] for i,j in zip(a,f)]
    cost = sorted(cost)
    max_cost = cost[0]
    ans = max_cost
    if sum(a) <= k:
        ans = 0
    else:
        while True:
            first = heappop(cost)
            f = -first[0]
            if f == 0:
                ans = 0
                break
            second = heappop(cost)
            s = -second[0]
            x = (f-s)//first[2]+1
            x = min([x,k])
            if x == k:
                k = 0
                f -= x*first[2]
                first[1] -= x
                heappush(cost,second)
                first[0] = -f
                heappush(cost,first)
            else:
                if x <= first[1]:
                    k -= x
                    f -= x*first[2]
                    first[1] -= x
                else:
                    k -= first[1]
                    first[1] = 0
                    f = 0
                heappush(cost,second)
                first[0] = -f
                heappush(cost,first)
            if k == 0:
              ans = -heappop(cost)[0]
              break
    print(ans)
        
if __name__ == '__main__':
    main()

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()))
X = []
A = sorted(A)
F = sorted(F)[::-1]
for i in range(N):
  X.append([A[i] * F[i],A[i],F[i]])

import heapq
from heapq import heapify

heapq._heapify_max(X)

def _heappush_max(heap, item):
    heap.append(item)
    heapq._siftdown_max(heap, 0, len(heap)-1)
def _heappop_max(heap):
    lastelt = heap.pop()
    if heap:
        returnitem = heap[0]
        heap[0] = lastelt
        heapq._siftup_max(heap, 0)
        return returnitem
    return lastelt
  
for i in range(K):
    x = _heappop_max(X)
    if x[0] < 1:
        _heappush_max(X,x)
        break
    else:       
        x[1] -= 1
        x[0] = x[1] * x[2]
        _heappush_max(X,x)
from operator import itemgetter
X.sort(key=itemgetter(0))
print(X[-1][0])

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
a_array = list(map(int, input().split()))
f_array = list(map(int, input().split()))
a_array.sort()
f_array.sort()
f_array.reverse()
# print(a_array)
# print(f_array)
time = [[0, 0] for _ in range(n)]
for i in range(n):
    time[i][0] = i
    time[i][1] = f_array[i] * a_array[i]
time.sort(key=lambda x: x[1])
while k > 0:
    while k > 0:
        a_array[time[-1][0]] -= 1
        k -= 1
        if a_array[time[-1][0]] * f_array[time[-1][0]] < a_array[time[-2][0]] * f_array[time[-2][0]]:
            break
    a_array.sort()
    for i in range(n):
        time[i][0] = i
        time[i][1] = f_array[i] * a_array[i]
    time.sort(key=lambda x:x[1])
print(time[-1][1])

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

N, K = map(int, input().split())
A = np.fromiter(map(int, input().split()), dtype=int)
F = np.fromiter(map(int, input().split()), dtype=int)
F.sort()
F = F[::-1]

for _ in range(K):
    A.sort()
    i = np.argmax(A * F)
    if A[i] <= 0:
        break
    A[i] -= 1
print(np.amax(A * F))

Submission status: Time Limit Exceeded

Solution code:
import heapq
class Heapq:
    def __init__(self, arr, desc=False):
        if desc:
            arr = [-a for a in arr]
        self.sign = -1 if desc else 1
        self.hq = arr
        heapq.heapify(self.hq)
 
    def pop(self):
        return heapq.heappop(self.hq) * self.sign
 
    def push(self, a):
        heapq.heappush(self.hq, a * self.sign)
 
    def top(self):
        return self.hq[0] * self.sign

def resolve():
    n,k  = map(int,input().split())
    
    member_cost = list(map(int,input().split()))
    food_cost = list(map(int,input().split()))

    member_cost.sort()
    food_cost.sort(reverse=True)
    total_cost = []
    for i in range(len(member_cost)):
        total_cost.append(member_cost[i]*food_cost[i])

    total_cost_heap = Heapq(total_cost,True)
    if k >= sum(member_cost):
        print(0)
    else:
        while k > 0:
            k -= 1
            max_cost  = total_cost_heap.pop()
            reduce_index = total_cost.index(max_cost)
            member_cost[reduce_index] -= 1
            total_cost[reduce_index] -= food_cost[reduce_index]
            total_cost_heap.push(total_cost[reduce_index])
        print(max(total_cost))
        
            
resolve()

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)
import heapq

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)
S = [(-A[i]*F[i], i) for i in range(N)]

heapq.heapify(S)

for i in range(K):
    ma = heapq.heappop(S)
    A[ma[1]] -= 1
    heapq.heappush(S, (-A[ma[1]]*F[ma[1]], ma[1]))

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

Submission status: Time Limit Exceeded

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

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

for i in range(0, K):
    max_value=0
    max_index=-1
    for j in range(len(A)):
        temp=A[j]*F[j]
        if temp>max_value:
            max_value=temp
            max_index=j
    if A[max_index]>0:
        A[max_index]-=1
        if A[max_index]<A[max_index-1]:
            A.sort()


max_value=0
max_index=-1
for j in range(len(A)):
    temp=A[j]*F[j]
    if temp>max_value:
        max_value=temp
        max_index=j

print(max_value)

Submission status: Time Limit Exceeded

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


def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    A.sort()
    F = list(map(int, input().split()))
    F.sort(reverse=True)
    H = []
    for i in range(N):
        heapq.heappush(H, [-A[i] * F[i], A[i], F[i]])

    while K > 0:
        m1 = heapq.heappop(H)
        m2 = heapq.heappop(H)
        if m1[0] == 0:
            break
        while m1[1] * m1[2] >= m2[1] * m2[2]:
            m1[1] -= 1
            K -= 1
            if K == 0:
                break
        heapq.heappush(H, [-m1[1] * m1[2], m1[1], m1[2]])
        heapq.heappush(H, m2)

    print(-min(H)[0])


if __name__ == '__main__':
    main()

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

# 修行Phase
F = sorted(F)
A = sorted(A, reverse=True)
for _ in range(K):
    resultArray = [x * y for (x, y) in zip(A, F)]
    max_index = resultArray.index(max(resultArray))
    A[max_index] = A[max_index] - 1

resultArray = [x * y for (x, y) in zip(A, F)]
ans = max(resultArray)
print(ans)

Submission status: Time Limit Exceeded

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

flg = 0
heap = []

for i in range(N):
	heapq.heappush(heap, [- A[i] * F[i], F[i]])
	
for i in range(K):
	score, tmp_F = heapq.heappop(heap)
	if score != 0:
		score += tmp_F
		heapq.heappush(heap, [score, tmp_F])
	else:
		flg = 1
		break
		
if flg:
	print(0)
else:
	ret = heapq.heappop(heap)
	print(-1 * ret[0])

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

N,K=map(int,input().split())
A=np.array(list(map(int,input().split())))
B=np.array(list(map(int,input().split())))

AA=np.sort(A)
BB=np.sort(B)[::-1]

CC=[AA[i]*BB[i] for i in range(N)]

for j in range(K):
    if np.max(AA)==0:
        break
    m=CC.index(max(CC))
    AA[m]-=1
    CC[m]=AA[m]*BB[m]
print(max(CC))

Submission status: Time Limit Exceeded

Solution code:
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)
time=[]
for i in range(n):
	time.append(a[i]*f[i])
while k>=1:
	i=np.argmax(time)
	a[i]-=1
	time[i]-=f[i]
	k-=1
print(max(time))

Submission status: Time Limit Exceeded

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

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

pq = []
for i in range(N):
    heappush(pq, (-A[i] * F[i], F[i]))
    
for _ in range(K):
    c, u = heappop(pq)
    heappush(pq, (c + u, u))
    
print(max(0, -min(v for v, _ in pq)))

Submission status: Time Limit Exceeded

Solution code:
import heapq
import sys
#input = sys.stdin.readline
sys.setrecursionlimit(10**7)
N, K = map(int, input().split())
A = list(map(int, input().split()))
F = list(map(int, input().split()))

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

q = []
for a, f in zip(A, F):
    v = -1 * a * f
    heapq.heappush(q, (v, a, f))

for _ in range(K):
    val = heapq.heappop(q)
    v = val[0]
    a = val[1]
    f = val[2]
    newa = val[1] - 1
    newv = -1 * (a-1) * f
    heapq.heappush(q, (newv, a-1, f))

ans = heapq.heappop(q)[0]

if ans >= 0:
    print(0)
else:
    print(-1 * ans)

Submission status: Time Limit Exceeded

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

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()
f.sort(reverse=True)
cost = []
for i, j in zip(a, f):
    heapq.heappush(cost, (-i * j, -j))

cnt = 0
while True:
    tmp = heapq.heappop(cost)
    if cnt >= k:
        ans = -tmp[0]
        break
    if tmp[0] == 0:
        ans = 0
        break
    tmp = (tmp[0] - tmp[1], tmp[1])
    heapq.heappush(cost, tmp)

    cnt += 1

print(ans)

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()
m = []
for i, j in zip(a, f):
    heapq.heappush(m, (-i*j, i, j))
for i in range(k):
    t = heapq.heappop(m)
    heapq.heappush(m, (-(t[1] - 1)*t[2], t[1]-1, t[2]))
    if m[0][0]== 0:
        print(0)
        break
else:
    print(-m[0][0])

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

for _ in range(K):
    A.sort()
    i = np.argmax(A * F)
    if A[i] <= 0:
        break
    A[i] -= 1
print(np.amax(A * F))

Submission status: Time Limit Exceeded

Solution code:
from heapq import heappush, heappop
def main():
    n,k = map(int,input().split())
    a = list(map(int,input().split()))
    a = sorted(a)
    f = list(map(int,input().split()))
    f = sorted(f,reverse=True)
    cost = [[-i*j,i,j] for i,j in zip(a,f)]
    cost = sorted(cost)
    max_cost = cost[0]
    ans = max_cost
    while True:
        first = heappop(cost)
        f = -first[0]
        if f == 0:
            ans = 0
            break
        second = heappop(cost)
        s = -second[0]
        x = (f-s)//first[2]+1
        x = min([x,k])
        if x == k:
            k = 0
            f -= x*first[2]
            first[1] -= x
            heappush(cost,second)
            first[0] = -f
            heappush(cost,first)
        else:
            if x <= first[1]:
                k -= x
                f -= x*first[2]
                first[1] -= x
            else:
                k -= first[1]
                first[1] = 0
                f = 0
            heappush(cost,second)
            first[0] = -f
            heappush(cost,first)
        if k == 0:
          ans = -heappop(cost)[0]
          break
    print(ans)
        
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

if sum(A) <= K:
    print(0)
    exit()
else:
    hq = [(-(A[i]*F[i]), A[i], F[i]) for i in range(N)]
    heapq.heapify(hq)  # リストhqのheap化

    for i in range(K):
        c, a, f = heapq.heappop(hq)  # heap化されたリストhqから最小値を削除＆その最小値を出力

        a -= 1
        heapq.heappush(hq, (-(a*f), a, f))

    c, a, f = heapq.heappop(hq)  # heap化されたリストhqから最小値を削除＆その最小値を出力
    print(-c)

Submission status: Time Limit Exceeded

Solution code:
def resolve():
    n,k  = map(int,input().split())
    
    member_cost = list(map(int,input().split()))
    food_cost = list(map(int,input().split()))

    member_cost.sort()
    food_cost.sort(reverse=True)
    total_cost = []
    for i in range(len(member_cost)):
        total_cost.append(member_cost[i]*food_cost[i])

    if k >= sum(member_cost):
        print(0)
    else:
        while k > 0:
            k -= 1
            reduce_index = total_cost.index(max(total_cost))
            member_cost[reduce_index] -= 1
            total_cost[reduce_index] -= food_cost[reduce_index]
        print(max(total_cost))
        
        
            
resolve()

Submission status: Time Limit Exceeded

Solution code:
N,K= input().split()
N = int(N)
K = int(K)

A = input().split()
F = input().split()

A = [int(i) for i in A]
F = [int(i) for i in F]

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

for i in range(K+1):
  ma = []
  for a,f in zip(A,F):
    ma.append(a*f)
  A[ma.index(max(ma))] -= 1
  A.sort()
print(max(ma))