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):
from itertools import product
N, A, B, C = map(int, input().split())
l = [int(input()) for i in range(N)]
INF = 10 ** 9
t1 = set([0,1,2,3])
t2= set([1,2,3])
def calc(t):
    t3 = set(t)
    if not (t3 == t1 or t3 == t2): return INF
    a, b, c = 0, 0, 0
    cnt = 0
    for i, v in enumerate(t):
        if v != 0: cnt += 1
        if v == 1: a += l[i]
        elif v == 2: b += l[i]
        elif v == 3: c += l[i]
    return abs(a-A) + abs(b-B) + abs(c-C) + (cnt-3) * 10

ans = 10 ** 9


for p in product(range(4), repeat=N):
    ans = min(ans, calc(p))

print(ans)

Solution (Python):
from itertools import product
N, A, B, C = map(int, input().split())
l = [int(input()) for i in range(N)]
INF = 10 ** 9
t1 = set([0,1,2,3])
t2= set([1,2,3])
def calc(t):
    t3 = set(t)
    if not (t3 == t1 or t3 == t2): return INF
    a, b, c = 0, 0, 0
    cnt = 0
    for i, v in enumerate(t):
        if v != 0: cnt += 1
        if v == 1: a += l[i]
        elif v == 2: b += l[i]
        elif v == 3: c += l[i]
    return abs(a-A) + abs(b-B) + abs(c-C) + max((cnt-3) * 10, 0)


ans = 10 ** 9
for p in product(range(4), repeat=N):
    ans = min(ans, calc(p))

print(ans)

Solution (Python):
from itertools import product
N, A, B, C = map(int, input().split())
l = [int(input()) for i in range(N)]
INF = 10 ** 9
t1 = set([0,1,2,3])
t2= set([1,2,3])
def calc(t):
    t3 = set(t)
    if not (t3 == t1 or t3 == t2): return INF
    a, b, c = 0, 0, 0
    cnt = 0
    for i, v in enumerate(t):
        if v != 0: cnt += 1
        if v == 1: a += l[i]
        elif v == 2: b += l[i]
        elif v == 3: c += l[i]
    return abs(a-A) + abs(b-B) + abs(c-C) + max((cnt-3) * 10, 0)


ans = 10 ** 9
for p in product(range(2), repeat=N*2):
    t = [0] * N
    for i in range(N):
        for j in range(2):
            t[i] += p[i*2+j] * (2 ** j)
    
    ans = min(ans, calc(t))

print(ans)

Solution (Python):

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


from itertools import permutations,product
inf=float("inf")
min_mp = inf
for k in product(range(4),repeat=n):
    if 2 not in k or 1 not in k or 0 not in k:
        continue
    len_abc = [0,0,0]
    mp = - 30
    for i,v in enumerate(k):
        if v == 3:
            pass
        else:
            len_abc[v]+=l[i]
            mp += 10
    mp += (abs(a-len_abc[0])+abs(b-len_abc[1])+abs(c-len_abc[2]))
    min_mp = min(min_mp,mp)
print(min_mp)

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

s_list = [a, b, c]

ans = 10000
for i in range(4 ** n):
    mp = 0
    temp_l_list = [0, 0, 0]
    
    for j in range(n):
        temp = (i >> (2 * j)) & 3
        if temp == 3:
            continue

        if temp_l_list[temp] != 0:
            mp += 10
        temp_l_list[temp] += l_list[j]

    for s, temp_l in zip(s_list, temp_l_list):
        if temp_l == 0:
            break
        mp += abs(s - temp_l)
    else:
        if ans > mp:
            ans = mp
print(ans)

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

from itertools import product
ptns = list(product([0,1,2,-1], repeat = n))

ans = 10**9
for i in range(len(ptns)):
    if 0 not in ptns[i] or 1 not in ptns[i] or 2 not in ptns[i]:
        continue
    abc = [0, 0, 0, -30]
    for j in range(n):
        tmp = ptns[i][j]
        if tmp >= 0:
            abc[tmp] += l[j]
            abc[3] += 10
    mp = abc[3]
    mp += abs(abc[0] - a)
    mp += abs(abc[1] - b)
    mp += abs(abc[2] - c)
    ans = min(ans, mp)

print(ans)

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

from itertools import product
ptns = list(product([0,1,2,-1], repeat = n))

