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())
li = [int(input()) for _ in range(n)]

def searchans(count,a,b,c):
    if count == n:
        if not 0 in [a,b,c]:
            return abs(A-a) + abs(B-b) + abs(C-c) - 30
        else:
            return 10**100
    else:
        ret0 = searchans(count+1,a,b,c)
        ret1 = searchans(count+1,a+li[count],b,c) + 10
        ret2 = searchans(count+1,a,b+li[count],c) + 10
        ret3 = searchans(count+1,a,b,c+li[count]) + 10
        return min([ret0,ret1,ret2,ret3])

print(searchans(0,0,0,0))

Solution (Python):
try:
    from math import inf
except:
    inf = float("inf")
from collections import deque

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

q = deque()
q.append((0, -30, 0, 0, 0))
minmp = inf
while len(q):
    n, mp, a, b, c = q.pop()
    if n == N:
        if min(a, b, c) == 0:
            continue
        minmp = min(minmp, mp + abs(a - A) + abs(b - B) + abs(c - C))
    else:
        q.append((n + 1, mp, a, b, c))
        q.append((n + 1, mp + 10, a + ls[n + 1], b, c))
        q.append((n + 1, mp + 10, a, b + ls[n + 1], c))
        q.append((n + 1, mp + 10, a, b, c + ls[n + 1]))

print(minmp)

Solution (Python):
import itertools

N,A,B,C = map(int,input().split())
comb = [] 
L = []
for i in range(N):
  l = int(input())
  L.append(l)
  comb.append([0,1,2,3])

All_comb =  list(itertools.product(*comb))
min_MP = float('inf')
for i in range(len(All_comb)):
  tmp = All_comb[i]
  Non_material = []
  A_material = []
  B_material = []
  C_material = []
  for j in range(len(tmp)):
    if(tmp[j] == 0): Non_material.append(L[j])
    elif(tmp[j] == 1): A_material.append(L[j])
    elif(tmp[j] == 2): B_material.append(L[j])
    elif(tmp[j] == 3): C_material.append(L[j])
    a,b,c = len(A_material),len(B_material),len(C_material)
    if((a>=1)and(b>=1)and(c>=1)):
     sum_A,sum_B,sum_C = sum(A_material),sum(B_material),sum(C_material)
     MP = abs(A-sum_A)+abs(B-sum_B)+abs(C-sum_C) +10*(a+b+c-3)
     min_MP = min(MP,min_MP)

print(min_MP)

Solution (Python):
import numpy as np

N,A,B,C = map(int,input().split())
target = [A,B,C]
l = []


for _ in range(N):
    l.append(int(input()))


def calc2(v):
    res = 0
    for i in range(3):
        if len(v[i])==0:
            return np.inf
        res += 10*(len(v[i])-1) + abs(sum(v[i]) - target[i])
    return res

v=[[],[],[],[]]
def dfs2(depth=0):
    
    if depth == N:
        return calc2(v)
    
    ans = np.inf
    for i in range(4):
        
        
        v[i].append(l[depth])
        ans = min(ans,dfs2(depth+1))
        del v[i][-1]

    return ans

print(dfs2())

Solution (Python):




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

take = []
INF = 10 ** 18

for _ in range(N) : 
    tmp = int(input())
    take.append(tmp)
    
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
    
    ret0 = dfs(i+1, a, b, c)
    ret1 = dfs(i+1, a + take[i], b, c) + 10
    ret2 = dfs(i+1, a, b + take[i], c) + 10
    ret3 = dfs(i+1, a, b, c + take[i]) + 10
        
    return(min(ret0, ret1, ret2, ret3))
        
print(dfs(0, 0, 0, 0))

Solution (Python):
def int_raw():
    return int(raw_input())
 
def words_raw():
    return raw_input().split()
 
def ints_raw():
    return map(int, words_raw())

N, A, B, C = ints_raw()
l = [int_raw() for _ in range(N)]

