Problem p03111 (python)

Fixed submission:
import itertools
N,A,B,C=map(int,input().split())
l=list()
for i in range(N):
    l.append(int(input()))


def solve(x,ans,m):
    global N,A,B,C,l

    c=[10000,-1]
    for i in  range(0,len(l)):
        if(abs(l[i]-x)<=10 and abs(l[i]-x)<=c[0]):
            c[0]=abs(l[i]-x)
            c[1]=i
    
    if(c[1]!=-1):
        tmp=l[c[1]]
        l.pop(c[1])

        return(ans+abs(tmp-x))
    else:
        minst=1000000
        minset=[]
        for i in range(1,len(l)-m+1):
                for j in itertools.combinations(l,i):
                        if(abs(sum(j)-x)+(len(j)-1)*10<minst):
                                minst=abs(sum(j)-x)+(len(j)-1)*10
                                minset=[]
                                for z in j:
                                        minset.append(z)
        for i in minset:
                l.remove(i)
        return(abs(sum(minset)-x)+(len(minset)-1)*10)




totalAns=1000000000
import copy
L=copy.deepcopy(l)

ans=0
ans+=solve(A,0,2)
ans+=solve(B,0,1)
ans+=solve(C,0,0)
if(ans<totalAns):
    totalAns=ans

l=copy.deepcopy(L)
ans=0
ans+=solve(A,0,2)
ans+=solve(C,0,1)
ans+=solve(B,0,0)
if(ans<totalAns):
    totalAns=ans

l=copy.deepcopy(L)
ans=0
ans+=solve(B,0,2)
ans+=solve(A,0,1)
ans+=solve(C,0,0)
if(ans<totalAns):
    totalAns=ans

l=copy.deepcopy(L)
ans=0
ans+=solve(B,0,2)
ans+=solve(C,0,1)
ans+=solve(A,0,0)
if(ans<totalAns):
    totalAns=ans

l=copy.deepcopy(L)
ans=0
ans+=solve(C,0,2)
ans+=solve(A,0,1)
ans+=solve(B,0,0)
if(ans<totalAns):
    totalAns=ans

l=copy.deepcopy(L)
ans=0
ans+=solve(C,0,2)
ans+=solve(B,0,1)
ans+=solve(A,0,0)
if(ans<totalAns):
    totalAns=ans

print(totalAns)

Buggy submission (verdict: Runtime Error):
import itertools
N,A,B,C=map(int,input().split())
l=list()
for i in range(N):
    l.append(int(input()))


def solve(x,ans):
    global N,A,B,C,l
    c=[10000,-1]
    for i in  range(0,len(l)):
        if(abs(l[i]-x)<=10 and abs(l[i]-x)<=c[0]):
            c[0]=abs(l[i]-x)
            c[1]=i
    
    if(c[1]!=-1):
        tmp=l[c[1]]
        l.pop(c[1])
        return(ans+abs(tmp-x))
    else:
        mins=100000
        minstmp=[0,0]
        for i in range(0,len(l)):
            if(mins<abs(x-l[i])):
                pass
            else:
                minstmp=[i,i]
                mins=abs(x-l[i])-10
        check=False
    
        for i in itertools.combinations(l,2):
            tmp=i[0]+i[1]
            if(abs(x-tmp)<mins):
                mins=abs(x-tmp)
                minstmp=[i[0],i[1]]
                check=True
        if(check):
            ans+=10
            l.append(minstmp[0]+minstmp[1])
            l.remove(minstmp[0])
            l.remove(minstmp[1])
            return solve(x,ans)
        else:
            l.pop(minstmp[0])
            return(ans+mins+10)



totalAns=1000000000
import copy
L=copy.deepcopy(l)

ans=0
ans+=solve(A,0)
ans+=solve(B,0)
ans+=solve(C,0)
if(ans<totalAns):
    totalAns=ans

l=copy.deepcopy(L)
ans=0
ans+=solve(A,0)
ans+=solve(C,0)
ans+=solve(B,0)
if(ans<totalAns):
    totalAns=ans

l=copy.deepcopy(L)
ans=0
ans+=solve(B,0)
ans+=solve(A,0)
ans+=solve(C,0)
if(ans<totalAns):
    totalAns=ans

l=copy.deepcopy(L)
ans=0
ans+=solve(B,0)
ans+=solve(C,0)
ans+=solve(A,0)
if(ans<totalAns):
    totalAns=ans

l=copy.deepcopy(L)
ans=0
ans+=solve(C,0)
ans+=solve(A,0)
ans+=solve(B,0)
if(ans<totalAns):
    totalAns=ans

l=copy.deepcopy(L)
ans=0
ans+=solve(C,0)
ans+=solve(B,0)
ans+=solve(A,0)
if(ans<totalAns):
    totalAns=ans

print(totalAns)