anss = []
for i in range(len(ptns)):
    if 0 not in ptns[i] or 1 not in ptns[i] or 2 not in ptns[i]:
        continue
    abc = [0, 0, 0, -30]
    ans = 10**9
    for j in range(n):
        tmp = ptns[i][j]
        if tmp >= 0:
            abc[tmp] += l[j]
            abc[3] += 10

    anss.append(abc[3] + abs(abc[0] - a) + abs(abc[1] - b) + abs(abc[2] - c))

print(min(anss))

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

from itertools import product
ptns = list(product([0,1,2,-1], repeat = n))

ans = 10**9
for i in range(len(ptns)):
    if 0 not in ptns[i] or 1 not in ptns[i] or 2 not in ptns[i]:
        continue
    abc = [0, 0, 0, -30]
    for j in range(n):
        tmp = ptns[i][j]
        if tmp >= 0:
            abc[tmp] += l[j]
            abc[3] += 10

    ans = min(ans, abc[3] + abs(abc[0] - a) + abs(abc[1] - b) + abs(abc[2] - c))

print(ans)

Solution (Python):

def minMP(length,LIST):
    k=len(LIST)
    LONG=[]
    if k==0:
        return -1
    for i in range(1,2**k):
        long=0
        cnt=-1
        for j in range(k):
            mod=i%2
            if mod==1:
                long+=LIST[j]
                cnt+=1
            i//=2
        LONG.append([long,10*cnt])
    
    ans=float("inf")
    for ll,mp in LONG:
        ans=min(ans,mp+abs(ll-length))
    return ans 

N,a,b,c=map(int,input().split())
L=[]
Ans=float("inf")
for i in range(N):
    l=int(input())
    L.append(l)

for n in range(3**N):
    L_A=[];L_B=[];L_C=[]
    for i in range(N):
        mod=n%3
        n//=3
        if mod==0:
            L_A.append(L[i])
        elif mod==1:
            L_B.append(L[i])
        else:
            L_C.append(L[i])
    MP_A=minMP(a,L_A)
    MP_B=minMP(b,L_B)
    MP_C=minMP(c,L_C)
    
    if MP_A==-1 or MP_B==-1 or MP_C==-1:
        continue
    else:
        Ans=min(Ans,MP_A+MP_B+MP_C)

print(Ans)

Solution (Python):
def abc119_c():
  n, a, b, c = map(int, input().split())
  L = [int(input()) for _ in range(n)]
  ans = 10**9

  def dfs(arr):
    if len(arr) == n:
      
      if arr.count(1) > 0 and arr.count(2) > 0 and arr.count(3) > 0:
        calc_score(arr)
      return
    for i in range(4):
      dfs(arr + [i])

  def calc_score(arr):
    nonlocal ans
    
    tools_a = [L[i] for i in range(n) if arr[i] == 1]
    tools_b = [L[i] for i in range(n) if arr[i] == 2]
    tools_c = [L[i] for i in range(n) if arr[i] == 3]
    
    score = magic(tools_a, a)
    score += magic(tools_b, b)
    score += magic(tools_c, c)
    ans = min(score, ans)

  def magic(tools, target):
    
    pt = abs(target - sum(tools))
    
    if len(tools) >= 2:
      pt += 10 * (len(tools) - 1)
    return pt

  dfs([])
  print(ans)

abc119_c()

Solution (Python):
from itertools import permutations
from copy import deepcopy
from collections import Counter

n,a,b,c = map(int,input().split())
take = []
for _ in range(n):
	take.append(int(input()))
tmp = deepcopy(take)
ans_list = []

sum_abc = 10 ** 5
for abc in permutations([a,b,c],3):
	take = deepcopy(tmp)
	ans = 0
	l = []
	l2 = []
	s = Counter(take)
	
	for x in abc:
		res = []
		l2 = []
		take = []
		for i,num in s.items():
			for j in range(num):
				take.append(i)
		
		for i in range(2**n):
			l = []
			for j in range(len(take)):
					if i & (1<<j):
						l.append(take[j])
			if len(l) == 0:
				continue
			res.append(10*(len(l)-1) + abs(sum(l) - x))
			l2.append(l)
		minimum = sum_abc
		for i in range(len(res)):
			if minimum > res[i]:
				minimum = res[i]
				s2 = l2[i]
		for i in range(len(s2)):
			s[s2[i]] -= 1
			if s[s2[i]] == 0:
				s.pop(s2[i])
		
		ans += minimum
		
	ans_list.append(ans)

