Problem:


Score: 500 points

Problem Statement

Takahashi will take part in an eating contest. Teams of N members will compete in this contest, and Takahashi's team consists of N players numbered 1 through N from youngest to oldest. The consumption coefficient of Member i is A_i.

In the contest, N foods numbered 1 through N will be presented, and the difficulty of Food i is F_i. The details of the contest are as follows:

A team should assign one member to each food, and should not assign the same member to multiple foods.

It will take x \times y seconds for a member to finish the food, where x is the consumption coefficient of the member and y is the difficulty of the dish.

The score of a team is the longest time it takes for an individual member to finish the food.

Before the contest, Takahashi's team decided to do some training. In one set of training, a member can reduce his/her consumption coefficient by 1, as long as it does not go below 0. However, for financial reasons, the N members can do at most K sets of training in total.

What is the minimum possible score of the team, achieved by choosing the amounts of members' training and allocating the dishes optimally?

Constraints

All values in input are integers.

1 \leq N \leq 2 \times 10^5

0 \leq K \leq 10^{18}

1 \leq A_i \leq 10^6\ (1 \leq i \leq N)

1 \leq F_i \leq 10^6\ (1 \leq i \leq N)

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 ... A_N
F_1 F_2 ... F_N

Output

Print the minimum possible score of the team.

Sample Input 1

3 5
4 2 1
2 3 1

Sample Output 1

2

They can achieve the score of 2, as follows:

Member 1 does 4 sets of training and eats Food 2 in (4-4) \times 3 = 0 seconds.

Member 2 does 1 set of training and eats Food 3 in (2-1) \times 1 = 1 second.

Member 3 does 0 sets of training and eats Food 1 in (1-0) \times 2 = 2 seconds.

They cannot achieve a score of less than 2, so the answer is 2.

Sample Input 2

3 8
4 2 1
2 3 1

Sample Output 2

0

They can choose not to do exactly K sets of training.

Sample Input 3

11 14
3 1 4 1 5 9 2 6 5 3 5
8 9 7 9 3 2 3 8 4 6 2

Sample Output 3

12



Sample input:
3 5
4 2 1
2 3 1


Sample output:
2

Submission status: Time Limit Exceeded

Solution code:
import heapq

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

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 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:
def main():
    n, k = map(int, input().split())
    costs = list(map(int, input().split()))
    difficulties = list(map(int, input().split()))
    difficulties.sort(reverse=True)

    max_time = -1
    for x in range(k + 1):
        costs.sort()

        max_time = -1
        max_member = 0
        for i in range(n):
            temp_time = costs[i] * difficulties[i]
            if max_time < temp_time:
                max_time = temp_time
                max_member = i
        costs[max_member] -= 1

    print(max_time)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from heapq import heappop, heappush, heapify
import sys
input = sys.stdin.readline

def main():
    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 = [None] * n
    for i in range(n):
        q[i] = (-a[i]*f[i], -f[i])
    heapify(q)

    for i in range(k):
        t, g = heappop(q)
        heappush(q, (t-g, g))

    ans, _ = heappop(q)
    print(-ans)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]
import heapq
n,k=map(int,input().split())
a=list(map(int,input().split()))
f=list(map(int,input().split()))
a.sort()
f.sort()
ans=[]
for i in range(n):
    ans.append([f[i]*a[n-i-1]*-1,a[n-i-1]*-1])
heapq.heapify(ans)
tmp=[]
for i in range(k):
    if i==0:
        tmp=heapq.heappop(ans)
    # print(tmp)
    if tmp[1]==0:
        heapq.heappush(ans,tmp)
        break
    tmp[0]=tmp[0]//tmp[1]
    tmp[1]+=1
    tmp[0]=tmp[0]*tmp[1]
    if i != k-1:
        tmp=heapq.heappushpop(ans,tmp)
    else:
        ans.append(tmp)

print(ans[0][0]*-1)

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)
    A[i] -= 1
print(np.amax(A * F))

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 min(m, key=lambda x:x[0]) == 0:
        print(0)
        break
else:
    print(-min(m, key=lambda x:x[0])[0])

Submission status: Time Limit Exceeded

