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):
import itertools

n,a,b,c=map(int,input().split())
l0=[a,b,c]
l0.sort()
l02=l0[:]
l02.reverse()
l=[]
for i in range(n):
  x=int(input())
  if l0.count(x)>0:
    l0.remove(x)
  else:
    l.append(x)
if len(l0)==0:
  print(0)
  exit()
l.sort()
ll=l[:]

cst1=0
l2=[]
for i in l0:
  if i<l[0]:
    cst1=l[0]-i
    l.remove(l[0])
  else:
    p=100000000000000
    r=[]
    for j in range(1,len(l)+1):
      for k in list(itertools.combinations(l,j)):
        q=abs(i-sum(k))+(j-1)*10
        if q<p:
          p=q
          r=k
    l2.append(tuple([r,p]))
    for j in r:
      l.remove(j)
for i in l2:
  cst1+=i[1]
print(cst1)

import itertools

n,a,b,c=map(int,input().split())
l0=[a,b,c]
l0.sort()
l02=l0[:]
l02.reverse()
l=[]
for i in range(n):
  x=int(input())
  if l0.count(x)>0:
    l0.remove(x)
  else:
    l.append(x)
if len(l0)==0:
  print(0)
  exit()
l.sort()
ll=l[:]

cst1=0
l2=[]
for i in l0:
  if i<l[0]:
    cst1=l[0]-i
    l.remove(l[0])
  else:
    p=100000000000000
    r=[]
    for j in range(1,len(l)+1):
      for k in list(itertools.combinations(l,j)):
        q=abs(i-sum(k))+(j-1)*10
        if q<p:
          p=q
          r=k
    l2.append(tuple([r,p]))
    for j in r:
      l.remove(j)
for i in l2:
  cst1+=i[1]

cst2=0
l2=[]
for i in l02:
  if len(ll)>0 and i<ll[0]:
    cst2=ll[0]-i
    ll.remove(ll[0])
  else:
    p=100000000000000
    r=[]
    for j in range(1,len(ll)+1):
      for k in list(itertools.combinations(ll,j)):
        q=abs(i-sum(k))+(j-1)*10
        if q<p:
          p=q
          r=k
    l2.append(tuple([r,p]))
    for j in r:
      ll.remove(j)
for i in l2:
  cst2+=i[1]
print(cst1,cst2)
print(min(cst1,cst2))

import itertools

n,a,b,c=map(int,input().split())
l0=[a,b,c]
l0.sort()
l02=l0[:]
l02.reverse()
l=[]
for i in range(n):
  x=int(input())
  if l0.count(x)>0:
    l0.remove(x)
  else:
    l.append(x)
if len(l0)==0:
  print(0)
  exit()
ll=l[:]

cst1=0
l2=[]
for i in l0:
  if i<min(l):
    cst1=min(l)-i
    l.remove(min(l))
  else:
    p=100000000000000
    r=[]
    for j in range(1,len(l)+1):
      for k in list(itertools.combinations(l,j)):
        q=abs(i-sum(k))+(j-1)*10
        if q<p:
          p=q
          r=k
    l2.append(tuple([r,p]))
    for j in r:
      l.remove(j)
for i in l2:
  cst1+=i[1]
print(cst1)

import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
# def LF(): return [float(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

n,a,b,c=LI()
l=[I() for _ in range(n)]

def dfs(cnt,aa,bb,cc):
  if cnt==n:
    if aa==0 or bb==0 or cc==0:
      return inf
    return abs(aa-a)+abs(bb-b)+abs(cc-c)-30

  aaa=dfs(cnt+1,aa+l[cnt],bb,cc)+10
  bbb=dfs(cnt+1,aa,bb+l[cnt],cc)+10
  ccc=dfs(cnt+1,aa,bb,cc+l[cnt])+10
  ddd=dfs(cnt+1,aa,bb,cc)
  return min(aaa,bbb,ccc,ddd)

print(dfs(0,0,0,0))


Verdicts: Wrong Answer

Wrong Answer

Wrong Answer

Accepted