print(min(ans_list))

Solution (Python):
n,a,b,c = map(int,input().split())
L = []
for i in range(n):
    L.append(int(input()))
import itertools
ans = 10**9
tmp = 0
for use in itertools.product([0,1,2,-1] ,repeat=n):
    tmp = 0
    ls = [0,0,0]
    for idx,u in enumerate(use):
        if u>-1:
            if ls[u] >0:
                tmp+=10
            ls[u]+=L[idx]

    if ls[0] ==0 or ls[1] ==0 or ls[2] ==0:continue
    tmp = tmp + abs(ls[0] -a) +abs(ls[1] -b)+abs(ls[2] -c)
    ans = min(tmp,ans)
print(ans)

Solution (Python):
n,a,b,c = map(int,input().split())
L = []
for i in range(n):
    L.append(int(input()))
import itertools
ans = 10**9
tmp = 0
for perm in itertools.permutations(range(n),3):
    ua,ub,uc = perm
    la,lb,lc = L[ua],L[ub],L[uc]
    for use in itertools.product([0,1,2,-1] ,repeat=n-3):
        idx = 0
        tmp = 0
        ls = [la,lb,lc]
        for i,l in enumerate(L):
            if i== ua or  i== ub or i== uc:
                continue
            else:
                if use[idx]>-1:
                    ls[use[idx]] +=l
                    tmp+=10
            idx+=1
        tmp = tmp + abs(ls[0] -a) +abs(ls[1] -b)+abs(ls[2] -c)
        ans=min(ans,tmp)
print(ans)

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

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**12
  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 = []
for i in range(n):
    l.append(int(input()))
from itertools import product
bamboo = [0, a, b, c]
min_num = float('inf')
for i in product([0, 1, 2, 3], repeat=n):
    if set(i) >= set([1, 2, 3]):
        sums = [0, 0, 0, 0]
        tmp = -30
        for j in range(n):
            if i[j] != 0:
                sums[i[j]] += l[j]
                tmp += 10
        min_num = min(min_num,tmp+sum(abs(sums[k] - bamboo[k]) for k in range(1, 4)))
print(min_num)

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 main():
    N, A, B, C = (int(_) for _ in input().split())
    L = [int(input()) for _ in range(N)]
    INF = 10 ** 12

    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 INF
        c0 = dfs(i+1, a, b, c)
        c1 = dfs(i+1, a+L[i], b, c) + 10
        c2 = dfs(i+1, a, b+L[i], c) + 10
        c3 = dfs(i+1, a, b, c+L[i]) + 10
        return min(c0, c1, c2, c3)

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

if __name__ == '__main__':
    main()

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

def dfs(i, cnt, a, b, c):
    if i == N:
        if a == 0 or b == 0 or c == 0:
            return INF
        return abs(a - A) + abs(b - B) + abs(c - C) + 10 * cnt
    MP = INF
    if a == 0:
        MP = min(MP, dfs(i+1, cnt, a+L[i], b, c))
    else:
        MP = min(MP, dfs(i+1, cnt+1, a+L[i], b, c))
    if b == 0:
        MP = min(MP, dfs(i+1, cnt, a, b+L[i], c))
    else:
        MP = min(MP, dfs(i+1, cnt+1, a, b+L[i], c))
    if c == 0:
        MP = min(MP, dfs(i+1, cnt, a, b, c+L[i]))
    else:
        MP = min(MP, dfs(i+1, cnt+1, a, b, c+L[i]))
    MP = min(MP, dfs(i+1, cnt, a, b, c))
    
    return MP

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

Solution (Python):
N,A,B,C = map(int,input().split())
l = [int(input()) for i in range(N)]
INF = 100000000
def rec(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 INF)
    ret0 = rec(i+1,a,b,c)
    ret1 = rec(i+1,a+l[i],b,c)+10
    ret2 = rec(i+1,a,b+l[i],c)+10
    ret3 = rec(i+1,a,b,c+l[i])+10
    return min(ret0,ret1,ret2,ret3)
print(rec(0,0,0,0))

Solution (Python):
MOD = 10 ** 9 + 7
INF = 10 ** 11
import sys
sys.setrecursionlimit(100000000)

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

