Problem:


Score : 500 points

Problem Statement

You are going to eat X red apples and Y green apples.

You have A red apples of deliciousness p_1,p_2, \dots, p_A, B green apples of deliciousness q_1,q_2, \dots, q_B, and C colorless apples of deliciousness r_1,r_2, \dots, r_C.

Before eating a colorless apple, you can paint it red or green, and it will count as a red or green apple, respectively.

From the apples above, you will choose the apples to eat while making the sum of the deliciousness of the eaten apples as large as possible.

Find the maximum possible sum of the deliciousness of the eaten apples that can be achieved when optimally coloring zero or more colorless apples.

Constraints

1 \leq X \leq A \leq 10^5

1 \leq Y \leq B \leq 10^5

1 \leq C \leq 10^5

1 \leq p_i \leq 10^9

1 \leq q_i \leq 10^9

1 \leq r_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X Y A B C
p_1 p_2 ... p_A
q_1 q_2 ... q_B
r_1 r_2 ... r_C

Output

Print the maximum possible sum of the deliciousness of the eaten apples.

Sample Input 1

1 2 2 2 1
2 4
5 1
3

Sample Output 1

12

The maximum possible sum of the deliciousness of the eaten apples can be achieved as follows:

Eat the 2-nd red apple.

Eat the 1-st green apple.

Paint the 1-st colorless apple green and eat it.

Sample Input 2

2 2 2 2 2
8 6
9 1
2 1

Sample Output 2

25

Sample Input 3

2 2 4 4 4
11 12 13 14
21 22 23 24
1 2 3 4

Sample Output 3

74



Sample input:
1 2 2 2 1
2 4
5 1
3


Sample output:
12

Submission status: Wrong Answer

Solution code:
from heapq import heappop,heappush
x,y,a,b,c=map(int,input().split())
p=list(map(int,input().split()))
p.sort(reverse=1)
q=list(map(int,input().split()))
q.sort(reverse=1)
r=list(map(int,input().split()))+[0]
r.sort(reverse=1)
h=[]
m=p[0]
for i in range(x):
  heappush(h,p[i])
  m=min(m,p[i])
for i in range(y):
  heappush(h,q[i])
  m=min(m,q[i])
i=0
while m<r[i]:
  m=heappop(h)
  heappush(h,r[i])
  i+=1
print(sum(h))

Submission status: Wrong Answer

Solution code:
def main():
    x,y,a,b,c=map(int,input().split())
    P=list(map(int,input().split()))
    Ps=[(p,1) for p in P]
    Q=list(map(int,input().split()))
    R=list(map(int,input().split()))
    R.sort()
    Rs=[(r,2) for r in R]
    PR=Ps+Rs
    PR.sort()
    #print(PR)
    ans=0
    cnt_r=0
    for _ in range(x):
        val,kind=PR.pop()
        ans+=val
        if kind==2:
            cnt_r+=1
    #print(ans,cnt_r)
    QR=Q+R[cnt_r:]
    QR.sort(reverse=True)
    ans+=sum(QR[:y])
    print(ans)
    
if __name__=='__main__':
    main()

Submission status: Wrong Answer

Solution code:
x, y, a, b, c = map(int, input().split())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
r = list(map(int, input().split()))

p.sort()
q.sort()
r.sort()

tmp = p[-x:] + q[-y:]
tmp.sort()

from collections import deque
tmp = deque(tmp)

while r and tmp[0] < r[-1]:
    tmp.popleft()
    tmp.append(r[-1])

print(sum(list(tmp)))

Submission status: Time Limit Exceeded

Solution code:
x,y,a,b,c = map(int,input().split())
p = list(map(int,input().split()))
q = list(map(int,input().split()))
r = list(map(int,input().split()))

p.sort(reverse=True)
q.sort(reverse=True)
r.sort(reverse=True)

p2 = p[:x]
q2 = q[:y]

for i in range(c):
  if p2[-1] <= q2[-1] and p2[-1] < r[i]:
    p2[-1] = r[i]
    p2.sort(reverse=True)
  if p2[-1] > q2[-1] and q2[-1] < r[i]:
    q2[-1] = r[i]
    q2.sort(reverse=True)
    
ans = sum(p2)+sum(q2)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
nii=lambda:map(int,stdin.readline().split())
lnii=lambda:list(map(int,stdin.readline().split()))
from bisect import insort

x,y,a,b,c=nii()
p=lnii()
q=lnii()
r=lnii()

p.sort(reverse=True)
q.sort(reverse=True)
r.sort(reverse=True)

l=p[:x]+q[:y]
l.sort()

for i in r:
  insort(l,i)

l.sort(reverse=True)
print(sum(l[:x+y]))

Submission status: Time Limit Exceeded

