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())
length=[]
for i in range(n):
  k=int(input())
  length.append(k)
length=sorted(length)
x=[0,1,2,3]
ans=[]
if len(length)==3:
    for p in x:
      for q in x:
        for r in x:
            value=0
            a_length=0
            b_length=0
            c_length=0
            line=[p,q,r]
            if line.count(1)==0:
                value+=10*18
            if line.count(2)==0:
                value+=10*18
            if line.count(3)==0:
                value+=10*18
            if line.count(1)>1:
              value+=10*(line.count(1)-1)
            if line.count(2)>1:
              value+=10*(line.count(2)-1)
            if line.count(3)>1:
              value+=10*(line.count(3)-1)
            A=[i for i, y in enumerate(line) if y ==1]
            B=[i for i, y in enumerate(line) if y ==2]
            C=[i for i, y in enumerate(line) if y ==3]
            for i in A:
              a_length+=length[i]
            for i in B:
              b_length+=length[i]
            for i in C:
              c_length+=length[i]
            value+=abs(a-a_length)
            value+=abs(b-b_length)
            value+=abs(c-c_length)
            ans.append(value)
if len(length)==4:
    for p in x:
      for q in x:
        for r in x:
          for s in x:
              value=0
              a_length=0
              b_length=0
              c_length=0
              line=[p,q,r,s]
              if line.count(1)==0:
                  value+=10*18
              if line.count(2)==0:
                  value+=10*18
              if line.count(3)==0:
                  value+=10*18
              if line.count(1)>1:
                value+=10*(line.count(1)-1)
              if line.count(2)>1:
                value+=10*(line.count(2)-1)
              if line.count(3)>1:
                value+=10*(line.count(3)-1)
              A=[i for i, y in enumerate(line) if y ==1]
              B=[i for i, y in enumerate(line) if y ==2]
              C=[i for i, y in enumerate(line) if y ==3]
              for i in A:
                a_length+=length[i]
              for i in B:
                b_length+=length[i]
              for i in C:
                c_length+=length[i]
              value+=abs(a-a_length)
              value+=abs(b-b_length)
              value+=abs(c-c_length)
              ans.append(value)
if len(length)==5:
    for p in x:
      for q in x:
        for r in x:
          for s in x:
            for t in x:
                value=0
                a_length=0
                b_length=0
                c_length=0
                line=[p,q,r,s,t]
                if line.count(1)==0:
                    value+=10*18
                if line.count(2)==0:
                    value+=10*18
                if line.count(3)==0:
                    value+=10*18
                if line.count(1)>1:
                  value+=10*(line.count(1)-1)
                if line.count(2)>1:
                  value+=10*(line.count(2)-1)
                if line.count(3)>1:
                  value+=10*(line.count(3)-1)
                A=[i for i, y in enumerate(line) if y ==1]
                B=[i for i, y in enumerate(line) if y ==2]
                C=[i for i, y in enumerate(line) if y ==3]
                for i in A:
                  a_length+=length[i]
                for i in B:
                  b_length+=length[i]
                for i in C:
                  c_length+=length[i]
                value+=abs(a-a_length)
                value+=abs(b-b_length)
                value+=abs(c-c_length)
                ans.append(value)
if len(length)==6:
    for p in x:
      for q in x:
        for r in x:
          for s in x:
            for t in x:
              for u in x:
                  value=0
                  a_length=0
                  b_length=0
                  c_length=0
                  line=[p,q,r,s,t,u]
                  if line.count(1)==0:
                      value+=10*18
                  if line.count(2)==0:
                      value+=10*18
                  if line.count(3)==0:
                      value+=10*18
                  if line.count(1)>1:
                    value+=10*(line.count(1)-1)
                  if line.count(2)>1:
                    value+=10*(line.count(2)-1)
                  if line.count(3)>1:
                    value+=10*(line.count(3)-1)
                  A=[i for i, y in enumerate(line) if y ==1]
                  B=[i for i, y in enumerate(line) if y ==2]
                  C=[i for i, y in enumerate(line) if y ==3]
                  for i in A:
                    a_length+=length[i]
                  for i in B:
                    b_length+=length[i]
                  for i in C:
                    c_length+=length[i]
                  value+=abs(a-a_length)
                  value+=abs(b-b_length)
                  value+=abs(c-c_length)
                  ans.append(value)
