Problem p02727 (python)

Fixed submission:
import sys
import math
from collections import defaultdict
x,y,a,b,c=map(int,sys.stdin.readline().split())
red=list(map(int,sys.stdin.readline().split()))
green=list(map(int,sys.stdin.readline().split()))
color=list(map(int,sys.stdin.readline().split()))
red.sort()
green.sort()
color.sort()
s=0
for i in range(a-x,a):
    s+=red[i]
for i in range(b-y,b):
    s+=green[i]
i,j,k=c-1,a-x,b-y
#print(red,'red')
#print(green,'green')
#print(color,'color')
#print(s,'s',x,'x',y,'y',i,'i',j,'j',k,'k')
while i>=0 and j<a and k<b:
    if color[i]>red[j] and color[i]>green[k]:
        if green[k]>red[j]:
            s-=red[j]
            s+=color[i]
            j+=1
            i-=1
            continue
        else:
            s-=green[k]
            s+=color[i]
            k+=1
            i-=1
            continue
    if color[i]>red[j]:
        s-=red[j]
        s+=color[i]
        i-=1
        j+=1
        continue
    if color[i]>green[k]:
        s-=green[k]
        s+=color[i]
        i-=1
        k+=1
        continue
    i-=1
if i==0:
    print(s)
else:
    while i>=0 and j<a:
        if color[i]>red[j]:
            s-=red[j]
            s+=color[i]
            i-=1
            j+=1
            continue
        i-=1
    while i>=0 and k<b:
        if color[i]>green[k]:
            s-=green[k]
            s+=color[i]
            i-=1
            k+=1
            continue
        i-=1
    print(s)

Buggy submission (verdict: Runtime Error):
import sys
import math
from collections import defaultdict
x,y,a,b,c=map(int,sys.stdin.readline().split())
red=list(map(int,sys.stdin.readline().split()))
green=list(map(int,sys.stdin.readline().split()))
color=list(map(int,sys.stdin.readline().split()))
red.sort()
green.sort()
color.sort()
s=0
for i in range(a-x,a):
    s+=red[i]
for i in range(b-y,b):
    s+=green[i]
i,j,k=c-1,a-x,b-y
#print(red,'red')
#print(green,'green')
#print(color,'color')
#print(s,'s',x,'x',y,'y',i,'i',j,'j',k,'k')
while i>=0 and j<a and k<b:
    if color[i]>red[j] and color[i]>green[k]:
        if green[k]>red[j]:
            s-=red[j]
            s+=color[i]
            j+=1
            i-=1
            continue
        else:
            s-=green[k]
            s+=color[i]
            k+=1
            i-=1
            continue
    if color[i]>red[j]:
        s-=red[j]
        s+=color[i]
        i-=1
        j+=1
        continue
    if color[i]>green[k]:
        s-=green[k]
        s+=color[i]
        i-=1
        k+=1
        continue
    i-=1
if i==0:
    print(s)
else:
    while i>=0 and j<a:
        if color[i]>red[j]:
            s-=red[j]
            s+=color[i]
            i-=1
            j+=1
            continue
        i+=1
    while i>=0 and k<b:
        if color[i]>green[k]:
            s-=green[k]
            s+=color[i]
            i-=1
            k+=1
            continue
        i-=1
    print(s)

Buggy submission (verdict: Runtime Error):
import sys
import math
from collections import defaultdict
x,y,a,b,c=map(int,sys.stdin.readline().split())
red=list(map(int,sys.stdin.readline().split()))
green=list(map(int,sys.stdin.readline().split()))
color=list(map(int,sys.stdin.readline().split()))
red.sort()
green.sort()
color.sort()
s=0
for i in range(a-x,a):
    s+=red[i]
for i in range(b-y,b):
    s+=green[i]
i,j,k=c-1,a-x,b-y
#print(red,'red')
#print(green,'green')
#print(color,'color')
#print(s,'s',x,'x',y,'y',i,'i',j,'j',k,'k')
while i>=0 and j<a and k<b:
    if color[i]>red[j] and color[i]>green[k]:
        if green[k]>red[j]:
            s-=red[j]
            s+=color[i]
            j+=1
            i-=1
            continue
        else:
            s-=green[k]
            s+=color[i]
            k+=1
            i-=1
            continue
    if color[i]>red[j]:
        s-=red[j]
        s+=color[i]
        i-=1
        j+=1
        continue
    if color[i]>green[k]:
        s-=green[k]
        s+=color[i]
        i-=1
        k+=1
        continue
    i-=1
if i==c:
    print(s)
else:
    while i>=0 and j<a:
        if color[i]>red[j]:
            s-=red[j]
            s+=color[i]
            i-=1
            j+=1
            continue
        i+=1
    while i>=0 and k<b:
        if color[i]>green[k]:
            s-=green[k]
            s+=color[i]
            i-=1
            k+=1
            continue
        i-=1
    print(s)