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

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

ans = float("inf")
for i in range(4**n):
    cnt = Base_10_to_n(i,4)
    cnt = cnt.rjust(n,"0")

    num_a = cnt.count("1")
    num_b = cnt.count("2")
    num_c = cnt.count("3")

    if num_a >= 1 and num_b >= 1 and num_c >= 1:
        bb_a = [l[j] for j in range(n) if cnt[j] == "1"]
        bb_b = [l[j] for j in range(n) if cnt[j] == "2"]
        bb_c = [l[j] for j in range(n) if cnt[j] == "3"]

        temp = 10 * (len(bb_a) + len(bb_b) + len(bb_c) -3) + abs(a-sum(bb_a)) + abs(b-sum(bb_b)) + abs(c-sum(bb_c))

        ans = min([ans, temp])

print(ans)

Solution (Python):

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

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

inf = 10**9

def solve(aa, bb, cc, xs):
    if not xs:
        if min(aa,bb,cc) == 0:
           return inf
        else:
            return abs(a-aa) + abs(b-bb) + abs(c-cc) - 30

    x, *rest = xs
    res0 = solve(aa, bb, cc,     rest)
    res1 = solve(aa + x, bb, cc, rest) + 10
    res2 = solve(aa, bb + x, cc, rest) + 10
    res3 = solve(aa, bb, cc + x, rest) + 10

    return min(res0, res1, res2, res3)

print(solve(0,0,0,xs))

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 i in range(n)]
INF = 10 ** 9

def dfs(used, a, b, c):
    if used == n: 
        return abs(a - A) + abs(b - B) + abs(c - C) - 30 if min(a, b, c) > 0 else INF 
    
    ret0 = dfs(used+1, a, b, c) 
    ret1 = dfs(used+1, a + l[used], b, c) + 10 
    ret2 = dfs(used+1, a, b + l[used], c) + 10 
    ret3 = dfs(used+1, a, b, c + l[used]) + 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 base10to(x, y):  
    if int(x/y):                                                                       
        return base10to(int(x/y), y)+str(x%y)                                          
    return str(x%y)                                                                    
                                                                                       
ans = float("inf")                                                                     
                                                                                       
for i in range(4**n):                                                                  
    i_base4 = base10to(i, 4)                                                           
    while len(i_base4)<n:                                                              
        i_base4 = "0"+i_base4                                                          
    if ("0" not in i_base4) or ("1" not in i_base4) or ("2" not in i_base4):           
        continue                                                                       
    memo = [0, 0, 0]  
    fusion = -3  
    for j in range(n):                                                                 
        if i_base4[j]=="3":                                                            
            continue                                                                   
        fusion += 1                                                                    
        memo[int(i_base4[j])] += l[j]                                                  
    mp = abs(memo[0]-a) + abs(memo[1]-b) + abs(memo[2]-c) + fusion*10                  
    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(a,b,c,cnt):

    if cnt >= len(l):
        if a==0 or b==0 or c==0:
            return 10**10
        return abs(a-A)+abs(b-B)+abs(c-C)
    
    res0 = dfs(a,b,c,cnt+1)
    if a>0:
        res1 = dfs(a+l[cnt],        b, c       ,cnt+1) + 10
    else:
        res1 = dfs(a+l[cnt],        b, c       ,cnt+1)
    
    if b>0:
        res2 = dfs(       a, b+l[cnt], c       ,cnt+1) + 10
    else:
        res2 = dfs(       a, b+l[cnt], c       ,cnt+1)

    if c>0:
        res3 = dfs(       a,        b, c+l[cnt],cnt+1) + 10
    else:
        res3 = dfs(       a,        b, c+l[cnt],cnt+1)
    
    return min(res0,res1,res2,res3)

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

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

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

    if cnt >= len(l):
        if a==0 or b==0 or c==0:
            return 10**10
        return abs(a-A)+abs(b-B)+abs(c-C)
    
    res0 = dfs(a,b,c,cnt+1)
    res1 = dfs(a+l[cnt],        b, c       ,cnt+1) + 10
    res2 = dfs(       a, b+l[cnt], c       ,cnt+1) + 10
    res3 = dfs(       a,        b, c+l[cnt],cnt+1) + 10
    
    return min(res0,res1,res2,res3)

print(dfs(0,0,0,0)-30)

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