if len(length)==7:
    for p in x:
      for q in x:
        for r in x:
          for s in x:
            for t in x:
              for u in x:
                for v in x:
                    value=0
                    a_length=0
                    b_length=0
                    c_length=0
                    line=[p,q,r,s,t,u,v]
                    if line.count(1)==0:
                        value+=10*18
                    if line.count(2)==0:
                        value+=10*18
                    if line.count(3)==0:
                        value+=10*18
                    if line.count(1)>1:
                      value+=10*(line.count(1)-1)
                    if line.count(2)>1:
                      value+=10*(line.count(2)-1)
                    if line.count(3)>1:
                      value+=10*(line.count(3)-1)
                    A=[i for i, y in enumerate(line) if y ==1]
                    B=[i for i, y in enumerate(line) if y ==2]
                    C=[i for i, y in enumerate(line) if y ==3]
                    for i in A:
                      a_length+=length[i]
                    for i in B:
                      b_length+=length[i]
                    for i in C:
                      c_length+=length[i]
                    value+=abs(a-a_length)
                    value+=abs(b-b_length)
                    value+=abs(c-c_length)
                    ans.append(value)
if len(length)==8:
    for p in x:
      for q in x:
        for r in x:
          for s in x:
            for t in x:
              for u in x:
                for v in x:
                  for w in x:
                    value=0
                    a_length=0
                    b_length=0
                    c_length=0
                    line=[p,q,r,s,t,u,v,w]
                    if line.count(1)==0:
                        value+=10*18
                    if line.count(2)==0:
                        value+=10*18
                    if line.count(3)==0:
                        value+=10*18
                    if line.count(1)>1:
                      value+=10*(line.count(1)-1)
                    if line.count(2)>1:
                      value+=10*(line.count(2)-1)
                    if line.count(3)>1:
                      value+=10*(line.count(3)-1)
                    A=[i for i, y in enumerate(line) if y ==1]
                    B=[i for i, y in enumerate(line) if y ==2]
                    C=[i for i, y in enumerate(line) if y ==3]
                    for i in A:
                      a_length+=length[i]
                    for i in B:
                      b_length+=length[i]
                    for i in C:
                      c_length+=length[i]
                    value+=abs(a-a_length)
                    value+=abs(b-b_length)
                    value+=abs(c-c_length)
                    ans.append(value)
print(min(ans))

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

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 itertools as itr
n, a, b, c = list(map(int, input().split()))
abc = [a, b ,c]
l = [int(input()) for _ in range(n)]
ans = float('inf')
for i in list(itr.product([0, 1, 2, 3], repeat=n)):
    tans = 0
    pd = [True]*3
    tabc = abc.copy()
    if 0 not in i or 1 not in i or 2 not in i:
        continue
    for j in range(len(i)):
        if i[j] != 3:
            tabc[i[j]] -= l[j]
            if pd[i[j]]:
                pd[i[j]] = False
            else:
                tans += 10
    for k in tabc:
        tans += abs(k)
    ans = min(ans, tans)
print(ans)

Solution (Python):
from itertools import product

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

abc = [a, b, c]

res = float('inf')
for p in product(range(4), repeat=n):
    num = [0, 0, 0, 0]
    le = [0, 0, 0, 0]
    for i, category in enumerate(p):
        num[category] += l[i]
        le[category] += 1
    tmp = 0
    if le[0] * le[1] * le[2] == 0:
        continue
    for i in range(3):
        tmp += abs(num[i] - abc[i]) + 10 * (le[i] - 1)
    res = min(res, tmp)

print(res)

Solution (Python):
from itertools import product

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

abc = [a, b, c]

res = float('inf')
for p in product(range(4), repeat=n):
    num = [0, 0, 0, 0]
    le = [0, 0, 0, 0]
    for i, category in enumerate(p):
        num[category] += l[i]
        le[category] += 1
    tmp = 0
    if le[0] * le[1] * le[2] == 0:
        continue
    for i in range(3):
        tmp += abs(num[i] - abc[i]) + 10 * (le[i] - 1)
    res = min(res, tmp)

