Problem p02727 (python)

Fixed submission:
from collections import *
from heapq import *
from itertools import *
from fractions import gcd
import sys
from decimal import *
import copy
from bisect import *
input=lambda :sys.stdin.readline().rstrip()


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()))
lst=[]
P.sort(reverse=True)
Q.sort(reverse=True)
R.sort()

while len(P)>X:
    P.pop()
while len(Q)>Y:
    Q.pop()

flag=1
while flag:
    if not R:
        break

    flag=0
    if P and Q:
        if P[-1]<Q[-1]:
            if P[-1]<R[-1]:
                P.pop()
                lst.append(R.pop())
                flag=1
        else:
            if Q[-1]<R[-1]:
                Q.pop()
                lst.append(R.pop())
                flag=1
    else:
        if P:
            if P[-1]<R[-1]:
                P.pop()
                lst.append(R.pop())
                flag=1
        elif Q:
            if Q[-1]<R[-1]:
                Q.pop()
                lst.append(R.pop())
                flag=1

for i in range( X+Y-(len(P)+len(Q)+len(lst))):
    if R:
        lst.append(R.pop())
    else:
        break
print(sum(P)+sum(Q)+sum(lst))

Buggy submission (verdict: Runtime Error):
from collections import *
from heapq import *
from itertools import *
from fractions import gcd
import sys
from decimal import *
import copy
from bisect import *
input=lambda :sys.stdin.readline().rstrip()


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()))
lst=[]
P.sort(reverse=True)
Q.sort(reverse=True)
R.sort()

while len(P)>X:
    P.pop()
while len(Q)>Y:
    Q.pop()

flag=1
while flag:
    if not R:
        break

    flag=0
    if P and R:
        if P[-1]<Q[-1]:
            if P[-1]<R[-1]:
                P.pop()
                lst.append(R.pop())
                flag=1
        else:
            if Q[-1]<R[-1]:
                Q.pop()
                lst.append(R.pop())
                flag=1
    else:
        if P:
            if P[-1]<R[-1]:
                P.pop()
                lst.append(R.pop())
                flag=1
        elif R:
            if Q[-1]<R[-1]:
                Q.pop()
                lst.append(R.pop())
                flag=1
for i in range( X+Y-(len(P)+len(Q)+len(lst))):
    if R:
        lst.append(R.pop())
print(sum(P)+sum(Q)+sum(lst))

Buggy submission (verdict: Runtime Error):
from collections import *
from heapq import *
from itertools import *
from fractions import gcd
import sys
from decimal import *
import copy
from bisect import *
input=lambda :sys.stdin.readline().rstrip()


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()))
lst=[]
P.sort(reverse=True)
Q.sort(reverse=True)
R.sort()

while len(P)>X:
    P.pop()
while len(Q)>Y:
    Q.pop()

flag=1
while flag:
    if not R:
        break
    flag=0
    if P[-1]<Q[-1]:
        if P[-1]<R[-1]:
            P.pop()
            lst.append(R.pop())
            flag=1
    else:
        if Q[-1]<R[-1]:
            Q.pop()
            lst.append(R.pop())
            flag=1
print(sum(P)+sum(Q)+sum(lst))