Solution code:
import sys
x,y,a,b,c = map(int,input().split())
r = list(map(int,input().split()))
g = list(map(int,input().split()))
un = list(map(int,input().split()))
un.sort(reverse=True)

r.sort()
g.sort()
buy = []
r_s = 0
g_s = 0

for i in range(x+y):
  
  if (r_s <= x-1 and g_s <= y-1 and r[a-x+r_s] > g[b-y+g_s]) or r_s == x:
    buy.append(g[b-y+g_s])
    g_s += 1
  else:
    buy.append(r[a-x+r_s])
    r_s += 1    

s = 0

for i in range(x+y):
  if un != [] and buy[i] < un[0]:
    p = un.pop(0)
    s += p
  else:
    s += sum(buy[i:])
    print(s)
    sys.exit()
    
    
print(s)

Submission status: Time Limit Exceeded

Solution code:
import sys
x,y,a,b,c = map(int,input().split())
r = list(map(int,input().split()))
g = list(map(int,input().split()))
un = list(map(int,input().split()))
un.sort(reverse=True)

r.sort()
g.sort()
buy = r[(a-x):]+g[(b-y):]
buy.sort()
s = 0

for i in range(x+y):
  if un != [] and buy[i] < un[0]:
    p = un.pop(0)
    s += p
  else:
    s += sum(buy[i:])
    print(s)
    sys.exit()
    
    
print(s)

Submission status: Time Limit Exceeded

Solution code:
X, Y, A, B, C = map(int, input().split())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
R = list(map(int, input().split()))

P.sort(reverse=True)
Q.sort(reverse=True)
R.sort(reverse=True)

temp = P[:X] + Q[:Y]

temp.sort()
ans = sum(temp)
for ri in R:
    if temp[0] < ri:
        ans += ri - temp[0]
        temp.pop(0)
    else:
        break

print(ans)

Submission status: Time Limit Exceeded

Solution code:
x,y,a,b,c = map(int,input().split())
p = sorted(list(map(int,input().split())), reverse=True)
q = sorted(list(map(int,input().split())), reverse=True)
r = sorted(list(map(int,input().split())), reverse=True)
pq = sorted(p[:x] + q[:y])
ans = sum(pq)
i = 0
while i < min(a+b, c):
  if r[i] <= pq[i]:
    break
  ans += r[i] - pq[i]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
x,y,a,b,c=map(int,input().split())
p=sorted(list(map(int,input().split())),reverse=True)
q=sorted(list(map(int,input().split())),reverse=True)
r=deque(sorted(list(map(int,input().split())),reverse=True))
p=deque(p[:x])
q=deque(q[:y])
ans=sum(p)+sum(q)

while p[-1]<r[0] or q[-1]<r[0]:
  if p[-1]<q[-1]:
    ans+=r[0]-p[-1]
    r.popleft()
    p.pop()
  else:
    ans+=r[0]-q[-1]
    r.popleft()
    q.pop()
  if p==deque([]) or q==deque([]) or r==deque([]):
    break
if r==deque([]):
  pass
elif p==deque([]):
  while q[-1]<r[0]:
    ans+=r[0]-q[-1]
    r.popleft
    q.pop
    if q==deque([]) or r==deque([]):
      break
elif q==deque([]):
  while p[-1]<r[0]:
    ans+=r[0]-p[-1]
    r.popleft
    p.pop
    if p==deque([]) or r==deque([]):
      break
  
print(ans)

Submission status: Time Limit Exceeded

Solution code:
X,Y,A,B,C = map(int, input().split())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
R = list(map(int, input().split()))
P.sort(reverse=True)
P = P[:X]
Q.sort(reverse=True)
Q = Q[:Y]
sp = sum(P)
sq = sum(Q)
ans = sp+sq
R.sort(reverse=True)
import heapq  # heapqライブラリのimport
heapq.heapify(P)  # リストを優先度付きキューへ
heapq.heapify(Q)
p = heapq.heappop(P) # 最小値の取り出し
q = heapq.heappop(Q)
r = R.pop(0)
while p<r or q<r:
  #print(p,q,r,ans)
  if r-p > r-q:
    heapq.heappush(P, r)
    ans += r-p
    p = heapq.heappop(P)
  else:
    heapq.heappush(Q, r)
    ans += r-q
    q = heapq.heappop(Q)
  if len(R)==0:
    break
  r = R.pop(0)
  #print(p,q,r,ans)
print(ans)

#print(*ans[1:], sep='\n')

Submission status: Time Limit Exceeded

Solution code:
X, Y, A, B, C = input().split(" ")
Xa = input().split(" ")
Ya = input().split(" ")
Notra = input().split(" ")
def toint(a):
	for i in range(len(a)):
		a[i]=int(a[i])
