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

    F.sort(reverse=True)
    A.sort()
    l = -1
    r = 0
    for i in range(N):
        if A[i]*F[i] > r:
            r = A[i]*F[i]
    r += 1
    while r-l > 1:
        m = (r+l)//2
        q = []
        cost = 0
        for i in range(N):
            if F[i]*A[i] > m:
                cost += (F[i]*A[i]-m)//F[i]
                if (F[i]*A[i]-m)%F[i] != 0:
                    cost += 1
        if cost <= K:
            r = m
        else:
            l = m

    print(r)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10000000)
import heapq
import numpy as np
import math

def main():
    N, K = map(int, input().split())
    A = [int(c) for c in input().split()]
    F = [int(c) for c in input().split()]
    
    A.sort()
    F.sort(reverse=True)

    A = np.array(A)
    F = np.array(F)

    num = 0
    sum_ = A * F
    left = 0
    right = 10 ** 12
    while(num < 100):
        number = 0
        mid = (left + right) // 2
        # print(mid)
        for i in range(len(A)):
            if sum_[i] <= mid:
                continue
            else:
                number += math.ceil((sum_[i] - mid) / F[i])
            # print(number)
        if number > K:
            # 最大値をmidより小さくできない -> 最大値はもっとでかい
            left = mid + 1
        else:
            right = mid
        num += 1
    print(mid)
                
        



if __name__ == '__main__':
    main()

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())))
A.sort()
F=np.array(list(map(int,input().split())))
F.sort()
F=F[::-1]
def is_ok(x):
  C=A-x//F
  c=sum(C[C>=0])
  if c<=k:
    return True
  else:
    return False
l=0
r=A[-1]*F[0]
while l!=r:
  m=(l+r)//2
  if is_ok(m):
    r=m
  else:
    l=m+1
print(l)

Submission status: Time Limit Exceeded

Solution code:
import sys
from bisect import bisect_left


def solve():
    input = sys.stdin.readline
    N, K = map(int, input().split())
    A = [int(a) for a in input().split()]
    F = [int(f) for f in input().split()]
    A.sort(reverse = True)
    F.sort()
    low, high = -1, 10 ** 12 + 1
    while high - low > 1:
        mid = (low + high) // 2
        count = 0
        for i in range(N):
            if A[i] * F[i] > mid: count += A[i] - (mid // F[i])
        if count > K: low = mid
        else: high = mid
    print(high)

    return 0

if __name__ == "__main__":
    solve()

Submission status: Time Limit Exceeded

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

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


def check(x):
    red = [a[i] - x // f[i] for i in range(n) if a[i] * f[i] > x]
    red = sum(red)
    return (red <= k)


lb = -1  # false
ub = max([a[i] * f[i] for i in range(n)])  # true
while ub - lb > 1:
    mid = (ub + lb) // 2
    if check(mid):
        ub = mid
    else:
        lb = mid

print(ub)

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())))
a.sort()
f.sort()
b = f[::-1]
p = list(a*f[::-1])

for i in range(K):
    x = max(p)
    y = p.index(x)
    a[y] -= 1
    p[y] -= b[y]
    if sum(a) == 0:
        break

print(max(p))

Submission status: Time Limit Exceeded

Solution code:
N,K = map(int, input().strip().split(' '))
A = sorted(list(map(int, input().strip().split(' '))), reverse=True)
F = sorted(list(map(int, input().strip().split(' '))))
if sum(A) <= K:
  print(0)
  exit()

ng = 0
ok = max([a*f for a,f in zip(A,F)])
while True:
  if ok-ng < 2: break
  k = int((ng+ok)/2)
  now_cost = 0
  for a,f in zip(A,F):
    while a*f > k:
      a -= 1
      now_cost += 1
    if now_cost > K: break
  if now_cost > K:
    ng = k
  else:
    ok = k
    
print(ok)

Submission status: Time Limit Exceeded

Solution code:
import sys
import bisect
import math
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)]
t = A[N-1] * F[N-1]
B.sort(key=lambda x: x[0]*x[1])

for x in range(B[N-1][0]*B[N-1][1]):
    table = [0]*N
    for i in range(N):
        if B[i][0]*B[i][1] > x:
            k = x // B[i][1]
            table[i] = max(B[i][0] - k, 0)
    if sum(table) <= K:
        print(x)
        break

