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/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
    k = K
    while k > 0:
        af = a * f
        i = np.argmax(af)
        af = np.sort(af)
        n = min(k, (af[-1]-af[-2]) // f[i], a[i])
        if n <= 0:
            n = 1
        a[i] = a[i] - n
        k = k - n

    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:
from operator import itemgetter
import heapq
 
a,b = list(map(int,input().split()))
l = list(map(int,input().split()))
L = list(map(int,input().split()))
l.sort()
L.sort(reverse = True)
res = []
ans = 0
for i,j in zip (l,L):
    res.append([-i*j,-j,i])
    ans += abs(i*j)

a = sorted(res, key=itemgetter(0), reverse=True)
heapq.heapify(a)
# print(a)
k = b

while k!= 0 and ans != 0:
    t = heapq.heappop(a)
    # print(0,t,ans)
    if t[2]>0:
        ans +=t[0]
        s = -t[0]
        t[2]-=1
        k-=1
        t[0] = t[1]*t[2]
        ans += -t[1]*t[2]
        heapq.heappush(a,t)

        # print(1,t,ans,s+t[1]*t[2])
    # print(ans)
# print(ans)
# print(a)
print(abs(a[0][0]))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

def train(a,f,k):
    f.sort(reverse=True)
    F_ = np.array(f)
    while k != 0:
        a.sort()
        A_ = np.array(a)
        X = A_*F_
        i = np.argmax(X)
        X.sort()
        x = X[-1] - X[-2]
        y = x // a[i] + 1
        if k >= y:
            a[i] -= y
            k -= y
        else:
            a[i] -= k
            k = 0
    a.sort()
    A_ = np.array(a)
#    print(A_)
    return max(A_ *  F_)

#    return(a)




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

c = train(A, F, K)
if c < 0:
    c = 0

print(c)

Submission status: Time Limit Exceeded

Solution code:

# D = list(map(int, input().split()))
import bisect

D = input().split()
N = int(D[0])
K = int(D[1])
A = list(map(int, input().split()))
F = list(map(int, input().split()))

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

data = []
for i in range(N):
    d = A[i] * F[i]
    data.append({'r': d, 'a': A[i], 'b': F[i]})

sort_data = sorted(data, key=lambda x:x['r'])

for i in range(K):
    d = (sort_data[-1]['a'] - 1) * sort_data[-1]['b']
    sort_data[-1] = {'r': d, 'a': sort_data[-1]['a'] - 1, 'b': sort_data[-1]['b']}
    sort_data = sorted(sort_data, key=lambda x:x['r'])

a = sort_data.pop(-1)
print(a['r'])

Submission status: Time Limit Exceeded

Solution code:
import heapq, math

N, K = map(int, input().split())

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

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

costHeap = []

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

cost = K
trainTarget = heapq.heappop(costHeap)
while cost > 0:
    nextMax = costHeap[0][0]
    trainCost = min(cost, math.ceil((nextMax - trainTarget[0] + 1)/trainTarget[2]))
    cost -= trainCost
    trainedA = trainTarget[1] - trainCost
    trainTarget = heapq.heappushpop(costHeap, (trainedA * trainTarget[2] * (-1), trainedA, trainTarget[2]))

print(trainTarget[0] * -1)

Submission status: Time Limit Exceeded

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


for _ in range(K+1):
    summary = []
    for i,j in zip(A, F):
        summary.append(i*j)
    tgt = summary.index(max(summary))
    A[tgt] -= 1
print(max(summary))

Submission status: Time Limit Exceeded

Solution code:
N, K = input().split()
A_input = input().split()
F_input = input().split()
#print('end')
N = int(N)
K = int(K)
A_list = [int(A) for A in A_input]
F_list = [int(F) for F in F_input]
score = -1
#print('end')
while K > 0 :#or score != 0:
    A_list.sort(reverse=True)
    F_list.sort(reverse=False)
    #print(A_list)
    #print(F_list)
    a_by_f_list = [x * y for (x, y) in zip(A_list, F_list)]
    place = a_by_f_list.index(max(a_by_f_list))
    A_list[place] -= 1
    K -= 1
    #print(K)

#A_list.sort(reverse=True)
#F_list.sort(reverse=False)
a_by_f_list = [x * y for (x, y) in zip(A_list, F_list)]
print(max(a_by_f_list))

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

F = sorted(F, reverse = True)
A = sorted(A)
flg = 0
heap = []

for i in range(N):
	if A[i] == 0 or F[i] == 0:
		break
	else:
		heapq.heappush(heap, [- A[i] * F[i], F[i]])
	
for i in range(K):
	score, tmp_F = heapq.heappop(heap)
	s_score, s_tmp_F = heapq.heappop(heap)
	
	
	if score < 0:
		while s_score >= score:
			score += tmp_F
		heapq.heappush(heap, [score, tmp_F])
		heapq.heappush(heap, [s_score, s_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
import sys
import bisect
N,K = map(int,input().split())
A=np.array([int(i) for i in input().split()])
F=np.array([int(i) for i in input().split()])
A=np.sort(A)
F=np.sort(F)[::-1]
#print(A)
#print(F)
B = A*F
#print(B)
x=np.argsort(B)
B=B[x]
F=F[x]
#print()
#print(F)
#print(B)
for j in range(K):
    index = bisect.bisect_left(B,B[-1]-F[-1])
    B = np.insert(B, index, B[-1]-F[-1])
    F = np.insert(F, index, F[-1])
    B = np.delete(B,-1)
    F = np.delete(F,-1)
    if B[-1] == 0:
        print(0)
        sys.exit()
print(B[-1])

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))
  del t[ind]
  tmp = alist[ind]
  del alist[ind]
  alist.insert(ind, tmp-1)
  t.insert(ind, alist[ind]*flist[ind])
print(max(t))

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)

