Problem p02727 (python)

Fixed submission:
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()))


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

ans = 0
cnt_r, cnt_g, cnt_nc = 0, 0, 0


while cnt_r + cnt_g + cnt_nc < X + Y:

    if cnt_r == X:
        while rg[-1][1] == "r":
            rg.pop()

    if cnt_g == Y:
        while rg[-1][1] == "g":
            rg.pop()


    if len(nc) > 0 and nc[-1] > rg [-1][0]:
        tmp = nc.pop()
        cnt_nc += 1
    else:
        tmp, c = rg.pop()
        if c == "r":
            cnt_r += 1

        else:
            cnt_g += 1


    ans += tmp

print(ans)

Buggy submission (verdict: Runtime Error):
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)

Buggy submission (verdict: Runtime Error):
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 X > 0 and r[0] < nc[0]:
            ans += -1 * r[0]
            heapq.heappop(r)
            X -= 1

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

    #print("--", r,g,nc,X,Y)
            

print(ans)