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):
l=[]
n,a,b,c=map(int,input().split())
for i in range(n):
    li=int(input())
    l.append(li)

d=[a,b,c]
d.sort()

mpmin=sum(l)*4+len(l)*10+sum(d)
for i in range(4**n):
    k=[]
    kj=i%4
    k.append(kj)
    b=int((i-kj)/4)
    l0=[]
    l1=[]
    l2=[]
    l3=[]
    if kj==0:
        l0.append(l[0])
    elif kj==1:
        l1.append(l[0])
    elif kj==2:
        l2.append(l[0])
    elif kj==3:
        l3.append(l[0])
        
    for j in range(n-1):
        kj=b%4
        k.append(kj)
        b=int((b-kj)/4)
        if kj==0:
            l0.append(l[j+1])
        elif kj==1:
            l1.append(l[j+1])
        elif kj==2:
            l2.append(l[j+1])
        elif kj==3:
            l3.append(l[j+1])
    mp=0
    if len(l0)>=1:
        mp=mp+(len(l0)-1)*10
    if len(l1)>=1:
        mp=mp+(len(l1)-1)*10
    if len(l2)>=1:
        mp=mp+(len(l2)-1)*10
    ls=[]
    ls=[sum(l0),sum(l1),sum(l2)]
    ls.sort()
    mp=mp+abs(d[0]-ls[0])+abs(d[1]-ls[1])+abs(d[2]-ls[2])    
    if mpmin>mp and len(l0)>=1 and len(l1)>=1 and len(l2)>=1:
        mpmin=mp
print(mpmin)

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):
N, A, B, C = map(int, input().split())
Ln = [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+Ln[cur], b, c) + 10
    ret2 = dfs(cur+1, a, b+Ln[cur], c) + 10
    ret3 = dfs(cur+1, a, b, c+Ln[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)]
ans=float('inf')
for i in range(4**n):
  la,lb,lc=[],[],[]
  for j in range(n):
    if i%4==3:
      lc.append(l[j])
    elif i%4==2:
      lb.append(l[j])
    elif i%4==1:
      la.append(l[j])
    i//=4
  if la and lb and lc:
    synthesize=(len(la)-1)*10+(len(lb)-1)*10+(len(lc)-1)*10
    extend_cutdown=abs(sum(la)-a)+abs(sum(lb)-b)+abs(sum(lc)-c)
    ans=min(ans,synthesize+extend_cutdown)
print(ans)

Solution (Python):
import itertools

N,A,B,C = map(int,input().split())
L = [int(input()) for i in range(N)]
ans = 10**4
for v in itertools.product([0,1,2,3], repeat=N):
    if 1 not in v:
        continue
    if 2 not in v:
        continue
    if 3 not in v:
        continue
    a,b,c,m = 0,0,0,0
    for i in range(N):
        if v[i] == 1:
            a += L[i]
            m += 10
        if v[i] == 2:
            b += L[i]
            m += 10
        if v[i] == 3:
            c += L[i]
            m += 10
    m += abs(A-a) + abs(B-b) + abs(C-c) -30
    ans = min(ans,m)
print(ans)

Solution (Python):
import itertools

N,A,B,C = map(int,input().split())
L = [int(input()) for i in range(N)]
ans = 10**4
for v in itertools.product([0,1,2,3], repeat=N):
    if 1 not in v or 2 not in v or 3 not in v:
        continue
    a = 0
    b = 0
    c = 0
    m = -30
    for i in range(N):
        if v[i] == 1:
            a += L[i]
            m += 10
        if v[i] == 2:
            b += L[i]
            m += 10
        if v[i] == 3:
            c += L[i]
            m += 10
    m += abs(A-a) + abs(B-b) + abs(C-c)
    ans = min(ans,m)
print(ans)

