So, here's my solution in Python, it seems to be working for all the test cases I've input but CodeChef is marking it NZEC Runtime Error for some reason. It would be great if someone shall be able to help me figure out the way to prevent this error.

for q in range(2):a=list(map(int,input().split()))n = a[0]k = a[1]one = a[2:n+2]two = a[n+2:]three = a[2:n+2]four = a[n+2:]one.sort()two.sort()three.sort()four.sort()for i in range(0,k):if max(one) > min(two):temp = max(one)one[-1] = min(two)two[0] = tempans1 = max(one) + max(two)