Problem p03078 (python)

Fixed submission:
X,Y,Z,K = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = list(map(int,input().split()))
asort,bsort,csort = sorted(A,reverse=True),sorted(B,reverse=True),sorted(C,reverse=True)
print(asort[0] + bsort[0] + csort[0])
already = [[0,0,0]]
x,y,z = 0,0,0
place_sum = []
check = []
for k in range(K - 1):
    if [x + 1,y,z] not in already and x + 1 < X:
        already.append([x + 1,y,z])
        place_sum.append([x + 1,y,z])
        check.append(asort[x + 1] + bsort[y] + csort[z])
    if [x,y + 1,z] not in already and y + 1 < Y:
        already.append([x,y + 1,z])
        place_sum.append([x,y + 1,z])
        check.append(asort[x] + bsort[y + 1] + csort[z])
    if [x,y,z + 1] not in already and z + 1 < Z:
        already.append([x,y,z + 1])
        place_sum.append([x,y,z + 1])
        check.append(asort[x] + bsort[y] + csort[z + 1])
    print(max(check))
    x,y,z = place_sum[check.index(max(check))][0],place_sum[check.index(max(check))][1],place_sum[check.index(max(check))][2]
    place_sum.pop(check.index(max(check)))
    check.remove(max(check))

Buggy submission (verdict: Runtime Error):
x,y,z,k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = list(map(int,input().split()))
sam = []
a.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)
ba,bb,bc = 0,0,0
print(a[0] + b[0] + c[0])
used = set()
for K in range(k - 1):
	if ba < x - 1 and (ba + 1,bb,bc) not in used:
		sam.append([a[ba + 1] + b[bb] + c[bc],ba + 1,bb,bc])
        used.add((ba + 1,bb,bc))
	if bb < y - 1 and (ba,bb + 1,bc) not in used:
		sam.append([a[ba] + b[bb + 1] + c[bc],ba,bb + 1,bc])
        used.add((ba,bb + 1,bc))
	if bc < z - 1 and (ba,bb,bc + 1) not in used:
		sam.append([a[ba] + b[bb] + c[bc + 1],ba,bb,bc + 1])
        used.add((ba,bb,bc + 1))
	big = max(sam)
	sam.remove(big)
	print(big[0])
	ba,bb,bc = big[1],big[2],big[3]

Buggy submission (verdict: Runtime Error):
x,y,z,k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = list(map(int,input().split()))
sam = []
a.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)
ab = a[0] + b[0]
ac = a[0] + c[0]
bc = b[0] + c[0]
sam.append(a[0] + b[0] + c[0])
if len(a) > k:
	A = k
else:
	A = len(b)
if len(b) > k:
	B = k
else:
	B = len(b)
if len(c) > k:
	C = k
else:
	C = len(C)
for i in range(1,C):
	    sam.append(ab + c[K])
for i in range(1,B):
    	sam.append(ac + b[K])
for i in range(1,A):
    	sam.append(bc + a[K])
sam.sort(reverse=True)
for K in range(k):
	print(sam[K])

Buggy submission (verdict: Runtime Error):
import heapq
 
_x, _y, _z, k = map(int, input().split())
aa = list(reversed(sorted(map(int, input().split()))))
bb = list(reversed(sorted(map(int, input().split()))))
cc = list(reversed(sorted(map(int, input().split()))))
 
ar = []
history = set()
 
 
def push(x, y, z):
    if (x, y, z) in history:
        pass
    else:
        v = aa[x] + bb[y] + cc[z]
        heapq.heappush(ar, (-v, x, y, z))
        history.add((x, y, z))
 
 
def pop():
    (v, x, y, z) = heapq.heappop(ar)
    return (-v, x, y, z)
 
 
push(0, 0, 0)
for _ in range(k):
    (total, x, y, z) = pop()
    print(total)
    if x + 1 < len(aa):
        push(x + 1, y, z)
    if y + 1 < len(bb):
        push(x, y + 1, z)
    if z + 1 < len(cc):
        push(x, y, z + 1)
import heapq

_x, _y, _z, k = map(int, input().split())
aa = list(reversed(sorted(map(int, input().split()))))
bb = list(reversed(sorted(map(int, input().split()))))
cc = list(reversed(sorted(map(int, input().split()))))

ar = []
history = set()


def push(x, y, z):
    if (x, y, z) in history:
        pass
    else:
        v = aa[x] + bb[y] + cc[z]
        heapq.heappush(ar, (-v, x, y, z))
        history.add((x, y, z))


def pop():
    (v, x, y, z) = heapq.heappop(ar)
    return (-v, x, y, z)


push(0, 0, 0)
for _ in range(k):
    (total, x, y, z) = pop()
    print(total)
    if x + 1 < len(aa):
        push(x + 1, y, z)
    if y + 1 < len(bb):
        push(x, y + 1, z)
    if z + 1 < len(cc):
        push(x, y, z + 1)