Solution (Python):
import sys
from itertools import product
def input(): return sys.stdin.readline().rstrip()

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

    ans = 10 ** 7
    for l_li in product(range(4), repeat = N):
        cost = -30
        a = 0
        b = 0
        c = 0
        for i, li in enumerate(l_li):
            if li > 0:
                cost += 10
                if li == 1:
                    a += l[i]
                elif li == 2:
                    b += l[i]
                elif li == 3:
                    c += l[i]
        cost += abs(A - a) + abs(B - b) + abs(C - c)
        if a * b * c > 0 and ans > cost:
            ans = cost
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
N,A,B,C = map(int,input().split())
l = sorted([int(input()) for i in range(N)],reverse=True)
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 = float('inf')
for i in range(4**N):
    q = Base_10_to_n(i,4)
    q = str('0'*(N-len(q))+q)
    a = []
    b = []
    c = []
    for j in range(N):
        if q[j] == '1':
            a.append(l[j])
        if q[j] == '2':
            b.append(l[j])
        if q[j] == '3':
            c.append(l[j])
    cost = 0
    if a and b and c:
        cost += 10*(len(a)+len(b)+len(c)-3)
        cost += abs(A-sum(a)) + abs(B-sum(b))+ abs(C-sum(c))
        ans = min(ans,cost)
print(ans)

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

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

import itertools as it
ans = 3000
for p in it.product(range(4),repeat=n):
  ll = [0]*4
  lc = [0]*4
  for i in range(n):
    ll[p[i]] += lt[i]
    lc[p[i]] += 1
  if lc[0]*lc[1]*lc[2] > 0:
    mp = 10*(sum(lc[:3])-3) + sum(abs(ll[i]-la[i]) for i in range(3))
    ans = min(ans, mp)
print(ans)

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

import itertools as it
ans = 3000
for p in it.product(range(4),repeat=n):
  ll = [0]*4
  lc = [0]*4
  for i in range(n):
    ll[p[i]] += lt[i]
    lc[p[i]] += 1
  if lc[0]*lc[1]*lc[2] > 0:
    mp = 10*(sum(lc[:3])-3) + sum(abs(ll[i]-la[i]) for i in range(3))
    ans = min(ans, mp)
print(ans)

Solution (Python):
import itertools
n,a,b,c=map(int,input().split())
l=[int(input()) for i in range(n)]
l.sort()
ll=[i for i in range(1,n+1)]
x=[]
for i in range(1,n+1):
    z=list(itertools.combinations(l,i))
    zz=list(itertools.combinations(ll,i))
    for j in range(len(z)):
        x.append([sum(z[j]),(i-1)*10,list(zz[j])])
x.sort()
A=[];B=[];C=[]
for i in range(len(x)):
    A.append([x[i][1]+abs(a-x[i][0]),x[i][2]])
    B.append([x[i][1]+abs(b-x[i][0]),x[i][2]])
    C.append([x[i][1]+abs(c-x[i][0]),x[i][2]])
A.sort();B.sort();C.sort()
m=len(A)
ans=10**10
for i in range(m):
    for j in range(m):
        for k in range(m):
            if len(A[i][1]+B[j][1]+C[k][1])>=n+1:
                break
            elif len(A[i][1]+B[j][1]+C[k][1])==len((set(A[i][1]+B[j][1]+C[k][1]))):
                ans=min(ans,A[i][0]+B[j][0]+C[k][0])
print(ans)

Solution (Python):
import itertools
n,a,b,c=map(int,input().split())
l=[int(input()) for i in range(n)]
l.sort()
ll=[i for i in range(1,n+1)]
x=[]
for i in range(1,n+1):
    z=list(itertools.combinations(l,i))
    zz=list(itertools.combinations(ll,i))
    for j in range(len(z)):
        x.append([sum(z[j]),(i-1)*10,list(zz[j])])
x.sort()

A=[];B=[];C=[]
for i in range(len(x)):
    A.append([x[i][1]+abs(a-x[i][0]),x[i][2]])
    B.append([x[i][1]+abs(b-x[i][0]),x[i][2]])
    C.append([x[i][1]+abs(c-x[i][0]),x[i][2]])
A.sort();B.sort();C.sort()



m=len(A)
ans=float('inf')
for i in range(m):
    for j in range(m):
        for k in range(m):
            if len(A[i][1]+B[j][1]+C[k][1])>=9:
                break
            elif len(A[i][1]+B[j][1]+C[k][1])==len((set(A[i][1]+B[j][1]+C[k][1]))):
                ans=min(ans,A[i][0]+B[j][0]+C[k][0])
print(ans)

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

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

A, B, C = sorted([A, B, C])

