Problem p02727 (python)

Fixed submission:
def main():
    import sys
    input = sys.stdin.buffer.readline
    X, Y, A, B, C = (int(i) for i in input().split())
    P = [-int(i) for i in input().split()]
    Q = [-int(i) for i in input().split()]
    R = [-int(i) for i in input().split()]
    from heapq import heapify, heappop
    heapify(P)
    heapify(Q)
    heapify(R)
    eatP = 0
    eatQ = 0
    eatR = 0
    ans = 0
    while X != eatP and Y != eatQ and eatP + eatQ + eatR < X + Y:
        if P and Q and R:
            cur = min(P[0], Q[0], R[0])
        elif P and Q:
            cur = min(P[0], Q[0])
        elif Q and R:
            cur = min(Q[0], R[0])
        elif R and P:
            cur = min(R[0], P[0])
        elif P:
            cur = P[0]
        elif Q:
            cur = Q[0]
        elif R:
            cur = R[0]
        else:
            break
        ans -= cur
        if R and R[0] == cur:
            heappop(R)
            eatR += 1
        elif P and P[0] == cur:
            heappop(P)
            eatP += 1
        elif Q and Q[0] == cur:
            heappop(Q)
            eatQ += 1

    if eatP + eatQ + eatR == X + Y:
        return print(ans)
    if X != eatP:
        while X != eatP and eatP + eatQ + eatR < X + Y:
            if R and P:
                cur = min(R[0], P[0])
            elif R:
                cur = R[0]
            elif P:
                cur = P[0]
            else:
                break
            ans -= cur
            if R and R[0] == cur:
                heappop(R)
                eatR += 1
            elif P and P[0] == cur:
                heappop(P)
                eatP += 1
    elif Y != eatQ:
        while Y != eatQ and eatP + eatQ + eatR < X + Y:
            if R and Q:
                cur = min(R[0], Q[0])
            elif R:
                cur = R[0]
            elif Q:
                cur = Q[0]
            else:
                break
            ans -= cur
            if R and R[0] == cur:
                heappop(R)
                eatR += 1
            elif Q and Q[0] == cur:
                heappop(Q)
                eatQ += 1
    print(ans)


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
def main():
    import sys
    input = sys.stdin.buffer.readline
    X, Y, A, B, C = (int(i) for i in input().split())
    P = [-int(i) for i in input().split()]
    Q = [-int(i) for i in input().split()]
    R = [-int(i) for i in input().split()]
    from heapq import heapify, heappop
    heapify(P)
    heapify(Q)
    heapify(R)
    eatP = 0
    eatQ = 0
    eatR = 0
    ans = 0
    while X != eatP and Y != eatQ and eatP + eatQ + eatR < X + Y:
        cur = min(P[0], Q[0], R[0])
        ans -= cur
        if R[0] == cur:
            heappop(R)
            eatR += 1
        elif P[0] == cur:
            heappop(P)
            eatP += 1
        elif Q[0] == cur:
            heappop(Q)
            eatQ += 1

    if eatP + eatQ + eatR == X + Y:
        return print(ans)
    if X != eatP:
        while X != eatP and eatP + eatQ + eatR < X + Y:
            cur = min(P[0], R[0])
            ans -= cur
            if R[0] == cur:
                heappop(R)
                eatR += 1
            elif P[0] == cur:
                heappop(P)
                eatP += 1
    elif Y != eatQ:
        while Y != eatQ and eatP + eatQ + eatR < X + Y:
            cur = min(Q[0], R[0])
            ans -= cur
            if R[0] == cur:
                heappop(R)
                eatR += 1
            elif Q[0] == cur:
                heappop(Q)
                eatQ += 1
    print(ans)


if __name__ == '__main__':
    main()