Problem: 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.

Submission attempts (in order):
import heapq


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

r = []
g = []
nc = []
for p in P:
    heapq.heappush(r, -p)

for q in Q:
    heapq.heappush(g, -q)

for i in R:
    heapq.heappush(nc, -i)

ans = 0
#print(r,g,nc)
while X > 0 or Y > 0:


    if X > 0 and Y > 0:
        if len(nc) > 0 and r[0] < nc[0]:
            ans += -1 * r[0]
            heapq.heappop(r)
            X -= 1

        elif len(nc) > 0 and g[0] < nc[0]:
            ans += -1 * g[0]
            heapq.heappop(g)
            Y -= 1
        else:
            if  len(nc) > 0 and abs(nc[0] - r[0]) >= abs(nc[0] - g[0]):
                ans += -1 * nc[0]
                heapq.heappop(nc)
                X -= 1
            elif len(nc) > 0 and abs(nc[0] - r[0]) < abs(nc[0] - g[0]):
                ans += -1 * nc[0]
                heapq.heappop(nc)
                Y -= 1
            else:
                if r[0] < g[0]:
                    ans += -1 * r[0]
                    heapq.heappop(r)
                    X -= 1
                else:
                    ans += -1 * g[0]
                    heapq.heappop(g)
                    Y -= 1
        
    elif X > 0:
        if len(nc) > 0 and  r[0] < nc[0]:
            ans += -1 * r[0]
            heapq.heappop(r)
            X -= 1

        elif len(nc) > 0 and  r[0] >= nc[0]:
            ans += -1 * nc[0]
            heapq.heappop(nc)
            X -= 1
        elif :
            ans += -1 * r[0]
            heapq.heappop(r)
            X -= 1
    else:
        if len(nc) > 0 and  g[0] < nc[0]:
            ans += -1 * g[0]
            heapq.heappop(g)
            Y -= 1

        elif len(nc) > 0 and  g[0] >= nc[0]:
            ans += -1 * nc[0]
            heapq.heappop(nc)
            Y -= 1
        elif :
            ans += -1 * g[0]
            heapq.heappop(g)
            Y -= 1
            

print(ans)




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

red_green = [(p, "r") for p in P] + [(q, "g") for q in Q]
red_green.sort()
nc = R
nc.sort()

cnt_r , cnt_g , cnt_n = 0,0,0

ans = 0
while cnt_r + cnt_g + cnt_n < X + Y:

    
    if cnt_r == X:
        while red_green[-1][1] == "r":
            red_green.pop()
    if cnt_g == Y:
        while red_green[-1][1] == "g":
            red_green.pop()

    
    if len(nc) > 0 and nc[-1] > red_green[-1][0]:
        point = nc.pop()
        ans += point
        cnt_n += 1
    else:
        point, color = red_green.pop()
        ans += point
        if color == "r":
            cnt_r += 1
        else:
            cnt_g += 1


print(ans)

Verdicts: Runtime Error

Accepted