INF = 1e10
def cost(idx, a, b, c):
	if idx == N:
		if min(a,b,c)  == 0:
			return INF
		else:
			return abs(a - A) + abs(b - B) + abs(c - C) - 30
	val1 = cost(idx + 1, a, b, c)
	val2 = cost(idx + 1, a + l[idx], b, c) + 10
	val3 = cost(idx + 1, a, b + l[idx], c) + 10
	val4 = cost(idx + 1, a, b, c + l[idx]) + 10
	return min(val1, val2, val3, val4)

print cost(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+7

def dfs(count,a,b,c):
	if count == N:
		if min(a,b,c) > 0:
			return abs(a-A)+abs(b-B)+abs(c-C)-30
		else:
			return INF
	non = dfs(count+1,a,b,c)
	useA = dfs(count+1,a+l[count],b,c)+10
	useB = dfs(count+1,a,b+l[count],c)+10
	useC = dfs(count+1,a,b,c+l[count])+10
	return min(non,useA,useB,useC)

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

Solution (Python):
import itertools
N,A,B,C = map(int,input().split())
L = []
Comb = [] 

for i in range(N):
  l = int(input())
  L.append(l)
  Comb.append([0,1,2,3])
all_Comb = list(itertools.product(*Comb))

min_MP = float('inf')
for i in range(len(all_Comb)):
 tmp = list(all_Comb[i])
 Non_material = []
 A_material = []
 B_material = []
 C_material = []
 for j in range(N):
   if(tmp[j] == 0): Non_material.append(L[j])
   elif(tmp[j] == 1):  A_material.append(L[j])
   elif(tmp[j] == 2):  B_material.append(L[j])
   elif(tmp[j] == 3):  C_material.append(L[j])
   a,b,c = len(A_material),len(B_material),len(C_material)
   if((a>=1)and(b>=1)and(c>=1)):
    sum_A,sum_B,sum_C = sum(A_material),sum(B_material),sum(C_material)
    MP = abs(A-sum_A) + abs(B-sum_B) + abs(C-sum_C) + 10*(a+b+c-3)
    min_MP = min(MP,min_MP)
 
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 cost(index, a,b,c):
    if index == N:
        return (abs(a-A) + abs(b-B) + abs(c-C) - 30) if min(a,b,c) > 0 else INF
    ret0 = cost(index+1, a,b,c)
    ret1 = cost(index+1, a+l[index],b,c) + 10
    ret2 = cost(index+1, a,b+l[index],c) + 10
    ret3 = cost(index+1, a,b,c+l[index]) + 10
    return min(ret0, ret1, ret2, ret3)

print(cost(0,0,0,0))

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

def base10to(n, b):
    if (int(n/b)):
        return base10to(int(n/b), b) + str(n%b)
    return str(n%b)

maxL = 4 ** N - 1
maxL = base10to(maxL,4)
ans = []
for i in range(1, 4 ** N + 1):
    n = 4 ** N - i
    n = base10to(n,4)
    if len(maxL) - len(n) > 0:
        tmp = ['0'] * (len(maxL) - len(n))
        tmp.extend(list(n))
    else:
        tmp = list(n)
    AA, BB, CC = A, B, C
    aaa = set(tmp)
    anss = 0
    if '1' not in aaa or '2' not in aaa or '3' not in aaa:
        continue
    for j, num in enumerate(tmp):
        if num == '1':
            if AA != A:
                anss += 10
            AA -= L[j]
        elif num == '2':
            if BB != B:
                anss += 10
            BB -= L[j]
        elif num == '3':
            if CC != C:
                anss += 10
            CC -= L[j]
    ans.append(anss + abs(AA) + abs(BB) + abs(CC))
print(min(ans))

Solution (Python):
N,A,B,C = map(int, input().split())
ll = [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+ll[cur],b,c)+10 
    ret2 = dfs(cur+1, a,b+ll[cur],c)+10 
    ret3 = dfs(cur+1, a,b,c+ll[cur])+10 
    return min(ret0,ret1,ret2,ret3)
print(dfs(0,0,0,0))

Solution (Python):
n,a,b,c=map(int,input().split())
l=[int(input()) for i in range(n)]
def nsin(X,y):
    if(int(X/y)):
        return nsin(int(X/y),y)+str(X%y)
    return str(X%y)

cost=10000
for i in range(3**n):
    g=list(nsin(i,3).zfill(n))
    if len(set(g))!=3:
        continue
    la=[]
    lb=[]
    lc=[]
    for j in range(n):        
        if g[j]=="0":
            la.append(l[j])
        elif g[j]=="1":
            lb.append(l[j])
        elif g[j]=="2":
            lc.append(l[j])
    costa=10000
    costb=10000
    costc=10000
    for j in range(1,2**len(la)):
        ga=list(nsin(j,2).zfill(len(la)))
        A=0
        cont=-1
        for k in range(len(la)):
            if ga[k]=="1":
                A+=la[k]
                cont+=1
        tmp=cont*10+abs(A-a)
        if tmp<costa:
            costa=tmp
    for j in range(1,2**len(lb)):
        gb=list(nsin(j,2).zfill(len(lb)))
        B=0
        cont=-1
        for k in range(len(lb)):
            if gb[k]=="1":
                B+=lb[k]
                cont+=1
        tmp=cont*10+abs(B-b)
        if tmp<costb:
            costb=tmp
    for j in range(1,2**len(lc)):
        gc=list(nsin(j,2).zfill(len(lc)))
        C=0
        cont=-1
        for k in range(len(lc)):
            if gc[k]=="1":
                C+=lc[k]
                cont+=1
        tmp=cont*10+abs(C-c)
        if tmp<costc:
            costc=tmp
    if cost>(costa+costb+costc):
        cost=costa+costb+costc
print(cost)

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

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


def dfs(idx, a, b, c):

    if idx == N:
        if a == 0 or b == 0 or c == 0:
            return 1e9  

        return abs(A - a) + abs(B - b) + abs(C - c)

    x = dfs(idx + 1, a, b, c)
    y = dfs(idx + 1, a + L[idx], b, c) + 10*(a != 0)
    z = dfs(idx + 1, a, b + L[idx], c) + 10*(b != 0)
    w = dfs(idx + 1, a, b, c + L[idx]) + 10*(c != 0)
    return min(x, y, z, w)


ans = dfs(0, 0, 0, 0)
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())
takelist = [a,b,c]

