Problem p03078 (python)

Fixed submission:
X, Y, Z, K = map(int, input().split())

A_list = list(map(int, input().split()))
B_list = list(map(int, input().split()))
C_list = list(map(int, input().split()))

val_list = []


A_list = sorted(A_list, reverse=True)
B_list = sorted(B_list, reverse=True)
C_list = sorted(C_list, reverse=True)

#print(A_list)
#print(B_list)
#print(C_list)

for a in range(len(A_list)):
    if a + 1 > K:
        break
    for b in range(len(B_list)):
        if b + 1 > K:
            break
        if (a+1) * (b+1) > K:
            break
            
        for c in range(len(C_list)):
            if c + 1 > K:
                break
            if (a+1) * (b+1) * (c+1) > K:
                break
        
            val_list.append(A_list[a] + B_list[b] + C_list[c])

val_list = sorted(val_list, reverse=True)
for i in range(K):
    print(val_list[i])

Buggy submission (verdict: Runtime Error):
X, Y, Z, K = map(int, input().split())

A_list = list(map(int, input().split()))
B_list = list(map(int, input().split()))
C_list = list(map(int, input().split()))

def qsort(inlist, reverse=True):
    if inlist == []: 
        return []
    else:
        pivot = inlist[0]
        if reverse == False:
            lesser = qsort([x for x in inlist[1:] if x < pivot])
            greater = qsort([x for x in inlist[1:] if x >= pivot])
        else:
            lesser = qsort([x for x in inlist[1:] if x >= pivot])
            greater = qsort([x for x in inlist[1:] if x < pivot])
        return lesser + [pivot] + greater
      
val_list = []


A_list = qsort(A_list, reverse=True)
B_list = qsort(B_list, reverse=True)
C_list = qsort(C_list, reverse=True)

#print(A_list)
#print(B_list)
#print(C_list)

for a in range(len(A_list)):
    if a + 1 > K:
        break
    for b in range(len(B_list)):
        if b + 1 > K:
            break
        if (a+1) * (b+1) > K:
            break
            
        for c in range(len(C_list)):
            if c + 1 > K:
                break
            if (a+1) * (b+1) * (c+1) > K:
                break
        
            val_list.append(A_list[a] + B_list[b] + C_list[c])

val_list = qsort(val_list, reverse=True)
for i in range(K):
    print(val_list[i])

Buggy submission (verdict: Runtime Error):
X, Y, Z, K = map(int, input().split())

A_list = list(map(int, input().split()))
B_list = list(map(int, input().split()))
C_list = list(map(int, input().split()))

def qsort(inlist, reverse=True):
    if inlist == []: 
        return []
    else:
        pivot = inlist[0]
        if reverse == False:
            lesser = qsort([x for x in inlist[1:] if x < pivot])
            greater = qsort([x for x in inlist[1:] if x >= pivot])
        else:
            lesser = qsort([x for x in inlist[1:] if x >= pivot])
            greater = qsort([x for x in inlist[1:] if x < pivot])
        return lesser + [pivot] + greater
      
val_list = []


A_list = qsort(A_list, reverse=True)
B_list = qsort(B_list, reverse=True)
C_list = qsort(C_list, reverse=True)

#print(A_list)
#print(B_list)
#print(C_list)

for a in range(len(A_list)):
    if a + 1 > K:
        break
    for b in range(len(B_list)):
        if b + 1 > K:
            break
        if (a+1) * (b+1) > K:
            continue
            
        for c in range(len(C_list)):
            if c + 1 > K:
                break
            if (a+1) * (b+1) * (c+1) > K:
                continue
        
            val_list.append(A_list[a] + B_list[b] + C_list[c])

val_list = qsort(val_list, reverse=True)
for i in range(K):
    print(val_list[i])

Buggy submission (verdict: Runtime Error):
X, Y, Z, K = map(int, input().split())

A_list = list(map(int, input().split()))
B_list = list(map(int, input().split()))
C_list = list(map(int, input().split()))

K = 2

import itertools
val_list = []


A_list = sorted(A_list, reverse=True)
B_list = sorted(B_list, reverse=True)
C_list = sorted(C_list, reverse=True)

#print(A_list)
#print(B_list)
#print(C_list)

for a, b, c in itertools.product(range(len(A_list)), range(len(B_list)), range(len(C_list))):
    if (a+1) * (b+1) * (c+1) > K:
        continue
        
    val_list.append(A_list[a] + B_list[b] + C_list[c])

val_list = sorted(val_list, reverse=True)
for i in range(K):
    print(val_list[i])