ans = INF
def dfs(i,before,score):
    global ans
    if i == N:
        if all(length > 0  for length in before):
            x,y,z = before
            for _ in range(3):
                ans = min(ans,abs(x - A) + abs(y - B) + abs(z - C) + score - 30)
                x,y,z = y,z,x
    else:
        dfs(i + 1,before,score)
        for j in range(3):
            before[j] += L[i]
            dfs(i + 1,before,score + 10)
            before[j] -= L[i]
dfs(0,[0,0,0],0)
print(ans)

Solution (Python):

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

inf=10**9

def dfs(cur,aa,bb,cc):
    if cur==n:
        if min(aa,bb,cc)>0:
            return abs(a-aa)+abs(b-bb)+abs(c-cc)-30
        else:
            return inf
    ret0=dfs(cur+1,aa,bb,cc)
    ret1=dfs(cur+1,aa+l[cur],bb,cc)+10
    ret2=dfs(cur+1,aa,bb+l[cur],cc)+10
    ret3=dfs(cur+1,aa,bb,cc+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())
    
import itertools
 
mpmin=a+b+c+1
for i in itertools.product([0,1,2,3], repeat=n):
    aa=0
    bb=0
    cc=0
    mpa=0
    mpb=0
    mpc=0
    ia=0
    ib=0
    ic=0
    for ii in range(n):
        if i[ii]==1:
            ia=1
            if aa>0:
                mpa+=10
            aa+=l[ii]
        elif i[ii]==2:
            ib=1
            if bb>0:
                mpb+=10
            bb+=l[ii]
        elif i[ii]==3:
            ic=1
            if cc>0:
                mpc+=10
            cc+=l[ii]
    if ia==1 and ib==1 and ic==1:
        mp=mpa+abs(a-aa)+mpb+abs(b-bb)+mpc+abs(c-cc)


        mpmin=min(mp,mpmin)

print(mpmin)

Solution (Python):

'''
100, 90, 80
98, 80, 40, 30, 21
L[0]〜L[N-1]がそれぞれ、A,B,C,使わないのいずれかかを考える。
4**8=65536
'''
import itertools
INF = 10**18
N, A, B, C = map(int, input().split())
L = [0] * N
for i in range(N):
    L[i] = int(input())
    
c = (0, 1, 2, 3) 
ans = INF

for comb in itertools.product(c,repeat=N):
    tmp = [0]*4 
    ansArr = [0]*3
    
    for i, j in enumerate(comb):
        if tmp[j]==0:
            tmp[j] += L[i]
        elif j!=3:
            tmp[j] += L[i]
            ansArr[j] += 10
    if tmp[0]==0 or tmp[1]==0 or tmp[2]==0:
        continue
    ansArr[0] += abs(A-tmp[0])
    ansArr[1] += abs(B-tmp[1])        
    ansArr[2] += abs(C-tmp[2])
    
    
    ans = min(ans, sum(ansArr))
    

print(ans)

Solution (Python):
import sys
readline = sys.stdin.readline

