Problem:
 Score : 300 points    Problem Statement 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?      Constraints  3 \leq N \leq 8  1 \leq C < B < A \leq 1000  1 \leq l_i \leq 1000  All values in input are integers.         Input Input is given from Standard Input in the following format:  N  A  B  C  l_1  l_2  :  l_N       Output Print the minimum amount of MP needed to achieve the objective.        Sample Input 1 5 100 90 80
98
40
30
21
80      Sample Output 1 23  We are obtaining three bamboos of lengths 100, 90, 80 from five bamboos 98, 40, 30, 21, 80 . We already have a bamboo of length 80 , and we can obtain bamboos of lengths 100, 90 by using the magics as follows at the total cost of 23 MP, which is optimal.   Use Extension Magic twice on the bamboo of length 98 to obtain a bamboo of length 100 . (MP consumed: 2 )  Use Composition Magic on the bamboos of lengths 40, 30 to obtain a bamboo of length 70 . (MP consumed: 10 )  Use Shortening Magic once on the bamboo of length 21 to obtain a bamboo of length 20 . (MP consumed: 1 )  Use Composition Magic on the bamboo of length 70 obtained in step 2 and the bamboo of length 20 obtained in step 3 to obtain a bamboo of length 90 . (MP consumed: 10 )        Sample Input 2 8 100 90 80
100
100
90
90
90
80
80
80      Sample Output 2 0  If we already have all bamboos of the desired lengths, the amount of MP needed is 0 . As seen here, we do not necessarily need to use all the bamboos.       Sample Input 3 8 1000 800 100
300
333
400
444
500
555
600
666      Sample Output 3 243

Solution (Python):
N, A, B, C = map(int, input().split())
l = [int(input()) for i in range(N)]
INF = 10 ** 9
def dfs(cur, a, b, c):
    if cur == N:
        return abs(a - A) + abs(b - B) + abs(c - C) - 30 if min(a, b, c) > 0 else INF
    ret0 = dfs(cur + 1, a, b, c)
    ret1 = dfs(cur + 1, a + l[cur], b, c) + 10
    ret2 = dfs(cur + 1, a, b + l[cur], c) + 10
    ret3 = dfs(cur + 1, a, b, c + l[cur]) + 10
    return min(ret0, ret1, ret2, ret3)
print(dfs(0, 0, 0, 0))

Solution (Python):
def Base_10_to_n(x,n):
  tmp=x
  out=''
  while tmp>0:
    out=str(tmp%n)+out
    tmp=int(tmp/n)
  return out
n,a,b,c=map(int,input().split())
l=[int(input()) for i in range(n)]
ans=10**18
for i in range(4**n):
  cost=0
  d=[[] for j in range(4)]
  s=Base_10_to_n(i,4).zfill(n)
  for j in range(n):
    k=int(s[j])
    d[k].append(l[j])
  if len(d[1])*len(d[2])*len(d[3])==0:
    continue
  for j in range(1,4):
    cost+=(len(d[j])-1)*10
  d1=sum(d[1])
  d2=sum(d[2])
  d3=sum(d[3])
  L=[abs(d1-a)+abs(d2-b)+abs(d3-c),abs(d1-a)+abs(d2-c)+abs(d3-b),abs(d1-b)+abs(d2-a)+abs(d3-c),abs(d1-b)+abs(d2-c)+abs(d3-a),abs(d1-c)+abs(d2-a)+abs(d3-b),abs(d1-c)+abs(d2-b)+abs(d3-a)]
  cost+=min(L)
  ans=min(ans,cost)
print(ans)

Solution (Python):
N, A, B, C = map(int, input().split())
l = [int(input()) for _ in range(N)]
INF =  10 ** 9

def dfs(cur, a, b, c):
    if cur == N:
        return abs(a-A) + abs(b-B) + abs(c-C) - 30 if min(a, b, c) > 0 else INF
    ret0 = dfs(cur+1, a, b, c)
    ret1 = dfs(cur+1, a+l[cur], b, c) + 10
    ret2 = dfs(cur+1, a, b+l[cur], c) + 10
    ret3 = dfs(cur+1, a, b, c+l[cur]) + 10
    return min(ret0, ret1, ret2, ret3)

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

Solution (Python):
def dfs(i, a, b, c):
    if i == N:
        return abs(a-A) + abs(b-B) + abs(c-C) - 30 if min(a, b, c) > 0 else float('inf')
    mx = dfs(i+1, a, b, c)
    ma = dfs(i+1, a+l[i], b, c) + 10
    mb = dfs(i+1, a, b+l[i], c) + 10
    mc = dfs(i+1, a, b, c+l[i]) + 10
    return min(mx, ma, mb, mc)


N, A, B, C = map(int, input().split())
l = [int(input()) for _ in range(N)]
print(dfs(0, 0, 0, 0))