while k > 0 and Z:
    _, f0, a0 = heapq.heappop(Z)
    if a0 > 0:
        a0 -= 1
        k -= 1
    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 operator import itemgetter
import heapq
 
a,b = list(map(int,input().split()))
l = list(map(int,input().split()))
L = list(map(int,input().split()))
l.sort()
L.sort(reverse = True)
res = []
ans = 0
for i,j in zip (l,L):
    res.append([-i*j,-j,i])
    ans += abs(i*j)

a = sorted(res, key=itemgetter(0), reverse=True)
heapq.heapify(a)
# print(a)
k = b

while k!= 0 and ans != 0 and len(a):
    t = heapq.heappop(a)
    # print(0,t,ans)
    if t[2]>0:
        ans +=t[0]
        s = -t[0]
        t[2]-=1
        k-=1
        t[0] = t[1]*t[2]
        ans += -t[1]*t[2]
        heapq.heappush(a,t)

        # print(1,t,ans,s+t[1]*t[2])
    # print(ans)
# print(ans)
# print(a)
print(abs(a[0][0]))

Submission status: Time Limit Exceeded

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

a = [int(x) for x in a]
f = [int(x) for x in f]

a.sort()
f.sort()

a = a[::-1]
tmp = 0
time_max = 0

for i in range(k):
    time_max = 0
    for j in range(n):
        time_j = a[j]*f[j]
        if time_max < time_j:
            time_max = time_j
            tmp = j
        if time_max == 0:
            break
    if time_max == 0:
        break
    a[tmp] = a[tmp] - 1

time_max = 0
for j in range(n):
    time_j = a[j]*f[j]
    if time_max < time_j:
        time_max = time_j

print(time_max)

Submission status: Time Limit Exceeded

Solution code:
import sys

input = sys.stdin.readline


def read_i():
    return list(map(int, input().split()))


def check(m, k, vs):
    s = 0
    for x, y, v in vs:
        if v >= m:
            s += (v - m + y - 1) // y
    return s <= k