Submission status: Time Limit Exceeded

Solution code:
# At ABC 144 E (500)
n, k = map(int, input().split())
A = list(map(int, input().split()))
F = list(map(int, input().split()))

# N foods, difficuly F[i]
# max(A[i] * F[i])
# K trainings to reduce F[i] to at most 0

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

import heapq
Z = [] # heap
for i in range(n):
    mul = -1 * F[i] * A[i]
    t = (mul, F[i], A[i])
    if mul != 0:
        heapq.heappush(Z, t)
import math
while k > 0 and Z:
    m0, f0, a0 = heapq.heappop(Z)
    if Z:
        m1, _, _ = Z[0]
    else:
        m1 = m0
    m0 *= -1
    m1 *= -1
    if m0 == m1:
        a0 -= 1
        k -= 1
    else:
        # reduce a0 to make f1*a1 larger
        diff = m0 - m1
        assert diff >= 0
        div = diff / a0
        div = int(math.floor(div)) + 1
        assert div > 0
        div = min([div, k, a0])
        a0 -= div
        k -= div
    if a0 > 0:
        heapq.heappush(Z, (-1 * f0 * a0, f0, a0))

ans = 0
for i in range(len(Z)):
    ans = max(ans, Z[i][1] * Z[i][2])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from heapq import *
def abc144c():
  n,k=map(int,input().split())
  a=sorted(list(map(int,input().split())))
  f=sorted(list(map(int,input().split())))[::-1]
  q=[]
 
  for i,j in zip(a,f):
    heappush(q,(-i*j,j))
  for i in range(k):
    tmp,key=heappop(q)
    heappush(q,(tmp+key,key))
  ans,key=heappop(q)
  print(-ans)
abc144c()

Submission status: Time Limit Exceeded

Solution code:
import heapq
n, k = (int(i) for i in input().split())
a = [int(i) for i in input().split()]
f = [int(i) for i in input().split()]
a.sort()
f.sort(reverse=True)
h = []
for i in range(n):
    heapq.heappush(h, (-a[i]*f[i], a[i], f[i]))