Solution (Python):
def change_base(n, base):
    q = n // base
    r = n % base
    if q == 0:
        return str(r)
    return change_base(q, base) + str(r)


N, A, B, C = map(int, input().split())
target = [A, B, C]
l = [int(input()) for _ in range(N)]
ans = float('inf')
for n in range(4**N):
    bit = change_base(n, 4)
    bit = '0'*(N-len(bit)) + bit
    start = [0] * 3
    mp = 0
    for i in range(N):
        for j in range(3):
            if bit[i] == str(j):
                if start[j] > 0:
                    mp += 10
                start[j] += l[i]
    if any(s == 0 for s in start):
        continue
    for j in range(3):
        mp += abs(target[j]-start[j])
    ans = min(ans, mp)
print(ans)

Solution (Python):
n, a, b, c = map(int, input().split())
l = [int(input()) for _ in range(n)]


def dfs(i, x, y, z):
    if i == n:
        return abs(a - x) + abs(b - y) + abs(c - z) - 30 if min(
            x, y, z) > 0 else float('inf')
    res0 = dfs(i + 1, x, y, z)
    res1 = dfs(i + 1, x + l[i], y, z) + 10
    res2 = dfs(i + 1, x, y + l[i], z) + 10
    res3 = dfs(i + 1, x, y, z + l[i]) + 10
    return min(res0, res1, res2, res3)


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

Solution (Python):
N,A,B,C = map(int,input().split())
L = [int(input()) for _ in range(N)]

minmp = 10**18

for i in range(4**N):
    mp = 0
    a = []
    b = []
    c = []
    d = []
    
    for j in range(N):
        if i>>(2*j) & 1:
            if i>>(2*j+1) & 1:
                a.append(j)
            else:
                b.append(j)
        else:
            if i>>(2*j+1) & 1:
                c.append(j)
            else:
                d.append(j)
                
    if not a or not b or not c:
        continue
        
    mp += (len(a)-1)*10
    mp += abs(A-sum([L[j] for j in a]))
    mp += (len(b)-1)*10
    mp += abs(B-sum([L[j] for j in b]))
    mp += (len(c)-1)*10
    mp += abs(C-sum([L[j] for j in c]))
    minmp = min(mp,minmp)

print(minmp)

Solution (Python):
n,a,b,c = map(int,input().split())
l = [int(input()) for _ in range(n)]
ans = 10000
for i in range(2 ** (2*n)):
  v = [0,0,0,0]
  assign = [(i>>(2*j)) & 3 for j in range(n)]
  cost = 0
  for j in range(n):
    if assign[j] > 0 and v[assign[j]] > 0:
      cost += 10
    v[assign[j]] += l[j]
  if v[1] == 0 or v[2] == 0 or v[3] == 0:
    continue
  ans = min(ans, cost + abs(v[1]-a) + abs(v[2]-b) + abs(v[3]-c))
print(ans)

Solution (Python):
n, A, B, C = map(int, input().split())
l = [int(input()) for _ in range(n)]


def recur(n, i, a, b, c):
    if i == n:
        if a < 1 or b < 1 or c < 1:
            return 10 ** 18
        return abs(A-a) + abs(B-b) + abs(C-c)
    return min([
        recur(n, i + 1, a, b, c),
        recur(n, i + 1, a + l[i], b, c) + (10 if a > 0 else 0),
        recur(n, i + 1, a, b + l[i], c) + (10 if b > 0 else 0),
        recur(n, i + 1, a, b, c + l[i]) + (10 if c > 0 else 0)
    ])


print(recur(n, 0, 0, 0, 0))

Solution (Python):
n, A, B, C = map(int, input().split())
l = [int(input()) for _ in range(n)]


def recur(n, i, a, b, c):
    if i == n:
        if a < 1 or b < 1 or c < 1:
            return 10 ** 18
        return abs(A-a) + abs(B-b) + abs(C-c)
    return min([
        recur(n, i + 1, a, b, c),
        recur(n, i + 1, a + l[i], b, c) + (10 if a > 0 else 0),
        recur(n, i + 1, a, b + l[i], c) + (10 if b > 0 else 0),
        recur(n, i + 1, a, b, c + l[i]) + (10 if c > 0 else 0)
    ])


print(recur(n, 0, 0, 0, 0))

Solution (Python):
N, A, B, C = map(int, input().split())
L = [int(input()) for _ in range(N)]


def dfs(i, a, b, c):
    if i == N:
        if min(a, b, c) != 0:  
            return abs(A - a) + abs(B - b) + abs(C - c)
        else:
            return float('inf')
        
    res1 = dfs(i+1, a, b, c)
    
    
    mp2 = 10 if a > 0 else 0
    mp3 = 10 if b > 0 else 0
    mp4 = 10 if c > 0 else 0
    
    res2 = dfs(i+1, a+L[i], b, c) + mp2
    res3 = dfs(i+1, a, b+L[i], c) + mp3
    res4 = dfs(i+1, a, b, c+L[i]) + mp4
    
    return min(res1, res2, res3, res4)

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