n, k = read_i()
ls0 = sorted(read_i())
ls1 = sorted(read_i(), reverse=True)
if sum(ls0) <= k:
    print(0)
    exit()

vs = [(x, y, x * y) for x, y in zip(ls0, ls1)]

lo = 0
hi = sum(v for _, _, v in vs)
while hi - lo > 1:
    mid = (lo + hi) // 2
    if check(mid, k, vs):
        hi = mid
    else:
        lo = mid
print(hi)

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=max(A[i]*F[i] for i in range(n))
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 bisect
import math
(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 = []
    t = []
    for x,y in zip(a,f):
        s.append((x*y, y))
    s.sort()

    while len(s) > 1:
        t.append(s[-1])
        tmp = 0
        for l, y in t:
            tmp += math.ceil((l - s[-2][0]) / y)
        if k <= tmp:
            t = t[:-1]
            break
        s = s[:-1]
    
    for l, y in t:
        tmp = math.ceil((l - s[-1][0]) / y)
        k -= tmp
        l -= tmp * y
        bisect.insort(s, (l, y))
    
    while k > 0 and s[-1][0] > 0:
        l, y = s[-1]
        s = s[:-1]
        tmp = math.ceil((l - s[-1][0]) / y)
        if tmp == 0:
            tmp = 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()))
digest_power = list(map(int,input().split()))
digest_power.sort()
digest_power.reverse()
food_eat_difficulty = list(map(int,input().split()))
food_eat_difficulty.sort()

h = []

for i in range(n):
    heapq.heappush(h, [-1 * digest_power[i]*food_eat_difficulty[i], food_eat_difficulty[i], digest_power[i], 0])

for i in range(k):
    highest_cost = heapq.heappop(h)
    #print(highest_cost)
    if highest_cost[0] == 0:
        heapq.heappush(h, highest_cost)
        break
    if highest_cost[2] == highest_cost[3]:
        heapq.heappush(h, highest_cost)
        break
    
    highest_cost[0] += highest_cost[1]
    highest_cost[2] += 1
    heapq.heappush(h, highest_cost)
          
ans = -1*heapq.heappop(h)[0]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys, math


def solve(N: int, K: int, A: "List[int]", F: "List[int]"):
    A = sorted(A)
    F = sorted(F, reverse=True)
    B = [a*f for a, f in zip(A, F)]
    b = max(B)
    left = -1
    right = b

    def ok(mid):
        r = 0
        for i in range(N):
            a, f = A[i], F[i]
            r += max(0, a - mid//f)
        if r > K:
            return True
        else:
            return False

    while right - left > 1:
        mid = (right + left) // 2
        if ok(mid):
            left = mid
        else:
            right = mid
    print(right)
    return


# Generated by 1.1.5 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    F = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, K, A, F)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import heapq
import sys


n,k = [int(_) for _ in input().split()]
A = [int(_) for _ in input().split()]
F = [int(_) for _ in input().split()]
a = sorted(A,reverse=True)
f = sorted(F)
l = []
for i in range(n):
    l.append([-a[i]*f[i],a[i],f[i]])
heapq.heapify(l)
for i in range(k):
    t_v,t_a,t_f = heapq.heappop(l)
    if t_a == 0:
        print(0)
        sys.exit()
    else:
        t_a -= 1
        heapq.heappush(l, [-t_a*t_f, t_a, t_f])
ans = (-1) * heapq.heappop(l)[0]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
D = input().split()
N = int(D[0])
K = int(D[1])
A = list(map(int, input().split()))
F = list(map(int, input().split()))

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

data = []
for i in range(N):
    d = A[i] * F[i]
    data.append({'r': d, 'a': A[i], 'b': F[i]})

sort_data = sorted(data, key=lambda x:x['r'])

for i in range(K):
    a = sort_data.pop(-1)
    d = (a['a'] - 1) * a['b']
    tmp = {'r': d, 'a': a['a'] - 1, 'b': a['b']}
    sort_data.append(tmp)
    sort_data = sorted(sort_data, key=lambda x:x['r'])

a = sort_data.pop(-1)
print(a['r'])

Submission status: Time Limit Exceeded

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

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

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

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)
m=0
for i in range(n):
    m=max(m,a[i]*f[i])