print(res)

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:
        if min(a, b, c) > 0:
            return abs(a - A) + abs(b - B) + abs(c - C) - 30  
        else: return 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 = float('inf')


def calc(mask):
    abc = [0 for _ in range(4)]
    cnt = 0
    for j in range(N):
        idx = (mask >> (2 * j)) & 3
        abc[idx] += l[j]
        if 0 <= idx < 3:
            cnt += 1
    if 0 in abc[:3]:
        return INF
    return sum(abs(x - t) for x, t in zip(abc, [A, B, C])) + (cnt - 3) * 10


def dfs(cur, mask):
    if cur == N:
        return calc(mask)

    res = INF
    for i in range(4):
        res = min(res, dfs(cur + 1, mask | i << (2 * cur)))
    return res


print(dfs(0, 0))

Solution (Python):
N,A,B,C=map(int,input().split())
T=[A,B,C]
L=[int(input()) for i in range(N)]
ans=1999999999
for i in range(4**N):
  L_=[[] for i in range(4)]
  for k in range(N):
    r=i%4
    i=(i-r)//4
    L_[r].append(L[k])
  if len(L_[0])*len(L_[1])*len(L_[2])==0:
    continue
  MP=10*(len(L_[0])+len(L_[1])+len(L_[2])-3)
  for r in range(3):
    s=0
    for l in L_[r]:
      s+=l
    MP+=abs(s-T[r])
  ans=min(ans,MP)
print(ans)

Solution (Python):
from itertools import product

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

def get_mp(q):
    assert len(q) == N
    res = 0
    assign = [[] for _ in range(4)]
    
    for i, j in enumerate(q):
        assign[j].append(bl[i])

    if any([l == [] for l in assign[:3]]):
        return 10**18
    
    for l, p in zip(assign, [A, B, C]):
        ext_sht = abs(sum(l) - p)
        syn = (len(l) - 1) * 10
        res += ext_sht + syn
    
    return res

res = []
for q in product(*([[0,1,2,3]]*N)):
    res.append(get_mp(q))
print(min(res))

Solution (Python):


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

ans = INF
for ptn in range(1 << (2 * N)):
    abc = [0 for _ in range(4)]

    v = [None for _ in range(N)]
    

    cnt = 0
    for j in range(N):
        v[j] = (ptn >> (2 * j)) & 3
        abc[v[j]] += l[j]
        if 0 <= v[j] < 3:
            cnt += 1

    if 0 in abc[:3]: continue
    ans = min(ans, sum(abs(x - t) for x, t in zip(abc[:3], [A, B, C])) + (cnt - 3) * 10)
print(ans)

Solution (Python):
import sys
from collections import deque
import copy
import math
def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline

def calc(L, nowA, nowB, nowC, nowD, i, j):
    if i == 0:
        nowD += L[j]
    elif i == 1:
        nowA += L[j]
    elif i == 2:
        nowB += L[j]
    elif i == 3:
        nowC += L[j]

    return nowA, nowB, nowC, nowD

def num(i1, i2, i3, i4, i5, i6, i7, i8, t):
    return (i1 == t) + (i2 == t) + (i3 == t) + (i4 == t) + (i5 == t) + (i6 == t) + (i7 == t) + (i8 == t)