toint(Xa)
toint(Ya)
toint(Notra)
def maxs(liste):
	maxchk = 0
	for i in liste:
		if i > maxchk:
			maxchk = i
	return maxchk
def mins(liste):
	maxchk = 10000000000
	for i in liste:
		if i < maxchk:
			maxchk = i
	return maxchk
Xa.sort(reverse=True)
Ya.sort(reverse=True)
Xa = Xa[0:int(X)]
Ya = Ya[0:int(Y)]
def change():
	answer = 0
	while True:
		if mins(Xa) > mins(Ya):
			try:
				if mins(Ya) < maxs(Notra):
					Ya[Ya.index(mins(Ya))] =maxs(Notra)
					Notra.remove(maxs(Notra))
				else:
					break;
			except:
				break;
		elif mins(Xa) < mins(Ya):
			try:
				if mins(Xa) < maxs(Notra):
					Xa[Xa.index(mins(Xa))] = maxs(Notra)
					Notra.remove(maxs(Notra))
				else:
					break;
			except:
				break;
		elif mins(Xa)  == mins(Ya):
			try:
				if mins(Xa)  < maxs(Notra):
					Xa[Xa.index(mins(Xa))] = maxs(Notra)
					Notra.remove(maxs(Notra))
				else:
					break;
			except:
				break;
	for i in Xa:
		answer += i
	for i in Ya:
		answer += i
	print(answer)
change()

Submission status: Time Limit Exceeded

Solution code:
X, Y, A, B, C = input().split(" ")
Xa = input().split(" ")
Ya = input().split(" ")
Notra = input().split(" ")
def toint(a):
	for i in range(len(a)):
		a[i]=int(a[i])
toint(Xa)
toint(Ya)
toint(Notra)
Notrmax = 0
Notra.sort(reverse=True)
Xa.sort(reverse=True)
Ya.sort(reverse=True)
Xa = Xa[0:int(X)]
Ya = Ya[0:int(Y)]
def change():
	answer = 0
	while True:
		Notra.sort(reverse=True)
		Xa.sort()
		Ya.sort()
		if Xa[0] > Ya[0]:
			try:
				if Ya[0] < Notra[0]:
					Ya[0] = Notra[0]
					Notra.remove(Notra[0])
				else:
					break;
			except:
				break;
		elif Xa[0] < Ya[0]:
			try:
				if Xa[0] < Notra[0]:
					Xa[0] = Notra[0]
					Notra.remove(Notra[0])
				else:
					break;
			except:
				break;
		elif Xa[0] == Ya[0]:
			try:
				if Xa[0] < Notra[0]:
					Xa[0] = Notra[0]
					Notra.remove(Notra[0])
				else:
					break;
			except:
				break;
	for i in Xa:
		answer += i
	for i in Ya:
		answer += i
	print(answer)
change()

Submission status: Time Limit Exceeded

Solution code:
#aaaaa
#bbbb
#cccc
#dddd
#eeee
#ffff
#gggg
X, Y, A, B, C = input().split(" ")
Xa = input().split(" ")
Ya = input().split(" ")
Notra = input().split(" ")
def toint(a):
	for i in range(len(a)):
		a[i]=int(a[i])
toint(Xa)
toint(Ya)
toint(Notra)
Notrmax = 0
Notra.sort(reverse=True)
Xa.sort(reverse=True)
Ya.sort(reverse=True)
Xa = Xa[0:int(X)]
Ya = Ya[0:int(Y)]
def change():
	answer = 0
	while True:
		Notra.sort(reverse=True)
		Xa.sort()
		Ya.sort()
		if Xa[0] > Ya[0]:
			try:
				if Ya[0] < Notra[0]:
					Ya[0] = Notra[0]
					Notra.remove(Notra[0])
				else:
					break;
			except:
				break;
		elif Xa[0] < Ya[0]:
			try:
				if Xa[0] < Notra[0]:
					Xa[0] = Notra[0]
					Notra.remove(Notra[0])
				else:
					break;
			except:
				break;
		elif Xa[0] == Ya[0]:
			try:
				if Xa[0] < Notra[0]:
					Xa[0] = Notra[0]
					Notra.remove(Notra[0])
				else:
					break;
			except:
				break;
	for i in Xa:
		answer += i
	for i in Ya:
		answer += i
	print(answer)
change()

Submission status: Time Limit Exceeded

Solution code:
x, y, a, b, c = map(int, input().split())
list_A = list(map(int, input().split()))
list_B = list(map(int, input().split()))
list_C = list(map(int, input().split()))
list_N = []

for i in list_A:
    list_N.append([i, "A"])

for i in list_B:
    list_N.append([i, "B"])

