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):
nabc = input().split()
n = int(nabc[0])
a = int(nabc[1])
b = int(nabc[2])
c = int(nabc[3])
llist = [int(input()) for i in range(n)]


def make_select(idx):
    select = {'a': [], 'b': [], 'c': []}

    for i in range(n):
        bits = (idx>>(i*2)) & 3
        if bits == 1:
            select['a'].append(i)
        if bits == 2:
            select['b'].append(i)
        if bits == 3:
            select['c'].append(i)

    if not select['a'] or not select['b'] or not select['c']:
        return None
    return select


minmp = 10**32


for i in range(4**n):
    select = make_select(i)
    if not select:
        continue

    asum = bsum = csum = 0

    for idx in select['a']:
        asum += llist[idx]
    for idx in select['b']:
        bsum += llist[idx]
    for idx in select['c']:
        csum += llist[idx]

    mp = abs(asum - a) + abs(bsum - b) + abs(csum - c)
    mp += (len(select['a']) - 1) * 10
    mp += (len(select['b']) - 1) * 10
    mp += (len(select['c']) - 1) * 10

    minmp = min(minmp, mp)

print(minmp)

Solution (Python):
nabc = input().split()
n = int(nabc[0])
a = int(nabc[1])
b = int(nabc[2])
c = int(nabc[3])
llist = [int(input()) for i in range(n)]


def make_select(idx):
    select = {'a': [], 'b': [], 'c': []}

    for i in range(n):
        bits = (idx>>(i*2)) & 3
        if bits == 1:
            select['a'].append(i)
        if bits == 2:
            select['b'].append(i)
        if bits == 3:
            select['c'].append(i)

    if not select['a'] or not select['b'] or not select['c']:
        return None
    return select


minmp = 10**32


for i in range(4**n):
    select = make_select(i)
    if not select:
        continue

    asum = bsum = csum = 0

    for idx in select['a']:
        asum += llist[idx]
    for idx in select['b']:
        bsum += llist[idx]
    for idx in select['c']:
        csum += llist[idx]

    mp = abs(asum - a) + abs(bsum - b) + abs(csum - c)
    mp += (len(select['a']) - 1) * 10
    mp += (len(select['b']) - 1) * 10
    mp += (len(select['c']) - 1) * 10

    minmp = min(minmp, mp)

print(minmp)

Solution (Python):
N,A,B,C = map(int, input().split())
l = [0]*N
for i in range(N):
    l[i] = int(input())
def dfs(i,a,b,c):
    if i == N:
        if a==0 or b==0 or c==0:
            return 100000
        else:
            return abs(A-a)+abs(B-b)+abs(C-c)-30
    else:
         return min(dfs(i+1,a+l[i],b,c)+10, dfs(i+1,a,b+l[i],c)+10, dfs(i+1,a,b,c+l[i])+10, dfs(i+1,a,b,c))

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

Solution (Python):
import copy

nabc = input().split()
n = int(nabc[0])
a = int(nabc[1])
b = int(nabc[2])
c = int(nabc[3])
llist = [int(input()) for i in range(n)]


def calc_minmp(lenlist, vals, validx):
    minmp = 9999999999999999999999
    minbit = None

    for i in range(1<<len(lenlist)):
        count = totallen = 0
        for j in range(len(lenlist)):
            if (i>>j) & 1:
                count += 1
                totallen += lenlist[j]
        if count <= 0 or len(lenlist) - (2 - validx) < count:
            continue
        mp = (count - 1) * 10 + abs(totallen - vals[validx])
        if mp <= minmp:
            minmp = mp
            minbit = i

    for i in range(len(lenlist)):
        if minbit & (1<<i):
            lenlist[i] = None
    for i in reversed(range(len(lenlist))):
        if lenlist[i] is None:
            del lenlist[i]

    return minmp


