Problem p02727 (python)

Fixed submission:
from sys import stdin, stdout  

X,Y,A,B,C = [int(c) for c in input().split()]

p= [int(c) for c in input().split()]
q= [int(c) for c in input().split()]
r= [int(c) for c in input().split()]

p.sort(reverse=True)
q.sort(reverse=True)
r.sort(reverse=True)

p=p[:X]
q=q[:Y]
    
p.sort()
q.sort()
res=sum(p)+sum(q)

i=j=0

for k,val in enumerate(r):
    if(i<len(p) and j<len(q)):
        if(val<=p[i] and val <= q[j]):
            break
        elif(val>p[i] and val<q[j]):
            res=res+val-p[i]
            i=i+1
        elif(val<p[i] and val>q[j]):
            res=res+val-q[j]
            j=j+1
        elif(val>p[i] and val>q[j]):
            if(p[i]>q[j]):
                res=res+val-q[j]
                j=j+1
            else:
                res=res+val-p[i]
                i=i+1
    elif(i<len(p) and j==len(q)):
        if(val<=p[i]):
            break
        elif(val>p[i]):
            res=res+val-p[i]
            i=i+1
    elif(i==len(p) and j<len(q)):
        if(val<=q[j]):
            break
        elif(val>q[j]):
            res=res+val-q[j]
            j=j+1
    elif(i==len(p) and j==len(q)):
        break
        
print(res)

Buggy submission (verdict: Runtime Error):
from sys import stdin, stdout  

X,Y,A,B,C = [int(c) for c in input().split()]

p= [int(c) for c in input().split()]
q= [int(c) for c in input().split()]
r= [int(c) for c in input().split()]

p.sort(reverse=True)
q.sort(reverse=True)
r.sort(reverse=True)

p=p[:X]
q=q[:Y]
    
p.sort()
q.sort()
res=sum(p)+sum(q)

i=j=0

for k,val in enumerate(r):
    if(i<len(p) and j<len(q)):
        if(val<=p[i] and val <= q[j]):
            break
        if(val>p[i] and val<q[j]):
            res=res+val-p[i]
            i=i+1
        if(val<p[i] and val>q[j]):
            res=res+val-q[j]
            j=j+1
        if(val>p[i] and val>q[j]):
            if(p[i]>q[j]):
                res=res+val-q[j]
                j=j+1
            else:
                res=res+val-p[i]
                i=i+1
    if(i<len(p) and j==len(q)):
        if(val<=p[i]):
            break
        if(val>p[i]):
            res=res+val-p[i]
            i=i+1
    if(i==len(p) and j<len(q)):
        if(val<=q[j]):
            break
        if(val>q[j]):
            res=res+val-q[j]
            j=j+1
    if(i==len(p) and j==len(q)):
        break
        
print(res)

Buggy submission (verdict: Runtime Error):
from sys import stdin, stdout  

X,Y,A,B,C = [int(c) for c in input().split()]

p= [int(c) for c in input().split()]
q= [int(c) for c in input().split()]
r= [int(c) for c in input().split()]

p.sort(reverse=True)
q.sort(reverse=True)
r.sort(reverse=True)

p=p[:X]
q=q[:Y]
    
p.sort()
q.sort()
res=sum(p)+sum(q)

i=j=0

for k,val in enumerate(R):
    if(i<len(p) and j<len(q)):
        if(val<=p[i] and val <= q[j]):
            break
        if(val>p[i] and val<q[j]):
            res=res+val-p[i]
            i=i+1
        if(val<p[i] and val>q[j]):
            res=res+val-q[j]
            j=j+1
        if(val>p[i] and val>q[j]):
            if(p[i]>q[j]):
                res=res+val-q[j]
                j=j+1
            else:
                res=res+val-p[i]
                i=i+1
    if(i<len(p) and j==len(q)):
        if(val<=p[i]):
            break
        if(val>p[i]):
            res=res+val-p[i]
            i=i+1
    if(i==len(p) and j<len(q)):
        if(val<=q[j]):
            break
        if(val>q[j]):
            res=res+val-q[j]
            j=j+1
    if(i==len(p) and j==len(q)):
        break
        
print(res)

Buggy submission (verdict: Runtime Error):
from sys import stdin, stdout  

X,Y,A,B,C = [int(c) for c in input().split()]

p= [int(c) for c in input().split()]
q= [int(c) for c in input().split()]
r= [int(c) for c in input().split()]

p.sort(reverse=True)
q.sort(reverse=True)
r.sort(reverse=True)

p=p[:X]
q=q[:Y]
    
p.sort()
q.sort()
res=sum(p)+sum(q)

i=j=0

for k,val in enumerate(r):
    if(val<=p[i] and val <= q[j]):
        break
    if(val>p[i] and val<q[j]):
        res=res+val-p[i]
        i=i+1
    if(val<p[i] and val>q[j]):
        res=res+val-q[j]
        j=j+1
    if(val>p[i] and val>q[j]):
        if(p[i]>q[j]):
            res=res+val-q[j]
            j=j+1
        else:
            res=res+val-p[i]
            i=i+1
        
print(res)