def f(n, a, b, c):
    if n == N:
        if a == 0 or b == 0 or c == 0:
            return 1000000
        else:
            return abs(a - A) + abs(b - B) + abs(c - C) - 30
    x = f(n + 1, a, b, c)
    y = f(n + 1, a + l[n], b, c) + 10
    z = f(n + 1, a, b + l[n], c) + 10
    w = f(n + 1, a, b, c + l[n]) +10
    return min(x, y, z, w)

print(f(0, 0, 0, 0))

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

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

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

Solution (Python):
n, a, b, c = [ int(v) for v in input().split() ]
num_list = [ int(input()) for i in range(n) ]
n_2 = 2 * n
def magic(inlist,barlength):
    return abs( barlength - sum(inlist) ) + ( 10 * ( len(inlist) - 1 ) )

ans = 10**9
for i in range(1,4**n):
    distribute_list = [ [] for i in range(3) ]
    s = bin(i)[2:].zfill(n_2)

    for j in range(0,n_2,2):
        p = int(s[j:j+2],2)
        if p != 0:
            distribute_list[p-1].append(num_list[j//2])
    if all(map(lambda x: x != [],distribute_list)):
        bar_sum = magic(distribute_list[0],a) + magic(distribute_list[1],b) + magic(distribute_list[2],c)
        ans = min(bar_sum,ans)
print(ans)

Solution (Python):
str_in = input().split()
num   = int(str_in[0])
tgt_a = int(str_in[1])
tgt_b = int(str_in[2])
tgt_c = int(str_in[3])

source = [int(input()) for i in range(num)]

mp = float('inf')

for i in range(1,2**num):

  sum_a = 0
  len_a = 0
  
  bin_i = format(i,'08b')  
  for t in range(1,num+1):
    sum_a += int(bin_i[-t])
    len_a += int(bin_i[-t]) * source[t-1]
    
  for j in range(1,2**num):
 
    sum_b = 0
    len_b = 0

    if (i & j) > 0:
      continue
    
    bin_j = format(j,'08b')
    for t in range(1,num+1):
      sum_b += int(bin_j[-t])
      len_b += int(bin_j[-t]) * source[t-1]

    for k in range(1,2**num):

      sum_c = 0
      len_c = 0
      
      if (i & k) > 0:
        continue
      elif(j & k) > 0:
        continue
        
      bin_k = format(k,'08b')        
      
      for t in range(1,num+1):
        sum_c += int(bin_k[-t])
        len_c += int(bin_k[-t]) * source[t-1]
        
      mp_try = abs(len_a - tgt_a) + abs(len_b - tgt_b) + abs(len_c - tgt_c) + (sum_a + sum_b + sum_c - 3) * 10 
      if(mp > mp_try):
        mp = mp_try

print (mp)

Solution (Python):
import itertools

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

ans = 100000000
for ns in itertools.product([0,1,2,3], repeat=N):
    t_arr = [0,0,0,0]
    cost = -30
    for n,l in zip(ns, l_arr):
        t_arr[n] += l
        if n < 3:
            cost += 10
    if t_arr[0] == 0 or t_arr[1] == 0 or t_arr[2] == 0:
        continue
    cost += abs(A-t_arr[0]) + abs(B-t_arr[1]) + abs(C-t_arr[2])
    ans = min(ans, cost)

print(ans)

Solution (Python):

import sys


def solve(N: int, A: int, B: int, C: int, l: "List[int]"):
    mn = 1e9
    for i in range(1 << N):
        for j in range(1 << N):
            n = []
            a = []
            b = []
            c = []
            for k in range(N):
                if (i >> k) % 2 == 0 and (j >> k) % 2 == 0:
                    n.append(k)
                if (i >> k) % 2 == 0 and (j >> k) % 2 == 1:
                    a.append(k)
                if (i >> k) % 2 == 1 and (j >> k) % 2 == 0:
                    b.append(k)
                if (i >> k) % 2 == 1 and (j >> k) % 2 == 1:
                    c.append(k)
            if len(a) == 0 or len(b) == 0 or len(c) == 0:
                continue
            else:
                m = 10 * (len(a) + len(b) + len(c) - 3) + abs(A - sum([l[aa] for aa in a])) + abs(B - sum([l[bb] for bb in b])) + abs(C - sum([l[cc] for cc in c]))
                if mn > m:
                    mn = m
    print(mn)
    return



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

import sys


def solve(N: int, A: int, B: int, C: int, l: "List[int]"):
    mn = 1e9
    for i in range(1 << N):
        for j in range(1 << N):
            n = []
            a = []
            b = []
            c = []
            for k in range(N):
                if (i >> k) % 2 == 0 and (j >> k) % 2 == 0:
                    n.append(k)
                if (i >> k) % 2 == 0 and (j >> k) % 2 == 1:
                    a.append(k)
                if (i >> k) % 2 == 1 and (j >> k) % 2 == 0:
                    b.append(k)
                if (i >> k) % 2 == 1 and (j >> k) % 2 == 1:
                    c.append(k)
            if len(a) == 0 or len(b) == 0 or len(c) == 0:
                continue
            else:
                m = 10 * (len(a) + len(b) + len(c) - 3) + abs(A - sum([l[aa] for aa in a])) + abs(B - sum([l[bb] for bb in b])) + abs(C - sum([l[cc] for cc in c]))
                if mn > m:
                    mn = m
    print(mn)
    return



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())
x=list(int(input()) for _ in range(n))

import itertools
import numpy as np

f=[0,1,2,3]

pt=list(itertools.product(f, repeat=n))

mpl=[]

for i in range(len(pt)):
	if (1 in pt[i]) and (2 in pt[i]) and (3 in pt[i]):
		rl1=0;rc1=0
		rl2=0;rc2=0
		rl3=0;rc3=0
		mp=0
		for j in range(n):
			if pt[i][j]==1:
				rl1+=x[j]
				rc1+=1
			if pt[i][j]==2:
				rl2+=x[j]
				rc2+=1
			if pt[i][j]==3:
				rl3+=x[j]
				rc3+=1
		mp=abs(rl1-a)+abs(rl2-b)+abs(rl3-c)
		if rc1>1:
			mp+=(rc1-1)*10
		if rc2>1:
			mp+=(rc2-1)*10
		if rc3>1:
			mp+=(rc3-1)*10
		mpl.append(mp)

print(min(mpl))

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


def solve(X):
    if not ("1" in X and "2" in X and "3" in X):
        return 1e10

    MS = [[L[i] for i in range(N) if X[i] == j] for j in "123"]
    ret = 1e10
    A_ans = []
    B_ans = []
    C_ans = []
    for i in range(3):
        A_ans.append(solve2(MS[i], A))
        B_ans.append(solve2(MS[i], B))
        C_ans.append(solve2(MS[i], C))
    ret = min(ret, A_ans[0] + B_ans[1] + C_ans[2])
    ret = min(ret, A_ans[0] + B_ans[2] + C_ans[1])
    ret = min(ret, A_ans[1] + B_ans[0] + C_ans[2])
    ret = min(ret, A_ans[1] + B_ans[2] + C_ans[0])
    ret = min(ret, A_ans[2] + B_ans[0] + C_ans[1])
    ret = min(ret, A_ans[2] + B_ans[1] + C_ans[0])
    return ret


def solve2(M, target):
    def rec(Y, target):
        
        if len(Y) == len(M):
            if len(set(Y)) == 1 and Y[0] == 0:
                return 1e10
            sum_M = sum([M[i] for i in range(len(M)) if Y[i] == 1])
            return abs(target - sum_M) + 10 * (Y.count(1) - 1)
        return min(rec(Y + [0], target), rec(Y + [1], target))
    ret = rec([], target)
    return ret


ans = 1e10


def rec(X):
    global ans
    if len(X) == N:
        ans = min(ans, solve(X))
        return
    rec(X + "1")
    rec(X + "2")
    rec(X + "3")


rec("")

print(ans)

Solution (Python):
def main():
    import itertools as it
    N,A,B,C = map(int,input().split())
    pre = [int(input()) for _ in range(N)]
    ss = max(pre)*N <= min(A,B,C)
    if N == 3:print(A-pre[0]+B-pre[1]+C-pre[2])
    else:
        m = [0,1,2,3,4,5,6,7]
        cpm = it.permutations(pre,N)
        ccb = list(it.combinations(m[:N], 3))
        minc = 99999999999999999
        for l in cpm:
            for d in ccb:
                cmp = (d[2] - 2) * 10
                if ss:
                    asum = (cmp+A+B+C-sum(l[:d[2]+1]))
                    minc = min(minc,asum)
                else:
                    asum = abs(A - sum(l[:d[0]+1]))
                    if cmp+asum >= minc: continue
                    bsum = abs(B - sum(l[d[0]+1:d[1]+1]))
                    if cmp+asum+bsum >= minc: continue
                    csum = abs(C - sum(l[d[1]+1:d[2]+1]))
                    tc = cmp+asum+bsum+csum
                    minc = min(minc,tc)
        print(minc)
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
    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:
        
        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**4

def bamboo_connect(i, a, b, c):
    if i == N:
        if all((a, b, c)):
            return abs(a-A) + abs(b-B) + abs(c-C) - 30
        else:
            return inf
    no_connect = bamboo_connect(i+1, a, b, c)
    choice_a = bamboo_connect(i+1, a+l[i], b, c) + 10
    choice_b = bamboo_connect(i+1, a, b+l[i], c) + 10
    choice_c = bamboo_connect(i+1, a, b, c+l[i]) + 10
    return min(no_connect, choice_a, choice_b, choice_c)

print(bamboo_connect(0, 0, 0, 0))

Solution (Python):




import copy as cp
inf = 10**9
def cost(li, length):
    if len(li)==0:
        return inf
    return (len(li)-1)*10 + abs(sum(li)-length)

def search_cost(ln,assignment,length):

    if len(ln)==0:
        costA = cost(assignment["A"], length[0])
        costB = cost(assignment["B"], length[1])
        costC = cost(assignment["C"], length[2])
        return costA + costB + costC
    val = ln[-1]
    candidate = []
    for take in assignment.keys():
        nextdic = cp.deepcopy(assignment)
        nextdic[take].append(val)
        cand = search_cost(ln[:-1],nextdic,length)
        candidate.append(cand)
    return min(candidate)
  
  

N, A, B, C =map(int,input().split())
length = [A,B,C]
ln = []
assignment = {"A":[],"B":[],"C":[],"D":[]}

for i in range(N):
    a = int(input())
    ln.append(a)
   
print(search_cost(ln,assignment,length))

Solution (Python):
minMP = 100000


def make_patterns(nowstr, count0, count1, count2, count3, rest):
    if count0 > N - 2:
        return
    if count1 > N - 2:
        return
    if count2 > N - 2:
        return
    if count3 > N - 3:
        return
    if rest == 0:
        if 0 == count0:
            return
        if 0 == count1:
            return
        if 0 == count2:
            return
        do_calc(list(nowstr), count0, count1, count2)
        return
    make_patterns(nowstr+"0", count0+1, count1, count2, count3, rest-1)
    make_patterns(nowstr+"1", count0, count1+1, count2, count3, rest-1)
    make_patterns(nowstr+"2", count0, count1, count2+1, count3, rest-1)
    make_patterns(nowstr+"3", count0, count1, count2, count3+1, rest-1)
    return


def do_calc(pattern_list, c0, c1, c2):
    global minMP
    
    tempA = 0
    tempB = 0
    tempC = 0
    tempcost = (c0+c1+c2-3)*10
    for i in range(N):
        num = int(pattern_list[i])
        if num == 0:
            tempA += L_list[i]
        if num == 1:
            tempB += L_list[i]
        if num == 2:
            tempC += L_list[i]
    tempcost += abs(A - tempA) + abs(B - tempB) + abs(C - tempC)

    if tempcost < minMP:
        minMP = tempcost


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

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


make_patterns("", 0, 0, 0, 0, N)

print(minMP)

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

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

inf = 10 ** 10

def mp(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
  mp0 = mp(i+1,a,b,c)
  mp1 = mp(i+1,a+l[i],b,c)+10
  mp2 = mp(i+1,a,b+l[i],c)+10
  mp3 = mp(i+1,a,b,c+l[i])+10
  return min(mp0,mp1,mp2,mp3)

print(mp(0,0,0,0))

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

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

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

Solution (Python):
N,A,B,C=map(int,input().split())
l=[int(input()) for i in range(N)]
odr=[4**i for i in range(N)][::-1]
minL=1145141919
for i in range(4**N):
    a=0
    b=0
    c=0
    count=0
    mod=i
    for j in range(N):
        idx,mod=divmod(mod,odr[j])
        if idx==1:
            a+=l[j]
            count+=10
        elif idx==2:
            b+=l[j]
            count+=10
        elif idx==3:
            c+=l[j]
            count+=10
    sumL = abs(a-A)+abs(b-B)+abs(c-C)+count-30
    minL = sumL if sumL<minL and min(a,b,c)>0 else minL
print(minL)

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

def dfs(cur, a, b, c):
    if cur == N:
        return abs(A - a) + abs(B - b) + abs(C - c) - 30 if min(a, b, c) > 0 else 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()))