x=m//2
l=0
r=m
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 bisect
import math
(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 = []
    t = []
    for x,y in zip(a,f):
        s.append((x*y, y))
    s.sort()

    while len(s) > 1:
        t.append(s[-1])
        tmp = 0
        for l, y in t:
            tmp += max(1, math.ceil((l - s[-2][0]) / y))
        if k <= tmp:
            t = t[:-1]
            break
        s = s[:-1]
    
    for l, y in t:
        tmp = max(1, math.ceil((l - s[-1][0]) / y))
        k -= tmp
        l -= tmp * y
        bisect.insort(s, (l, y))
    
    while k > 0 and s[-1][0] > 0:
        l, y = s[-1]
        s = s[:-1]
        tmp = math.ceil((l - s[-1][0]) / y)
        if tmp == 0:
            tmp = 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:
from operator import itemgetter
import heapq
 
a,b = list(map(int,input().split()))
l = list(map(int,input().split()))
L = list(map(int,input().split()))
l.sort()
L.sort()
L = L[::-1]
res = []
ans = 0
for i,j in zip (l,L):
    res.append([-i*j,-j,i])
    ans += abs(i*j)

a = sorted(res, key=itemgetter(0), reverse=True)
heapq.heapify(a)
# print(a)
k = b

while k!= 0 and ans != 0 and len(a):
    t = heapq.heappop(a)
    # print(0,t,ans)
    if t[2]>0:
        ans +=t[0]
        s = -t[0]
        t[2]-=1
        k-=1
        t[0] = t[1]*t[2]
        ans += -t[1]*t[2]
        heapq.heappush(a,t)

        # print(1,t,ans,s+t[1]*t[2])
    # print(ans)
# print(ans)
# print(a)
print(abs(a[0][0]))

Submission status: Time Limit Exceeded

Solution code:
import sys
input=sys.stdin.readline
import heapq
import math
def main():
  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])
  