cnt = k
for _ in range(k):
    af, aa, ff = heapq.heappop(h)
    if aa == 0:
        break
    af2, aa2, ff2 = heapq.heappop(h)
    x = max(1, min(((-1)*af - (-1)*af) // ff, cnt))
    cnt = cnt - x
    heapq.heappush(h, (af+ff*x, aa-x,ff))
    heapq.heappush(h, (af2, aa2, ff2))
    #print(h)
    if cnt == 0:
        break
print(heapq.heappop(h)[0]*(-1))

Submission status: Time Limit Exceeded

Solution code:
import bisect

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:
    left = 0
    right = max([A[i]*F[i] for i in range(N)])
    while left + 1 != right:
        mid = int((left + right) / 2)
        train_n = 0
        for i in range(N):
            a = A[i]
            while a*F[i] > mid:
                a -= 1
                train_n += 1
        if train_n > K:
            left = mid
        else:
            right = mid
    print(right)

Submission status: Time Limit Exceeded

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

_n, _k = map(int, input().split())
_a = input()[:-1]
_f = input()[:-1]
alist = [item for item in map(int, _a.split())]
flist = [item for item in map(int, _f.split())]


def chker(costs, diffs, max_k):
    if sum(costs) <= max_k:
        print(0)
        return

    costs.sort()
    diffs.sort(reverse=True)
    sclist = sorted([[item1, item2, item1 * item2] for item1, item2 in zip(costs, diffs)], key=lambda x:x[2], reverse=True)
    k = max_k
    while k > 0:
        sclist[0][0] = sclist[0][0] - 1
        if sclist[0][0] == 0:
            sclist = sclist[1:]
        else:
            sclist[0][2] = sclist[0][0] * sclist[0][1]
            sclist.sort(key=lambda x:x[2], reverse=True)

        k -= 1

    print(sclist[0][2])


chker(alist, flist, _k)

Submission status: Time Limit Exceeded

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

def chk(x):
  ct=0
  for i in range(n):
    a,f=A[i],F[-i-1]
    ct+=max(0,a-x//f)
    if ct>k:
      return False
  if ct<=k:
    return True
  else:
    return False
l=0
r=0
for i in range(n):
  r=max(r,A[i]*F[-i-1])

while r-l>1:
  m=(l+r)//2
  if chk(m):
    r=m
  else:
    l=m

if chk(l):
  print(l)
else:
  print(r)

Submission status: Time Limit Exceeded

Solution code:
import math
import numpy as np
N,K =map(int,input().split( ))
A=[int(i) for i in input().split( )]
F=[int(i) for i in input().split( )]
A=sorted(A)
F=sorted(F,reverse=True)
cost=[0]*N
for i in range(N):
    cost[i]=A[i]*F[i]
cost_add=np.argsort(cost)
i=-1
if K<sum(A):
    shugyou=0
    for _ in range(K):
        cost_add[-1]
        shu=((cost[cost_add[-1]]-cost[cost_add[-2]])//F[cost_add[-1]]+1)
        cost[cost_add[-1]]-=shu*F[cost_add[-1]]
        shugyou+=shu
        for i in range(1,N):
            if cost[cost_add[-i]]<cost[cost_add[-1-i]]:
                m=cost_add[-i]
                cost_add[-i]=cost_add[-1-i]
                cost_add[-1-i]=m
            else:
                break
    print(cost[cost_add[-1]])

else:
    print('0')

Submission status: Time Limit Exceeded

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

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

for ans in range(a[-1] * f[-1] + 1):
    tmp = 0
    for x, y in zip(a,f):
        tmp += x - math.floor(ans / y)
    if tmp <= k:
        break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import heapq


def calc(N, K, A, F):
    if N == 1:
        return max((A[0] - K) * F[0], 0)
    A.sort()
    F = sorted(F, key=lambda x: -x)
    h = []
    for i in range(N):
        heapq.heappush(h, (- A[i] * F[i], A[i], F[i]))
    t1 = heapq.heappop(h)
    t2 = heapq.heappop(h)
    while K > 0:
        d = (-t1[0] + t2[0]) // t1[2]
        e = min(d + 1, K)
        heapq.heappush(h, (-(t1[1] - e) * t1[2], t1[1] - e, t1[2]))
        K -= e
        t1 = t2
        t2 = heapq.heappop(h)
        if t1[0] >= 0:
            return 0
    return -t1[0]


(N, K) = tuple([int(s) for s in input().split()])
A = [int(s) for s in input().split()]
F = [int(s) for s in input().split()]
print(calc(N, K, A, F))

Submission status: Time Limit Exceeded

Solution code:
from math import*
n,k=map(int,input().split())
*a,=map(int,input().split())
*f,=map(int,input().split())
ref=sorted([(x*y,y) for x,y in zip(sorted(a),sorted(f)[::-1])])[::-1]
l=-1
r=10**12
while r-l>1:
	m=(l+r)//2
	rest=k
	flg=1
	for z,y in ref:
		if m>=z:
			break
		if rest>=ceil((z-m)/y):
			rest-=ceil((z-m)/y)
		else:
			flg=0
			break
	if flg:
		r=m
	else:
		l=m
print(r)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
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))

l=-1
r=10**12+2
mid=(l+r)//2
while r-l>1:
    m=0
    for i in range(N):
        a=mid//F[i]
        m+=max(0,A[i]-a)
    if m>K:
        l=mid
    else:
        r=mid
    mid=(l+r)//2
print(l+1)

Submission status: Time Limit Exceeded

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

a.sort()
f.sort(reverse=True)
s = []
for x,y in zip(a,f):
    s.append((x,y))

for ans in range(s[-1][0] * s[-1][1] + 1):
    tmp = 0
    for x, y in s:
        tmp += x - math.floor(ans / y)
    if tmp <= k:
        break
print(ans)

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

while K != 0:
    s = np.argmax(S)
    if A[s] == 0:
        break
    A[s] -= 1
    S[s] = A[s] * F[s]
    K -= 1
    
print(max(S))

Submission status: Time Limit Exceeded

Solution code:
from heapq import heappush, heappop
n, k = map(int, input().split())
a = list(map(int, input().split()))
f = list(map(int, input().split()))

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


def check(x):
    red = 0
    for i in range(n):
        if a[i] * f[i] > x:
            red += a[i] - x // f[i]

    return (red <= k)


lb = -1  # false
ub = max([a[i] * f[i] for i in range(n)])  # true
while ub - lb > 1:
    mid = (ub + lb) // 2
    if check(mid):
        ub = mid
    else:
        lb = mid

print(ub)

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


#F.sort(reverse = True)
#A.sort()
cost = max(sum(A)-K,0)
#print(A)
#print(F)
F = np.array(F)
A = np.array(A)

furi =[0]*N

temp =np.copy(F)
for i in range(cost):
  ind = np.argmin(temp)
  temp[ind] = temp[ind]+F[ind]
  furi[ind] += 1
  

print(max(furi*F))

Submission status: Time Limit Exceeded

Solution code:


n,k = map(int,input().split())	
p_li = list(map(int,input().split()))
f_li = list(map(int,input().split()))

p_li.sort()

f_li.sort()
f_li.reverse()

p_f=[ p_li[i]*f_li[i] for i in range(n)]


while(k>0):
    max_index=p_f.index(max(p_f))
    p_li[max_index]-=1
    p_f[max_index]=p_li[max_index]*f_li[max_index]
   
    k-=1
print(max(p_f))

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

for i in range(k):
    index_time = time.index(max(time))
    time[index_time] -= f[index_time]
print(max(time))

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):
    if sum(A) == 0:
        break
    else:
        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:
n, k = map(int, input().split())
a = list(map(int, input().split()))
f = list(map(int, input().split()))
a.sort()
f.sort(reverse = True)
a = [[-a[i]*f[i], a[i], f[i]] for i in range(n)]
import heapq
heapq.heapify(a)

for i in range(k):
    h = heapq.heappop(a)
    if h[1] > 0:
        h[1] -= 1
        h[0] = -h[2]*h[1]
        heapq.heappush(a, h)
    else:
        heapq.heappush(a, h)
        break

result = heapq.heappop(a)
print(-result[0])

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
f=list(map(int,input().split()))
a.sort()
if sum(a)<=k:
    print(0)
    exit()
f.sort(reverse=True)
l=0
r=10**12
x=(r+l)//2
def check(x):
    p=0
    for i in range(n):
        if x//f[i]<a[i]:
            p+=a[i]-x//f[i]
            if p>k:
                return False
    if p>k:
        return False
    else:
        return True
while l<r:
    if check(x):
        r=x
        x=(r+l)//2
    else:
        l=x
        x=(r+l)//2
print(r)

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


F.sort(reverse = True)
A.sort()
cost = max(sum(A)-K,0)
#print(A)
#print(F)
F = np.array(F)
A = np.array(A)

furi =[0]*N
#furi = np.array(furi)


temp =np.copy(F)
for i in range(cost):
  ind = np.argmin(temp)
  temp[ind] = temp[ind]+F[ind]
  furi[ind] += 1
  

print(max(furi*F))

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
A = [int(a) for a in input().split()]
F = [-int(a) for a in input().split()]

A = sorted(A)
F = sorted(F)

import numpy as np

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

for i in range(k):
    _max = np.argmax(ans)
    ans[_max] = max(0, ans[_max]+F[_max])

print(max(ans))

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
c=sum(A)
sss=c-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(reverse=True)
f.sort()
#print(a);print(f)

from heapq import *
q=[]
for i,j in zip(a,f):
  #print(i*j,i,j)
  heappush(q,(-i*j,str(j)))
#print(q)
for i in range(k):
  temp,key=heappop(q)
  heappush(q,(temp+int(key),key))
  
#print(q)

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

Submission status: Time Limit Exceeded

Solution code:
from heapq import heapify, 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)
af = tuple(zip(A, F))