li = [int(input()) for _ in range(n)]
li2 = [0 for _ in range(n)]
min = 10**100

def movelist(li,n):
    for i in range(n):
        if li[i] != 3:
            li[i] += 1
            break
        else:
            li[i] = 0

for i in range(4**n):
    li3 = [0,0,0,0]
    sum = 0
    for j in range(n):
        if li3[li2[j]] != 0 and li2[j] != 3:
            sum += 10
        li3[li2[j]] += li[j]
    if li3[0] != 0 and li3[1] != 0 and li3[2] != 0:
        for k in range(3):
            sum += abs(li3[k] - takelist[k])
        if sum < min:
            min = sum
    movelist(li2,n)

print(min)

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):
count = {}
count["A"] = 0
count["B"] = 0
count["C"] = 0
a = b = c = 0
N,A,B,C = map(int,input().split())
L = []
for _ in range(N):
    L.append(int(input()))

def score():
    joinNum = 0
    joinNum += max(0,count["A"]-1)
    joinNum += max(0,count["B"]-1)
    joinNum += max(0,count["C"]-1)
    scoreValue = 10 * joinNum + abs(A-a) + abs(B-b) + abs(C-c)
    return(scoreValue)
answer = 1000*3
for i in range(pow(4,N)):
    count["A"] = 0
    count["B"] = 0
    count["C"] = 0
    a = b = c = 0
    digits=[]
    for j in range(N):
        digit = (i%pow(4,j+1))//pow(4,j)
        digits.append(digit)
        if digit == 0:
            a += L[j]
            count["A"] += 1
        elif digit == 1:
            b += L[j]
            count["B"] += 1
        else:
            if digit == 2:
                c += L[j]
                count["C"] += 1
    if count["A"]*count["B"]*count["C"] == 0: continue
    candidate = score()
    if candidate < answer:
        
        answer = candidate