def calc_minmp_for_vals(lenlist, vals, old_minmp):
    lenlist = copy.copy(lenlist)
    mp = calc_minmp(lenlist, vals, 0)
    mp += calc_minmp(lenlist, vals, 1)
    mp += calc_minmp(lenlist, vals, 2)
    return min(mp, old_minmp)


mp = calc_minmp_for_vals(llist, [a, b, c], 99999999999999999999)
mp = calc_minmp_for_vals(llist, [a, c, b], mp)
mp = calc_minmp_for_vals(llist, [b, a, c], mp)
mp = calc_minmp_for_vals(llist, [b, c, a], mp)
mp = calc_minmp_for_vals(llist, [c, a, b], mp)
mp = calc_minmp_for_vals(llist, [c, b, a], mp)

print(mp)

Solution (Python):
from itertools import product

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

mp = {}
for ptn in product([0, 1, 2, 3], repeat=N):
    a, b, c = 0, 0, 0
    x = 0
    for i in range(N):
        if ptn[i] == 1:
            if a != 0:
                x += 10
            a += l[i]
        elif ptn[i] == 2:
            if b != 0:
                x += 10
            b += l[i]
        elif ptn[i] == 3:
            if c != 0:
                x += 10
            c += l[i]
    if a * b * c == 0:
        continue
    mp[ptn] = abs(A - a) + abs(B - b) + abs(C - c) + x
print(min(mp.values()))

Solution (Python):
import collections
from copy import deepcopy

n,a,b,c=map(int,input().split())
arr=[int(input()) for _ in range(n)]
l=collections.deque()
l.append([[],[],[]])
cnt=0

while 1:
  if cnt==n:
    break
  base=l.popleft()
  if len(base[0])==0 and len(base[1])==0 and len(base[2])==0:
    l.append([[arr[cnt]],[],[]])
    l.append([[],[arr[cnt]],[]])
    l.append([[],[],[arr[cnt]]])
    l.append([[],[],[]])
    cnt+=1
  else:
    tmp1=deepcopy(base)
    tmp1[0].append(arr[cnt])
    l.append(tmp1)
    tmp2=deepcopy(base)
    tmp2[1].append(arr[cnt])
    l.append(tmp2)
    tmp3=deepcopy(base)
    tmp3[2].append(arr[cnt])
    l.append(tmp3)
    tmp4=deepcopy(base)
    l.append(tmp4)

mincost=10**9
for bl in l:
  if len(bl[0])==0 or len(bl[1])==0 or len(bl[2])==0:
    continue
  else:
    tmpcost=abs(sum(bl[0])-a)+abs(sum(bl[1])-b)+abs(sum(bl[2])-c)+10*(len(bl[0])-1+len(bl[1])-1+len(bl[2])-1)
    if mincost>tmpcost:
      mincost=tmpcost
print(mincost)

Solution (Python):
N,A,B,C=map(int,input().split())
l_list=[]
for i in range(N):
    l_list.append(int(input()))
def f(i,a,b,c):
    if i==N:
        if a*b*c==0:
            return 10**10
        else:
            return abs(a-A)+abs(b-B)+abs(c-C)-30
    else:
        addno=f(i+1,a,b,c)
        addA=f(i+1,a+l_list[i],b,c)+10
        addB=f(i+1,a,b+l_list[i],c)+10
        addC=f(i+1,a,b,c+l_list[i])+10
        return(min(addno,addA,addB,addC))
print(f(0,0,0,0))

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

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

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

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

def dfs(num, a, b, c):
    if N == num:
        return abs(A-a)+abs(B-b)+abs(C-c) - 30 if min(a,b,c)>=1 else 10**10
    ret = []
    ret.append(dfs(num+1, a, b, c))
    ret.append(dfs(num+1, a+l[num], b, c)+10)
    ret.append(dfs(num+1, a, b+l[num], c)+10)
    ret.append(dfs(num+1, a, b, c+l[num])+10)
    return min(ret)

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 = []
INF = float('inf')
for i in range(N):
    L.append(int(input()))