if __name__ == '__main__':
  main()

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)
        top=0
        for _ in range(k):
            mul[top][0]-=(mul[top][0]//mul[top][1])
            mul[top][1]-=1
            index=1
            if mul[top][0]<mul[top+1][0]:
                mul.append([0,0])
            while mul[top][0]<mul[top+1][0] and mul[top][0]>=mul[-1-index][0]:
                mul[-index]=mul[-1-index]
                index+=1
            if mul[top][0]<mul[top+1][0]:
                mul[-index]=mul[top]
                mul[top]=[0,0]
                top+=1
        print(mul[top][0])

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

    k = K
    while k > 0:
        p, a, f = heapq.heappop(q)
        if p < 0:
            nextp = q[0][0]
            tmp = min(min(max((nextp-p+f-1)//f, 1), k), a)
            heapq.heappush(q, ((-(a-tmp)*f), a-tmp, f))
            k -= tmp
        else:
            break

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

main()

Submission status: Time Limit Exceeded

Solution code:
from operator import itemgetter
import heapq
 
a,b = list(map(int,input().split()))
l = list(map(int,input().split()))
L = list(map(int,input().split()))
l.sort()
L.sort()
L = L[::-1]
res = []
ans = 0
for i,j in zip (l,L):
    res.append([-i*j,-j,i])
    ans += abs(i*j)

a = sorted(res, key=itemgetter(0), reverse=True)
heapq.heapify(a)
# print(a)
k = b

while k!= 0 and ans != 0 and len(a):
    t = heapq.heappop(a)
    # print(0,t,ans)
    if t[2]>0:
        ans +=t[0]
        s = -t[0]
        t[2]-=1
        k-=1
        t[0] = t[1]*t[2]
        ans += -t[1]*t[2]
        heapq.heappush(a,t)

        # print(1,t,ans,s+t[1]*t[2])
    # print(ans)
# print(ans)
# print(a)
M = 0
for i in a:
    M = max(M,abs(i[0]))


print(M)

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:
    _, f0, a0 = heapq.heappop(Z)
    if a0 > 0:
        a0 -= 1
        k -= 1
    heapq.heappush(Z, (-1 * f0 * a0, f0, a0))

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

Submission status: Time Limit Exceeded

Solution code:
from heapq import heappop, heappush

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

heap = []
for aa,ff in zip(a,f):
    heappush(heap, [-aa*ff, aa, ff])

for _ in range(k):
    cost, aa, ff = heappop(heap)
    if aa == 0:
        break
    aa -= 1 
    heappush(heap, [-aa*ff, aa, ff])
print(-min(heap)[0])

Submission status: Time Limit Exceeded

Solution code:
import heapq
import sys


n,k = [int(_) for _ in input().split()]
A = [int(_) for _ in input().split()]
F = [int(_) for _ in input().split()]
a = sorted(A,reverse=True)
f = sorted(F)
ans = 0
l = []
for i in range(n):
    l.append([-a[i]*f[i],a[i],f[i]])
heapq.heapify(l)
for i in range(k):
    t_v,t_a,t_f = heapq.heappop(l)
    if t_a == 0:
        print(0)
        sys.exit()
    else:
        t_a -= 1
        heapq.heappush(l, [-t_a*t_f, t_a, t_f])
ans = (-1) * heapq.heappop(l)[0]
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)
m=0
for i in range(n):
    m=max(m,a[i]*f[i])
x=m//2
l=0
r=m
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
    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:
N, K =map(int,input().split())
A = list(map(int,input().split()))
F = list(map(int,input().split()))
A.sort(reverse=True)
F.sort()
Seki = [F[i]*A[i] for i in range(N)]
for i in range(K):
    if max(Seki)==0: break
    A[Seki.index(max(Seki))]+=-1
    Seki = [F[i]*A[i] for i in range(N)]
print(max(Seki))

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]))
for _ in range(k):
    af, aa, ff = heapq.heappop(h)
    if aa == 0:
        break
    heapq.heappush(h, (af+ff, aa-1,ff))
print(heapq.heappop(h)[0]*(-1))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

N, K = [int(i) for i in input().split()]
A = np.array(sorted([int(i) for i in input().split()], reverse=True))
F = np.array(sorted([int(i) for i in input().split()]))

for _ in range(K):
    index = np.argmax(A*F)
    if A[index]<=0:
        break
    else:
        A[index]-=1

print(np.max(A*F))

Submission status: Time Limit Exceeded

Solution code:
import sys
import heapq


def main():
    n, k = [int(s) for s in sys.stdin.readline().strip().split()]
    a = [int(s) for s in sys.stdin.readline().strip().split()]
    f = [int(s) for s in sys.stdin.readline().strip().split()]
    # n, k = 3, 5
    # a = [4, 2,1]
    # f = [2, 3, 1]

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

    heap = []
    for i in range(n):
        ai = a[i]
        fi = f[i]
        heap.append((- ai * fi, ai, fi))
    heapq.heapify(heap)

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

    print(- heap[0][0])


main()

Submission status: Time Limit Exceeded

Solution code:
# import sys
import numpy as np

import itertools
import heapq as hq


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

heap = [(-s, a, f) for s, a, f in zip(S, A, F)]

hq.heapify(heap)

for i in range(K):
    s, a, f = hq.heappop(heap)
    if a != 0:
        a -= 1
    s = (a * f)
    hq.heappush(heap, (-s, a, f))

s, a, f = hq.heappop(heap)
print(-s)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