Solution code:
#A=int(input())
#A=list(map(int,(input().split())))
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)
noruma=10**18 // 2
sabun=10**18 // 4
for x in range(83):
	tasuku=0
	for i in range(N):
		if (A[i]*F[i]>noruma):
			tasuku+=A[i]-(noruma//F[i])
	if tasuku>K:#無理
		noruma+=sabun
		sabun=sabun//2
	else:#いけた
		noruma-=sabun
		sabun=sabun//2
	if sabun==0:sabun=1
if tasuku<=K:
	noruma+=1
	#print("modosu",sabun)
print(noruma)

Submission status: Time Limit Exceeded

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

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

A = np.sort(A)
F = np.sort(F)[::-1]
temp = A * F
for i in range(K):
    a = temp.argmax()
    temp[a] -= F[a]
    if temp.sum() == 0:
        break
print(temp.max())

Submission status: Time Limit Exceeded

Solution code:
import math
N,K=[int(s) for s in input().split()]
ls1=[int(s) for s in input().split()]
ls2=[int(s) for s in input().split()]
ls1.sort()
ls2.sort()
M=max([ls1[i]*ls2[N-1-i] for i in range(N)])
#print(M)
###aまで下げることが可能か？
def pos(a):
  s=0
  for i in range(N):
    s+=max([0,math.ceil(ls1[i]-a/ls2[N-1-i])])
  if s>K:
    return 0
  else:
    return 1
x=0
y=M//2
z=M
while z-x>1:
  if pos(y)==1:
    z=y
  else:
    x=y
  y=(x+z)//2
#  print(x,y,z)
if pos(y)==1:
  print(y)
else:
  print(z)

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

F_sort = sorted(F,reverse = True)
A_sort = sorted(A)

for j in range(K):
    A_sort = sorted(A_sort)
    
    max_num = 0
    max_index = 0
    for i in range(N):
        if max_num < A_sort[i]*F_sort[i]:
            max_index = i
            max_num = A_sort[i]*F_sort[i]
    if max_num == 0:
        break
    else:
        A_sort[max_index] -= 1
    
ans = 0
A_sort = sorted(A_sort)
for i in range(N):
    if ans < A_sort[i]*F_sort[i]:
        ans = A_sort[i]*F_sort[i]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
people, tp = map(int,input().split())
cc = list(map(int, input().split()))
costs = list(map(int, input().split()))
pairs = []
for i in range(people):
	min_cost = min(costs)
	costs.remove(min_cost)
	max_cc = max(cc)
	cc.remove(max_cc)
	pairs.append([min_cost, max_cc])

k = 0
flag = False
pair = max(pairs, key=lambda pair: pair[0] * pair[1])
pairs.remove(pair)
while True:
	snd_max = max(pairs, key=lambda pair: pair[0] * pair[1])
	snd_total = snd_max[0] * snd_max[1]
	while pair[0] * pair[1] >= snd_total:
		if(pair[1] == 0) or k == tp:
			flag = True
			break
		pair[1] -= 1
		k += 1
	if flag:
		print(max(pair[0] * pair[1], snd_total))
		break
	pairs.append(pair)
	pair = snd_max
	pairs.remove(pair)

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

for _ in range(K):
    A.sort(kind='heapsort')
    F.sort(kind='heapsort')
    F = F[::-1]
    i = np.argmax(A * F)
    A[i] -= 1
print(np.amax(A * F))

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)
    for i in range(K):
        x = heapq.heappop(X)
        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:
# -*- coding: utf-8 -*-
"""
Created on Sun Oct 27 21:52:40 2019

@author: doppe
"""
import numpy as np

N, K = (int(x) for x in input().split())
list1 = list(map(int, input().split()))
list2 = list(map(int, input().split()))

list1.sort()
list2.sort(reverse=True)

x = np.array(list1)
y = np.array(list2)
z = x * y

for i in range(0, K):
    max_index = np.argmax(z)
    x[max_index] = x[max_index] - 1
    z[max_index] = x[max_index] * y[max_index]
if (z.max() < 0):
    print(0)
else:
    print(z.max())

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
A=list(map(int,input().split()))
A.sort()
F=list(map(int,input().split()))
F.sort(reverse=True)
def is_ok(x):
  c=0
  for i in range(n):
    c+=max(A[i]-x//F[i],0)
  if c<=k:
    return True
  else:
    return False
l=0
r=A[-1]*F[0]
while l+1<r:
  m=(l+r)//2
  if is_ok(m):
    r=m
  else:
    l=m+1
if l==r or not is_ok(l):
  print(r)
else:
  print(l)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10000000)