L = [int(input()) for _ in range(N)]

assign = [0]*N

cost_min = 10000

while assign.count(3)<N:
    NL = [0]*4
    XL = [0]*4
    for i in range(N):
        NL[assign[i]] += 1
        XL[assign[i]] += L[i]

    if min(NL[:3])>=1:
        cost = (sum(NL[:3])-3)*10 + abs(A-XL[0]) + abs(B-XL[1]) + abs(C-XL[2])
        cost_min = min(cost_min, cost)

    for i in range(N):
        if assign[i]<3:
            assign[i] += 1
            for j in range(i):
                assign[j] = 0
            break

print(cost_min)

Solution (Python):
from collections import deque

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

stack = deque()
init_data = (0, [])
stack.append(init_data)

mp_results = []


def dfs():
    depth, curr_path = stack.pop()
    if depth == N:
        filter_a = 'A' not in curr_path
        filter_b = 'B' not in curr_path
        filter_c = 'C' not in curr_path
        if filter_a or filter_b or filter_c:
            return
        a_combi = []
        b_combi = []
        c_combi = []
        for idx, history in enumerate(curr_path):
            if history == 'A':
                a_combi.append(lll[idx])
            elif history == 'B':
                b_combi.append(lll[idx])
            elif history == 'C':
                c_combi.append(lll[idx])
        n_concat = len(a_combi)-1 + len(b_combi)-1 + len(c_combi)-1
        mp_used_for_concat = n_concat * 10
        mp_used_for_ext_shr = \
            abs(A-sum(a_combi)) + abs(B-sum(b_combi)) + abs(C-sum(c_combi))

        mp_results.append(mp_used_for_concat + mp_used_for_ext_shr)
    else:
        depth += 1
        stack.append((depth, curr_path + ['A']))
        stack.append((depth, curr_path + ['B']))
        stack.append((depth, curr_path + ['C']))
        stack.append((depth, curr_path + ['O']))