Solution (Python):
import itertools
n,a,b,c=map(int,input().split())
x=[a,b,c]
l=[int(input()) for i in range(n)]
ans=1000000
loop='012'
for i in range(2**n):
    use=[]
    for j in range(n):
        if((i>>j)&1):
            use.append(l[j])
    if(len(use)>=3):
        for j in (itertools.product(loop,repeat=len(use))):
            A=[[] for i in range(3)]
            for k in range(len(use)):
                A[int(j[k])].append(use[k])
                
            if(len(A[0])!=0 and len(A[1])!=0 and len(A[2])!=0):
                res=0
                for i in range(3):
                    res+=abs(sum(A[i])-x[i])+(len(A[i])-1)*10
                ans=min(res,ans)
print(ans)

Solution (Python):
import itertools
import time
start=time.time()
n,a,b,c=map(int,input().split())
l=[int(input()) for i in range(n)]
ans=1000000007
for i in range(2**n):
    use=[]
    for j in range(n):
        if((i>>j)&1):
            use.append(l[j])
    loop='123'
    if(len(use)>=3):
        for j in (itertools.product(loop,repeat=len(use))):
            A=[]
            B=[]
            C=[]
            for k in range(len(use)):
                if(j[k]=='1'):
                    A.append(use[k])
                elif(j[k]=='2'):
                    B.append(use[k])
                elif(j[k]=='3'):
                    C.append(use[k])
            if(len(A)!=0 and len(B)!=0 and len(C)!=0):
                res=abs(sum(A)-a)+((len(A)-1)*10)+abs(sum(B)-b)+((len(B)-1)*10)+abs(sum(C)-c)+((len(C)-1)*10)
                ans=min(res,ans)
    else:
        continue
print(ans)

Solution (Python):
def solve(cur, A, B, C):
    if cur == n:
        if min(A, B, C) > 0:
            return abs(A - a) + abs(B - b) + abs(C - c) - 30
        else:
            return 10 ** 9 + 7


    use_for_a = solve(cur + 1, A + l[cur], B, C) + 10
    use_for_b = solve(cur + 1, A, B + l[cur], C) + 10
    use_for_c = solve(cur + 1, A, B, C + l[cur]) + 10
    not_use = solve(cur + 1, A, B, C)

    return min(use_for_a, use_for_b, use_for_c, not_use)


n, a, b, c = map(int, input().split())
l = [int(input()) for i in range(n)]
print(solve(0, 0, 0, 0))

Solution (Python):
N,A,B,C = map(int, input().split())
l = [int(input()) for _ in range(N)]

INF = 10**9

def mag(i, a, b, c):
    if i == N:
        if min(a,b,c) > 0:
            return abs(A-a) + abs(B-b) + abs(C-c) - 30
        else:
            return INF

    r0 = mag(i+1, a     , b     , c     )
    r1 = mag(i+1, a+l[i], b     , c     ) + 10
    r2 = mag(i+1, a     , b+l[i], c     ) + 10
    r3 = mag(i+1, a     , b     , c+l[i]) + 10

    return min(r0, r1, r2, r3)

print(mag(0,0,0,0))

Solution (Python):
N, A, B, C, *L = map(int, open(0).read().split())
ans = float('inf')
for i in range(4**N):
  a = 0
  b = 0
  c = 0
  m = 0
  for j in range(N):
    if i%4==0:
      if a!=0:
        m += 10
      a += L[j]
    elif i%4==1:
      if b!=0:
        m += 10
      b += L[j]
    elif i%4==2:
      if c!=0:
        m += 10
      c += L[j]
    i //= 4
  if a==0 or b==0 or c==0:
    continue
  ans = min(ans, m+abs(A-a)+abs(B-b)+abs(C-c))
print(ans)

Solution (Python):
N,A,B,C=map(int,input().split())
l_list=[]
for i in range(N):
    l_list.append(int(input()))
def f(i,a,b,c):
    if i==N:
        if a*b*c==0:
            return 10**10
        else:
            return abs(a-A)+abs(b-B)+abs(c-C)-30
    else:
        addno=f(i+1,a,b,c)
        addA=f(i+1,a+l_list[i],b,c)+10
        addB=f(i+1,a,b+l_list[i],c)+10
        addC=f(i+1,a,b,c+l_list[i])+10
        return(min(addno,addA,addB,addC))
print(f(0,0,0,0))