def input():
    return sys.stdin.readline()[:-1]
# from bisect import *
# from collections import *
from heapq import *
# import itertools
import math
# INF = 10**9
# MOD = 10**9+7

N, K = map(int, input().split())
a = [int(x) for x in input().split()]
a.sort()
f = [int(x) for x in input().split()]
f.sort(reverse = 1)
h = [(-x*y, x, y) for x, y in zip(a, f)]
heapify(h)
while K > 0:
    v, x, y = heappop(h)
    vn, xn, yn = h[0]
    xd = int(xn*yn/y)
    if xd*y == -vn:
        xd -= 1
    if x-xd <= K:
        K -= x-xd
        heappush(h, (-xd*y, xd, y))
    else:
        heappush(h, (-(x-K)*y, x-K, y))
        break
print(-h[0][0])


# cnt = defaultdict(int)
# for x in f:
#     cnt[x] += 1
# cnt.sort(key = lambda v : v[0])
# lst = defaultdict(list)
# l = 0
# for k, v in cnt:
#     lst[k] = Counter(a[l:l+cnt[k]])
#     l += cnt[k]
# h = [(cnt[k]*lst[k][-1][0], k) for k in cnt]
# h.heapify()
# while K > 0:
#     v, k = heappop(h)
#     if v == 0:
#         break

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)

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 sys
import bisect
input = sys.stdin.readline
N, K = map(int, input().split())
A = list(map(int, input().split()))
F = list(map(int, input().split()))

A.sort()
F.sort(reverse=True)
B = [[A[i], F[i]] for i in range(N)]
B.sort(reverse=True, key=lambda x: x[0]*x[1])


for i in range(K):
    B.sort(key=lambda x: x[0]*x[1])
    if B[N-1][0] > 0:
        tmp = B.pop()
        tmp[0] -= 1
        bisect.insort_left(B, tmp)
    else:
        break
B.sort(reverse=True, key=lambda x: x[0]*x[1])
print(B[0][0]*B[0][1])

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

for _ in range(K):
    A.sort(kind='mergesort')
    F.sort(kind='mergesort')
    F = F[::-1]
    i = np.argmax(A * F)
    A[i] -= 1
print(np.amax(A * F))

Submission status: Time Limit Exceeded

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]
import heapq
n,k=map(int,input().split())
a=list(map(int,input().split()))
f=list(map(int,input().split()))
a.sort()
f.sort()
ans=[]
for i in range(n):
    ans.append([f[i]*a[n-i-1]*-1,a[n-i-1]*-1])
heapq.heapify(ans)
tmp=[]
for i in range(k):
    if i==0:
        tmp=heapq.heappop(ans)
    # print(tmp)
    if tmp[1]==0:
        heapq.heappush(ans,tmp)
        break
    tmp[0]=tmp[0]//tmp[1]
    tmp[1]+=1
    tmp[0]=tmp[0]*tmp[1]
    if i != k-1:
        tmp=heapq.heappushpop(ans,tmp)
    else:
        ans.append(tmp)
cnt=0
for i in range(n):
    cnt=max(cnt,ans[i][0]*-1)
print(cnt)

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)

for i in range(k):
    m = heapq.heappop(time)
    val = m[0]
    j = m[1]
    if a[j] == 0:
        break
    else:
        a[j] -= 1
        val = a[j] * f[j] * (-1)
        heapq.heappush(time, [val, j])
ans = heapq.heappop(time)
print( ans[0] * (-1) )

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
    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
n, k = [int(i) for i in sys.stdin.readline().split()]
a_ls = [int(i) for i in sys.stdin.readline().split()]
f_ls = [int(i) for i in sys.stdin.readline().split()]
a_ls.sort(reverse=True)
f_ls.sort()
res = [- a * f for a, f in zip(a_ls, f_ls)]
heapq.heapify(res)
cnt = 0
flg = False
while True:
    for i in range(n):
        if a_ls[i] == 0:
            continue
        if a_ls[i] * f_ls[i] == - res[0]:
            cnt += 1
            a_ls[i] -= 1
            heapq.heappop(res)
            heapq.heappush(res, - a_ls[i] * f_ls[i])
        if cnt == k:
            flg = True
            break
    if not any(a_ls):
        flg = True
    if flg:
        break
print(- res[0])

Submission status: Time Limit Exceeded

Solution code:
import heapq

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

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