v = [i * j for i, j in af]

heap = list(zip([-i for i in v], A, F))
heapify(heap)

for _ in range(K):
    a, b, c = heappop(heap)
    a = -a
    new = (a - 1) * b
    heappush(heap, (-(b - 1) * c, b - 1, c))

a, b, c = heappop(heap)
print(-a)

Submission status: Time Limit Exceeded

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

A_n = np.array(A)
F_n = np.array(F)
A_sort = np.sort(A_n)
F_sort = np.sort(F_n)[::-1]
 
for j in range(K):
    A_sort.sort()

    np_list = A_sort*F_sort
    max_index = np_list.argmax()
    if max(A_sort) == 0:
        break
    else:
        A_sort[max_index] -= 1
    
ans = 0
A_sort.sort()
print(max(A_sort*F_sort))

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
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())
alist = [s for s in map(int,input().split())]
flist = [s for s in map(int,input().split())]
alist.sort()
flist.sort(reverse=True)
t = [alist[i]*flist[i] for i in range(n)]
for i in range(k):
  ind = t.index(max(t))
  tmp = t[ind]
  del t[ind]
  t.insert(ind, tmp - flist[ind])
print(max(t))

Submission status: Time Limit Exceeded

Solution code:
inp = input().split(" ")
n = int(inp[0])
k = int(inp[1])