Solution (Python):
n,a,b,c=map(int,input().split())
tree=[a,b,c]
ls=[int(input()) for _ in range(n)]
ans=10000
for i in range(4**n):
    group=[[] for _ in range(4)]
    for j in range(n):
        k,i=i%4,i//4
        group[k].append(ls[j])
    buf=0
    for l in range(3):
        group_len=len(group[l])
        if group_len==0:
            buf=10000
            break
        buf+=(group_len-1)*10+abs(sum(group[l])-tree[l])
    ans=min(ans,buf)
print(ans)

Solution (Python):

N,A,B,C=map(int,input().split())
l=[int(input()) for _ in range(N)]
INF=10**9

def dfs(x,a,b,c):
    if x==N:
        return abs(a-A)+abs(b-B)+abs(c-C)-30 if min(a,b,c)>0 else INF
    res0=dfs(x+1,a,b,c)
    res1=dfs(x+1,a+l[x],b,c)+10
    res2=dfs(x+1,a,b+l[x],c)+10
    res3=dfs(x+1,a,b,c+l[x])+10
    return min(res0,res1,res2,res3)
    
print(dfs(0,0,0,0))

Solution (Python):
n, A, B, C = map(int, input().split())
l = [int(input()) for _ in range(n)]


def recur(i, a, b, c):
    if n == i:
        if a < 1 or b < 1 or c < 1:
            return 10 ** 18
        return abs(A-a) + abs(B-b) + abs(C-c)
    return min([
        recur(i + 1, a, b, c),
        recur(i + 1, a + l[i], b, c) + (10 if a > 0 else 0),
        recur(i + 1, a, b + l[i], c) + (10 if b > 0 else 0),
        recur(i + 1, a, b, c + l[i]) + (10 if c > 0 else 0)
    ])


print(recur(0, 0, 0, 0))

Solution (Python):
n, A, B, C = map(int, input().split())
l = [int(input()) for _ in range(n)]


def recur(i, a, b, c):
    if n == i:
        if a < 1 or b < 1 or c < 1:
            return 10 ** 18
        return abs(A-a) + abs(B-b) + abs(C-c)
    return min([
        recur(i + 1, a, b, c),
        recur(i + 1, a + l[i], b, c) + (10 if a > 0 else 0),
        recur(i + 1, a, b + l[i], c) + (10 if b > 0 else 0),
        recur(i + 1, a, b, c + l[i]) + (10 if c > 0 else 0)
    ])


print(recur(0, 0, 0, 0))

Solution (Python):
N,A,B,C = map(int,input().split())
l = [int(input()) for _ in range(N)]
INF = 10**10

def dfs(cur, a, b, c):
    if cur == N:
        return abs(a-A) + abs(b-B) + abs(c-C) -30 if min(a,b,c) > 0 else INF
    ret0 = dfs(cur + 1, a, b, c)
    ret1 = dfs(cur + 1, a + l[cur], b, c) + 10
    ret2 = dfs(cur + 1, a, b + l[cur], c) + 10
    ret3 = dfs(cur + 1, a, b, c + l[cur]) + 10
    return min(ret0, ret1, ret2, ret3)

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

Solution (Python):
N,A,B,C=map(int,input().split())
L=[0]*N
for i in range(N):
  L[i]=int(input())




stack=[]

stack.append([0,0,0,0,0])
ans=1000*10
while stack:
  a,b,c,num,mp=stack.pop()
  if num==N:
    if a==0 or b==0 or c==0:
      continue
    mp+=abs(A-a)+abs(B-b)+abs(C-c)
    if mp<ans:
      ans=mp
    continue
  stack.append([a+L[num],b,c,num+1,mp+(a!=0)*10])
  stack.append([a,b+L[num],c,num+1,mp+(b!=0)*10])
  stack.append([a,b,c+L[num],num+1,mp+(c!=0)*10])
  stack.append([a,b,c,num+1,mp])
print(ans)

Solution (Python):
N,A,B,C=map(int,input().split())
L=[0]*N
for i in range(N):
  L[i]=int(input())




stack=[]

stack.append([0,0,0,0,0])
ans=1000*10
while stack:
  a,b,c,num,mp=stack.pop()
  if num==N:
    if a==0 or b==0 or c==0:
      continue
    mp+=abs(A-a)+abs(B-b)+abs(C-c)
    if mp<ans:
      ans=mp
    continue
  for i in range(4):
    if i==0:
      stack.append([a+L[num],b,c,num+1,mp+(a!=0)*10])
    if i==1:
      stack.append([a,b+L[num],c,num+1,mp+(b!=0)*10])
    if i==2:
      stack.append([a,b,c+L[num],num+1,mp+(c!=0)*10])
    if i==3:
      stack.append([a,b,c,num+1,mp])
print(ans)

Solution (Python):
n,a,b,c=map(int,input().split())
L=[int(input()) for _ in range(n)]