for i in range(K):
	A = heapq.heappop(h)
	if A[0]<0:
		heapq.heappush(h,[A[0]-A[1], A[1]])
	else:
		heapq.heappush(h,[A[0], A[1]])
		break

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

Submission status: Time Limit Exceeded

Solution code:
import bisect
(n,k),a,f = [list(map(int, s.split())) for s in open(0)]

if n == 1:
    ans = max((a[0] - k) * f[0], 0)
    print(ans)

else:
    a.sort()
    f.sort(reverse=True)
    s = []
    for x,y in zip(a,f):
        bisect.insort(s, (x*y, y))

    while k > 0 and s[-1][0] > 0:
        l, y = s[-1]
        s = s[:-1]
        tmp = int((l - s[-1][0]) / y) + 1
        if k > tmp:
            k -= tmp
        else:
            tmp = k
            k = 0
        l = l - tmp * y
        bisect.insort(s, (l, y))
    print(s[-1][0])

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)
h = [(-ai*fi, ai, fi) for ai, fi in zip(a,f)]

heapq.heapify(h)

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

(ci, ai, fi) = heapq.heappop(h)
print(-ci)

Submission status: Time Limit Exceeded

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

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

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

for i in range(K):
    temp = A * F
    A[temp.argmax()] -= 1
    if np.all(A == 0):
        break
print((A * F).max())

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)

AF = [(-a*f, -a) for a, f in zip(A, F)]

heapq.heapify(AF)
for i in range(K):
    mul, val = heapq.heappop(AF)
    val = -val
    if val:
        temp = mul // val
        val -= 1
        heapq.heappush(AF, (temp * val, -val))


print(-AF[0][0])

Submission status: Time Limit Exceeded

Solution code:
N,K=map(int,input().split())
a_list=[int(x) for x in input().split()]
f_list=[int(x) for x in input().split()]
import heapq
if sum(a_list)<=K:
    print(0)
else:
    sa=sorted(a_list)
    sf=sorted(f_list)[::-1]
    pl_orig=[-x*y for (x,y) in zip(sa,sf)]
    pl=[-x*y for (x,y) in zip(sa,sf)]

    for k in range(K):
        heapq.heapify(pl)
        max_pl=heapq.heappop(pl)
        max_id=pl_orig.index(max_pl)
        sa[max_id]-=1
        max_pl=heapq.heappush(pl,-1*sa[max_id]*sf[max_id])
        pl_orig[max_id]=-1*sa[max_id]*sf[max_id]
    print(-1*heapq.heappop(pl))

Submission status: Time Limit Exceeded

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

for i in range(k):
    maxaf=0
    maxj=0
    for j in range(n):
        if(a[j]*f[n-1-j] > maxaf):
            maxaf=a[j]*f[n-1-j]
            maxj=j
    if(a[j]==0):
        break
    a[maxj]-=1

maxaf=0
for j in range(n):
    if(a[j]*f[n-1-j] > maxaf):
        maxaf=a[j]*f[n-1-j]
print(maxaf)

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int,input().split())
dull=sorted([int(s) for s in input().split()])
dull.reverse()
cost=sorted([int(s) for s in input().split()])

time=[]
for i in range(n):
  time.append(dull[i]*cost[i])
for i in range(k):
  j = time.index(max(time))
  dull[j]-=1
  time[j]= dull[j]*cost[j]

p=max(time)
if p >=0:
  print(p)
else:
  print(0)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!

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)

for i in range(k):
    ans = [i*j for i, j in zip(a, f)]
    a[np.argmax(ans)] -= 1
    
ans = [i*j for i, j in zip(a, f)]
print(max(ans))

Submission status: Time Limit Exceeded

Solution code:
from heapq import heappop, heappush, heapify
import sys
input = sys.stdin.readline

def main():
    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 = [(-a[i]*f[i], -f[i]) for i in range(n)]
    heapify(q)

    for i in range(k):
        t, g = heappop(q)
        heappush(q, (t-g, g))

    ans, _ = heappop(q)
    print(-ans)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]
import heapq
n,k=map(int,input().split())
a=list(map(int,input().split()))
f=list(map(int,input().split()))
a.sort()
f.sort()
ans=[]
for i in range(n):
    ans.append([f[i]*a[n-i-1]*-1,a[n-i-1]*-1])
