Problem p02727 (python)

Fixed submission:
X, Y, A, B, C = [int(s) for s in input().split()]
p_list = [int(s) for s in input().split()]
q_list = [int(s) for s in input().split()]
r_list = [int(s) for s in input().split()]

p_list = list(reversed(sorted(p_list)))
p_list = p_list[:X]
# print(X, p_list)

q_list = list(reversed(sorted(q_list)))
q_list = q_list[:Y]
# print(Y, q_list)

r_list = list(reversed(sorted(r_list)))
# print(r_list)


p_i = -1
q_i = -1
r_i = 0

while True:
    if r_i == C:
        break

    if p_i + X >= 0 and q_i + Y >= 0:
        if p_list[p_i] <= q_list[q_i]:
            if p_list[p_i] < r_list[r_i]:
                p_list[p_i] = r_list[r_i]
                p_i -= 1
                r_i += 1
            else:
                break
        elif q_list[q_i] <= p_list[p_i]:
            if q_list[q_i] < r_list[r_i]:
                q_list[q_i] = r_list[r_i]
                q_i -= 1
                r_i += 1
            else:
                break
    elif p_i + X >= 0:
        if p_list[p_i] < r_list[r_i]:
            p_list[p_i] = r_list[r_i]
            p_i -= 1
            r_i += 1
        else:
            break
    elif q_i + Y >= 0:
        if q_list[q_i] < r_list[r_i]:
            q_list[q_i] = r_list[r_i]
            q_i -= 1
            r_i += 1
        else:
            break
    else:
        break

d = 0
d += sum(p_list)
d += sum(q_list)

print(d)

Buggy submission (verdict: Runtime Error):
X, Y, A, B, C = [int(s) for s in input().split()]
p_list = [int(s) for s in input().split()]
q_list = [int(s) for s in input().split()]
r_list = [int(s) for s in input().split()]

p_list = list(reversed(sorted(p_list)))
p_list = p_list[:X]
# print(X, p_list)

q_list = list(reversed(sorted(q_list)))
q_list = q_list[:Y]
# print(Y, q_list)

r_list = list(reversed(sorted(r_list)))
# print(r_list)


p_i = -1
q_i = -1
r_i = 0

while True:
    if r_i == C:
        break

    if p_i + A >= 0 and q_i + B >= 0:
        if p_list[p_i] <= q_list[q_i]:
            if p_list[p_i] < r_list[r_i]:
                p_list[p_i] = r_list[r_i]
                p_i -= 1
                r_i += 1
            else:
                break
        elif q_list[q_i] <= p_list[p_i]:
            if q_list[q_i] < r_list[r_i]:
                q_list[q_i] = r_list[r_i]
                q_i -= 1
                r_i += 1
            else:
                break
    elif p_i + A >= 0:
        if p_list[p_i] < r_list[r_i]:
            p_list[p_i] = r_list[r_i]
            p_i -= 1
            r_i += 1
        else:
            break
    elif q_i + B >= 0:
        if q_list[q_i] < r_list[r_i]:
            q_list[q_i] = r_list[r_i]
            q_i -= 1
            r_i += 1
        else:
            break
    else:
        break

d = 0
d += sum(p_list)
d += sum(q_list)

print(d)

Buggy submission (verdict: Runtime Error):
X, Y, A, B, C = [int(s) for s in input().split()]
p_list = [int(s) for s in input().split()]
q_list = [int(s) for s in input().split()]
r_list = [int(s) for s in input().split()]

p_list = list(reversed(sorted(p_list)))
p_list = p_list[:X]
# print(X, p_list)

q_list = list(reversed(sorted(q_list)))
q_list = q_list[:Y]
# print(Y, q_list)

r_list = list(reversed(sorted(r_list)))
# print(r_list)

p_i = -1
q_i = -1
r_i = 0

while True:
    if p_i + A >= 0 and p_list[p_i] < q_list[q_i]:
        if p_list[p_i] < r_list[r_i]:
            p_list[p_i] = r_list[r_i]
            p_i -= 1
            r_i += 1
            if r_i == C:
                break
        else:
            break
    elif q_i + B >= 0 and q_list[q_i] < p_list[p_i]:
        if q_list[q_i] < r_list[r_i]:
            q_list[q_i] = r_list[r_i]
            p_i -= 1
            r_i += 1
            if r_i == C:
                break
        else:
            break
    else:
        break

d = 0
d += sum(p_list)
d += sum(q_list)

print(d)