ans=float('inf')
for i in range(4**n):
  G=[[] for _ in range(4)]
  for j in range(n):
    G[(i//(4**j))%4].append(L[j])
  if len(G[0])==0 or len(G[1])==0 or len(G[2])==0: continue
  tmp=0
  for j in range(3):
    tmp+=max(0,len(G[j])-1)*10
    if j==0: tmp+=abs(a-sum(G[j]))
    if j==1: tmp+=abs(b-sum(G[j]))
    if j==2: tmp+=abs(c-sum(G[j]))
  ans=min(ans,tmp)
print(ans)

Solution (Python):
import itertools

N,A,B,C=map(int,input().split())
l=[int(input()) for _ in range(N)]

d=list(itertools.product([0,1,2,3], repeat=N))

MIN=10**10
for i in range(len(d)):
    ans=0
    a_num=0
    b_num=0
    c_num=0
    a_kadomatu=A
    b_kadomatu=B
    c_kadomatu=C
    if {0,1,2} <= set(d[i]):
        for g in range(len(d[i])):
            if d[i][g]==0:
                a_num+=1
                a_kadomatu-=l[g]
            elif d[i][g]==1:
                b_num+=1
                b_kadomatu-=l[g]
            elif d[i][g]==2:
                c_num+=1
                c_kadomatu-=l[g]
        ans=abs(a_kadomatu)+abs(b_kadomatu)+abs(c_kadomatu)+10*(a_num-1)+10*(b_num-1)+10*(c_num-1)
        MIN=min(MIN,ans)
print(MIN)

Solution (Python):
N, A, B, C = list(map(int, input().split()))
l = [int(input()) for x in range(N)]
INF = 10 ** 9

def dfs(cur, a, b, c):
    if cur == N:
        return abs(a - A) + abs(b - B) + abs(c - C) - 30 if min(a, b, c) > 0 else INF
    ret0 = dfs(cur+1, a, b, c)
    ret1 = dfs(cur+1, a + l[cur], b, c) + 10
    ret2 = dfs(cur+1, a, b + l[cur], c) + 10
    ret3 = dfs(cur+1, a, b, c + l[cur]) + 10
    return min(ret0, ret1, ret2, ret3)

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

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


def dfs(cur, a, b, c):
    if cur == N:
        return abs(A - a) + abs(B - b) + abs(C - c) - 30 if min(a, b, c) > 0 else 10 ** 9
    no_add = dfs(cur + 1, a, b, c)
    add_a = dfs(cur + 1, a + l[cur], b, c) + 10
    add_b = dfs(cur + 1, a, b + l[cur], c) + 10
    add_c = dfs(cur + 1, a, b, c + l[cur]) + 10

    return min(no_add, add_a, add_b, add_c)


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

Solution (Python):
n, A, B, C = map(int, input().split())
l = [int(input()) for _ in range(n)]


def recur(i, a, b, c):
    if n == i:
        if a < 1 or b < 1 or c < 1:
            return 10 ** 18
        return abs(A-a) + abs(B-b) + abs(C-c)
    return min([
        recur(i + 1, a, b, c),
        recur(i + 1, a + l[i], b, c) + (10 if a > 0 else 0),
        recur(i + 1, a, b + l[i], c) + (10 if b > 0 else 0),
        recur(i + 1, a, b, c + l[i]) + (10 if c > 0 else 0)
    ])


print(recur(0, 0, 0, 0))

Solution (Python):
n, A, B, C = map(int, input().split())
l = [int(input()) for _ in range(n)]


def recur(num, i, a, b, c):
    if num == i:
        if a < 1 or b < 1 or c < 1:
            return 10 ** 18
        return abs(A-a) + abs(B-b) + abs(C-c)
    return min([
        recur(num, i + 1, a, b, c),
        recur(num, i + 1, a + l[i], b, c) + (10 if a > 0 else 0),
        recur(num, i + 1, a, b + l[i], c) + (10 if b > 0 else 0),
        recur(num, i + 1, a, b, c + l[i]) + (10 if c > 0 else 0)
    ])


print(recur(n, 0, 0, 0, 0))

Solution (Python):
n,a,b,c=map(int,input().split())
L=[int(input()) for _ in range(n)]

ans=float('inf')
for i in range(4**n):
  G=[[] for _ in range(4)]
  for j in range(n):
    G[(i//(4**j))%4].append(L[j])
  if len(G[0])==0 or len(G[1])==0 or len(G[2])==0: continue 
  tmp=0
  for i,g in enumerate(G):
    if i==3: continue
    wood=sum(g) 
    tmp+=(len(g)-1)*10
    if i==0: tmp+=abs(wood-a)
    if i==1: tmp+=abs(wood-b)
    if i==2: tmp+=abs(wood-c)
  ans=min(ans,tmp)
print(ans)

Solution (Python):
import itertools

N, A, B, C = map(int, input().split())
L = [int(input()) for i in range(N)]

def min_cost(target, l_list):
  return 10*(len(l_list) - 1) + abs(target - sum(l_list))


ans = 1000 * 3
combs = list(itertools.product([0, 1, 2, 3], repeat=N))
for comb in combs:
  labcn = [[], [], [], []]
  for i, idx in enumerate(comb):
    labcn[idx].append(L[i])
  
  la, lb, lc, ln = labcn
  if len(la)*len(lb)*len(lc):
    ans = min(ans, min_cost(A, la)+min_cost(B, lb)+min_cost(C, lc))

print(ans)

Solution (Python):
import itertools

N, A, B, C = map(int, input().split())
L = [int(input()) for i in range(N)]

def min_cost(target, l_list):
  if target in l_list:
    return 0
  else:
    res = 1000
    for l in l_list:
      res = min(res, abs(target - l))
    
    for i, l in enumerate(l_list):
      res = min(res, min_cost(target-l, l_list[:i]+l_list[i+1:]) + 10)
  
    return res

ans = 1000 * 3
combs = list(itertools.product([0, 1, 2], repeat=N))
for comb in combs:
  labc = [[], [], []]
  for i, idx in enumerate(comb):
    labc[idx].append(L[i])
  
  la, lb, lc = labc
  if len(la)*len(lb)*len(lc):
    ans = min(ans, min_cost(A, la)+min_cost(B, lb)+min_cost(C, lc))

print(ans)

Solution (Python):
import sys
import math
import fractions
import bisect
import queue
import heapq
from collections import deque
sys.setrecursionlimit(4100000)

MOD = int(1e9+7)
PI = 3.14159265358979323846264338327950288
INF = 1e18

'''
1行のint
N, K = map(int, input().split())

1行のstring
S, T = input().split()

1行の整数配列
P = list(map(int,input().split()))

改行あり整数
x = []
y = []
for i in range(N):
    x1,y1=[int(i) for i in input().split()]
    x.append(x1)
    y.append(y1)

N行M列の初期化
dp = [[INF] * M for i in range(N)]

'''

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



ans = INF
for use in range(4**N):  
    a = 0
    b = 0
    c = 0
    d = 0
    local = 0
    unuse = 0
    for i in range(N):
        where = use%(4**(i+1))//(4**i)
        if where == 1:
            a+=L[i]
        elif where == 2:
            b+= L[i]
        elif where == 3:
            c+=L[i]
        else :
            d+=L[i]
            unuse+=1

    if a*b*c == 0:
        continue

    local = abs(A-a) + abs(B-b) + abs(C-c) + (N-unuse-3)*10
    ans = min(ans, local)


print(ans)

Solution (Python):
import itertools 
def diff(a,b,c):
    return abs(A-a)+abs(B-b)+abs(C-c)
    
N,A,B,C,*l=map(int,open(0).read().split())
_min=10**6
for i in itertools.product([0,1,2,3],repeat=N):
    cnt=0
    abc=[0,0,0]
    for j in range(N):
        if i[j] !=3:
            abc[i[j]]+=l[j]
            cnt+=1
    if 0 in abc:
        continue
    _min=min(_min,10*cnt-30+diff(abc[0],abc[1],abc[2]))
print(_min)

Solution (Python):
N,A,B,C=map(int, input().split())
L=[int(input()) for _ in range(N)]

ans=10**9

for i in range(4**8):
    mp=0
    a=b=c=0
    ac=bc=cc=0
    for j in range(N):
        f=i&3
        i>>=2
        if f==1:
            a+=L[j]
            ac+=1
        elif f==2:
            b+=L[j]
            bc+=1
        elif f==3:
            c+=L[j]
            cc+=1
    if ac==0 or bc==0 or cc==0: continue
    mp+=10*(ac-1+bc-1+cc-1)
    mp+=abs(A-a)+abs(B-b)+abs(C-c)
    ans=min(ans,mp)

print(ans)

Solution (Python):
from itertools import product

def dif(abc):
    return abs(A-abc[0]) + abs(B-abc[1]) + abs(C-abc[2])

N,A,B,C,*l=map(int,open(0).read().split())

_min=10**6
for i in product([0,1,2,3],repeat=N): 
    abc=[0,0,0] 
    cnt=0 
    for ii,ll in zip(i,l): 
        if ii != 3:
            abc[ii]+=ll 
            cnt+=1 
    if 0 in abc:
        continue 
    _min=min(_min,cnt*10 - 30 + dif(abc))        
print(_min)

Solution (Python):
from itertools import product
def main():
    N, A, B, C = map(int, input().split())
    L = [int(input()) for _ in range(N)]
    m = 10**6
    def diff(a, b, c):
        return abs(A - a) + abs(B - b) + abs(C - c)

    for I in product((0, 1, 2, 3), repeat=N):
        abc = [0, 0, 0]
        t = 0
        for i, l in zip(I, L):
            if i != 3:
                abc[i] += l
                t += 1
        if 0 in abc:
            continue
        m = min(m, (t - 3) * 10 + diff(*abc))

    print(m)
main()

Solution (Python):
n, A, B, C = map(int, input().split())
l = [int(input()) for _ in range(n)]


def recur(i, a, b, c):
    if i == n:
        if a < 1 or b < 1 or c < 1:
            return 10 ** 18
        return abs(a - A) + abs(b - B) + abs(c - C)
    return min([
        recur(i + 1, a, b, c),
        recur(i + 1, a + l[i], b, c) + (10 if a > 0 else 0),
        recur(i + 1, a, b + l[i], c) + (10 if b > 0 else 0),
        recur(i + 1, a, b, c + l[i]) + (10 if c > 0 else 0)
    ])

print(recur(0, 0, 0, 0))

Solution (Python):
n, A, B, C = map(int, input().split())
l = [int(input()) for _ in range(n)]


def recur(i, a, b, c):
    if i == n:
        if a < 1 or b < 1 or c < 1:
            return 10 ** 18
        return abs(a - A) + abs(b - B) + abs(c - C)

    return min([
        recur(i + 1, a, b, c),
        recur(i + 1, a + l[i], b, c) + (10 if a > 0 else 0),
        recur(i + 1, a, b + l[i], c) + (10 if b > 0 else 0),
        recur(i + 1, a, b, c + l[i]) + (10 if c > 0 else 0)
    ])


print(recur(0, 0, 0, 0))

Solution (Python):

ans = 100000000000000000000000000
N, A, B, C = map(int,input().split())
l = [int(input()) for _ in range(N)]


def rec(a,b,c,gousei,now):
    global ans

    if(a != 0 and b != 0 and c != 0):
        ans = min(ans,(abs(a-A)+abs(b-B)+abs(c-C)+gousei*10))

        

    if now == N:
        return 0


    
    rec(a,b,c, gousei,now+1)
    rec(a+l[now],b,c,gousei+1 if a!= 0 else gousei,now+1)
    rec(a,b+l[now],c,gousei+1 if b!= 0 else gousei,now+1)
    rec(a,b,c+l[now],gousei+1 if c!= 0 else gousei,now+1)


    
rec(0,0,0,0,0)
print(ans)

Solution (Python):
n,a,b,c = map(int,input().split())
abc = [a,b,c]
l = [int(input()) for i in range(n)]
ans = 10**10

def bfs(use):
    global l,abc,ans
    if len(use)==n:
        mp = 0
        x = [0]*4
        c = [0]*4
        for i in range(n):
            x[use[i]]+=l[i]
            c[use[i]]+=1
        for i in range(3):
            if x[i]==0: 
                mp=10**10
                break
            else :mp += (c[i]-1)*10+abs(abc[i]-x[i])
        if ans>mp: ans=mp
    else:
        bfs(use+[0])
        bfs(use+[1])
        bfs(use+[2])
        bfs(use+[3])

bfs([])
print(ans)

Solution (Python):
N,A,B,C = [int(x) for x in input().split()]
l = []
for _ in range(N):
    l.append(int(input()))

INF = 10**9

def dfs(cur,a,b,c):
    if cur == N:
        return abs(a-A) + abs(b-B) + abs(c-C) - 30 if min(a,b,c) > 0 else INF
    ret0 = dfs(cur+1, a,b,c)
    ret1 = dfs(cur+1, a+l[cur],b,c) +10
    ret2 = dfs(cur+1, a, b+l[cur],c)+10
    ret3 = dfs(cur+1, a, b,c+l[cur])+10
    return min(ret0,ret1,ret2,ret3)

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

Solution (Python):
N,A,B,C=map(int, input().split())
l=[int(input()) for i in range(N)]
from itertools import product
p=list(product([0,1,2,3],repeat=N))
t=10000
for i in p:
    a=b=c=ac=0
    for j in range(N):
        if i[j]==1:
            a+=l[j]
            ac+=1
        elif i[j]==2:
            b+=l[j]
            ac+=1
        elif i[j]==3:
            c+=l[j]
            ac+=1
    if a and b and c:
        tt=abs(a-A)+abs(b-B)+abs(c-C)+(ac-3)*10
        if t>tt:
            t=tt
print(t)

Solution (Python):
from bisect import *
from collections import *
from fractions import gcd
from math import factorial
from itertools import *
N,A,B,C=map(int,input().split())
L=[int(input()) for i in range(N)]
value=10**10
itelst=[list(permutations(L,i)) for i in range(3,N+1)]
for n in range(N-2):
    for lst in itelst[n]:
        lstsum=sum(lst)
        a=0
        for i1 in range(len(lst)-2):
            a+=lst[i1]
            b=0
            for i2 in range(i1+1,len(lst)-1):
                b+=lst[i2]
                c=lstsum-a-b
                c1,b1,a1=sorted([a,b,c])
                a1=A-a1 if A-a1>0 else a1-A
                b1=B-b1 if B-b1>0 else b1-B
                c1=C-c1 if C-c1>0 else c1-C
                value=min(value, (len(lst)-3)*10 + a1+b1+c1)





print(value)

Solution (Python):
N, A, B, C = map(int, input().split())
l = [int(input()) for _ in range(N)]
INF = float('inf')


def dfs(cue, a, b, c):
    if cue == N:
        if min(a, b, c) > 0:  
            return abs(a - A) + abs(b - B) + abs(c - C) - 30
        else:  
            return INF
    ret0 = dfs(cue + 1, a, b, c)  
    ret1 = dfs(cue + 1, a + l[cue], b, c) + 10  
    ret2 = dfs(cue + 1, a, b + l[cue], c) + 10  
    ret3 = dfs(cue + 1, a, b, c + l[cue]) + 10  
    return min(ret0, ret1, ret2, ret3)


ans = dfs(0, 0, 0, 0)
print(ans)

Solution (Python):
n,a,b,c = map(int, input().split())
l=list(int(input()) for i in range(n))
v=[a,b,c]
v.sort()
ans=10**20
for i in range(4**n):
    x=[0,0,0]
    cnt=0
    for j in range(n):
        p=i%4
        i=i//4
        if p==0:
            if x[0]!=0:
                cnt+=10
            x[0]+=l[j]
        elif p==1:
            if x[1]!=0:
                cnt+=10
            x[1]+=l[j]
        elif p==2:
            if x[2]!=0:
                cnt+=10
            x[2]+=l[j]
        else:
            continue

    x.sort()
    if x[0]==0:
        continue
    else:
        cnt+=abs(x[0]-v[0])+abs(x[1]-v[1])+abs(x[2]-v[2])
        ans=min(cnt,ans)
print(ans)

Solution (Python):
import itertools
n,*abc=map(int,input().split())
l=[int(input()) for i in range(n)]
inf=100000000000000
mi=inf
for i in list(itertools.product([i for i in range(3)],repeat=n)):
    sub=[[] for j in range(3)]
    for j in range(n):
        sub[i[j]].append(j)
    mi_sub=0
    for j in range(3):
        l_sub=len(sub[j])
        if l_sub==0:
            mi_sub=inf
            break
        mi_subsub=inf
        for k in range(2**l_sub):
            mi_subsubsub=[0,0]
            for l_subsub in range(l_sub):
                if ((k>>l_subsub) &1):
                    mi_subsubsub[0]+=1
                    mi_subsubsub[1]+=l[sub[j][l_subsub]]
            if mi_subsubsub[0]!=0:
                mi_subsub=min(mi_subsub,abs(mi_subsubsub[1]-abc[j])+mi_subsubsub[0]*10-10)
        mi_sub+=mi_subsub
    mi=min(mi,mi_sub)
print(mi)

Solution (Python):
def main():
    N, A, B, C = map(int, input().split())
    l = []
    ll = [A, B, C]
    for _ in range(N):
        l.append(int(input()))
    r = 3000
    for i in range(4 ** N):
        t = [[],[],[]]
        for k in reversed(range(N)):
            if i // (4 ** k) == 0:
                t[0].append(l[k])
            elif i // (4 ** k) == 1:
                t[1].append(l[k])
            elif i // (4 ** k) == 2:
                t[2].append(l[k])
            i %= (4 ** k)
        if len(t[0]) == 0 or len(t[1]) == 0 or len(t[2]) == 0:
            continue
        r = min(r, sum(abs(sum(t[k]) - ll[k]) + 10 * (len(t[k]) - 1)for k in range(3)))
    print(r)
main()

Solution (Python):
from sys import stdout
printn = lambda x: stdout.write(str(x))
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True  and False
BIG = 999999999
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

n,a,b,c = inm()
l = []
for i in range(n):
    l.append(inn())
mn = BIG
for i in range(4**n):
    la = []
    lb = []
    lc = []
    x = i
    for j in range(n):
        y = x%4
        if y==1:
            la.append(l[j])
        if y==2:
            lb.append(l[j])
        if y==3:
            lc.append(l[j])
        x //= 4
    if len(la)==0 or len(lb)==0 or len(lc)==0:
        continue
    cost = len(la)*10-10 + abs(sum(la)-a) + \
           len(lb)*10-10 + abs(sum(lb)-b) + \
           len(lc)*10-10 + abs(sum(lc)-c)
    mn = min(mn, cost)
    if DBG and cost==110:
        print("la {} lb {} lc {}".format(la,lb,lc))
print(mn)