def base_10_to_n(i, n):
    if i//n:
        return base_10_to_n(i//n, n) + str(i % n)
    return str(i % n)

def main():
    N, A, B, C = map(int, readline().rstrip().split())
    l = [int(readline()) for _ in range(N)]
    ans = 10**6
    for i in range(4**N):
        b = base_10_to_n(i, 4).zfill(N)
        if '1' not in b or '2' not in b or '3' not in b:
            continue
        ai = [l[j] for j, e in enumerate(b) if e == '1']
        bi = [l[j] for j, e in enumerate(b) if e == '2']
        ci = [l[j] for j, e in enumerate(b) if e == '3']
        cost = (len(ai)+len(bi)+len(ci)-3) * 10 + abs(A-sum(ai)) + abs(B-sum(bi)) + abs(C-sum(ci))
        ans = min(ans, cost)

    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
import math











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


def dfs(deep, crr):
    if deep == N:
        ok, res = calculate(crr)
        if ok:
            result.append(int(res))
    else:
        for i in range(4):
            crr[deep] = i
            dfs(deep + 1, crr)


def calculate(crr):
    sA = []
    sB = []
    sC = []
    sN = []
    for index, cr in enumerate(crr):
        if cr == 0:
            sN.append(ARR[index])
        if cr == 1:
            sA.append(ARR[index])
        if cr == 2:
            sB.append(ARR[index])
        if cr == 3:
            sC.append(ARR[index])

    if len(sA) == 0:
        return False, -1
    if len(sB) == 0:
        return False, -1
    if len(sC) == 0:
        return False, -1


    s1 = (len(sA) - 1) * 10 + math.fabs(sum(sA) - A)
    s2 = (len(sB) - 1) * 10 + math.fabs(sum(sB) - B)
    s3 = (len(sC) - 1) * 10 + math.fabs(sum(sC) - C)

    return True, s1 + s2 + s3


dfs(0, [0 for i in range(N)])

print(min(result))

Solution (Python):

from collections import Counter
def inputlist(): return [int(k) for k in input().split()]
def base10to(n, b):
    if (int(n/b)):
        return base10to(int(n/b), b) + str(n%b)
    return str(n%b)

ans = 10**18
N,A,B,C = inputlist()
l = [0]*N
for i in range(N):
    l[i] = int(input())

n = 4**N

def count(li,n):
    co = 0
    co += (len(li)-1)*10
    co += abs(sum(li) - n)
    return co

for i in range(n+1):
    k = base10to(i,4)
    if len(k) < N:
        d = N-len(k)
        for j in range(d):
            k = '0'+k
    c = Counter(list(k))
    if c['1'] < 1 or c['2'] < 1 or c['3'] < 1:
        continue
    ans_k = 0
    la = []
    lb = []
    lc = []
    for j in range(N):
        if k[j] == '1':
            la.append(l[j])
        if k[j] == '2':
            lb.append(l[j])
        if k[j] == '3':
            lc.append(l[j])
    ans_k += count(la,A) + count(lb,B) + count(lc,C)
    ans = min(ans,ans_k)
print(ans)

Solution (Python):

from collections import Counter
def inputlist(): return [int(k) for k in input().split()]
def base10to(n, b):
    if (int(n/b)):
        return base10to(int(n/b), b) + str(n%b)
    return str(n%b)

ans = 10**18
N,A,B,C = inputlist()
l = [0]*N
for i in range(N):
    l[i] = int(input())

n = 4**N

def count(li,n):
    co = 0
    co += (len(li)-1)*10
    co += abs(sum(li) - n)
    return co

for i in range(n+1):
    k = base10to(i,4)
    if len(k) < N:
        d = N-len(k)
        for j in range(d):
            k = '0'+k
    c = Counter(list(k))
    if c['1'] < 1 or c['2'] < 1 or c['3'] < 1:
        continue
    ans_k = 0
    la = []
    lb = []
    lc = []
    for j in range(N):
        if k[j] == '1':
            la.append(l[j])
        if k[j] == '2':
            lb.append(l[j])
        if k[j] == '3':
            lc.append(l[j])
    ans_k += count(la,A) + count(lb,B) + count(lc,C)
    ans = min(ans,ans_k)
print(ans)

Solution (Python):
def base_10_to_n(X, n):
    if (int(X/n)):
        return base_10_to_n(int(X/n), n)+str(X%n)
    return str(X%n)

def f(a,la,A):
    if la == 0:
        return 1e12
    return (la-1)*10 + abs(a-A)

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

ans = 1e12
for i in range(4**N+1):
    tmp = list(base_10_to_n(i,4).zfill(N))
    a,la,b,lb,c,lc = 0,0,0,0,0,0
    for s,t in zip(tmp,l):
        if s == "1":
            a += t
            la += 1
        elif s == "2":
            b += t
            lb += 1
        elif s == "3":
            c += t
            lc += 1
    ans = min(ans,f(a,la,A)+f(b,lb,B)+f(c,lc,C))

print(ans)

Solution (Python):
def base_10_to_n(X, n):
    if (int(X/n)):
        return base_10_to_n(int(X/n), n)+str(X%n)
    return str(X%n)

def f(a,A):
    if len(a) == 0:
        return 1e12
    return (len(a)-1)*10 + abs(sum(a)-A)

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

ans = 1e12
for i in range(4**N+1):
    tmp = list(base_10_to_n(i,4).zfill(N))
    a,b,c = [],[],[]
    for s,t in zip(tmp,l):
        if s == "1":
            a.append(t)
        elif s == "2":
            b.append(t)
        elif s == "3":
            c.append(t)
    ans = min(ans,f(a,A)+f(b,B)+f(c,C))

print(ans)

Solution (Python):
N,A,B,C =map(int,input().split())

l = [int(input()) for i in range(N)]

INF =10**9

def dfs(cnt,a,b,c):
    if cnt == N:
    
        return abs(A-a)+abs(B-b) + abs(C-c)-30 if min(a,b,c)>0 else INF
    
    ret0 = dfs(cnt+1 ,a,b,c)
    ret1 = dfs(cnt+1,a+l[cnt],b,c)+10
    ret2 = dfs(cnt+1,a,b+l[cnt],c)+10
    ret3 = dfs(cnt+1,a,b,c+l[cnt])+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 _ in range(n)]