for i in list_C:
    list_N.append([i, "C"])

k = x + y
cnt = 0
ans = 0
list_N.sort(key=lambda x:x[0], reverse=True)
while True:
    if cnt == k:
        break
    l = list_N.pop(0)

    if l[1] == "A" and x != 0:
        x -= 1
        cnt += 1
        ans += l[0]

    if l[1] == "B" and y != 0:
        y -= 1
        cnt += 1
        ans += l[0]

    if l[1] == "C":
        cnt += 1
        ans += l[0]

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from itertools import accumulate
X, Y, A, B, C = map(int, input().split())
p = [int(i) for i in input().split()]
q = [int(i) for i in input().split()]
r = [int(i) for i in input().split()]

p.sort(reverse=True)
q.sort(reverse=True)
r.sort(reverse=True)
l = [0] + list(accumulate(p))
m = [0] + list(accumulate(q))
n = [0] + list(accumulate(r))
ans = 0
for a in range(X+1):
    for b in range(Y+1):
        if a+b <= X+Y and a+b+C >= X+Y:
            ans = max(ans, l[a]+m[b]+n[X+Y-a-b])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def main():
    import bisect
    x,y,a,b,c = map(int,input().split())
    p = sorted(list(map(int,input().split())))[-1*x:]
    q = sorted(list(map(int,input().split())))[-1*y:]
    r = sorted(list(map(int,input().split())),reverse=True)
    for i in range(c):
        if p[0]>=q[0]:
            if q[0]<r[i]:
                bisect.insort_left(q,r[i])
                q = q[1:]
            else:
                break
        else:
            if p[0]<r[i]:
                bisect.insort_right(p,r[i])
                p = p[1:]
    print(sum(p)+sum(q))

    
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import bisect
def bisect_left_reverse(a, x):
    if a[0] <= x:
        return 0
    if x < a[-1]:
        return len(a)
    ok = len(a) - 1
    ng = 0
    while (abs(ok - ng) > 1):
        mid = (ok + ng) // 2
        if a[mid] <= x:
            ok = mid
        else:
            ng = mid
    return ok
X,Y,A,B,C = map(int,input().split())
p = list(map(int,input().split()))
q = list(map(int,input().split()))
r = list(map(int,input().split()))
p.sort(reverse=True)
q.sort(reverse=True)
r.sort(reverse=True)
A = p[:X]+q[:Y]
A.sort(reverse=True)
for i in range(len(r)):
    n = r[i]
    if A[-1] < n:
        index = bisect_left_reverse(A,n)
        A.insert(index,n)
        A[-1-i] = 0
    else:
        break
print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
import bisect
X,Y,A,B,C = map(int,input().split())
p = list(map(int,input().split()))
q = list(map(int,input().split()))
r = list(map(int,input().split()))
p.sort(reverse=True)
q.sort(reverse=True)
r.sort(reverse=True)
A = p[:X]+q[:Y]
A.sort()
for i in range(len(r)):
    n = r[i]
    if A[0] < r[i]:
        index = bisect.bisect_left(A,n)
        A.insert(index,n)
        A[i] = 0
    else:
        break
print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
import bisect
X,Y,A,B,C = map(int,input().split())
p = list(map(int,input().split()))
q = list(map(int,input().split()))
r = list(map(int,input().split()))
p.sort(reverse=True)
q.sort(reverse=True)
r.sort(reverse=True)
A = p[:X]+q[:Y]
A.sort()
for n in r:
    if A[0] < n:
        index = bisect.bisect_left(A,n)
        A.insert(index,n)
        A.pop(0)
    else:
        break
print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
import bisect
def bisect_left_reverse(a, x):
    if a[0] <= x:
        return 0
    if x < a[-1]:
        return len(a)
    ok = len(a) - 1
    ng = 0
    while (abs(ok - ng) > 1):
        mid = (ok + ng) // 2
        if a[mid] <= x:
            ok = mid
        else:
            ng = mid
    return ok
X,Y,A,B,C = map(int,input().split())
p = list(map(int,input().split()))
q = list(map(int,input().split()))
r = list(map(int,input().split()))
p.sort(reverse=True)
q.sort(reverse=True)
r.sort(reverse=True)
A = p[:X]+q[:Y]
A.sort(reverse=True)
for n in r:
    if A[-1] < n:
        index = bisect_left_reverse(A,n)
        A.insert(index,n)
        A.pop(-1)
    else:
        break
print(sum(A))

Submission status: Time Limit Exceeded

Solution code:
N = [int(i) for i in input().split()]
P = [int(i) for i in input().split()]
Q = [int(i) for i in input().split()]
R = [int(i) for i in input().split()]

answerList = []
answerList1 = []

for i in range(N[0]):
    i = i + 1
    answerList.append(sorted(P)[-i])