while stack:
    dfs()

print(min(mp_results))

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


def rec(i, x, y, z):
    if i >= n:
        return abs(a - x) + abs(b - y) + abs(c - z) if x * y * z > 0 else INF

    return min(rec(i + 1, x, y, z),
               rec(i + 1, x + l[i], y, z) + (10 if x > 0 else 0),
               rec(i + 1, x, y + l[i], z) + (10 if y > 0 else 0),
               rec(i + 1, x, y, z + l[i]) + (10 if z > 0 else 0))


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

Solution (Python):
from itertools import *

N,a,b,c = map(int,input().split())
L = [int(input()) for i in range(N)]

ans = float("inf")

products = product(range(4), repeat=N)

for prod in products :
    A = []
    B = []
    C = []
    
    for i,v in enumerate(prod) :
        if v == 1 :
            A.append(L[i])
        elif v == 2 :
            B.append(L[i])
        elif v == 3 :
            C.append(L[i])
    
    if len(A) * len(B) * len(C) == 0 :
        continue
    
    cost = 0
    
    for li in [A, B, C] :
        cost += (len(li)-1) * 10
    
    cost += abs(a-sum(A))
    cost += abs(b-sum(B))
    cost += abs(c-sum(C))
    
    ans = min(ans, cost)

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):
from itertools import product

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