ans = float('inf')
for i in range(2**n):
    for j in range(2**n):
        where = [0]*n
        x,y = i,j
        b = 0
        while x > 0 or y > 0:
            where[b] += (x&1) + 2*(y&1)
            b += 1
            x,y = x >> 1 , y >> 1
        cnt = -30
        ABC = [0,0,0]
        for x,w in enumerate(where):
            if w == 0:continue
            ABC[w-1] += L[x]
            cnt += 10
        if 0 in ABC:continue
        ans = min(ans, cnt + abs(ABC[0] - A) + abs(ABC[1] - B) + abs(ABC[2] - C))
print(ans)

Solution (Python):
import math
N, A, B, C = 5, 100, 90, 80
ARR = [
    98,
    40,
    30,
    21,
    80
]


























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

def calculate(n, arr, a, b, c):
    result = []
    for i in range(4 ** n):
        materialA = set()
        materialB = set()
        materialC = set()
        materialNone = set()
        for j in range(n):
            s = (i >> 2 * j & 1)
            t = (i >> (2 * j + 1) & 1)
            if (s == 0) and (t == 0):
                materialA.add(j)
            if (s == 1) and (t == 0):
                materialB.add(j)
            if (s == 0) and (t == 1):
                materialC.add(j)
            if (s == 1) and (t == 1):
                materialNone.add(j)

        ok, mp = judge(n, arr, a, b, c, materialA, materialB, materialC, materialNone)
        if ok:
            result.append(mp)
    print(int(min(result)))

def judge(n, arr, a, b, c, materialA, materialB, materialC, materialNone):


    if materialNone == n:
        return False, -1

    if len(materialA) == 0:
        return False, -1

    if len(materialB) == 0:
        return False, -1

    if len(materialC) == 0:
        return False, -1

    mpA = 0
    a = a - calculateResult(arr,materialA)
    mpA += math.fabs(a)
    if len(materialA) > 1:
        mpA += 10 * (len(materialA) - 1)

    mpB = 0
    b = b - calculateResult(arr,materialB)
    mpB += math.fabs(b)
    if len(materialB) > 1:
        mpB += 10 * (len(materialB) - 1)

    mpC = 0
    c = c - calculateResult(arr,materialC)
    mpC += math.fabs(c)
    if len(materialC) > 1:
        mpC += 10 * (len(materialC) - 1)


    return True,mpA+mpB+mpC

def calculateResult(arr,material):
    result = 0
    for m in material:
        result = result + arr[m]
    return result

calculate(N, ARR, A, B, C)

Solution (Python):
import math
N, A, B, C = 5, 100, 90, 80
ARR = [
    98,
    40,
    30,
    21,
    80
]


























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

def calculate(n, arr, a, b, c):
    result = []
    for i in range(4 ** n):
        materialA = set()
        materialB = set()
        materialC = set()
        materialNone = set()
        for j in range(n):
            s = (i >> 2 * j & 1)
            t = (i >> (2 * j + 1) & 1)
            if (s == 0) and (t == 0):
                materialA.add(j)
            if (s == 1) and (t == 0):
                materialB.add(j)
            if (s == 0) and (t == 1):
                materialC.add(j)
            if (s == 1) and (t == 1):
                materialNone.add(j)

        ok, mp = judge(n, arr, a, b, c, materialA, materialB, materialC, materialNone)
        if ok:
            result.append(mp)
    print(int(min(result)))