for i in range(N[1]):
    i = i + 1
    answerList.append(sorted(Q)[-i])

for i in range(len(R)):
    if R[i] > min(answerList):
        for j in range(len(answerList)):
            if R[i] > answerList[j]:
                answerList[j] = R[i]

print(sum(answerList))

Submission status: Time Limit Exceeded

Solution code:
N = [int(i) for i in input().split()]
P = [int(i) for i in input().split()]
Q = [int(i) for i in input().split()]
R = [int(i) for i in input().split()]

answerList = []
answerList1 = []

for i in range(N[0]):
    i = i + 1
    answerList.append(sorted(P)[-i])

for i in range(N[1]):
    i = i + 1
    answerList.append(sorted(Q)[-i])

for i in range(N[4]):
    i = i + 1
    answerList1.append(sorted(R)[-i])

for i in range(len(answerList1)):
    if answerList1[i] > min(answerList):
        for j in range(len(answerList)):
            if R[i] > answerList[j]:
                answerList[j] = R[i]
    else:
        break

print(sum(answerList))

Submission status: Time Limit Exceeded

Solution code:
N = [int(i) for i in input().split()]
P = [int(i) for i in input().split()]
Q = [int(i) for i in input().split()]
R = [int(i) for i in input().split()]

answerList = []

for i in range(N[0]):
    i = i + 1
    answerList.append(sorted(P)[-i])

for i in range(N[1]):
    i = i + 1
    answerList.append(sorted(Q)[-i])

for i in range(N[4]):
    for j in range(len(answerList)):
        if R[i] > answerList[j]:
            answerList[j] = R[i]

print(sum(answerList))

Submission status: Time Limit Exceeded

Solution code:
N = [int(i) for i in input().split()]
P = [int(i) for i in input().split()]
Q = [int(i) for i in input().split()]
R = [int(i) for i in input().split()]

answerList = []

for i in range(N[0]):
    i = i + 1
    answerList.append(sorted(P)[-i])

for i in range(N[1]):
    i = i + 1
    answerList.append(sorted(Q)[-i])

for i in range(len(answerList)):
    i = i + 1
    for j in range(N[4]):
        if R[j] > answerList[-i]:
            answerList[-i] = R[j]

print(sum(answerList))

Submission status: Time Limit Exceeded

Solution code:
X, Y, A, B, C = map(int, input().split())
p, q, r = [list(map(int, input().split())) for _ in range(3)]

p = list(map(lambda x: [x, 'p'], p))
q = list(map(lambda x: [x, 'q'], q))
r = list(map(lambda x: [x, 'r'], r))

l = p+q+r

l.sort(reverse=True)

x = 0
y = 0

sum = 0

while not (x == X and y == Y):
    apple = l.pop(0)
    if apple[1] == 'p' and x < X:
        x += 1
        sum += apple[0]
    if apple[1] == 'q' and y < Y:
        y += 1
        sum += apple[0]
    if apple[1] == 'r':
        if X-x < Y-y and y < Y:
            y += 1
            sum += apple[0]
        if Y-y <= X-x and x < X:
            x += 1
            sum += apple[0]

print(sum)

Submission status: Time Limit Exceeded

Solution code:
x,y,a,b,c = map(int,input().split())
p = sorted(list(map(int,input().split())),reverse=True)
q = sorted(list(map(int,input().split())),reverse=True)
r = sorted(list(map(int,input().split())),reverse=True)
ans = 0
for i in range(x,max(-1,x-c-1),-1):
  z = c-(x-i)
  for j in range(y,max(-1,y-1-z),-1):
    ans = max(ans,sum(p[:i])+sum(q[:j])+sum(r[:(x+y-i-j)]))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
X,Y,A,B,C = map(int, input().split())
P=sorted(list(map(int,input().split())), reverse=True)
Q=sorted(list(map(int,input().split())), reverse=True)
R=sorted(list(map(int,input().split())), reverse=True)

a=[0]
b=[0]
c=[0]

for i in P[:X]:
    a.append(a[-1]+i)
    
for i in Q[:Y]:
    b.append(b[-1]+i)
      
for i in R:
    c.append(c[-1]+i)
    
ans={0}

for i in range(len(c)):
    for j in range(len(b)):
        for k in range(len(a)):
            if i+j+k==X+Y:
                tmp=c[i]+b[j]+a[k]
                #print(tmp)
                ans.add(tmp)
            else:
                pass
            
print(max(ans))

Submission status: Time Limit Exceeded

Solution code:
X,Y,A,B,C = map(int, input().split())
p=list(map(int, input().split()))
q=list(map(int, input().split()))
r=list(map(int, input().split()))