def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [N, A, B, C] = [int(input_raw[0]), int(input_raw[1]), int(input_raw[2]), int(input_raw[3])]
    L = []
    for i in range(N):
        input_raw = read_func().strip().split()
        L.append(int(input_raw[0]))
    min_MP = sys.maxint
    for i1 in range(0, 4 if N >= 1 else 1):
        for i2 in range(0, 4 if N >= 2 else 1):
            for i3 in range(0, 4 if N >= 3 else 1):
                for i4 in range(0, 4 if N >= 4 else 1):
                    for i5 in range(0, 4 if N >= 5 else 1):
                        for i6 in range(0, 4 if N >= 6 else 1):
                            for i7 in range(0, 4 if N >= 7 else 1):
                                for i8 in range(0, 4 if N >= 8 else 1):
                                    nowA = 0
                                    nowB = 0
                                    nowC = 0
                                    nowD = 0
                                    if N >= 1:
                                        nowA,nowB,nowC,nowD = calc(L, nowA,nowB,nowC,nowD, i1, 0)
                                    if N >= 2:
                                        nowA,nowB,nowC,nowD = calc(L, nowA,nowB,nowC,nowD, i2, 1)
                                    if N >= 3:
                                        nowA,nowB,nowC,nowD = calc(L, nowA,nowB,nowC,nowD, i3, 2)
                                    if N >= 4:
                                        nowA,nowB,nowC,nowD = calc(L, nowA,nowB,nowC,nowD, i4, 3)
                                    if N >= 5:
                                        nowA,nowB,nowC,nowD = calc(L, nowA,nowB,nowC,nowD, i5, 4)
                                    if N >= 6:
                                        nowA,nowB,nowC,nowD = calc(L, nowA,nowB,nowC,nowD, i6, 5)
                                    if N >= 7:
                                        nowA,nowB,nowC,nowD = calc(L, nowA,nowB,nowC,nowD, i7, 6)
                                    if N >= 8:
                                        nowA,nowB,nowC,nowD = calc(L, nowA,nowB,nowC,nowD, i8, 7)

                                    Anum = num(i1,i2,i3,i4,i5,i6,i7,i8,1)
                                    Bnum = num(i1,i2,i3,i4,i5,i6,i7,i8,2)
                                    Cnum = num(i1,i2,i3,i4,i5,i6,i7,i8,3)
                                    if Anum <= 0 or Bnum <= 0 or Cnum <= 0:
                                        continue
                                    MP = (Anum -1) * 10 + abs(nowA - A)
                                    MP += (Bnum - 1) * 10 + abs(nowB - B)
                                    MP += (Cnum - 1) * 10 + abs(nowC - C)
                                    min_MP = min(MP, min_MP)
    print min_MP






















if __name__ == '__main__':
    main()

Solution (Python):
from itertools import product
from collections import Counter
N, A, B, C = map(int, input().split())
h = {1: A, 2: B, 3: C}
l = [int(input()) for i in range(N)]
ans = float('inf')
for p in product([0, 1, 2, 3], repeat=N):
    c = Counter(p)
    if c[1] == 0 or c[2] == 0 or c[3] == 0:
        continue
    t = {1: 0, 2: 0, 3: 0}
    for i, v in enumerate(p):
        if v == 0:
            continue
        t[v] += l[i]
    mp = 0
    for i in range(1, 4):
        if c[i] >= 2:
            mp += (c[i] - 1) * 10
        mp += abs(h[i] - t[i])
    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(depth, a, b, c):
    if depth == N:
        return abs(A-a)+abs(B-b)+abs(C-c)-30 if min(a, b, c) > 0 else 10**9

    ret0 = dfs(depth+1, a, b, c)
    ret1 = dfs(depth+1, a+l[depth], b, c)+10
    ret2 = dfs(depth+1, a, b+l[depth], c)+10
    ret3 = dfs(depth+1, a, b, c+l[depth])+10
    return min(ret0, ret1, ret2, ret3)

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

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

class T:
    def __init__(self):
        self.ans=A+B+C

    def rec(self,i,a,b,c,m):
        if i==N:
            if a*b*c:
                self.ans=min(self.ans,abs(A-a)+abs(B-b)+abs(C-c)+m*10)
            return
        self.rec(i+1,a+take[i],b,c,m+1)
        self.rec(i+1,a,b+take[i],c,m+1)
        self.rec(i+1,a,b,c+take[i],m+1)
        self.rec(i+1,a,b,c,m)
        
    def __call__(self):
        self.rec(0,0,0,0,-3)
        return self.ans
        
print(T()())

Solution (Python):
import itertools

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


ans = 10 ** 8
for t in list(itertools.product((0, 1, 2, 3), repeat=len(l))):
    a = [i for i, j in zip(l, t) if j == 0]
    b = [i for i, j in zip(l, t) if j == 1]
    c = [i for i, j in zip(l, t) if j == 2]
    if len(a) * len(b) * len(c) == 0:
        continue
    tmp1 = abs(sum(a) - A) + max(0, len(a) - 1) * 10
    tmp2 = abs(sum(b) - B) + max(0, len(b) - 1) * 10
    tmp3 = abs(sum(c) - C) + max(0, len(c) - 1) * 10
    ans = min(ans, tmp1 + tmp2 + tmp3)