def judge(n, arr, a, b, c, materialA, materialB, materialC, materialNone):


    if materialNone == n:
        return False, -1

    if len(materialA & materialB) > 0:
        return False, -1

    if len(materialA & materialC) > 0:
        return False, -1

    if len(materialB & materialC) > 0:
        return False, -1

    if len(materialA) == 0:
        return False, -1

    if len(materialB) == 0:
        return False, -1

    if len(materialC) == 0:
        return False, -1

    mpA = 0
    a = a - calculateResult(arr,materialA)
    mpA += math.fabs(a)
    if len(materialA) > 1:
        mpA += 10 * (len(materialA) - 1)

    mpB = 0
    b = b - calculateResult(arr,materialB)
    mpB += math.fabs(b)
    if len(materialB) > 1:
        mpB += 10 * (len(materialB) - 1)

    mpC = 0
    c = c - calculateResult(arr,materialC)
    mpC += math.fabs(c)
    if len(materialC) > 1:
        mpC += 10 * (len(materialC) - 1)


    return True,mpA+mpB+mpC

def calculateResult(arr,material):
    result = 0
    for m in material:
        result = result + arr[m]
    return result

calculate(N, ARR, A, B, C)

Solution (Python):

def solve():

    N, A, B, C = map(int, input().split())
    llist = [int(input()) for i in range(N)]
    inf = 10**9

    def dfs(cursol, a, b, c):
        
        if cursol == N:
            return abs(A-a)+abs(B-b)+abs(C-c)-30 if min(a, b, c) > 0 else inf

        return min(
            dfs(cursol+1, a, b, c),
            
            dfs(cursol+1, a+llist[cursol], b, c)+10,
            dfs(cursol+1, a, b+llist[cursol], c)+10,
            dfs(cursol+1, a, b, c+llist[cursol])+10
        )

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


if __name__ == "__main__":
    solve()

Solution (Python):
N,A,B,C=map(int,input().split())
l=[int(input()) for _ in range(N)]
INF=10**9
def chk(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=chk(i+1,a,b,c)
    r1=chk(i+1,a+l[i],b,c)+10
    r2=chk(i+1,a,b+l[i],c)+10
    r3=chk(i+1,a,b,c+l[i])+10
    return min(r0,r1,r2,r3)
print(chk(0,0,0,0))

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):
n, A, B, C = map(int, input().split())
l = [int(input()) for _ in range(n)]
def Base_10_to_n(X, n):
    X_dumy = X
    out = ''
    while X_dumy>0:
        out = str(X_dumy%n)+out
        X_dumy = int(X_dumy/n)
    return out
ans =  10**6
for i in range(4**n):
  s = Base_10_to_n(i, 4)
  s = s.zfill(n)
  l_temp = [0, 0, 0]
  cnt = [0, 0, 0]
  for num, j in enumerate(s):
    if int(j):
      l_temp[int(j)-1] += l[num]
      cnt[int(j)-1] += 1
  if 0 in cnt:
    continue
  else:
    ans_temp = 10*(sum(cnt)-3) +abs(A-l_temp[0]) + abs(B-l_temp[1]) + abs(C-l_temp[2])
    ans = min(ans, ans_temp) 
print(ans)

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):
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(now1, now2, now3, d):
    if d==N:
        if min(now1, now2, now3)==0:
            return 10**18
            
        return abs(A-now1)+abs(B-now2)+abs(C-now3)
        
    res = 10**18
    res = min(res, dfs(now1+l[d], now2, now3, d+1)+10)
    res = min(res, dfs(now1, now2+l[d], now3, d+1)+10)
    res = min(res, dfs(now1, now2, now3+l[d], d+1)+10)
    res = min(res, dfs(now1, now2, now3, d+1))
    
    return res

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

Solution (Python):
def solve(i, al, bl, cl, nu):
    if i == N:
        lal, lbl, lcl = len(al), len(bl), len(cl)
        if lal == 0 or lbl == 0 or lcl == 0:
            return
        mp = 0
        mp += 10*(len(al)-1) if len(al) > 1 else 0
        mp += 10*(len(bl)-1) if len(bl) > 1 else 0
        mp += 10*(len(cl)-1) if len(cl) > 1 else 0
        mp += abs(sum(al)-A)
        mp += abs(sum(bl)-B)
        mp += abs(sum(cl)-C)
        global min_mp
        min_mp = min(min_mp, mp)
        return
    solve(i+1, al+[L[i]], bl, cl, nu)
    solve(i+1, al, bl+[L[i]], cl, nu)
    solve(i+1, al, bl, cl+[L[i]], nu)
    solve(i+1, al, bl, cl, nu+[L[i]])


N, A, B, C = map(int, input().split())
L = [int(input()) for _ in range(N)]
min_mp = 10**10
solve(0, [], [], [], [])
print(min_mp)

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))