Problem p02727 (Python) — buggy submission, verdict: Runtime Error

import sys
import numpy as np

X, Y, A, B, C = map(int, sys.stdin.readline().rstrip().split())
P = list(map(int, sys.stdin.readline().rstrip().split()))
Q = list(map(int, sys.stdin.readline().rstrip().split()))
R = list(map(int, sys.stdin.readline().rstrip().split()))

if A == 1 and B == 1 and C == 1:
    ans = sum(sorted([P[0], Q[0], R[0]], reverse=True)[:2])
    print(ans)
    sys.exit()

Pmin = np.min(P)
Qmin = np.min(Q)
Rmax = np.max(R)

# 着色する必要がない場合
if Pmin > Rmax and Qmin > Rmax:
    sP = np.sort(P)[::-1] if len(set(P)) > 1 else P
    sQ = np.sort(Q)[::-1] if len(set(Q)) > 1 else Q
    ans = np.sum(sP[:X]) + np.sum(sQ[:Y])
    print(ans)
    sys.exit()

gS = P + Q + R
p = A + B
iX = 0
iY = 0
iR = []
aX = [0] * X
aY = [0] * Y
aR = []
for index in np.argsort(gS)[::-1]:
    if iX == X and iY == Y:
        break
    if index < A:
        if iX == X:
            continue
        else:
            aX[iX] = gS[index]
            iX += 1
    elif index < p:
        if iY == Y:
            continue
        else:
            aY[iY] = gS[index]
            iY += 1
    else:
        iR.append((iX, iY))
        aR.append(gS[index])

sumX = sum(aX)
sumY = sum(aY)
for (iX, iY), s in zip(iR, aR):
    if len(aX) == 0 and len(aY) == 0:
        break
    if len(aX) == 0:
        if s <= aY[-1]:
            break
        sumY = sumY + s - aY.pop()
        if len(aY) == 0:
            break
    elif len(aY) == 0:
        if s <= aX[-1]:
            break
        sumX = sumX + s - aX.pop()
        if len(aX) == 0:
            break
    else:
        if s <= aX[-1] and s <= aY[-1]:
            break
        if iX == X:
            sumY = sumY + s - aY.pop()
        elif iY == Y:
            sumX = sumX + s - aX.pop()
        else:
            if aX[-1] < aY[-1]:
                sumX = sumX + s - aX.pop()
            else:
                sumY = sumY + s - aY.pop()

print(sumX + sumY)


Error: TLE

Fixed submission:

import sys
import numpy as np

X, Y, A, B, C = map(int, sys.stdin.readline().rstrip().split())
P = list(map(int, sys.stdin.readline().rstrip().split()))
Q = list(map(int, sys.stdin.readline().rstrip().split()))
R = list(map(int, sys.stdin.readline().rstrip().split()))

if A == 1 and B == 1 and C == 1:
    ans = sum(sorted([P[0], Q[0], R[0]], reverse=True)[:2])
    print(ans)
    sys.exit()

Pmin = np.min(P)
Qmin = np.min(Q)
Rmax = np.max(R)

# 着色する必要がない場合
if Pmin > Rmax and Qmin > Rmax:
    sP = np.sort(P)[::-1] if len(set(P)) > 1 else P
    sQ = np.sort(Q)[::-1] if len(set(Q)) > 1 else Q
    ans = np.sum(sP[:X]) + np.sum(sQ[:Y])
    print(ans)
    sys.exit()

gS = P + Q + R
p = A + B
iX = 0
iY = 0
iR = []
aX = [0] * X
aY = [0] * Y
aR = []
ans = 0
for index in np.argsort(gS)[::-1]:
    if iX == X and iY == Y:
        break
    if index < A:
        if iX == X:
            continue
        else:
            aX[iX] = gS[index]
            iX += 1
    elif index < p:
        if iY == Y:
            continue
        else:
            aY[iY] = gS[index]
            iY += 1
    else:
        iR.append((iX, iY))
        aR.append(gS[index])

sumX = sum(aX)
sumY = sum(aY)
for (iX, iY), s in zip(iR, aR):
    print(aX, aY)
    if len(aX) == 0:
        sumY = sumY + s - aY.pop()
        if len(aY) == 0:
            break
    elif len(aY) == 0:
        sumX = sumX + s - aX.pop()
        if len(aX) == 0:
            break
    else:
        if s <= aX[-1] and s <= aY[-1]:
            break
        if iX == X:
            sumY = sumY + s - aY.pop()
        elif iY == Y:
            sumX = sumX + s - aX.pop()
        else:
            if aX[-1] < aY[-1]:
                sumX = sumX + s - aX.pop()
            else:
                sumY = sumY + s - aY.pop()

print(sumX + sumY)