print(ans)

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

l_list = [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_list[cur], b, c) + 10
  ret2 = dfs(cur + 1, a, b + l_list[cur], c) + 10
  ret3 = dfs(cur + 1, a, b, c + l_list[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:
        if min(a, b, c) > 0:
            return abs(a-A) + abs(b-B) + abs(c-C) - 30
        else:
            return 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 = list(map(int, input().split()))
lll = [int(input()) for _ in range(n)]

ans = 10 ** 9
for i in range(4 ** n):
    abc = [0] * 3
    tmp = 0
    for j in range(n):
        k = (i >> (2 * j)) & 3
        if k < 3:
            if abc[k] > 0:
                tmp += 10
            abc[k] += lll[j]
    if any(t == 0 for t in abc):
        continue
    tmp += sum(abs(d - e) for d, e in zip((a, b, c), abc))
    ans = min(ans, tmp)
print(ans)

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

    take = []
    take.append(a)
    take.append(b)
    take.append(c)

    mp = 10000000000
    pt= []
    
    for i in range(2**(2*n)):
        pt = []
        for j in range(n):
            if (i >> j*2)&1:
                if (i >> j*2+1)&1:
                    pt.append(3)
                else:
                    pt.append(1)
            else:
                if (i >> j*2+1)&1:
                    pt.append(2)
                else:
                    pt.append(0)       

        
        mp_tmp = 0
        all_used = True
        for k in range(3):
            if pt.count(k) == 0:
                all_used = False
                break

            sum_tmp = 0
            mp_tmp += (pt.count(k) - 1)*10
            for l in range(n):
                if pt[l] == k:
                    sum_tmp += moto[l]
            
            mp_tmp += abs(sum_tmp-take[k])

        if all_used == True:
            mp = min(mp_tmp, mp)
    print(mp)

main()

Solution (Python):
from statistics import mean, median,variance,stdev
import numpy as np
import sys
import math
import fractions
import itertools

def j(q):
    if q==1: print("Heisei")
    else:print("TBD")
    exit(0)


def ct(x,y):
    if (x>y):print("")
    elif (x<y): print("")
    else: print("")

def ip():
    return int(input())

def swap(s,pos):
    s[pos]-=1
    s[pos+1]+=1
    print(s)
    if pos == n-2: return s
    if s[pos+2] == 0: swap(s,pos+1)
    return s





n,a,b,c= (int(i) for i in input().split())  








l = []
for i in range(n):
    l.append(ip())
if a in l and b in l and c in l:
    print(0)
    exit(0)
l.sort()
req = [a, b,c]
m  =set()
set = [0 for i in range(n)]
set[0] = -1
while sum(set) < n*3:
    while sum(set) < n*3:
        d = 0
        set[d]+=1
        while set[d] == 4:
            set[d]=0
            d+=1
            set[d]+=1
        if 0 in set and 1 in set and 2 in set: break
    if sum(set) != n*3:
        s = [0,0,0,0]
        mi = 0
        for i in range(n):
            s[set[i]]+= l[i]
        for i in range(3):
            mi += abs(s[i] - req[i]) + (set.count(i)-1) * 10
        m.add(mi)
print(min(m))

Solution (Python):
import itertools
N, A, B, C = map(int, raw_input().split())
banboos = [int(raw_input()) for _ in range(N)]

INF = 10**9


def dfs(k, a, b, c):
  if k == N:
    return abs(a-A)+abs(b-B)+abs(c-C) - 30 if min(a, b, c) > 0 else INF
  ret0 = dfs(k+1, a, b, c)
  ret1 = dfs(k+1, a+banboos[k], b, c)+10
  ret2 = dfs(k+1, a, b+banboos[k],  c)+10
  ret3 = dfs(k+1, a, b, c+banboos[k])+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 dps(a,b,c,index):
  if index == N:
    return abs(a-A) + abs(b-B) + abs(c-C) if min(a,b,c) != 0 else float('infinity')
  
  take = L[index]
  mp1 = dps(a,b,c,index+1)
  mp2 = dps(a+take,b,c,index+1) + 10
  mp3 = dps(a,b+take,c,index+1) + 10
  mp4 = dps(a,b,c+take,index+1) + 10

  return min(mp1,mp2,mp3,mp4)


print(dps(0,0,0,0)-30)

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

ans = 100000000000000
for li in itertools.product(range(4), repeat=n):
  tmp = [[] for i in range(4)] 
  for i in range(len(li)):
    tmp[li[i]].append(l[i])
  if len(tmp[0])*len(tmp[1])*len(tmp[2]):
    ans = min(ans,(len(tmp[0])-1)*(10)+(len(tmp[1])-1)*(10)+(len(tmp[2])-1)*(10)+abs(sum(tmp[0])-a)+abs(sum(tmp[1])-b)+abs(sum(tmp[2])-c))
print(ans)

Solution (Python):
import itertools
n, a, b, c = map(int, input().split())
NOT_USED = 0
USED_A = 1
USED_B = 2
USED_C = 3

ans = 10000
l = [0] * n

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

for use in itertools.product([0, 1, 2, 3], repeat=n):
    cnt_a = 0
    cnt_b = 0
    cnt_c = 0
    len_a = 0
    len_b = 0
    len_c = 0

    for i in range(n):
        if use[i] == USED_A:
            cnt_a += 1
            len_a += l[i]

        if use[i] == USED_B:
            cnt_b += 1
            len_b += l[i]

        if use[i] == USED_C:
            cnt_c += 1
            len_c += l[i]

    if cnt_a == 0 or cnt_b == 0 or cnt_c == 0:
        continue
        
    ans = min(ans,
              10 * (cnt_a - 1) + abs(len_a - a) +
              10 * (cnt_b - 1) + abs(len_b - b) +
              10 * (cnt_c - 1) + abs(len_c - c))
print(ans)

Solution (Python):
import sys
from itertools import combinations, product


fin = sys.stdin.readline
N, A, B, C = [int(elem) for elem in fin().split()]
l_list = [int(fin()) for _ in range(N)]

min_mp = float('inf')
for flags in product(range(4), repeat=N):
    powers = [0] * 3
    num_items = [0] * 3
    for l, flag in zip(l_list, flags):
        if flag == 3:
            continue
        powers[flag] += l
        num_items[flag] += 1
    if not all(powers):
        continue
    mp = sum(
        abs(power - necessary_power) 
        for power, necessary_power in zip(powers, (A, B, C))
        ) + 10 * (sum(num_items) - 3)
    min_mp = min(mp, min_mp)
print(min_mp)

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

from itertools import permutations, combinations


def dellist(items, contents):
    items = items.copy()
    for rm in contents:
        
        items.remove(rm)
    return items


ans = []
L_temp = L.copy()

for n_g1 in range(1, N):
    for n_g2 in range(1, N - n_g1+1):
        for n_g3 in range(1, N - n_g1 - n_g2 + 1):
            
            L_temp = L.copy()
            for G1 in combinations(L_temp, n_g1):
                L_temp = L.copy()
                L_temp = dellist(L_temp, G1)
                
                for G2 in combinations(L_temp, n_g2):
                    L_temp = L.copy()
                    L_temp = dellist(L_temp, G1)
                    L_temp = dellist(L_temp, G2)
                    
                    for G3 in combinations(L_temp, n_g3):
                        
                        s1, s2, s3 = sum(G1), sum(G2), sum(G3)
                        syn = len(G1) + len(G2) + len(G3) - 3
                        ans.append(abs(s1 - A) + abs(s2 - B) +
                                   abs(s3 - C) + syn * 10)


print(min(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 = float('inf')


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 itertools
INF = 1e10

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

ans = INF
for prod in itertools.product('NABC', repeat=N):
    if not ('A' in prod and 'B' in prod and 'C' in prod):
        continue

    D = {'A': [], 'B': [], 'C': [], 'N': []}
    for i in range(N):
        D[prod[i]].append(L[i])

    tmp = max(len(D['A']) - 1, 0) * 10 + abs(sum(D['A']) - A)
    tmp += max(len(D['B']) - 1, 0) * 10 + abs(sum(D['B']) - B)
    tmp += max(len(D['C']) - 1, 0) * 10 + abs(sum(D['C']) - C)
    ans = min(ans, tmp)

print(ans)