answer = 10 ** 15
for i in range(4 ** N):
  x =[0] * 3
  tmp = 0
  for j in range(N):
    index = (i // (4 ** j)) % 4
    if index != 3:
      if x[index]:
        tmp += 10
      x[index] += L[j]
  x.sort()
  if x[0] != 0:
    tmp += abs(x[0] - A) + abs(x[1] - B) + abs(x[2] - C)
    answer = min(answer, tmp)

print(answer)

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


def dfs(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([
        dfs(i + 1, a, b, c),
        dfs(i + 1, a + l[i], b, c) + (10 if a > 0 else 0),
        dfs(i + 1, a, b + l[i], c) + (10 if b > 0 else 0),
        dfs(i + 1, a, b, c + l[i]) + (10 if c > 0 else 0)
    ])


print(dfs(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 a < 1 or b < 1 or c < 1:
            return 10 ** 18
        return abs(A - a) + abs(B - b) + abs(C - c)
    return min([
        dfs(i + 1, a, b, c),
        dfs(i + 1, a + l[i], b, c) + (10 if a > 0 else 0),
        dfs(i + 1, a, b + l[i], c) + (10 if b > 0 else 0),
        dfs(i + 1, a, b, c + l[i]) + (10 if c > 0 else 0)])


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):
N,A,B,C = map(int ,input().split())
l = [int(input()) for i in range(N)]
INF = 10**9

def dfs(res, a, b, c):
	if res == N:
		if a != 0 and b != 0 and c != 0:
			return abs(a-A) + abs(b-B) + abs(c-C) -30
		else:
			return INF
	else:
		ret0 = dfs(res+1,a,b,c)
		ret1 = dfs(res+1,a+l[res],b,c) + 10
		ret2 = dfs(res+1,a,b+l[res],c) + 10
		ret3 = dfs(res+1,a,b,c+l[res]) + 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 bf(m):
    ret=[]
    while m!=0:
        ret.append(m%4)
        m=m//4
    return ret+[0]*(N-len(ret))

ans=10000
comp=[A,B,C]
for i in range(4**N): 
    ch=[0,0,0] 
    tmp=[0,0,0] 
    tgt=[A,B,C] 
    for j,l in zip(bf(i),L): 
        if j==0: 
            continue
        else:
            tgt[j-1]-=l 
            if ch[j-1]: 
                tmp[j-1]+=10 
            else:
                ch[j-1]+=1 
    flag=True 
    for l,com in zip(tgt,comp):
        if l==com: 
            flag=False
            break
    if flag:
        ans=min(ans,sum(abs(x) for x in tgt)+sum(tmp)) 
print(ans)

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 a < 1 or b < 1 or c < 1:
            return 10 ** 18
        return abs(A-a) + abs(B-b) + abs(C-c)
    res = dfs(i + 1, a, b, c)
    res = min([res, dfs(i + 1, a + l[i], b, c) + (10 if a > 0 else 0)])
    res = min([res, dfs(i + 1, a, b + l[i], c) + (10 if b > 0 else 0)])
    res = min([res, dfs(i + 1, a, b, c + l[i]) + (10 if c > 0 else 0)])
    return res


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

Solution (Python):

わからん=True
INF=1<<30

n,a,b,c=map(int,input().split())


l=[int(input()) for _ in range(n)]


def dfs(j,x,y,z):
    if n==j:
        return abs(a-x)+abs(b-y)+abs(c-z)-30 if min(x,y,z)>0 else INF
        
    t1 = dfs(j+1,x,y,z)
    t2 = dfs(j+1,x+l[j],y,z)+10
    t3 = dfs(j+1,x,y+l[j],z)+10
    t4 = dfs(j+1,x,y,z+l[j])+10
    return min(t1,t2,t3,t4)
    
ans=dfs(0,0,0,0)
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):
import itertools
n,a,b,c=map(int,input().split())
abc=[a,b,c]
l=[int(input()) for i in range(n)]
m=[i for i in range(3)]
mi=100000000000000
for i in list(itertools.product(*[m for _ in range(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):
        subsub=sub[j]
        l_sub=len(subsub)
        if l_sub==0:
            mi_sub=100000000000000
            break
        mi_subsub=100000000000000
        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[subsub[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):
n,a,b,c=map(int, input().split())
l=[int(input()) for _ in range(n)]
from itertools import product
X=product([0,1,2,3],repeat=n)

def score(x):
  atmp=0
  btmp=0
  ctmp=0
  score=0
  for i in range(n):
    if x[i]==1:
      if atmp!=0:
        score+=10
      atmp+=l[i]
    elif x[i]==2:
      if btmp!=0:
        score+=10
      btmp+=l[i]
    elif x[i]==3:
      if ctmp!=0:
        score+=10
      ctmp+=l[i]
    else:continue
  if atmp>0 and btmp>0 and ctmp>0:
    score+=(abs(a-atmp)+abs(b-btmp)+abs(c-ctmp))
  else: score=float("inf")
  return score

smin=float('inf')
for x in X:
  smin=min(smin,score(x))
print(smin)

Solution (Python):

import sys
import math
import os
import itertools
import _collections
import string

class cin():
    def int():
        return int(sys.stdin.readline().rstrip())
    def string():
        return sys.stdin.readline().rstrip()
    def mapInt():
        return [int(x) for x in cin.string().split()]
    def stringList(n):
        return [input() for i in range(n)]
    def intListList(n):
        return [cin.mapInt() for i in range(n)]
    def intColsList(n):
        return [int(input()) for i in range(n)]

class Util():
    def roundUp(a,b):
        return -(-a // b)
    def toUpperMultiple(a,x):
        return Util.round_up(a,x) * x
    def toLowerMultiple(a,x):
        return (a // x) * x
class Math():
    def gcd(a,b):
        if b == 0:
            return a
        return Math.gcd(b,a % b)

def main():
    N,A,B,C = cin.mapInt()
    L = cin.intColsList(N)

    ans = int(1e09)
    for i in range(1 << N * 2):
        D = [0] * 3
        mp = 0
        for j in range(N):
            n = (i >> (j * 2)) & 3
            if n == 0:
                continue
            if D[n - 1] > 0:
                mp += 10
            D[n - 1] += L[j]
        if 0 in D:
            continue
        mp += abs(A - D[0])
        mp += abs(B - D[1])
        mp += abs(C - D[2])
        ans = min(ans,mp)
    print(ans)
    return

if __name__ == "__main__":
    main()

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 a < 1 or b < 1 or c < 1:
            return 10 ** 18
        return abs(A - a) + abs(B - b) + abs(C - c)
    res = dfs(i + 1, a, b, c)
    res = min([res, dfs(i + 1, a + l[i], b, c) + (10 if a > 0 else 0)])
    res = min([res, dfs(i + 1, a, b + l[i], c) + (10 if b > 0 else 0)])
    res = min([res, dfs(i + 1, a, b, c + l[i]) + (10 if c > 0 else 0)])
    return res


print(dfs(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 a < 1 or b < 1 or c < 1:
            return 10 ** 18
        return abs(A - a) + abs(B - b) + abs(C - c)
    res = dfs(i + 1, a, b, c)
    res = min([res, dfs(i + 1, a + l[i], b, c) + (10 if a > 0 else 0)])
    res = min([res, dfs(i + 1, a, b + l[i], c) + (10 if b > 0 else 0)])
    res = min([res, dfs(i + 1, a, b, c + l[i]) + (10 if c > 0 else 0)])
    return res


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

def calc(m):
    s=[0,0,0,0]
    sy=-3
    for i in range(N):
        s[m%4]+=L[i]
        if m%4!=0:
            sy+=1
        m=m//4
    for i in range(1,4):
        if s[i]==0:
            return 10**9
    return abs(A-s[1])+abs(B-s[2])+abs(C-s[3])+sy*10
    
ans=10**9
for i in range(pow(4,N)):
    ans=min(ans,calc(i))
print(ans)

Solution (Python):
def dfs(i, a, b, c, t):
    global ans
    if a*b*c > 0:
        ans = min( ans, abs(A-a)+abs(B-b)+abs(C-c)+10*(t-3) )
    if i > N - 1: return 0
    dfs(i+1,a,b,c,t)
    dfs(i+1, a + L[i], b, c, t+1)
    dfs(i+1, a, b + L[i], c, t+1)
    dfs(i+1, a, b, c + L[i], t+1)
    return ans
N, A, B, C = map(int,input().split())
L = [int(input()) for i in range(N)]
ans = 10**9
print(dfs(0,0,0,0,0))

Solution (Python):
def solve(current, x, y, z):
    if current == n:
        if min(x, y, z) > 0:
            return abs(a - x) + abs(b - y) + abs(c - z) - 30
        else:
            return 10 ** 9 + 7
    use_for_a = solve(current + 1, x + l[current], y, z) + 10
    use_for_b = solve(current + 1, x, y + l[current], z) + 10
    use_for_c = solve(current + 1, x, y, z + l[current]) + 10
    not_use = solve(current + 1, x, y, z)

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


def dfs(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)
    res = dfs(i + 1, a, b, c)
    res = min([res, dfs(i + 1, a + l[i], b, c) + (10 if a > 0 else 0)])
    res = min([res, dfs(i + 1, a, b + l[i], c) + (10 if b > 0 else 0)])
    res = min([res, dfs(i + 1, a, b, c + l[i]) + (10 if c > 0 else 0)])
    return res


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

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


def dfs(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 float("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)]
from itertools import product

ans=10**18

for pattern in product(range(4),repeat=N):
    MP=0
    take=[0]*3
    for index,length in zip(pattern,L):
        if index==3:
            continue
        if take[index]!=0:
            MP+=10
        take[index]+=length
    if take[0]!=0 and take[1]!=0 and take[2]!=0:
        MP+=abs(A-take[0])
        MP+=abs(B-take[1])
        MP+=abs(C-take[2])
        ans=min(MP,ans)
print(ans)

Solution (Python):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
 
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

n, A, B, C = MAP()

l = [INT() for i in range(n)]

def dfs(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
    
    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())

orgPetternList =[]
def petternMake(cnt,s):
  if cnt == n+1:
    orgPetternList.append(s)
    return
  for i in range(4):
    petternMake(cnt+1,s+str(i))
  

banboo = [int(input()) for i in range(n)]


petternMake(1,'')

min = 999999999

for ptn in orgPetternList:

  if ptn.find('1') == -1 or ptn.find('2') == -1  or ptn.find('3') == -1 :
    continue
    
  ptnList = list(ptn)
  sumMp = nowa = nowb = nowc = acnt = bcnt = ccnt= 0
  for cnt in range(len(ptnList)):
    
    if ptnList[cnt] == '1':
      nowa += banboo[cnt]
      acnt+=1
      if acnt>1:
        sumMp +=10
    elif ptnList[cnt]=='2':
      nowb += banboo[cnt]
      bcnt+=1
      if bcnt>1:
        sumMp +=10
    elif ptnList[cnt]=='3':
      nowc += banboo[cnt]
      ccnt+=1
      if ccnt>1:
        sumMp +=10
      
  sumMp = sumMp + abs(a-nowa) + abs(b-nowb) +abs(c-nowc)

  if sumMp < min:
    min = sumMp

print(min)

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 a < 1 or b < 1 or c < 1:
            return 10 ** 18
        return abs(A - a) + abs(B - b) + abs(C - c)
    res = dfs(i + 1, a, b, c)
    res = min([res, dfs(i + 1, a + l[i], b, c) + (10 if a > 0 else 0)])
    res = min([res, dfs(i + 1, a, b + l[i], c) + (10 if b > 0 else 0)])
    res = min([res, dfs(i + 1, a, b, c + l[i]) + (10 if c > 0 else 0)])
    return res


print(dfs(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 n == i:
        if 1 > a or 1 > b or 1 > c:
            return 10 ** 18
        return abs(A-a) + abs(B-b) + abs(C-c)
    res = dfs(i + 1, a, b, c)
    res = min([res, dfs(i + 1, a + l[i], b, c) + (10 if a > 0 else 0)])
    res = min([res, dfs(i + 1, a, b + l[i], c) + (10 if b > 0 else 0)])
    res = min([res, dfs(i + 1, a, b, c + l[i]) + (10 if c > 0 else 0)])
    return res


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

Solution (Python):


"""
s = input().split("/")
if int(s[1]) <= 4:
    print("Heisei")
else:
    print("TBD")
"""

"""
n = int(input())
ans = 0
for i in range(n):
    a = input().split()
    if a[1] == "JPY":
        ans += int(a[0])
    else:
        ans +=  float(a[0]) * 380000
print(ans)
"""


import itertools

n, a, b, c = map(int, input().split())
abc = [a, b, c]
ans = float("INF")
x = [int(input()) for i in range(n)]
nums = list(itertools.product([0, 1, 2, 3], repeat = n))

for i in nums:
    tmp = 0
    aa = 0
    ba = 0
    ca = 0
    for number, k in enumerate(i):
        if k == 1:
            if aa != 0:
                tmp += 10
            aa += x[number]
        if k == 2:
            if ba != 0:
                tmp += 10
            ba += x[number]
        if k == 3:
            if ca != 0:
                tmp += 10
            ca += x[number]
    if aa == 0 or ba == 0 or ca == 0:
        tmp = float("INF")
    else:
        tmp += abs(aa - a)
        tmp += abs(ba - b)
        tmp += abs(ca - c)
    ans = min(tmp, ans)
print(ans)
        
        
            




"""
a, b, q = map(int, input().split())
s = [int(input()) for i in range(a)]
t = [int(input()) for i in range(b)]
s.sort()
t.sort()

import bisect


for i in range(q):
    x = int(input())
    s_l = bisect.bisect_left(s, x) - 1
    t_l = bisect.bisect_left(t, x) - 1
    s_r = s_l + 1
    t_r = t_l + 1
    if s_l == -1:
        s_l = 0
    if t_l == -1:
        t_l = 0
    if s_l == a :
        s_l = a - 1
        s_r = a - 1
    if s_r == a:
        s_r = a - 1
    if t_l == b:
        t_l = b - 1
        t_r = b - 1
    if t_r == b:
        t_r = b - 1
    ans = min(abs(t[t_l] - s[s_r]) + min(abs(t[t_l] - x), abs(s[s_r] - x)),
    abs(s[s_l] - t[t_r]) + min(abs(s[s_l] - x), abs(t[t_r] - x)),
    abs(s[s_l] - t[t_l]) + min(abs(s[s_l] - x), abs(t[t_l] - x)),
    abs(s[s_r] - t[t_r]) + min(abs(s[s_r] - x), abs(t[t_r] - x)),
    )
    
    print(ans)
"""

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


def bfs(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)
    res = bfs(i + 1, a, b, c)
    res = min([res, bfs(i + 1, a + l[i], b, c) + (10 if a > 0 else 0)])
    res = min([res, bfs(i + 1, a, b + l[i], c) + (10 if b > 0 else 0)])
    res = min([res, bfs(i + 1, a, b, c + l[i]) + (10 if c > 0 else 0)])
    return res


print(bfs(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 a < 1 or b < 1 or c < 1:
            return 10 ** 18
        return abs(A - a) + abs(B - b) + abs(C - c)

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


print(dfs(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 n == i:
        if a < 1 or b < 1 or c < 1:
            return 10 ** 18
        return abs(A - a) + abs(B - b) + abs(C - c)

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


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

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

N, A, B, C = [int(x) for x in input().split()]

L = [int(input()) for _ in range(N)]
INF = 10 ** 12

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):
import sys
sys.setrecursionlimit(10 ** 6)

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

L = [int(input()) for _ in range(N)]
INF = 10 ** 12


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 calc(T, bam):
    mix = len(bam) - 1
    total = sum(bam)
    return mix * 10 + abs(T - total)


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


var = [[] for _ in range(N + 1)]
var[0].append('')

for i in range(N):
    for j in var[i]:
        for c in '0abc':
            var[i + 1].append(j + c)

V = var[N]

ans = 10 ** 6
for v in V:
    a = []
    b = []
    c = []
    cost = 0
    for i, m in enumerate(v):
        if m == 'a':
            a.append(L[i])
        elif m == 'b':
            b.append(L[i])
        elif m == 'c':
            c.append(L[i])
    if len(a) and len(b) and len(c):
        CA = calc(A, a)
        CB = calc(B, b)
        CC = calc(C, c)
        cost = CA + CB + CC
        if cost == 5:
            print('hello')
        ans = min(ans, cost)

print(ans)

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


def dfs(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)

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


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