P=sorted(p, reverse=True)
Q=sorted(q, reverse=True)
R=sorted(r, reverse=True)

a=[0]
b=[0]
c=[0]

for i in P[:X]:
    a.append(a[-1]+i)
    
for i in Q[:Y]:
    b.append(b[-1]+i)
      
for i in R:
    c.append(c[-1]+i)
    
ans={0}

for i in range(len(c)):
    for j in range(len(b)):
        for k in range(len(a)):
            if i+j+k==X+Y:
                tmp=c[i]+b[j]+a[k]
                #print(tmp)
                ans.add(tmp)
            else:
                pass
            
print(max(ans))

Submission status: Time Limit Exceeded

Solution code:
x, y, a, b, c = map(int, input().split())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
r = list(map(int, input().split()))

ans = 0
p.sort(reverse = True)
q.sort(reverse = True)
r.sort(reverse = True)
p = p[:x]
q = q[:y]

P = sum(i for i in p)
Q = sum(j for j in q)
ans = P+Q
apples = p+q
apples.sort()
for i in range(len(r)):
  if apples[i] < r[0]:
    ans = ans-apples[i] + r[0]
    del r[0]
  else:
    break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
x, y, a, b, c = map(int, input().split())
p = [int(i) for i in input().split()]
q = [int(i) for i in input().split()]
r = [int(i) for i in input().split()]

p = list(reversed(sorted(p)))
q = list(reversed(sorted(q)))
r = list(reversed(sorted(r)))
p_cum = [0]
q_cum = [0]
r_cum = [0]
for i in range(a):
    p_cum.append(p_cum[i] + p[i])
for i in range(b):
    q_cum.append(q_cum[i] + q[i])
for i in range(c):
    r_cum.append(r_cum[i] + r[i])
ans = 0
for i in range(0, min(c, x) + 1):
    for j in range(0, min(c - i, y) + 1):
        ans = max(ans, p_cum[x - i] + q_cum[y - j] + r_cum[i + j])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
x,y,a,b,c = map(int, input().split(" "))
plist= sorted(list(map(int, input().split(" "))),reverse=True)[0:x]
qlist= sorted(list(map(int, input().split(" "))),reverse=True)[0:y]
rlist= list(map(int, input().split(" ")))

plist.extend(qlist)
plist.sort()

rlist.sort(reverse=True)

n = min(len(plist),len(rlist))
results = [sum(plist)]

for i in range(n):
  plist[i]=rlist[i]
  results.append(sum(plist))
  
print(max(results))

Submission status: Time Limit Exceeded

Solution code:
x, y, a, b, c = list(map(int, input().split()))
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
R = list(map(int, input().split()))

P.sort(reverse=True)
Q.sort(reverse=True)
R.sort(reverse=True)

p = P[:x]
q = Q[:y]
r = []

i = 0

def check(z , l):
    return z > l[-1]

while len(p) ** 2 + len(q) ** 2 > 0 and i < len(R):
    if len(p) * len(q) != 0:
        if R[i] > p[-1] or R[i] > q[-1]:
            r.append(R[i])
            if p[-1] > q[-1]:
                q = q[:-1]
            else:
                p = p[:-1]
    elif len(p) != 0:
        if R[i] > p[-1]:
            r.append(R[i])
            p = p[:-1]

    elif len(q) != 0:
        if R[i] > q[-1]:
            r.append(R[i])
            q = q[:-1]
    
    i +=1

print(sum(p)+sum(q)+sum(r))

Submission status: Time Limit Exceeded

Solution code:
X,Y,A,B,C = map(int,input().split())
p = list(map(int,input().split()))
q = list(map(int,input().split()))
r = list(map(int,input().split()))
p_1 = sorted(p,reverse = True)
q_1 = sorted(q,reverse = True)
r_1 = sorted(r,reverse = True)
p_2 = p_1[:X]
q_2 = q_1[:Y]
r_1.append(0)
while min([p_2[X-1],q_2[Y-1]]) < r_1[0]:
    if p_2[X-1] > q_2[Y-1]:
        q_2[Y-1] = r_1[0]
        q_2 = sorted(q_2,reverse = True)
        r_1 = r_1[1:]
    else:
        p_2[X-1] = r_1[0]
        p_2 = sorted(p_2,reverse = True)
        r_1 = r_1[1:]
        