a = input().split(" ")
a = [int(i) for i in a]
f = input().split(" ")
f = [int(i) for i in f]

while k >= 0:

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

  kekka = [a[i] * f[i] for i in list(range(0,n))]

  cd = [i for i in list(range(0, len(kekka))) if kekka[i] == max(kekka)]

  if(len(cd) <= k):
    for x in cd:
      a[x] = a[x] - 1
    k = k - len(cd)
  else:
    print(max(max(kekka), 0))
    break

Submission status: Time Limit Exceeded

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

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

  ans = [A[i]*F[i] for i in range(N)]
  for _ in range(K):
    m = max(ans)
    i = ans.index(m)
    ans[i] -= F[i]

  print(max(max(ans), 0))

if __name__ == '__main__':
  solve()

Submission status: Time Limit Exceeded

Solution code:
#gcdは3.4.3だとfractionsにある
from fractions import gcd
import math

from math import factorial as f

from math import ceil,floor,sqrt

from sys import exit

from copy import deepcopy
import numpy as np

import bisect
import re
import heapq


ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))



def main():
    n,k = mi()
    a = li()
    f = li()
    a = sorted(a,reverse=True)
    f.sort()
    su = 0
    for i in a:
        su+=i
    s = su-k
    if s <=0:
        print(0)
        exit()
    
    gyaku = np.array(f,dtype=np.float64)
    t = np.reciprocal(gyaku/sum(gyaku))
    ideal = (s*(t/sum(t))).astype("int64")
    need = 0
    for i in range(n):
        need+=max(a[i]-ideal[i],0)
    need -= k
    #print(a)
    #print("ideal is ",ideal)
    #print("need is ", need)
    if need == 0:
        ans = np.array(f)*ideal
        print(max(ans))
        exit()
    h = []
    for i in range(n):
        h.append([(ideal[i]+1)*f[i],f[i]])
    heapq.heapify(h)

    for i in range(need):
        tmp = heapq.heappop(h)
        tmp = [tmp[0]+tmp[1],tmp[1]]
        heapq.heappush(h,tmp)

    ans =0
    for i in range(n):
        ans = max(ans,h[i][0]-h[i][1])
    #print(f)
    print(ans)
    #ans = ans*np.array(f)
    #print(ans)

    


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

k = 0
flag = False
pairs.sort(key=lambda pair: -pair[0] * pair[1])
pair = pairs.pop(0)
while True:
	snd_max = pairs[0]
	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
		factor = (int(snd_total / pair[0]) - 1)
		scalar = max(1, min(pair[1] - factor - 1, tp - k))
		k += scalar
		pair[1] -= scalar
	if flag:
		print(max(pair[0] * pair[1], snd_total))
		break
	pair_score = pair[0] * pair[1]

	fflag = False
	for i in range(len(pairs)):
		current = pairs[i]
		if pair_score > current[0] * current[1]:
			pairs.insert(i, pair)
			fflag   = True
			break
	if not fflag:
		pairs.append(pair)
	pairs.remove(snd_max)
	pair = snd_max

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
            if mul[0][0]<mul[1][0]:
                mul.sort(reverse=True)
        print(mul[0][0])
main()

Submission status: Time Limit Exceeded

Solution code:
import heapq


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)

    h = []
    for a_e, f_e in zip(a, f):
        heapq.heappush(h, [-a_e * f_e, a_e, f_e])

    for _ in range(k):
        if not h:
            break

        m_e, a_e, f_e = heapq.heappop(h)
        if a_e != 1:
            heapq.heappush(h, [-(a_e-1) * f_e, a_e - 1, f_e])

    if h:
        ans, _, _ = heapq.heappop(h)
        ans = -ans
    else:
        ans = 0

    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()))