heapq.heapify(ans)
# print(ans)
for i in range(k):
    tmp=heapq.heappop(ans)
    # print(tmp)
    if tmp[1]==0:
        heapq.heappush(ans,tmp)
        break
    tmp[0]=tmp[0]//tmp[1]
    tmp[1]+=1
    tmp[0]=tmp[0]*tmp[1]
    heapq.heappush(ans,tmp)
cnt=0
for i in range(n):
    cnt=max(cnt,ans[i][0]*-1)
print(cnt)

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 = [0]*N

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


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




for i in range(K):
    maximum = 0
    maxline = 0
    for j in range(N):
        if X[j] > maximum:
            maximum = X[j]
            maxline = j
    if A[maxline] > 0:
        A[maxline] -= 1
        X[maxline] = A[maxline] * F[maxline]
    else:
        break

maximum = 0
maxline = 0

for j in range(N):
    if X[j] > maximum:
        maximum = X[j]

print(maximum)

Submission status: Time Limit Exceeded

Solution code:
import copy
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])
XX = copy.deepcopy(X)

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)
    id = XX.index(x)
    if x < 1:
        _heappush_max(X,x)
        break
    else:
        A[id] -= 1
        x = A[id] * F[id]
        XX[id] = x
        _heappush_max(X,x)
print(max(X))

Submission status: Time Limit Exceeded

Solution code:
import heapq as hq
N, K = map(int, input().split())
A = list(map(int,input().split()))
A.sort()
F = list(map(int,input().split()))
F.sort()

if sum(A) <= K:
	print(0)
	exit()
ans = max([A[i]*F[N-1-i] for i in range(N)])

def achievable(score):
	cnt = 0
	for i in range(N):
		a,f = A[i], F[N-1-i]
		if a*f > score:
			n = score // f
			cnt += a-n
			if cnt > K:
				return False
	return True

l,r = 0, ans
while r - l > 1:
	p = (l + r) // 2
	if achievable(p):
		r = p
	else:
		l = p
print(r)

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
a.sort()
b.sort(reverse=True)
#print(a)
#print(b)
c = []
for i in range(n):
  c.append([-a[i]*b[i],b[i]])
#print(c)
import heapq
heapq.heapify(c)
cnt = 0
while True:
  now = heapq.heappop(c)
  if now[0] == 0:
    heapq.heappush(c,now)
    break
  red = (c[0][0] - now[0]) // now[1]
  #print(red)
  if red > k - cnt:
    heapq.heappush(c, [now[0]+(k-cnt)*now[1],now[1]] )
    break
  elif red == 0:
    heapq.heappush(c, [now[0]+now[1],now[1]] )
    cnt += 1
  else:
    heapq.heappush(c, [now[0]+red*now[1],now[1]] )
    cnt += red
  if k == cnt:
    break
  #print(cnt,c)
#print(c)
ans = 0
for i in range(n):
  if ans > c[i][0]:
    ans = c[i][0]