L = list(int(input()) for i in range(N))

products = product(list(range(4)), repeat=N)

ans = float("inf")

for prod in products :
    A = []
    B = []
    C = []
    
    for i in range(len(prod)) :
        if prod[i] == 1 :
            A.append(L[i])
        elif prod[i] == 2 :
            B.append(L[i])
        elif prod[i] == 3 :
            C.append(L[i])
    if len(A) * len(B) * len(C) == 0 :
        continue
    
    cost = 0
    
    for li in [A, B, C] :
        cost += (len(li)-1) * 10
    
    cost += abs(a - sum(A))
    cost += abs(b - sum(B))
    cost += abs(c - sum(C))
    
    ans = min(ans, cost)

print(ans)

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


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

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

    ret1 = dfs(a, b, c, n + 1)
    ret2 = dfs(a + take_list[n], b, c, n + 1) + 10
    ret3 = dfs(a, b + take_list[n], c, n + 1) + 10
    ret4 = dfs(a, b, c + take_list[n], n + 1) + 10
    return min(ret1, ret2, ret3, ret4)

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

Solution (Python):
import itertools
n,a,b,c=map(int,input().split())
l = [int(input()) for i in range(n)]
zairyo = ('A','B','C','N')
zairyo_list=[0]*(4**n)
j=0
for i in itertools.product(zairyo, repeat=len(l)):
  zairyo_list[j]=i
  j+=1
