Problem p02727 (python)

Fixed submission:
import sys

X, Y, A, B, C = map(int, sys.stdin.readline().strip().split())

P = list(map(int, sys.stdin.readline().strip().split()))
Q = list(map(int, sys.stdin.readline().strip().split()))
R = list(map(int, sys.stdin.readline().strip().split()))

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

P = P[:X]
P.sort()
Q = Q[:Y]
Q.sort()

ans = 0
i, j, k = 0, 0, 0
while C > k:
    if X > i and Y > j:

        if P[i] >= Q[j]:
            if R[k] > Q[j] and Y > j:
                Q[j] = R[k]
                j += 1
                k += 1
            elif R[k] > P[i] and X > i:
                P[i] = R[k]
                i += 1
                k += 1
            else:
                break
 
        elif P[i] < Q[j]:
            if R[k] > P[i] and X > i:
                P[i] = R[k]
                i += 1
                k += 1
            elif R[k] > Q[j] and Y > j:
                Q[j] = R[k]
                j += 1
                k += 1
            else:
                break
    elif X > i:
        if R[k] > P[i]:
            P[i] = R[k]
            i += 1
            k += 1
        else:
            break
    elif Y > j:
        if R[k] > Q[j]:
            Q[j] = R[k]
            j += 1
            k += 1
    else:
        break

# print(P)
# print(Q)
print(sum(P) + sum(Q))

Buggy submission (verdict: Runtime Error):
import sys

X, Y, A, B, C = map(int, sys.stdin.readline().strip().split())

P = list(map(int, sys.stdin.readline().strip().split()))
Q = list(map(int, sys.stdin.readline().strip().split()))
R = list(map(int, sys.stdin.readline().strip().split()))

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

P = P[:X]
P.sort()
Q = Q[:Y]
Q.sort()

ans = 0
i, j, k = 0, 0, 0
while C > k:

    if P[i] >= Q[j]:
        if R[k] > Q[j] and Y > j:
            Q[j] = R[k]
            j += 1
            k += 1
        elif R[k] > P[i] and X > i:
            P[i] = R[k]
            i += 1
            k += 1
        else:
            break

    elif P[i] < Q[j]:
        if R[k] > P[i] and X > i:
            P[i] = R[k]
            i += 1
            k += 1
        elif R[k] > Q[j] and Y > j:
            Q[j] = R[k]
            j += 1
            k += 1
        else:
            break
        

# print(P)
# print(Q)
print(sum(P) + sum(Q))

Buggy submission (verdict: Runtime Error):
import sys

X, Y, A, B, C = map(int, sys.stdin.readline().strip().split())

P = list(map(int, sys.stdin.readline().strip().split()))
Q = list(map(int, sys.stdin.readline().strip().split()))
R = list(map(int, sys.stdin.readline().strip().split()))

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

P = P[:X]
P.sort()
Q = Q[:Y]
Q.sort()

ans = 0
i, j, k = 0, 0, 0
while C > k:

    if P[i] >= Q[j]:
        if R[k] > Q[i] and Y > j:
            Q[j] = R[k]
            j += 1
            k += 1
        elif R[k] > P[i] and X > i:
            P[i] = R[k]
            i += 1
            k += 1
        else:
            break

    elif P[i] < Q[j]:
        if R[k] > P[i] and X > i:
            P[i] = R[k]
            i += 1
            k += 1
        elif R[k] > Q[i] and Y > j:
            Q[j] = R[k]
            j += 1
            k += 1
        else:
            break
        

# print(P)
# print(Q)
print(sum(P) + sum(Q))