def dfs(k, a, b, c):
    if k == N:
        if min(a, b, c) == 0:
            return INF
        else:
            return abs(A - a) + abs(B - b) + abs(C - c) - 30
    else:
        d0 = dfs(k + 1, a, b, c)
        d1 = dfs(k + 1, a + L[k], b, c) + 10
        d2 = dfs(k + 1, a, b + L[k], c) + 10
        d3 = dfs(k + 1, a, b, c + L[k]) + 10
        return min(d0, d1, d2, d3)


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

Solution (Python):
def func(n, a, b, c, l):
    
    
    
    def search(now, cost):
        if len(now) == len(l):
            _a = []
            _b = []
            _c = []
            for i in range(len(now)):
                if now[i] == 0:
                    _a.append(l[i])
                elif now[i] == 1:
                    _b.append(l[i])
                elif now[i] == 2:
                    _c.append(l[i])
            if not _a or not _b or not _c:
                return 10**10
            _cost = abs(a - sum(_a)) + abs(b - sum(_b)) + abs(c - sum(_c))
            _cost += (len(_a) - 1 + len(_b) - 1 + len(_c) - 1) * 10
            return _cost
        else:  
            for i in range(4):
                cost = min(cost, search(now+[i], cost))
            return cost
    cost = search([], 10**10)
    return cost

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


print(func(n, a, b, c, l))
"""
print(func(5, 80, 90, 100, [21, 30, 40, 80, 98]))
print(func(8, 80, 90, 100, [80, 80, 80, 90, 90, 90, 100, 100]))
print(func(5, 100, 800, 1000, [300, 333, 400, 444, 500, 555, 600, 666]))
"""

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


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

def dcm(cou,a,b,c):
    if cou == N:
        if min(a,b,c)>0:
            return abs(A-a)+abs(B-b)+abs(C-c)-30
        else:
            return INF
    
    case0 = dcm(cou+1,a,b,c)
    case1 = dcm(cou+1,a+l[cou],b,c)+10
    case2 = dcm(cou+1,a,b+l[cou],c)+10
    case3 = dcm(cou+1,a,b,c+l[cou])+10
    
    return min(case0,case1,case2,case3)
        
print(dcm(0,0,0,0))

Solution (Python):
from itertools import product

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

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

cost = 10e6
s1 = {0, 1, 2, 3}
s2 = {1, 2, 3}
for i in product([0, 1, 2, 3], repeat=N):
    s = set(i)
    if s != s1 and s != s2:
        continue
    nums = [[], [], [], []]
    for j, indx in enumerate(i):
        nums[indx].append(L[j])
    cost_a = abs(A - sum(nums[1])) + 10 * (len(nums[1]) - 1)
    cost_b = abs(B - sum(nums[2])) + 10 * (len(nums[2]) - 1)
    cost_c = abs(C - sum(nums[3])) + 10 * (len(nums[3]) - 1)
    cost_tmp = cost_a + cost_b + cost_c
    cost = min(cost, cost_tmp)
print(cost)

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