mplist=[]
for zl in zairyo_list:
  if 'A' in zl and 'B' in zl and 'C' in zl:
    mp=0
    alist = [l[i] for i, e in enumerate(zl) if e == 'A']
    blist = [l[i] for i, e in enumerate(zl) if e == 'B']
    clist = [l[i] for i, e in enumerate(zl) if e == 'C']
    mp+=(len(alist)+len(blist)+len(clist)-3)*10
    akoho=sum(alist)
    bkoho=sum(blist)
    ckoho=sum(clist)
    mp+=abs(akoho-a)+abs(bkoho-b)+abs(ckoho-c)
    mplist.append(mp)
print(min(mplist))

Solution (Python):
N, A, B, C = map(int, input().split())
l = [int(input()) for i in range(N)]
def rec(i, a, b, c, d):
    if i == N:
        if a == 0 or b == 0 or c == 0:
            return float('inf')
        return abs(a-A)+abs(b-B)+abs(c-C)+d
    return min(rec(i+1,a+l[i],b,c,d+10),rec(i+1,a,b+l[i],c,d+10),rec(i+1,a,b,c+l[i],d+10),rec(i+1,a,b,c,d))
print(rec(0,0,0,0,-30))

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

def dfs(index, x, y, z):
    if index == n:
        return abs(a-x) + abs(b-y) + abs(c-z) - 30 if min(x, y, z) > 0 else INF

    cand1 = dfs(index+1, x+l[index], y, z) + 10
    cand2 = dfs(index+1, x, y+l[index], z) + 10
    cand3 = dfs(index+1, x, y, z+l[index]) + 10
    cand4 = dfs(index+1, x, y, z)

    return min(cand1, cand2, cand3, cand4)

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

Solution (Python):
import sys
import math
from collections import deque

sys.setrecursionlimit(4100000)


def inputs(num_of_input):
    ins = [input() for i in range(num_of_input)]
    return ins


def solve(N, A, B, C, inputs):
    bamboos = [int(i) for i in inputs]

    def cost(cursor, a, b, c):
        if cursor == N:
            if min(a, b, c) > 0:
                return abs(A-a) + abs(B-b) + abs(C-c) - 30
            else:
                return 1000000000

        t = bamboos[cursor]
        results = []
        results.append(cost(cursor+1, a, b, c))
        results.append(cost(cursor+1, a + t, b, c) + 10)
        results.append(cost(cursor+1, a, b + t, c) + 10)
        results.append(cost(cursor+1, a, b, c + t) + 10)

        return min(results)

    return cost(0, 0, 0, 0)


def string_to_int(string):
    return list(map(lambda x: int(x), string.split()))


if __name__ == "__main__":
    [N, A, B, C] = string_to_int(input())
    ret = solve(N, A, B, C, inputs(N))
    print(ret)

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

def dfs(cur,x,y,z):
  if cur==n and min(x,y,z)>0:
    return abs(a-x)+abs(b-y)+abs(c-z)-30
  elif cur==n:
    return 100000000000000000000
  ret0=dfs(cur+1,x,y,z)
  ret1=dfs(cur+1,x+l[cur],y,z)+10
  ret2=dfs(cur+1,x,y+l[cur],z)+10
  ret3=dfs(cur+1,x,y,z+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)]

ans = 0
initial_ans = True
for i in range(4**N):
    uses = [0] * (4)
    use_count = 0
    before_div = i
    for j in range(N):
        uses[before_div%4] += L[j]
        if before_div%4 != 0:
            use_count += 1
        before_div = before_div//4
    if uses[1] == 0 or uses[2] == 0 or uses[3] == 0:
        continue
    else:
        sum_mp = abs(A-uses[1]) + abs(B-uses[2]) + abs(C-uses[3]) + 10 * (use_count-3)
        if initial_ans:
            ans = sum_mp
            initial_ans = False
        else:
            ans = min(ans, sum_mp)
print(ans)

Solution (Python):
inf = float('inf')


def calc(mask):
    t = [0] * 3
    cnt = 0
    for j in range(n):
        idx = (mask >> (2 * j)) & 3
        if idx == 3:
            cnt += 1
            continue
        t[idx] += l[j]

    if 0 in t:
        return inf

    return sum(abs(tt - gg) for tt, gg in zip(t, goal)) + (n - 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


n, *goal = map(int, input().split())
l = [int(input()) for _ in range(n)]

print(dfs(0, 0))