def train(a,f,k):
    f.sort(reverse=True)
    F_ = np.array(f)
    while k != 0:
        a.sort()
        A_ = np.array(a)
        i = np.argmax(A_ * F_)
        
        a[i] -= 1
        k -= 1
    a.sort()
    A_ = np.array(a)
#    print(A_)
    return max(A_ *  F_)

#    return(a)




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

c = train(A, F, K)
if c < 0:
    c = 0

print(c)

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

F = sorted(F, reverse = True)
A = sorted(A)
flg = 0
heap = []

for i in range(N):
	if A[i] == 0 or F[i] == 0:
		break
	else:
		heapq.heappush(heap, [- A[i] * F[i], A[i], F[i]])
	
for i in range(K):
	score, tmp_A, tmp_F = heapq.heappop(heap)
	if tmp_A > 0:
		tmp_A -= 1
		heapq.heappush(heap, [- tmp_A * tmp_F, tmp_A, 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 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
        x = max(1, m1[1] - (m2[1] * m2[2]) // m1[2])
        if x > K:
            x = K
        m1[1] -= x
        K -= x
        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:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import bisect


def main():
  _, k = map(int, input().split())
  a_vals = list(map(int, input().split()))
  a_sum = sum(a_vals) - k
  f_vals = list(map(int, input().split()))
  f_rates = [(1 / v) for v in f_vals]
  f_sum = sum(f_rates)

  if a_sum < 1:
    print(0)
    return

  a_vals = [int(a_sum * v / f_sum) for v in f_rates]
  a_vals.sort(reverse=True)
  f_vals.sort()
  remain = a_sum - sum(a_vals)

  #print(a_vals)
  #print(f_vals)
  #print(remain)
  #print([a * f for a, f in zip(a_vals, f_vals)])

  costs = [[a * f, (a + 1) * f, f] for a, f in zip(a_vals, f_vals)]
  #print(costs, remain)

  while remain != 0:
    costs.sort(key=lambda x: x[1])
    #print('costs', costs, ', remain', remain)
    index = min(bisect.bisect_right([v[1] for v in costs], costs[0][1]), remain)

    for i in range(index):
      costs[i][0] += costs[i][2]
      costs[i][1] += costs[i][2]

    remain -= index
    costs.sort(key=lambda x: x[0])
  #print('final', costs)

  print(max(costs, key=lambda x: x[0])[0])


if __name__ == '__main__':
  main()

Submission status: Time Limit Exceeded

Solution code:
from heapq import heappush, heappop

if __name__ == "__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 i in range(N):
		heappush(h, (A[i]*F[i] * -1, i))
	for _ in range(K):
		cost, i = heappop(h)
		if A[i] == 0:
			heappush(h, (cost, i))
			break
		else:
			A[i] -= 1
			heappush(h, (A[i] * F[i] * -1, i))
	ans, _ = heappop(h)
	print(ans * -1)

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
		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
	pairs.append(pair)
	pair = snd_max
	pairs.remove(pair)

Submission status: Time Limit Exceeded

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

def main():
    N, K = [int(x) for x in input().split()]
    A = [int(x) for x in input().split()]
    F = [int(x) for x in input().split()]

    A_s = sorted(A, reverse=True)
    F_s = sorted(F)
    C = []
    C_h = []
    for i in range(N):
        x = A_s[i] * F_s[i]
        C.append(x)
        heapq.heappush(C_h, -x)

    for _ in range(K):
        x = -heapq.heappop(C_h)
        i = C.index(x)
        A_s[i] -= 1
        y = A_s[i] * F_s[i]
        C[i] = y
        heapq.heappush(C_h, -y)

    x = -heapq.heappop(C_h)
    print(x)











if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
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.sort()
F.sort()
F.reverse()
#print(A)
#print(F)
B = np.array(A)*np.array(F)
#print(B)
for j in range(K):
    max_i = np.argmax(B)
    B[max_i] -= F[max_i]
#print(B)
print(max(B))