A.sort()
F.sort(reverse=True)
AF = [A[i] * F[i] for i in range(N)]
for _ in range(K):
    j = AF.index(max(AF))
    A[j] -= 1
    AF[j] = A[j] * F[j]
print(max(AF))

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

for i in range(K):
    for j in range(N):
        AF[j] = A[j]*F[j]
    v = max(AF)
    maxIndex = AF.index(v)
    A[maxIndex] -= 1

for j in range(N):
      AF[j] = A[j]*F[j]

v = max(AF)
print(v)

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]) for i in range(N)]
    heapq.heapify(q)
    for _ in range(K):
        s,a = q[0]
        if s == 0:
            break
        heapq.heappushpop(q,(s*(a-1)//a,a-1))
    
    print(q[0][0]*(-1))
resolve()

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)
l = list(zip(A, F))


class Contest():
    def __init__(self, a, f):
        self.a = a
        self.f = f
    
    def __lt__(self, other):
        return self.a * self.f < other.a * other.f

h = []
for i in l:
    heappush(h, Contest(i[0], -1 * i[1]))


for k in range(K):
    a = heappop(h)
    heappush(h, Contest(a.a - 1, a.f))

ans = heappop(h)
print(-1 * ans.a * ans.f)

Submission status: Time Limit Exceeded

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

    T = []
    for i in range(N):
        t = A[i]*F[i]
        heapq.heappush(T, [t*(-1), F[i]*(-1), A[i]*(-1)])
    
    L = heapq.heappop(T)
    t, f, a = L[0]*(-1), L[1]*(-1), L[2]*(-1)
    ans = t
    heapq.heappush(T, [t*(-1), f*(-1), a*(-1)])
    for _ in range(K):
        # print(T)
        L = heapq.heappop(T)
        t, f, a = L[0]*(-1), L[1]*(-1), L[2]*(-1)
        if t==0:
            break
        a -= 1
        t = a*f
        heapq.heappush(T, [t*(-1), f*(-1), a*(-1)])
    L = heapq.heappop(T)
    t, f, a = L[0]*(-1), L[1]*(-1), L[2]*(-1)
    ans = t
    print(ans)


if __name__ == '__main__':
    main()

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]
S=np.sum(A)
CC=np.array([AA[i]*BB[i] for i in range(N)])
if K>S:
    K=S

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

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:
                if score[1][1] - F[0] > 0:
                    K -= score[1][1] - F[0]
                    new_A = F[0]
                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:

n, k = map(int, input().split())
l=list(map(int, input().split()))
f=list(map(int, input().split()))
t=[]
ans=int(0)
l.sort()
f.sort()
f.reverse()


for i in range(k+1):
    t=[]
    l.sort()
    for i in range(n):
        t.append(l[i]*f[i])


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

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

Submission status: Time Limit Exceeded

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

priority_que = []
N, K = map(int, input().split())
A = sorted(map(int, input().strip().split()))
F = sorted(map(int, input().strip().split()))[::-1]
if sum(A) <= K:
    print(0)
    exit()
hpop = heapq.heappushpop
hpush = heapq.heappush


for i, a in enumerate(A):
    f = F[i]
    key = f*a
    while key >= 0:
        # print(key)
        if len(priority_que) > K:
            if key <= priority_que[0]:
                break
            hpop(priority_que, key)
        else:
            hpush(priority_que, key)
        key -= f

# print(priority_que)
print(heapq.heappop(priority_que))

Submission status: Time Limit Exceeded

Solution code:
import math
import numpy as np
N,K =map(int,input().split( ))
A=[int(i) for i in input().split( )]
F=[int(i) for i in input().split( )]
A=sorted(A)
F=sorted(F,reverse=True)
cost=[0]*N
for i in range(N):
    cost[i]=A[i]*F[i]
cost_add=np.argsort(cost)
i=-1
shugyou=0
for _ in range(K):
    add=cost_add[-1]
    cost[add]-=F[add]
    for i in range(1,N):
        if cost[cost_add[-i]]<cost[cost_add[-1-i]]:
            m=cost_add[-i]
            cost_add[-i]=cost_add[-1-i]
            cost_add[-1-i]=m
        else:
            break

print(cost[cost_add[-1]])