print(sum(p_2)+sum(q_2))

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left, insort_left
from collections import deque
import sys
def main():
	readlines = sys.stdin.readline
	x,y,a,b,c = map(int,input().split())

	p = list(map(int,input().split()))
	q = list(map(int,input().split()))
	r = list(map(int,input().split()))

	p.sort()
	q.sort()
	r.sort(reverse=True)

	p = p[a-x:]
	q = q[b-y:]
	ans = sum(p) + sum(q)
	p_count = 0
	q_count = 0

	for i in range(c):
		if p[p_count] < r[i] and q[q_count] < r[i]:
			if p[p_count] < q[q_count]:
				ans -= p[p_count]
				ans += r[i]
				insort_left(p,r[i])
				p_count += 1
			else:
				ans -= q[q_count]
				ans += r[i]
				insort_left(q,r[i])
				q_count += 1
		elif p[p_count] < r[i]:
			ans -= p[p_count]
			ans += r[i]
			insort_left(p,r[i])
			p_count += 1
		elif q[q_count] < r[i]:
			ans -= q[q_count]
			ans += r[i]
			insort_left(q,r[i])
			q_count += 1
		else:
			break

	print(ans)
if __name__ == '__main__':
	main()

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left, insort_left
from collections import deque
import sys
readlines = sys.stdin.readline
def main():
	x,y,a,b,c = map(int,input().split())

	p = list(map(int,input().split()))
	q = list(map(int,input().split()))
	r = list(map(int,input().split()))

	p.sort()
	q.sort()
	r.sort(reverse=True)

	p = p[a-x:]
	q = q[b-y:]
	ans = sum(p) + sum(q)
	p_count = 0
	q_count = 0

	for i in range(c):
		if p[p_count] < r[i] and q[q_count] < r[i]:
			if p[p_count] < q[q_count]:
				ans -= p[p_count]
				ans += r[i]
				insort_left(p,r[i])
				p_count += 1
			else:
				ans -= q[q_count]
				ans += r[i]
				insort_left(q,r[i])
				q_count += 1
		elif p[p_count] < r[i]:
			ans -= p[p_count]
			ans += r[i]
			insort_left(p,r[i])
			p_count += 1
		elif q[q_count] < r[i]:
			ans -= q[q_count]
			ans += r[i]
			insort_left(q,r[i])
			q_count += 1
		else:
			break

	print(ans)
if __name__ == '__main__':
	main()

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left, insort_left
from collections import deque
import sys
readlines = sys.stdin.readline
def main():
	x,y,a,b,c = map(int,input().split())

	p = list(map(int,input().split()))
	q = list(map(int,input().split()))
	r = list(map(int,input().split()))

	p.sort()
	q.sort()
	r.sort(reverse=True)

	p = p[a-x:]
	q = q[b-y:]
	ans = sum(p) + sum(q)
	p_count = 0
	q_count = 0

	for i in range(c):
		if p[p_count] < r[i] and q[q_count] < r[i]:
			if p[p_count] < q[q_count]:
				ans -= p[p_count]
				ans += r[i]
				insort_left(p,r[i])
				p_count += 1
			else:
				ans -= q[q_count]
				ans += r[i]
				insort_left(q,r[i])
				q_count += 1
		elif p[p_count] < r[i] and q[q_count] >= r[i]:
			ans -= p[p_count]
			ans += r[i]
			insort_left(p,r[i])
			p_count += 1
		elif p[p_count] >= r[i] and q[q_count] < r[i]:
			ans -= q[q_count]
			ans += r[i]
			insort_left(q,r[i])
			q_count += 1
		else:
			break

	print(ans)
if __name__ == '__main__':
	main()

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left, insort_left
from collections import deque
import sys
readlines = sys.stdin.readline
def main():
	x,y,a,b,c = map(int,input().split())

	p = list(map(int,input().split()))
	q = list(map(int,input().split()))
	r = list(map(int,input().split()))

	p.sort()
	q.sort()
	r.sort(reverse=True)

	p = deque(p[a-x:])
	q = deque(q[b-y:])
	ans = sum(p) + sum(q)

	for i in range(c):
		if p[0] < r[i] and q[0] < r[i]:
			if p[0] < q[0]:
				ans -= p.popleft()
				ans += r[i]
				insort_left(list(p),r[i])
				p = deque(p)
			else:
				ans -= q.popleft()
				ans += r[i]
				insort_left(list(q),r[i])
				q = deque(q)
		elif p[0] < r[i] and q[0] >= r[i]:
			ans -= p.popleft()
			ans += r[i]
			insort_left(list(p),r[i])
			p = deque(p)
		elif p[0] >= r[i] and q[0] < r[i]:
			ans -= q.popleft()
			ans += r[i]
			insort_left(list(q),r[i])
			q = deque(q)
		else:
			break

	print(ans)
if __name__ == '__main__':
	main()

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left, insort_left
from collections import deque

x,y,a,b,c = map(int,input().split())

p = list(map(int,input().split()))
q = list(map(int,input().split()))
r = list(map(int,input().split()))

p.sort()
q.sort()
r.sort(reverse=True)