print(-1*ans)

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

  _max = max(F)
  r = sum([_max // f for f in F])
  cost = _max // r

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

print(solve())

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
import numpy as np
n, k = map(int, stdin.readline().rstrip().split())
dig = np.array(sorted(list(map(int, stdin.readline().rstrip().split()))))
diff_kari = np.array(sorted(list(map(int, stdin.readline().rstrip().split()))))
diff = diff_kari[::-1]
score = dig * diff

while k > 0 and sum(dig) > 0:
    tmp = np.argmax(score)
    dig[tmp] -= 1
    k -= 1
    score[tmp] -= diff[tmp]

print(np.max(score))

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

for i in range(k):

    a = sorted(a)
    f = sorted(f)[::-1]
    pro = [a[i] * f[i] for i in range(n)]

    max_index = -1
    max_f = -1
    max_value = max(pro)
    for i in range(n):
        if max_value == pro[i]:
            if max_f < f[i]:
                max_f = f[i]
                max_index = i

    a[max_index] -= 1

pro = [a[i] * f[i] for i in range(n)]
print(max(pro))

Submission status: Time Limit Exceeded

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline

def main():
    n, k = map(int, input().split())
    aa = list(map(int, input().split()))
    ff = list(map(int, input().split()))
    aa.sort(reverse=True)
    ff.sort()
    l = -1
    r = aa[0] * ff[-1]
    #print(aa)
    #print(ff)
    while l + 1 < r:
        #print(l,r)
        m = (l + r) // 2
        s = 0
        for a, f in zip(aa, ff):
            b = m // f
            s += max(a - b, 0)
            if s > k:
                l = m
                break
        else:
            r = m
    print(r)


main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    import heapq
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    if sum(A) <= K:
        print(0)
        return

    F = list(map(int, input().split()))

    A.sort()
    F.sort(reverse=True)
    q = []
    for i in range(N):
        heapq.heappush(q, (-(A[i]*F[i]), A[i], F[i]))

    for k in range(K):
        p, a, f = heapq.heappop(q)
        if p < 0:
            heapq.heappush(q, ((-(a-1)*f), a-1, f))
        else:
            break

    p, a, f = heapq.heappop(q)
    print(-p)
    return

main()

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor, atan2, degrees
from operator import mul
from functools import reduce
sys.setrecursionlimit(10**8)

INF = float('inf')
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n, k = LI()
A = sorted(LI())
F = sorted(LI(), reverse=True)


que = [(-A[i] * F[i], -F[i], A[i]) for i in range(n)]
heapify(que)
while k and que:
    x, f, a = heappop(que)
    if a:
        a -= 1
        x -= f
        heappush(que, (x, f, a))
        k -= 1




if que:
    print(-que[0][0])
else:
    print(0)

Submission status: Time Limit Exceeded

Solution code:
import queue as Q
N, M = list(map(int, input().split()))
K = list(map(int, input().split()))
C = list(map(int, input().split()))
K = sorted(K, reverse = True)
C = sorted(C, reverse = False)  

q = Q.PriorityQueue()
for k, c in zip(K, C):
  q.put((-k*c, k, c))
while M > 0:
  value, k, c = q.get()
  if value == 0:
    break
  k -= 1
  q.put((-k*c, k, c))
  M -= 1

#while not q.empty():
#  print(q.get())
print(q.get()[0] * -1)

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 = []
    heapq.heapify(hq)  # リストhqのheap化

    for i in range(N):
        heapq.heappush(hq, (-(A[i]*F[i]), A[i], F[i]))  # heap化されたリストhqに要素xを追加

    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:
import heapq
def makelist(a,f):
    return [a*f*-1,a,f]

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

li =list(map(makelist,A,F))

hq = []
notwork = []
for i in li:
    if i[1] == 0:
        notwork.append(i)
    else:
        heapq.heappush(hq,i)
    # print(hq)

for i in range(K):
    while len(hq)>0 and hq[0][1] == 0:
        notwork.append(heapq.heappop(hq))

    if len(hq)>0:
        temp = hq[0]
        temp[1] = temp[1] -1
        temp[0] = temp[1]*temp[2]*-1

        heapq.heapreplace(hq,temp)

for i in notwork:
    heapq.heappush(hq,i)

# print(hq)

print(hq[0][0]*-1)

Submission status: Time Limit Exceeded

Solution code:
from heapq import heappop, heappush, heapify
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 = [(-a[i]*f[i], -f[i]) for i in range(n)]
heapify(q)

for i in range(k):
    t, g = heappop(q)
    heappush(q, (t-g, g))

ans, _ = heappop(q)
print(-ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
import bisect
input = sys.stdin.readline
def main():
    n,k = map(int,input().split())
    a = list(map(int,input().split()))
    f = list(map(int,input().split()))
    if sum(a)<=k:
        print(0)
    else:
        a.sort()
        f.sort(reverse=True)
        mul=[]
        for i in range(n):
            mul.append([a[i]*f[i],a[i]])
        mul.sort(reverse=True)
        for _ in range(k):
            mul[0][0]-=(mul[0][0]//mul[0][1])
            mul[0][1]-=1
            mul.sort(reverse=True)
        print(mul[0][0])



main()

Submission status: Time Limit Exceeded

Solution code:
people, tp = map(int,input().split())
cc = list(map(int, input().split()))
costs = list(map(int, input().split()))
pairs = []
for i in range(people):
	min_cost = min(costs)
	costs.remove(min_cost)
	max_cc = max(cc)
	cc.remove(max_cc)
	pairs.append((min_cost, max_cc))

for i in range(tp):
	max_pair = max(pairs, key=lambda pair: pair[0] * pair[1])
	pairs.remove(max_pair)
	if(max_pair[1] == 0):
		break
	pairs.append((max_pair[0],max_pair[1] - 1))

total = 0
for pair in pairs:
	total = max(total, pair[0] * pair[1])
print(total)