Problem: You have N bamboos. The lengths (in centimeters) of these are l_1, l_2, ..., l_N, respectively.  Your objective is to use some of these bamboos (possibly all) to obtain three bamboos of length A, B, C. For that, you can use the following three kinds of magics any number:    * Extension Magic: Consumes 1 _MP_ (magic point). Choose one bamboo and increase its length by 1.   * Shortening Magic: Consumes 1 MP. Choose one bamboo of length at least 2 and decrease its length by 1.   * Composition Magic: Consumes 10 MP. Choose two bamboos and combine them into one bamboo. The length of this new bamboo is equal to the sum of the lengths of the two bamboos combined. (Afterwards, further magics can be used on this bamboo.)  At least how much MP is needed to achieve the objective?

Submission attempts (in order):



# A = 90, L! = [40,30,21] -> 20 + 1
import copy as cp
inf = 10**9
def cost(li, length):
    if len(li)==0:
        return inf
    return (len(li)-1)*10 + abs(sum(li)-length)

def search_cost(ln,assignment,length):
#     from IPython.core.debugger import Pdb; Pdb().set_trace()
    if len(ln)==0:
        costA = cost(assignment["A"], length[0])
        costB = cost(assignment["B"], length[1])
        costC = cost(assignment["C"], length[2])
        return costA + costB + costC
    val = ln[-1]
    candidate = []
    for take in assignment.keys():
        nextdic = cp.deepcopy(assignment)
        nextdic[take].append(val)
        print(nextdic)
        cand = search_cost(ln[:-1],nextdic,length)
        candidate.append(cand)
    return min(candidate)
  
  

N, A, B, C =map(int,input().split())
length = [A,B,C]
ln = []
assignment = {"A":[],"B":[],"C":[],"D":[]}

for i in range(N):
    a = int(input())
    ln.append(a)
   
search_cost(ln,assignment,length)


N, A, B, C =map(int,input().split())
l = []
for i in range(N):
    a = int(input())
    l.append(a)
print(l)

infty = 1000000

def DFS(i,uselist0,uselista,uselistb,uselistc):
    if i == N:
        return calc_mp(uselista,uselistb,uselistc)
    mplist = []
    u0 = uselist0 + [l[i]]
    ua = uselista + [l[i]]
    ub = uselistb + [l[i]]
    uc = uselistc + [l[i]]
    mplist.append(DFS(i+1, u0,uselista,uselistb,uselistc))
    mplist.append(DFS(i+1, uselist0,ua,uselistb,uselistc))
    mplist.append(DFS(i+1, uselist0,uselista,ub,uselistc))
    mplist.append(DFS(i+1, uselist0,uselista,uselistb,uc))
    return min(mplist)


def calc_mp(uselista,uselistb,uselistc):
    mp = 0
    
    mp += (max(len(uselista) - 1, 0)) * 10
    mp += (max(len(uselistb) - 1, 0)) * 10
    mp += (max(len(uselistc) - 1, 0)) * 10

    
    mp += abs(A - sum(uselista))
    mp += abs(B - sum(uselistb))
    mp += abs(C - sum(uselistc))

    if len(uselista)==0:
        mp += infty
    if len(uselistb)==0:
        mp += infty
    if len(uselistc)==0:
        mp += infty
    return mp

print(DFS(0,[],[],[],[]))


N, A, B, C =map(int,input().split())
l = []
for i in range(N):
    a = input()
    l.append(a)

def DFS(i,uselist0,uselista,uselistb,uselistc):
    if i == N:
        uselist = [uselist0,uselista,uselistb,uselistc]
        return calc_mp(uselist)
    mplist = []
    u0 = uselist0 + [l[i]]
    ua = uselista + [l[i]]
    ub = uselistb + [l[i]]
    uc = uselistc + [l[i]]
    mplist.append(DFS(i+1, u0,uselista,uselistb,uselistc))
    mplist.append(DFS(i+1, uselist0,ua,uselistb,uselistc))
    mplist.append(DFS(i+1, uselist0,uselista,ub,uselistc))
    mplist.append(DFS(i+1, uselist0,uselista,uselistb,uc))
    return min(mplist)


def calc_mp(uselist):
    mp = 0

    
    mp += (max(len(uselist[1]) - 1, 0)) * 10
    mp += (max(len(uselist[2]) - 1, 0)) * 10
    mp += (max(len(uselist[3]) - 1, 0)) * 10

    
    mp += abs(A - sum(uselist[1]))
    mp += abs(B - sum(uselist[2]))
    mp += abs(C - sum(uselist[3]))

    return mp

print(DFS(0,[],[],[],[]))




# A = 90, L! = [40,30,21] -> 20 + 1
import copy as cp
inf = 10**9
def cost(li, length):
    if len(li)==0:
        return inf
    return (len(li)-1)*10 + abs(sum(li)-length)

def search_cost(ln,assignment,length):
#     from IPython.core.debugger import Pdb; Pdb().set_trace()
    if len(ln)==0:
        costA = cost(assignment["A"], length[0])
        costB = cost(assignment["B"], length[1])
        costC = cost(assignment["C"], length[2])
        return costA + costB + costC
    val = ln[-1]
    candidate = []
    for take in assignment.keys():
        nextdic = cp.deepcopy(assignment)
        nextdic[take].append(val)
        cand = search_cost(ln[:-1],nextdic,length)
        candidate.append(cand)
    return min(candidate)
  
  

N, A, B, C =map(int,input().split())
length = [A,B,C]
ln = []
assignment = {"A":[],"B":[],"C":[],"D":[]}

for i in range(N):
    a = int(input())
    ln.append(a)
   
print(search_cost(ln,assignment,length))



Verdicts: Wrong Answer

Wrong Answer

Runtime Error

Accepted