p = deque(p[a-x:])
q = deque(q[b-y:])
ans = sum(p) + sum(q)

for i in range(c):
	if p[0] < r[i] and q[0] < r[i]:
		if p[0] < q[0]:
			ans -= p.popleft()
			ans += r[i]
			insort_left(list(p),r[i])
			p = deque(p)
		else:
			ans -= q.popleft()
			ans += r[i]
			insort_left(list(q),r[i])
			q = deque(q)
	elif p[0] < r[i] and q[0] >= r[i]:
		ans -= p.popleft()
		ans += r[i]
		insort_left(list(p),r[i])
		p = deque(p)
	elif p[0] >= r[i] and q[0] < r[i]:
		ans -= q.popleft()
		ans += r[i]
		insort_left(list(q),r[i])
		q = deque(q)
	else:
		break

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import heapq


def read_int():
    return int(input().strip())


def read_ints():
    return list(map(int, input().strip().split(' ')))


def solve():
    X, Y, A, B, C = read_ints()
    RED, GREEN, COLORLESS = 0, 1, 2
    apples = []
    reds = read_ints()
    reds.sort(reverse=True)
    greens = read_ints()
    greens.sort(reverse=True)
    colorless = read_ints()
    colorless.sort(reverse=True)
    i, j, k = 0, 0, 0
    total = 0
    while not (i == X and j == Y):
        if i < X:
            if k == C or reds[i] >= colorless[k]:
                total += reds[i]
                i += 1
            else:
                if colorless[k]-reds[X-1] > colorless[k]-greens[Y-1]:
                    X -= 1
                else:
                    Y -= 1
                total += colorless[k]
                k += 1
        if j < Y:
            if k == C or greens[j] >= colorless[k]:
                total += greens[j]
                j += 1
            else:
                if colorless[k]-greens[Y-1] > colorless[k]-reds[X-1]:
                    Y -= 1
                else:
                    X -= 1
                total += colorless[k]
                k += 1
    return total


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

Submission status: Time Limit Exceeded

Solution code:
import time
problem_info = input().split()
eat_red = int(problem_info[0])
eat_green = int(problem_info[1])
red_num = int(problem_info[2])
green_num = int(problem_info[3])
non_num = int(problem_info[4])

red_eval = sorted([int(i) for i in input().split()],reverse=True)[:eat_red]
green_eval = sorted([int(i) for i in input().split()],reverse=True)[:eat_green]
all_apple_eval = sorted(red_eval + green_eval,reverse=True)
non_eval = [int(i) for i in input().split()]

for i in range(non_num):
  if max(non_eval) > min(all_apple_eval):
    all_apple_eval[-1] = max(non_eval)
    all_apple_eval.sort(reverse=True)
  else:
    break
      
print(sum(all_apple_eval))

Submission status: Time Limit Exceeded

Solution code:
problem_info = input().split()
eat_red = int(problem_info[0])
eat_green = int(problem_info[1])
red_num = int(problem_info[2])
green_num = int(problem_info[3])
non_num = int(problem_info[4])

red_eval = sorted([int(i) for i in input().split()],reverse=True)[:eat_red]
green_eval = sorted([int(i) for i in input().split()],reverse=True)[:eat_green]
non_eval = sorted([int(i) for i in input().split()],reverse=True)

min_eval = min(green_eval) if min(red_eval) > min(green_eval) else min(red_eval)

if min_eval < max(non_eval):
  for i in non_eval:
    if min(red_eval) > min(green_eval):
      if min(green_eval) < i:
        green_eval[-1] = i
        green_eval.sort(reverse=True)
    else:
      if min(red_eval) < i:
        red_eval[-1] = i
        red_eval.sort(reverse=True)
    min_eval = min(green_eval) if min(red_eval) > min(green_eval) else min(red_eval)
    if min_eval > i:
      break
      
print(sum(red_eval)+sum(green_eval))

Submission status: Time Limit Exceeded

Solution code:
problem_info = input().split()
eat_red = int(problem_info[0])
eat_green = int(problem_info[1])
red_num = int(problem_info[2])
green_num = int(problem_info[3])
non_num = int(problem_info[4])

red_eval = sorted([int(i) for i in input().split()],reverse=True)[:eat_red]
green_eval = sorted([int(i) for i in input().split()],reverse=True)[:eat_green]
non_eval = sorted([int(i) for i in input().split()])

for i in range(non_num):
  if min(red_eval) > min(green_eval):
    if min(green_eval) < max(non_eval):
      green_eval[-1] = max(non_eval)
      green_eval.sort(reverse=True)
  else:
    if min(red_eval) < max(non_eval):
      red_eval[-1] = max(non_eval)
      red_eval.sort(reverse=True)

print(sum(red_eval)+sum(green_eval))