print(answer)

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
  else:  
    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(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
  else:  
    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 bamboo(count,a,b,c):
  if count==N:
    if min(a,b,c)>0:
      return abs(a-A)+abs(b-B)+abs(c-C)-30
    else:
      return INF
  rot0=bamboo(count+1,a,b,c)
  rot1=bamboo(count+1,a+l[count],b,c)+10
  rot2=bamboo(count+1,a,b+l[count],c)+10
  rot3=bamboo(count+1,a,b,c+l[count])+10
  return min(rot0,rot1,rot2,rot3)
print(bamboo(0,0,0,0))

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

def dfs(idx, a, b, c):
    if idx == N:
        if a == 0 or b == 0 or c == 0:
            return 10**9
        return abs(A - a) + abs(B - b) + abs(C - c)

    x = dfs(idx + 1, a, b, c)
    y = dfs(idx + 1, a + L[idx], b, c) + 10*(a != 0)
    z = dfs(idx + 1, a, b + L[idx], c) + 10*(b != 0)
    w = dfs(idx + 1, a, b, c + L[idx]) + 10*(c != 0)
    
    
    return min(x, y, z, w)

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

Solution (Python):
import itertools

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

use_flag = ['0', '1', '2', '-1']
use_list = list(itertools.product(use_flag, repeat=len(length_list)))

costs = []

for ls in use_list:
    if len(set(ls)&{'0','1', '2'}) == 3:
        
        A_diff = 0
        B_diff = 0
        C_diff = 0
        for length, use_for in zip(length_list, list(ls)):
            
            if use_for == '0':
                A_diff += length
                
            elif use_for == '1':
                B_diff += length
                
            elif use_for == '2':
                C_diff += length
                
            
        costs.append(abs(A-A_diff) + abs(B-B_diff) + abs(C-C_diff) + 10*max(ls.count('0') - 1, 0) + 10*max(ls.count('1') - 1, 0) + 10*max(ls.count('2') - 1, 0))
print(min(costs))

Solution (Python):
N,A,B,C = map(int,input().strip().split())
ls = [int(input()) for i in range(0,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 + ls[cur],b,c) + 10
    ret2 = dfs(cur + 1,a,b + ls[cur],c) + 10
    ret3 = dfs(cur + 1,a,b,c + ls[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(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(count, n_1, n_2, n_3, z):
    if (count == n):
        return abs(a-n_1) + abs(b-n_2) + abs(c-n_3) + z * 10 if (n_1*n_2*n_3 != 0) else 10**9
    r0 = dfs(count+1, n_1, n_2, n_3, z)
    r1 = dfs(count+1, n_1+d[count], n_2, n_3, z+1)
    r2 = dfs(count+1, n_1, n_2+d[count], n_3, z+1)
    r3 = dfs(count+1, n_1, n_2, n_3+d[count], z+1)
    return min(r0, r1, r2, r3)
n, a, b, c = map(int, input().split())
d = []
for i in range(n):
    d.append(int(input()))


print(dfs(0,0,0,0, -3))

Solution (Python):
N,A,B,C = map(int,input().split())
l = [int(input()) for _ in range(N)]
ans = float('inf')
def dfs(d=0,a=0,b=0,c=0,mp=0):
    if d == N:
        mp = mp + abs(A-a) + abs(B-b) + abs(C-c) - 30
        global ans
        if a*b*c > 0 and mp < ans:
            ans = mp
        return
    dfs(d+1,a,b,c,mp)
    dfs(d+1,a+l[d],b,c,mp+10)
    dfs(d+1,a,b+l[d],c,mp+10)
    dfs(d+1,a,b,c+l[d],mp+10)

dfs()
print(ans)

Solution (Python):



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

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

def dfs(cur, a, b, c) : 
    if cur == N : 
        return abs(a-A)+abs(b-B)+abs(c-C)-30 if min(a, b, c) > 0 else 10000
    ret0 = dfs(cur+1, a, b, c)
    ret1 = dfs(cur+1, a+list[cur], b, c) +10
    ret2 = dfs(cur+1, a, b+list[cur], c) +10
    ret3 = dfs(cur+1, a, b, c+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 i in range(n)]
def dfs(cur, q, w, e):
    if cur == n:
        return abs(a-q)+abs(b-w)+abs(c-e)-30 if min(q, w, e) != 0 else 10**18
    return min(
        dfs(cur+1, q+l[cur], w, e)+10,
        dfs(cur+1, q, w+l[cur], e)+10,
        dfs(cur+1, q, w, e+l[cur])+10,
        dfs(cur+1, q, w, e),
    )
print(dfs(0, 0, 0, 0))

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)


N, A, B, C = map(int, input().split(" "))
bamboos = [int(input()) for _ in range(N)]
ans = float("inf")
for i in range(4**N):
    now = Base_10_to_n(i, 4).zfill(N)
    length = [0, 0, 0, 0]
    cost = 0
    for j, c in enumerate(now):
        for k, num in enumerate("0123"):
            if c == num:
                length[k] += bamboos[j]
                break
    if length[0]*length[1]*length[2] == 0:
        continue
    cost += 10 * (now.count('0')-1)
    cost += 10 * (now.count('1')-1)
    cost += 10 * (now.count('2')-1)
    cost += abs(A-length[0])
    cost += abs(B-length[1])
    cost += abs(C-length[2])
    ans = min(cost, ans)
print(ans)

Solution (Python):


import sys

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

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

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

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

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 INF

    res1 = dfs(depth + 1, a, b, c)
    res2 = dfs(depth + 1, a + l[depth], b, c) + 10
    res3 = dfs(depth + 1, a, b + l[depth], c) + 10
    res4 = dfs(depth + 1, a, b, c + l[depth]) + 10

    return min(res1, res2, res3, res4)

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

Solution (Python):

import sys

BAMB_NUM = 0
BAMB_A = 0
BAMB_B = 0
BAMB_C = 0
BAMB_LIST = []

def calcMP(idx, lenA, lenB, lenC):
    """
    calculate MP.
    """
    
    if (idx == BAMB_NUM):
        return abs(lenA - BAMB_A) + abs(lenB - BAMB_B) + abs(lenC - BAMB_C) - 30 if min(lenA, lenB, lenC) > 0 else sys.maxsize
    
    
    mp1 = calcMP(idx + 1, lenA, lenB, lenC)
    
    mp2 = calcMP(idx + 1, lenA + BAMB_LIST[idx], lenB, lenC) + 10
    mp3 = calcMP(idx + 1, lenA, lenB + BAMB_LIST[idx], lenC) + 10
    mp4 = calcMP(idx + 1, lenA, lenB, lenC + BAMB_LIST[idx]) + 10

    return min(mp1, mp2, mp3, mp4)

if __name__ == "__main__":
    """
    http://drken1215.hatenablog.com/entry/2019/02/24/224100
    """
    
    BAMB_NUM, BAMB_A, BAMB_B, BAMB_C = map(lambda x: int(x), input().split())
    
    for bamb in range(BAMB_NUM):
        BAMB_LIST.append(int(input()))

    mp = calcMP(0, 0, 0, 0)
    print(mp)

Solution (Python):



def solve(n, a, b, c, l):

    target = [a, b, c]

    INF = 10 ** 4
    ans = INF
    for i in range(2 ** (2 * n)):
        counts = [0] * 3
        lengths = [0] * 3
        x = i
        for j in range(n):
            g = x % 4
            if g < 3:
                counts[g] += 1
                lengths[g] += l[j]
            x = x // 4
        r = 0
        for g in range(3):
            if counts[g] == 0:
                r = INF
                break
            r += abs(target[g] - lengths[g])
        r += (sum(counts) - 3) * 10
        ans = min(ans, r)

    return ans


def main():
    n, a, b, c = input().split()
    n = int(n)
    a = int(a)
    b = int(b)
    c = int(c)
    l = []
    for _ in range(n):
        li = input()
        li = int(li)
        l.append(li)

    print(solve(n, a, b, c, l))


if __name__ == '__main__':
    main()

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 
    
    
    
    
    else:
        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 func(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 = func(cur+1,a,b,c)
	ret1 = func(cur+1, a+l[cur],b,c) + 10
	ret2 = func(cur+1, a,b+l[cur],c) + 10
	ret3 = func(cur+1, a,b,c+l[cur]) + 10
	
	return min(ret0, ret1, ret2, ret3)

print(func(0,0,0,0))

Solution (Python):

import sys


def solve(N: int, A: int, B: int, C: int, l: "List[int]"):
    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 10e9
            )
        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))



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word

    tokens = iterate_tokens()
    N = int(next(tokens))  
    A = int(next(tokens))  
    B = int(next(tokens))  
    C = int(next(tokens))  
    l = [int(next(tokens)) for _ in range(N)]  
    solve(N, A, B, C, l)


if __name__ == "__main__":
    main()

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

def dfs(i, a, b, c):
    if i == n:
        if min(a, b, c) > 0:
            return abs(a - A) + abs(b - B) + abs(c - C) - 30
        else:
            return inf
        
    res0 = dfs(i + 1, a, b, c)
    res1 = dfs(i + 1, a + L[i], b, c) + 10
    res2 = dfs(i + 1, a, b + L[i], c) + 10
    res3 = dfs(i + 1, a, b, c + L[i]) + 10
    
    return min(res0, res1, res2, res3)

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

Solution (Python):
N, A, B, C = map(int, input().split())
l = [int(input()) for _ in range(N)]
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
    retNO = dfs(cur + 1, a, b, c)
    retA = dfs(cur + 1, a + l[cur], b, c) + 10
    retB = dfs(cur + 1, a, b + l[cur], c) + 10
    retC = dfs(cur + 1, a, b, c + l[cur]) + 10
    return min(retNO, retA, retB, retC)


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

Solution (Python):

from collections import defaultdict
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return list(sys.stdin.readline())
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
mod = 1000000007






def dfs(n,k,nu):
    global ans
    if n == 0:
        d = [0,0,0]
        m = 0
        for i in range(nu):
            if k[i] < 3:
                if d[k[i]] != 0:
                    m += 10
                d[k[i]] += l[i]
        if d[0]*d[1]*d[2] != 0:
            m += abs(a-d[0])+abs(b-d[1])+abs(c-d[2])
            ans = min(ans, m)
    else:
        for i in range(4):
            dfs(n-1,k+[i],nu)
n,a,b,c = LI()
l = IR(n)
li = []
ans = float("inf")
dfs(n,[],n)
print(ans)

"""
a,b,q = LI()
s = IR(a)
t = IR(b)
for i in range(q):
    x = I()
    c = bisect.bisect_left(s,x)
    ans = float("inf")
    if c == a:
        k = abs(x-s[-1])
        y = s[-1]
        d = bisect.bisect_left(t,y)
        if d == b:
            k += abs(y-t[-1])
        else:
            if abs(y-t[d-1]) < abs(y-t[d]):
                k += abs(y-t[d-1])
            else:
                k += abs(y-t[d])
        ans = min(k,ans)
    else:
        k = abs(x-s[c-1])
        y = s[c-1]
        d = bisect.bisect_left(t,y)
        if d == b:
            k += abs(y-t[-1])
        else:
            if abs(y-t[d-1]) < abs(y-t[d]):
                k += abs(y-t[d-1])
            else:
                k += abs(y-t[d])
        ans = min(k,ans)
        k = abs(x-s[c])
        y = s[c]
        d = bisect.bisect_left(t,y)
        if d == b:
            k += abs(y-t[-1])
        else:
            if abs(y-t[d-1]) < abs(y-t[d]):
                k += abs(y-t[d-1])
            else:
                k += abs(y-t[d])
        ans = min(k,ans)
    c = bisect.bisect_left(t,x)
    if c == b:
        k = abs(x-t[-1])
        y = t[-1]
        d = bisect.bisect_left(s,y)
        if d == a:
            k += abs(y-s[-1])
        else:
            if abs(y-s[d-1]) < abs(y-s[d]):
                k += abs(y-s[d-1])
            else:
                k += abs(y-s[d])
        ans = min(k,ans)
    else:
        k = abs(x-t[c-1])
        y = t[c-1]
        d = bisect.bisect_left(s,y)
        if d == a:
            k += abs(y-s[-1])
        else:
            if abs(y-s[d-1]) < abs(y-s[d]):
                k += abs(y-s[d-1])
            else:
                k += abs(y-s[d])
        ans = min(k,ans)
        k = abs(x-t[c])
        y = t[c]
        d = bisect.bisect_left(s,y)
        if d == a:
            k += abs(y-s[-1])
        else:
            if abs(y-s[d-1]) < abs(y-s[d]):
                k += abs(y-s[d-1])
            else:
                k += abs(y-s[d])
        ans = min(k,ans)
    print(ans)
"""

Solution (Python):

from collections import defaultdict
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return list(sys.stdin.readline())
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
mod = 1000000007






def dfs(n,k):
    if n == 0:
        li.append(k)
    else:
        for i in range(4):
            dfs(n-1,k+[i])
n,a,b,c = LI()
l = IR(n)
li = []
dfs(n,[])
ans = float("inf")
for k in li:
    d = [0,0,0]
    m = 0
    for i in range(n):
        if k[i] < 3:
            if d[k[i]] != 0:
                m += 10
            d[k[i]] += l[i]
    if d[0]*d[1]*d[2] == 0:continue
    m += abs(a-d[0])+abs(b-d[1])+abs(c-d[2])
    ans = min(ans, m)
print(ans)

"""
a,b,q = LI()
s = IR(a)
t = IR(b)
for i in range(q):
    x = I()
    c = bisect.bisect_left(s,x)
    ans = float("inf")
    if c == a:
        k = abs(x-s[-1])
        y = s[-1]
        d = bisect.bisect_left(t,y)
        if d == b:
            k += abs(y-t[-1])
        else:
            if abs(y-t[d-1]) < abs(y-t[d]):
                k += abs(y-t[d-1])
            else:
                k += abs(y-t[d])
        ans = min(k,ans)
    else:
        k = abs(x-s[c-1])
        y = s[c-1]
        d = bisect.bisect_left(t,y)
        if d == b:
            k += abs(y-t[-1])
        else:
            if abs(y-t[d-1]) < abs(y-t[d]):
                k += abs(y-t[d-1])
            else:
                k += abs(y-t[d])
        ans = min(k,ans)
        k = abs(x-s[c])
        y = s[c]
        d = bisect.bisect_left(t,y)
        if d == b:
            k += abs(y-t[-1])
        else:
            if abs(y-t[d-1]) < abs(y-t[d]):
                k += abs(y-t[d-1])
            else:
                k += abs(y-t[d])
        ans = min(k,ans)
    c = bisect.bisect_left(t,x)
    if c == b:
        k = abs(x-t[-1])
        y = t[-1]
        d = bisect.bisect_left(s,y)
        if d == a:
            k += abs(y-s[-1])
        else:
            if abs(y-s[d-1]) < abs(y-s[d]):
                k += abs(y-s[d-1])
            else:
                k += abs(y-s[d])
        ans = min(k,ans)
    else:
        k = abs(x-t[c-1])
        y = t[c-1]
        d = bisect.bisect_left(s,y)
        if d == a:
            k += abs(y-s[-1])
        else:
            if abs(y-s[d-1]) < abs(y-s[d]):
                k += abs(y-s[d-1])
            else:
                k += abs(y-s[d])
        ans = min(k,ans)
        k = abs(x-t[c])
        y = t[c]
        d = bisect.bisect_left(s,y)
        if d == a:
            k += abs(y-s[-1])
        else:
            if abs(y-s[d-1]) < abs(y-s[d]):
                k += abs(y-s[d-1])
            else:
                k += abs(y-s[d])
        ans = min(k,ans)
    print(ans)
"""