b = []
for x in range(4**N):
    o = []
    s = -30
    for y in range(N):
        o.append((x//(4**y))%4)
    if (0 in o) and (1 in o) and (2 in o):
        a = [A,B,C]
        for y in range(N):
            if o[y] != 3:
                a[o[y]] -= l[y]
                s += 10
        b.append(sum(map(lambda x: x if x > 0 else -x,a))+s)

print(min(b))

Solution (Python):
import functools
import itertools

import os
import sys

sys.setrecursionlimit(10000)
INF = float('inf')


def debug(fn):
    if not os.getenv('LOCAL'):
        return fn

    @functools.wraps(fn)
    def wrapper(*args, **kwargs):
        ret = fn(*args, **kwargs)
        print('DEBUG: {}({}) -> '.format(
            fn.__name__,
            ', '.join(
                list(map(str, args)) +
                ['{}={}'.format(k, str(v)) for k, v in kwargs.items()]
            )
        ), end='')
        print(ret)
        return ret

    return wrapper


def count_mp(target, sizes):
    return (len(sizes) - 1) * 10 + abs(target - sum(sizes))


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

ans = INF
for allocs in itertools.product('abc_', repeat=N):
    a = []
    b = []
    c = []
    mp = 0
    for i, al in enumerate(allocs):
        if al == 'a':
            a.append(sizes[i])
        if al == 'b':
            b.append(sizes[i])
        if al == 'c':
            c.append(sizes[i])
    if not a or not b or not c:
        continue
    ans = min(ans, count_mp(A, a) + count_mp(B, b) + count_mp(C, c))
    if ans == 0:
        break
print(ans)

Solution (Python):
import functools
import itertools

import os
import sys

sys.setrecursionlimit(10000)
INF = float('inf')


def debug(fn):
    if not os.getenv('LOCAL'):
        return fn

    @functools.wraps(fn)
    def wrapper(*args, **kwargs):
        ret = fn(*args, **kwargs)
        print('DEBUG: {}({}) -> '.format(
            fn.__name__,
            ', '.join(
                list(map(str, args)) +
                ['{}={}'.format(k, str(v)) for k, v in kwargs.items()]
            )
        ), end='')
        print(ret)
        return ret

    return wrapper


def count_mp(target, sizes):
    return (len(sizes) - 1) * 10 + abs(target - sum(sizes))


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

ans = INF
for allocs in itertools.product('abc_', repeat=N):
    a = []
    b = []
    c = []
    mp = 0
    for i, al in enumerate(allocs):
        if al == 'a':
            a.append(sizes[i])
        if al == 'b':
            b.append(sizes[i])
        if al == 'c':
            c.append(sizes[i])
    if not a or not b or not c:
        continue
    ans = min(ans, count_mp(A, a) + count_mp(B, b) + count_mp(C, c))
    if ans == 0:
        break
print(ans)

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



initialSum = []
for i in range(2**N): 
    nowsum = 0
    nowbamboocnt = 0
    for j in range(N): 
        if i % (2**(j+1)) >= 2**j:
            nowbamboocnt += 1
            nowsum += L[j] 
    initialSum.append([nowsum, nowbamboocnt])



bambooset = {i for i in range(N)} 
min = 3000 

lestbamboo = bambooset.copy() 

for i, initSumA in enumerate(initialSum): 

    if initSumA[1] ==0 or N - initSumA[1] < 2: continue 
    lestbamboo = bambooset.copy() 
    for j in range(N): 
        if i % (2**(j+1)) >= 2**j: 
            lestbamboo.remove(j)
    necesMPforA = abs(A - initSumA[0]) + (initSumA[1]-1)*10 

    
    for b in range(2**len(lestbamboo)): 
    
        lestbambooList = list(lestbamboo) 
        
        bamboocntB = 0 
        nowsumB = 0
        lestbambooforC = lestbamboo.copy() 
        for k in range(len(lestbambooList)): 
            if b % (2**(k+1)) >= 2**k: 
                bamboocntB += 1
                nowsumB += L[lestbambooList[k]] 
                lestbambooforC.remove(lestbambooList[k])
        if bamboocntB == 0: continue 

        for m in range(2**len(lestbambooforC)): 
        
        
            lestbambooforCList = list(lestbambooforC) 
            bamboocntC = 0
            nowsumC = 0
            for n in range(len(lestbambooforCList)):
                if m % (2**(n+1)) >= 2**n: 
                    bamboocntC += 1
                    nowsumC += L[lestbambooforCList[n]] 
            if bamboocntC == 0: continue 

            nowTotalSum = necesMPforA + abs(B - nowsumB) + (bamboocntB-1)*10 + abs(C - nowsumC) + (bamboocntC-1)*10
            
            if nowTotalSum < min: min = nowTotalSum

print(min)

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





initialSum = []
for i in range(2**N): 
    nowsum = 0
    nowbamboocnt = 0
    for j in range(N): 
        if i % (2**(j+1)) >= 2**j:
            nowbamboocnt += 1
            nowsum += L[j] 
    initialSum.append([nowsum, nowbamboocnt])




bambooset = {i for i in range(N)} 
min = 3000 

lestbamboo = bambooset.copy() 

for i, initSumA in enumerate(initialSum): 

    if initSumA[1] ==0 or N - initSumA[1] < 2: continue 
    lestbamboo = bambooset.copy() 
    for j in range(N): 
        if i % (2**(j+1)) >= 2**j: 
            lestbamboo.remove(j)
    necesMPforA = abs(A - initSumA[0]) + (initSumA[1]-1)*10 

    
    for b in range(2**len(lestbamboo)): 
    
        lestbambooList = list(lestbamboo) 
        
        bamboocntB = 0 
        nowsumB = 0
        lestbambooforC = lestbamboo.copy() 
        for k in range(len(lestbambooList)): 
            if b % (2**(k+1)) >= 2**k: 
                bamboocntB += 1
                nowsumB += L[lestbambooList[k]] 
                lestbambooforC.remove(lestbambooList[k])
        if bamboocntB == 0: continue 

        for m in range(2**len(lestbambooforC)): 
        
        
            lestbambooforCList = list(lestbambooforC) 
            bamboocntC = 0
            nowsumC = 0
            for n in range(len(lestbambooforCList)):
                if m % (2**(n+1)) >= 2**n: 
                    bamboocntC += 1
                    nowsumC += L[lestbambooforCList[n]] 
            if bamboocntC == 0: continue 

            nowTotalSum = necesMPforA + abs(B - nowsumB) + (bamboocntB-1)*10 + abs(C - nowsumC) + (bamboocntC-1)*10
            
            if nowTotalSum < min: min = nowTotalSum

print(min)

Solution (Python):
from itertools import product

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

mp = 10**9
cand = ['A', 'B', 'C', 'discard']
for t in product(cand, repeat=N):
    prepared = {'A': [], 'B': [], 'C': []}
    for i, p in enumerate(t):
        if p != 'discard':
            prepared[p].append(L[i])
    if prepared['A'] == [] or prepared['B'] == [] or prepared['C'] == []:
        continue
    temp = 0
    for p in ['A', 'B', 'C']:
        temp += abs(sum(prepared[p]) - target[p]) + (len(prepared[p]) - 1)*10
    if temp < mp:
        mp = temp
print(mp)

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

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 float('inf')
    
    tmp0 = dfs(i + 1, a, b, c)
    tmpa = dfs(i + 1, a + L[i], b, c) + 10
    tmpb = dfs(i + 1, a, b + L[i], c) + 10
    tmpc = dfs(i + 1, a, b, c + L[i]) + 10
    ans = min(tmp0, tmpa, tmpb, tmpc)
    
    return ans

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

Solution (Python):
def rec (i,a,b,c):
    if i==N:
        if a==0 or b==0 or c==0:
            return INF
        else:
            return abs(a-A)+abs(b-B)+abs(c-C)
    res = rec(i+1,a,b,c)
    
    res = min(res,rec(i+1,a+L[i],b,c)+(10 if a>0 else 0))
    res = min(res,rec(i+1,a,b+L[i],c)+(10 if b>0 else 0))
    res = min(res,rec(i+1,a,b,c+L[i])+(10 if c>0 else 0))
    
    return res
N,A,B,C = list(map(int,input().split()))
INF=10**8
L = [int(input()) for _ in range(N)]
print(rec(0,0,0,0))

Solution (Python):
n,a,b,c = map(int,input().split())
l = [int(input()) for i in range(n)]
def dfs(cnt,A,B,C):
    if cnt == n:
        if min(A,B,C) == 0:
            return float('inf')
        else:
            return abs(a-A)+abs(b-B)+abs(c-C)-30
    else:
        ret1 = dfs(cnt+1,A+l[cnt],B,C)+10
        ret2 = dfs(cnt+1,A,B+l[cnt],C)+10
        ret3 = dfs(cnt+1,A,B,C+l[cnt])+10
        ret4 = dfs(cnt+1,A,B,C)
        return min(ret1,ret2,ret3,ret4)

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(cnt,A,B,C):
    if cnt == n:
        if min(A,B,C) == 0:
            return float('inf')
        else:
            return abs(a-A)+abs(b-B)+abs(c-C)-30
    else:
        ret1 = dfs(cnt+1,A+l[cnt],B,C)+10
        ret2 = dfs(cnt+1,A,B+l[cnt],C)+10
        ret3 = dfs(cnt+1,A,B,C+l[cnt])+10
        ret4 = dfs(cnt+1,A,B,C)
        return min(ret1,ret2,ret3,ret4)
print(dfs(0,0,0,0))

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


def calc_mp(v):
    if v[0] == 0 or v[1] == 0 or v[2] == 0:
        return 10 ** 9
    return abs(A - v[0]) + abs(B - v[1]) + abs(C - v[2]) - 30


def dfs(n, v):
    if n == N:
        return calc_mp(v)
    ret0 = dfs(n + 1, (v[0] + l[n], v[1], v[2])) + 10
    ret1 = dfs(n + 1, (v[0], v[1] + l[n], v[2])) + 10
    ret2 = dfs(n + 1, (v[0], v[1], v[2] + l[n])) + 10
    ret3 = dfs(n + 1, (v[0], v[1], v[2]))

    return min(ret0, ret1, ret2, ret3)


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

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 float('inf')
        
    n1 = 10 + dfs(i + 1, a + L[i], b, c)
    n2 = 10 + dfs(i + 1, a, b + L[i], c)
    n3 = 10 + dfs(i + 1, a, b, c + L[i])
    n4 = dfs(i + 1, a, b, c)
    
    return min(n1, n2, n3, n4)
    
print(dfs(0, 0, 0, 0))

Solution (Python):
from itertools import*
n,a,b,c,*l=map(int,open(0).read().split())
d=[a,b,c]
ans=float("inf")
for i in product(range(4),repeat=n):
    take=[[],[],[]]
    for k,j in enumerate(i):  
        if j!=0:
            take[j-1].append(l[k])
    if all(take):
        take2=list(map(sum,take))
        
        ans=min(ans,sum(abs(s-t)for s,t in zip(d,take2))+(sum(map(len,take))-3)*10)
print(ans)

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

INF = 10**11
def magic(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 = magic(i+1, a, b, c)
  ret1 = magic(i+1, a+data[i], b, c)+10
  ret2 = magic(i+1, a, b+data[i], c)+10
  ret3 = magic(i+1, a, b, c+data[i])+10
  return min([ret0, ret1, ret2, ret3])

print(magic(0, 0, 0, 0))

Solution (Python):
N,A,B,C = map(int,input().split())
l = [int(input()) for _ in range(N)]
lnum = [0] * N
Alist = [A,B,C]
ans = 2**30+1
for i in range(4**N):
    cnt = -30
    tmp = i
    for j in range(N):
        lnum[j] = tmp%4
        tmp //= 4
    
    if not(1 in lnum and 2 in lnum and 3 in lnum):
        continue
    x = [0] * 3
    for j in range(N):
        if lnum[j] != 0:
            x[lnum[j]-1] += l[j]
            cnt += 10
    for j in range(3):
        cnt += abs(Alist[j]-x[j])
    
    ans = min(ans,cnt)
    
print(ans)

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

def dfs(now, a, b, c):
    if now == N:
        if a == 0 or b == 0 or c == 0:
            return 100000
        return abs(A-a) + abs(B-b) + abs(C-c)
    d1 = dfs(now+1, a+ll[now], b, c) + min(10, a*10)
    d2 = dfs(now+1, a, b+ll[now], c) + min(10, b*10)
    d3 = dfs(now+1, a, b, c+ll[now]) + min(10,c*10)
    d4 = dfs(now+1, a, b, c)
    return min(d1, d2, d3, d4)

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

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

def dfs(i, a, b, c, N):
    global ls,A,B,C

    if i == N:
        mp = 0

        for ans_item, item in zip([A,B,C], [a,b,c]):
            if len(item) == 0:
                return float("inf")
            elif len(item) > 1:
                mp += (len(item) - 1) * 10
            total = sum(item)
            if ans_item == total:
                pass
            else:
                mp += abs(ans_item-total)
        
        return mp

    l = ls[i]
    mps = []
    mps.append(dfs(i+1, a+[l], b, c, N))
    mps.append(dfs(i+1, a, b+[l], c, N))
    mps.append(dfs(i+1, a, b, c+[l], N))
    mps.append(dfs(i+1, a, b, c, N))
    return min(mps)

print(dfs(i=0, a=[], b=[], c=[], N=N))

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

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

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


"""
1, 2, 3, 4, 5, 6  7, 8
A  A  A  A  A  A  A  A
B  B  B  B  B  B  B  B
C  C  C  C  C  C  C  C
X  X  X  X  X  X  X  X

example
5 100 90 80
98
40
30
21
80

1. dfs(0,0,0,0)
  ret0 = dfs(1,0,0,0) + 10
  ret1 = dfs(1,98,0,0) + 10 <- will be chosen
  ret2 = dfs(1,0,98,0) + 10
  ret3 = dfs(1,0,0,98) + 10

2. dfs(1,98,0,0)
  ret0 = dfs(2,98,0,0) + 10
  ret1 = dfs(2,138,0,0) + 10
  ret2 = dfs(2,98,40,0) + 10 <- will be chosen
  ret3 = dfs(2,98,0,40) + 10

3. dfs(2,98,40,0)
  ret0 = dfs(3,98,40,0) + 10
  ret1 = dfs(3,128,40,0) + 10
  ret2 = dfs(3,98,70,0) + 10 <- will be chosen
  ret3 = dfs(3,98,40,30) + 10

4. dfs(3,98,70,0)
  ret0 = dfs(4,98,70,0) + 10
  ret1 = dfs(4,119,70,0) + 10
  ret2 = dfs(4,98,91,0) + 10 <- will be chosen
  ret3 = dfs(4,98,70,21) + 10

5. dfs(4,98,91,0)
  ret0 = dfs(5,98,91,0) + 10
  ret1 = dfs(5,178,91,0) + 10
  ret2 = dfs(5,98,171,0) + 10
  ret3 = dfs(5,98,91,80) + 10 <- will be chosen

6. dfs(5,98,91,80)
  return abs(98 - 100) + abs(91 - 90) + abs(80 - 80) - 30 if min(98,91,80) > 0 else INF
equals
  return 2 + 1 + 0 - 30
equals
  return -27

then,

5. dfs(4,98,91,0)
  ret3 = -27 + 10
  it returns -17

4. dfs(3,98,70,0)
  ret2 = -17 + 10
  it returns -7

3. dfs(2,98,40,0)
  ret2 = -7 + 10
  it returns 3

2. dfs(1,98,0,0)
  ret2 = 3 + 10
  it returns 13

1. dfs(0,0,0,0)
  ret1 = 13 + 10
  it returns 23

therefore, the answer is 23.
"""

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

def dfs(cnt,a,b,c):
    if cnt == N:
        return abs(A-a)+abs(B-b)+abs(C-c) if min(a,b,c) > 0 else float('inf')
    l0 = dfs(cnt+1,a,b,c)
    la = dfs(cnt+1,a+list[cnt],b,c) +(10 if a != 0 else 0)
    lb = dfs(cnt+1,a,b+list[cnt],c) +(10 if b != 0 else 0)
    lc = dfs(cnt+1,a,b,c+list[cnt]) +(10 if c != 0 else 0)
    return min(l0,la,lb,lc)

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

[n,a,b,c] = list(map(int, input().strip().split(' ')))
lst = []
for i in range(n):
    lst.append(int(input().strip()))

x = [0,1,2,3]
y = list(itertools.product(x,x,x,x,x,x,x,x))
minimum = 1000000
for pattern in y:
    aa = 0
    bb = 0
    cc = 0
    mp = 0
    for i,num in enumerate(pattern):
        if i > n - 1:
            break
        if num == 1:
            if aa != 0:
                mp += 10
            aa += lst[i]
        if num == 2:
            if bb != 0:
                mp += 10
            bb += lst[i]
        if num == 3:
            if cc!= 0:
                mp += 10
            cc += lst[i]
    if aa == 0 or bb == 0 or cc == 0:
        continue
    mp += abs(a-aa) + abs(b-bb) + abs(c-cc)
    if minimum > mp:
        minimum = mp
print(minimum)

Solution (Python):
N, A, B, C = map(int, input().split())
l_list = [0]*8
for i in range(N):
    l_list[i] = int(input())
INF = 1e9

target_length = {'A':A, 'B':B, 'C':C}

cost_list = []

all_combination = []
base = ['A', 'B', 'C', 'O']

def comb_to_cost(combination):
    cost = 0
    for char in ['A', 'B', 'C']:
        base_length = 0
        for i in range(8):
            c = combination[i]
            if c == char:
                base_length += l_list[i]
                cost += 10
        cost -= 10
        if base_length == 0:
            return INF
        cost += abs(base_length-target_length[char])
    return cost


for char1 in base:
    for char2 in base:
        for char3 in base:
            for char4 in base:
                for char5 in base:
                    for char6 in base:
                        for char7 in base:
                            for char8 in base:
                                all_combination.append(char1+char2+char3+char4+char5+char6+char7+char8)

for comb in all_combination:
    cost_list.append(comb_to_cost(comb))


print(min(cost_list))

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

l_in = []
for i in range(N):
    num = int(input())
    l_in.append(num)


def dfs(cur, a_sum, b_sum, c_sum):
    if cur == N:
        return abs(A-a_sum)+abs(B-b_sum)+abs(C-c_sum)-30 if a_sum*b_sum*c_sum > 0 else 100000000

    pat0 = dfs(cur+1, a_sum, b_sum, c_sum)
    pat1 = dfs(cur+1, a_sum+l_in[cur], b_sum, c_sum)+10
    pat2 = dfs(cur+1, a_sum, b_sum+l_in[cur], c_sum)+10
    pat3 = dfs(cur+1, a_sum, b_sum, c_sum+l_in[cur])+10

    return min(pat0, pat1, pat2, pat3)


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(x, y, z, now):

    if now == n:
        return abs(x - a) + abs(y - b) + abs(z - c) - 30 if not 0 in (x, y, z) else 3000

    usea = dfs(x + l[now], y, z, now + 1) + 10
    useb = dfs(x, y + l[now], z, now + 1) + 10
    usec = dfs(x, y, z + l[now], now + 1) + 10
    nouse = dfs(x, y, z, now + 1)
    return min(usea, useb, usec, nouse)

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

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

lList = []
for _ in range(N):
    lList.append(int(input()))

def dfs(idx, i, j, k):
    if idx == N:
        if min(i,j,k) > 0:
            return abs(A-i) + abs(B- j) + abs(C - k)
        else:
            return 10**9
    re4 = dfs(idx+1, i, j, k)
    re1 = dfs(idx+1, i+lList[idx], j, k)+10
    re2 = dfs(idx+1, i, j+lList[idx], k)+10
    re3 = dfs(idx+1, i, j, k+lList[idx])+10

    return min(re1,re2,re3,re4)

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