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):
from itertools import product
n,*B=map(int,input().split())
A=[int(input()) for i in range(n)]
s=10**9
for P in product([0,1,2,3],repeat=n):
  if {0,1,2}<=set(P):
    m=[0,0,0]
    for x,y in zip(P,A):
      if x!=3:
        m[x]+=y
    t=sum(abs(m-y) for m,y in zip(m,B))
    l=sum(1 for j in P if j!=3)-3
    s=min(s,t+l*10)
print(s)

Solution (Python):
N,A,B,C=map(int,input().split(" "))
INF=10**9
tmp=[int(input()) for _ in range(N)]
def calc(num,a,b,c):
    if(num==N):
        if(min(a,b,c)>0):
            return abs(A-a)+abs(B-b)+abs(C-c)-30
        else:
            return INF
    res=calc(num+1,a,b,c)
    res1=calc(num+1,a+tmp[num],b,c)+10
    res2=calc(num+1,a,b+tmp[num],c)+10
    res3=calc(num+1,a,b,c+tmp[num])+10
    return min(res,res1,res2,res3)
print(calc(0,0,0,0))

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


def recur(i, a, b, c, cost):
    if i == N:
        if a*b*c == 0:
            return INF
        else:
            return cost + abs(A-a) + abs(B-b) + abs(C-c) - 30

    cand1 = recur(i+1, a+l[i], b, c, cost + 10)
    cand2 = recur(i+1, a, b+l[i], c, cost + 10)
    cand3 = recur(i+1, a, b, c+l[i], cost + 10)
    cand4 = recur(i+1, a, b, c, cost)
    return min([cand1, cand2, cand3, cand4])


print(recur(0, 0, 0, 0, 0))

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

def dfs(n,a,b,c):
    if n == N:
        if min(a,b,c)>0:
            return abs(A-a)+abs(B-b)+abs(C-c)-30
        else:
            return 10000
    root1=dfs(n+1,a+L[n],b,c)+10
    root2=dfs(n+1,a,b+L[n],c)+10
    root3=dfs(n+1,a,b,c+L[n])+10
    root4=dfs(n+1,a,b,c)
    return min(root1,root2,root3,root4)
    
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(k,a,b,c):
    global N,A,B,C,l
    if k == N:
        return abs(a-A)+abs(b-B)+abs(c-C)-30 if min(a,b,c)>0 else 1000000
    det0 = dfs(k+1, a+l[k], b, c)+10
    det1 = dfs(k+1, a, b+l[k], c)+10
    det2 = dfs(k+1, a, b, c+l[k])+10
    det3 = dfs(k+1, a, b, c)
    return min(det0, det1, det2, det3)
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 return_4(x, k):
    four_hairetu = [0 for i in range(k)]
    for i in range(k):
        four_hairetu[i] = x%4
        x = x//4
    return four_hairetu
min_mp = 1000000
for i in range(4**N):
    ABC = [0,0,0,0] 
    zABC = return_4(i,N)
    if zABC.count(1)==0 or zABC.count(2)==0 or zABC.count(3)==0:
        continue
    else:
        kouho_mp = (zABC.count(1)-1)*10 + \
                   (zABC.count(2)-1)*10 + \
                   (zABC.count(3)-1)*10
        for j in range(N):
            ABC[zABC[j]] += l[j]
        kouho_mp += abs(ABC[1]-A) + abs(ABC[2]-B) + abs(ABC[3]-C)
        min_mp = min(min_mp, kouho_mp)
print(min_mp)

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

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

inf = float('inf')

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

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

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

INF = 10 ** 9
def dfs(cur, a, b, c, m):
  if cur == N:
    if min(a, b, c) == 0:
      return INF
    return abs(A-a)+abs(B-b)+abs(C-c)+(m-3)*10

  res0 = dfs(cur+1, a, b, c, m)
  res1 = dfs(cur+1, a+L[cur], b, c, m+1)
  res2 = dfs(cur+1, a, b+L[cur], c, m+1)
  res3 = dfs(cur+1, a, b, c+L[cur], m+1)

  return min(res0, res1, res2, res3)

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

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

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

def dfs(cnt,for_a,for_b,for_c):
    if cnt==n:
        if for_a != 0 and for_b != 0 and for_c != 0:
            return abs(a-for_a) + abs(b-for_b) + abs(c-for_c) - 30
        else:
            return 10**10
    else:
        return min(dfs(cnt+1,for_a,for_b,for_c),\
                   dfs(cnt+1,for_a+l[cnt],for_b,for_c) + 10,\
                   dfs(cnt+1,for_a,for_b+l[cnt],for_c) + 10,\
                   dfs(cnt+1,for_a,for_b,for_c+l[cnt]) + 10)

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 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 collections import Counter
import itertools
N,A,B,C=map(int,input().split())
l=[]
for i in range(N):
  l.append(int(input()))
ans=float('inf')
for item in itertools.product(range(4),repeat=N):
  c=Counter(item)
  if c[0]==0 or c[1]==0 or c[2]==0:
    continue
  d=[0,0,0,0]
  for i in range(N):
    d[item[i]]+=l[i]
  kans=(abs(A-d[0])+abs(B-d[1])+abs(C-d[2])+10*(c[0]+c[1]+c[2]-3))
  ans=min(ans,kans)
print(ans)

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

from collections import deque
q = deque()
q.append([0,0,0,0,0])
mp = []
while q:
    depth, a, b, c, mcnt= q.pop()
    if depth == N:
        mp.append(abs(A-a)+abs(B-b)+abs(C-c)+mcnt*10-30 if min(a,b,c) > 0 else 10**9)
    else:
        q.append([depth+1, a, b, c,mcnt])
        q.append([depth+1, a+l[depth], b, c,mcnt+1])
        q.append([depth+1, a, b+l[depth], c,mcnt+1])
        q.append([depth+1, a, b, c+l[depth],mcnt+1])

print(min(mp))

Solution (Python):

import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(10000)


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, A, B, C, L):
    ans = sys.maxsize
    
    S = (A, B, C)
    for l in product([0, 1, 2, None], repeat=len(L)):
        ta = 0
        s = [A, B, C]
        for i, f in enumerate(l):
            if f is None:
                continue
            if s[f] != S[f]:
                ta += 10
            s[f] -= L[i]

        if any(map(lambda x: x[0] == x[1], zip(s, S))):
            continue
        
        ta += sum(map(abs, s))
        ans = min(ans, ta)

    return ans


def main():
    
    N, A, B, C = read_int_n()
    L = [read_int() for _ in range(N)]
    print(slv(N, A, B, C, L))


if __name__ == '__main__':
    main()

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

ans=99999
for i in range(4**len(L)):
    i_4_list=list(Base_10_to_n(i, 4).zfill(len(L)))
    at,bt,ct=[],[],[]
    for num,i_4 in enumerate(i_4_list):
        if i_4=='0':
            at.append(L[num])
        elif i_4=='1':
            bt.append(L[num])
        elif i_4=='2':
            ct.append(L[num])
        else:
            continue
    if min(len(at),len(bt),len(ct))>0:
        al=0
        al+=(len(at)+len(bt)+len(ct)-3)*10
        al+=(abs(sum(at)-a)+abs(sum(bt)-b)+abs(sum(ct)-c))
        if al < ans:
            ans=al
print(ans)

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

def dfs(depth=0, a=0, b=0, c=0):
    if depth == N:
        if a*b*c != 0:
            return abs(a-A) + abs(b-B) + abs(c-C) - 30
        return inf
    res0 = dfs(depth+1,a,b,c)
    res1 = dfs(depth+1,a+l[depth],b,c) + 10
    res2 = dfs(depth+1,a,b+l[depth],c) + 10
    res3 = dfs(depth+1,a,b,c+l[depth]) + 10
    return min([res0,res1,res2,res3])

print(dfs())

Solution (Python):
import itertools
A=[0,0,0]
N,A[0],A[1],A[2]=map(int,input().split())
l=[int(input()) for i in range(N)]
ans=10**15
for seq in itertools.product(range(4),repeat=N):
    X=[0,0,0]
    Y=[0,0,0]
    for i in range(N):
        if seq[i]!=3:
            X[seq[i]]+=l[i]
            Y[seq[i]]+=1
    if Y[0]*Y[1]*Y[2]==0:
        continue
    MP=(sum(Y)-3)*10
    for i in range(3):
        MP+=abs(X[i]-A[i])
    if MP<ans:
        ans=MP
print(ans)

Solution (Python):
N, A, B, C = map(int, input().split())
l = [int(input()) for _ in range(N)]
def dfs(cur, a, b, c):
  if cur == N:
    return abs(a-A)+abs(b-B)+abs(c-C)-30 if min(a,b,c) > 0 else 10**9
  ret0 = dfs(cur+1, a, b, c)
  ret1 = dfs(cur+1, a+l[cur], b, c) + 10
  ret2 = dfs(cur+1, a, b+l[cur], c) + 10
  ret3 = dfs(cur+1, a, b, c+l[cur]) + 10
  return min(ret0, ret1, ret2, ret3)

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

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


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 1000000000

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

    return min(ret0, ret1, ret2, ret3)  


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

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

L = [int(input()) for i in range(N)]
INF = 10 ** 18

def bamboo_search(l,a,b,c):
    if l==N:
        return abs(A-a)+abs(B-b)+abs(C-c)-30 if min(a,b,c)>0 else INF
    else:
        bcan = bamboo_search(l+1,a,b,c)
        ba = bamboo_search(l+1,a+L[l],b,c)+10
        bb = bamboo_search(l+1,a,b+L[l],c)+10
        bc = bamboo_search(l+1,a,b,c+L[l])+10
        return min(bcan,ba,bb,bc)
    
print(bamboo_search(0,0,0,0))

Solution (Python):
INF = int(1e9)
n, A, B, C = map(int, input().split())
L = tuple(map(int, (int(input()) for _ in range(n))))
def f(a, b, c, num):
    if num == n:
        return abs(A - a) + abs(B - b) + abs(C - c) if min(a, b, c) > 0 else INF
    return min(
        f(a + L[num], b, c, num + 1) + (10 if a else 0),
        f(a, b + L[num], c, num + 1) + (10 if b else 0),
        f(a, b, c + L[num], num + 1) + (10 if c else 0),
        f(a, b, c, num + 1)
    )
print(f(0, 0, 0, 0))

Solution (Python):
INF = float("inf")
n, A, B, C = map(int, input().split())
L = tuple(map(int, (int(input()) for _ in range(n))))
def f(a, b, c, num):
    if num == n:
        return abs(A - a) + abs(B - b) + abs(C - c) if min(a, b, c) > 0 else INF
    return min(
        f(a + L[num], b, c, num + 1) + (10 if a else 0),
        f(a, b + L[num], c, num + 1) + (10 if b else 0),
        f(a, b, c + L[num], num + 1) + (10 if c else 0),
        f(a, b, c, num + 1)
    )
print(f(0, 0, 0, 0))

Solution (Python):
INF = float("inf")
n, A, B, C = map(int, input().split())
L = tuple(map(int, (int(input()) for _ in range(n))))
def f(a, b, c, num):
    if num == n:
        if min(a, b, c) > 0:
            return abs(A - a) + abs(B - b) + abs(C - c)
        else:
            return INF
    return min(
        f(a + L[num], b, c, num + 1) + (10 if a else 0),
        f(a, b + L[num], c, num + 1) + (10 if b else 0),
        f(a, b, c + L[num], num + 1) + (10 if c else 0),
        f(a, b, c, num + 1)
    )
print(f(0, 0, 0, 0))

Solution (Python):
import itertools
N,A,B,C = map(int,input().split())
l = [int(input()) for i in range(N)]
inf = float('inf')
ans = inf
for pair in itertools.product(range(4),repeat=N):
    mA = 0 ; mB = 0; mC = 0; cnt = 0
    for i in range(N):
        if pair[i] == 1:
            mA += l[i]
            cnt += 1
        elif pair[i] == 2:
            mB += l[i]
            cnt += 1
        elif pair[i] == 3:
            mC += l[i]
            cnt += 1
    if mA*mB*mC == 0:continue
    cost = abs(mA-A) + abs(mB-B) + abs(mC-C) + (cnt-3)*10
    if cost < ans:
        ans = cost
print(ans)

Solution (Python):
def solve(n, a, b, c, l):
    v = gen(a,b,c,l,n,0,[],[],[])
    print(v)

    
def gen(a,b,c,l,n,idx,al,bl,cl):
    if idx == n:
        if (len(al) > 0) and (len(bl) > 0) and (len(cl) > 0):
            v = calc(a,b,c,al,bl,cl)
            
            return v
        else:
            return 3000

    l1 = l[idx]
    nal = al.copy()
    nbl = bl.copy()
    ncl = cl.copy()

    nal.append(l1)
    nbl.append(l1)
    ncl.append(l1)

    va = gen(a, b, c, l, n, (idx+1), nal,bl,cl)
    vb = gen(a, b, c, l, n, (idx+1), al,nbl,cl)
    vc = gen(a, b, c, l, n, (idx+1), al,bl,ncl)
    vn = gen(a, b, c, l, n, (idx+1), al,bl,cl)
    return min(va, vb, vc, vn)

def calc(a,b,c,al,bl,cl):
    mp = 0
    
    t = al
    mp += (len(t) - 1) * 10
    length = sum(t)
    mp += abs(a - length)

    t = bl
    mp += (len(t) - 1) * 10
    length = sum(t)
    mp += abs(b - length)

    t = cl
    mp += (len(t) - 1) * 10
    length = sum(t)
    mp += abs(c - length)

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

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

Solution (Python):
n, *li = map(int, input().split())
L = list(map(int, (int(input()) for _ in range(n))))
ans = int(1e9)
def f(A, num, cnt):
    if num == n:
        if 0 in A:
            return
        global ans
        ans = min(ans, abs(A[0] - li[0]) + abs(A[1] - li[1]) + abs(A[2] - li[2]) + cnt)
        return
    for i in range(3):
        p = 0 if A[i] == 0 else 10
        B = A[::]
        B[i] += L[num]
        f(B, num + 1, cnt + p)
    f(A, num + 1, cnt)
f([0, 0, 0], 0, 0)
print(ans)

Solution (Python):
import sys
import numpy as np

def parse(inp):
    N,A,B,C=map(int,next(inp).split())
    L=[int(next(inp).split()[0]) for _ in range(N)]
        
    return N,[A,B,C],sorted(L,reverse=True)
  
def main(inp):
    N,target,L=inp
    
    pattern=[0 for _ in range(N)]
    
    ans = np.iinfo(np.int32).max
    
    for i in range(27,4**N-6):
        tmp = i
        for j in range(N-1,-1,-1):

            pattern[j]=int(tmp/(4**j))
            if(tmp >= 4**j): tmp -= int(tmp/(4**j))*4**j
        
        if(pattern.count(1)==0 or pattern.count(2)==0 or \
               pattern.count(3)==0): continue
            
        taketest = [0 for _ in range(3)]
        
        for j in range(N):
            if(pattern[j]==0): continue
            else: taketest[pattern[j]-1] += L[j]
        
        param=0
        for j in range(3):
            param += abs(taketest[j]-target[j]) + 10*(pattern.count(j+1)-1)
        ans = min(ans,param)
        
    return ans
  
print(main(parse(sys.stdin)))

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

def calc(v, trees):
    global minv
    for j, val in enumerate(v):
        trees[val] += l[j]

    if 0 in trees[:3]:
        return float("INF")

    work = abs(trees[0] - A) +  ((v.count(0) - 1) * 10 if v.count(0) > 1 else 0)
    work += abs(trees[1] - B) + ((v.count(1) - 1) * 10 if v.count(1) > 1 else 0)
    work += abs(trees[2] - C) + ((v.count(2) - 1) * 10 if v.count(2) > 1 else 0)
    return work

def dfs(depth, v):
    global minv
    if depth == N:
        w = calc(v, [0, 0, 0, 0])
        if w < minv:
            minv = min(w, minv)
        return

    for i in range(4):
        v[depth] = i
        dfs(depth + 1, v)

dfs(0,V)
print(minv)

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

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

Solution (Python):



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

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

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

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

INF = 1e+10

def DFS(depth, current_a, current_b, current_c):
    if depth == N:
        if min(current_a, current_b, current_c) > 0:
            return abs(current_a - A) + abs(current_b - B) + abs(current_c - C) -30
        else:
            return INF
    
    ret0 = DFS(depth+1, current_a, current_b, current_c)
    ret1 = DFS(depth+1, current_a + l[depth], current_b, current_c) + 10
    ret2 = DFS(depth+1, current_a , current_b+ l[depth], current_c) + 10
    ret3 = DFS(depth+1, current_a, current_b, current_c + l[depth]) + 10
    
    return min(ret0, ret1, ret2, ret3)

print(DFS(0, 0, 0, 0))

Solution (Python):



N,A,B,C = map(int, input().split())
L = [int(input()) for _ in range(N)]
ans = float('inf')
for i1 in range(4):
    for i2 in range(4):
        for i3 in range(4):
            for i4 in range(4):
                for i5 in range(4):
                    for i6 in range(4):
                        for i7 in range(4):
                            for i8 in range(4):
                                p = [i1,i2,i3,i4,i5,i6,i7,i8]
                                take = [[] for _ in range(4)] 
                                for i in range(N):
                                    take[p[i]].append(L[i])
                                a,b,c = take[0],take[1],take[2]
                                if len(a)*len(b)*len(c)==0:
                                    continue 
                                cost = (len(a)+len(b)+len(c)-3)*10
                                cost += abs(sum(a)-A)+abs(sum(b)-B)+abs(sum(c)-C)
                                ans = min(ans, cost)
print(ans)

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

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
















































def dfs(l, a, b, c):
    global A, B, C
    INF = 10**18
    if len(l) == 0:
        if min(a, b, c) > 0:
            return abs(a - A) + abs(b - B) + abs(c - C) - 30
        else:
            return INF

    ret0 = dfs(l[1:], a, b, c)  
    retA = dfs(l[1:], a + l[0], b, c) + 10  
    retB = dfs(l[1:], a, b + l[0], c) + 10  
    retC = dfs(l[1:], a, b, c + l[0]) + 10  

    return min(ret0, retA, retB, retC)


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

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

ko = [int(input()) for i in range(n)]
INF = 10**9

def dfs(q,w,e,r):
 	if q == n:
 		 return abs(a-w) + abs(b-e) + abs(c-r) - 30 if min(w,e,r) > 0 else INF
 	re0 = dfs(q+1,w,e,r)
 	re1 = dfs(q+1,w+ko[q],e,r) + 10
 	re2 = dfs(q+1,w,e+ko[q],r) + 10
 	re3 = dfs(q+1,w,e,r+ko[q]) + 10
 	return min(re0,re1,re2,re3)

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

Solution (Python):
def c_synthetic_kadomatsu(N, A, B, C, L):
    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 float('inf')
        
        ret0 = dfs(cur + 1, a, b, c)
        ret1 = dfs(cur + 1, a + L[cur], b, c) + 10
        ret2 = dfs(cur + 1, a, b + L[cur], c) + 10
        ret3 = dfs(cur + 1, a, b, c + L[cur]) + 10
        return min(ret0, ret1, ret2, ret3)
    ans = dfs(0, 0, 0, 0)  
    return ans

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

Solution (Python):
n, a, b, c = [int(it) for it in input().split()]
ls = [int(input()) for it in range(n)]
def dfs(cur, tmpA, tmpB, tmpC):
    if cur == n:
        return abs(a - tmpA) + abs(b - tmpB) + abs(c - tmpC) if tmpA > 0 and tmpB > 0 and tmpC else 10**20
    return min(
        dfs(cur + 1, tmpA + ls[cur], tmpB, tmpC) + (10 if tmpA > 0 else 0),
        dfs(cur + 1, tmpA, tmpB + ls[cur], tmpC) + (10 if tmpB > 0 else 0),
        dfs(cur + 1, tmpA, tmpB, tmpC + ls[cur]) + (10 if tmpC > 0 else 0),
        dfs(cur + 1, tmpA, tmpB, tmpC)
    )

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

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

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


def divide(l):
    if len(l) == 0:
        return []
    if len(l) == 1:
        return [
            [[], [], []],  
            [[l[0]], [], []],  
            [[], [l[0]], []],  
            [[], [], [l[0]]],  
        ]

    res = []
    for a, b, c in divide(l[1:]):
        
        res.append([a, b, c])
        
        res.append([l[0:1] + a, b, c])
        
        res.append([a, l[0:1] + b, c])
        
        res.append([a, b, l[0:1] + c])

    return res


min_mp = -1
for a, b, c in divide(bamboos):
    al, ac = sum(a), len(a)
    bl, bc = sum(b), len(b)
    cl, cc = sum(c), len(c)
    if al == 0 or bl == 0 or cl == 0:
        continue

    
    composite = 10 * ((ac - 1) + (bc - 1) + (cc - 1))
    
    mp = abs(al - A) + abs(bl - B) + abs(cl - C) + composite

    if mp < min_mp or min_mp == -1:
        min_mp = mp

print(min_mp)

Solution (Python):


import sys

INF = float('inf')
MOD = 10 ** 9 + 7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()


n, a, b, c = LI()
A = [a, b, c]
K = [II() for _ in range(n)]


def DFS(cur, x, y, z):
    if cur == n:
        return abs(x - a) + abs(y - b) + abs(z - c) - 30 if min(x, y, z) > 0 else INF
    ret0 = DFS(cur + 1, x, y, z)
    ret1 = DFS(cur + 1, x + K[cur], y, z) + 10
    ret2 = DFS(cur + 1, x, y + K[cur], z) + 10
    ret3 = DFS(cur + 1, x, y, z + K[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 = [int(input()) for _ in range(N)]
ans = float('inf')
for p in product(range(4), repeat=N): 
    take = [[] for _ in range(4)] 
    for i in range(N):
        take[p[i]].append(L[i])
    a,b,c = take[0],take[1],take[2]
    if len(a)*len(b)*len(c)==0:
        continue 
    cost = (len(a)+len(b)+len(c)-3)*10
    cost += abs(sum(a)-A)+abs(sum(b)-B)+abs(sum(c)-C)
    ans = min(ans, cost)
print(ans)

Solution (Python):

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


def dfs(now):
    global color
    if now == N:
        gosei = 0
        cnt = [0] * 4
        for i in range(N):
            if color[i] > 0 and cnt[color[i]] > 0:
                gosei += 1
            cnt[color[i]] += l[i]
        if min(cnt[1:]) == 0:
            return INF
        diff = abs(A - cnt[1]) + abs(B - cnt[2]) + abs(C - cnt[3])
        return gosei * 10 + diff

    res = INF
    for c in range(4):
        color[now] = c
        res = min(res, dfs(now + 1))
    return res


color = [None] * N
print(dfs(0))

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

def d(count1,count2,a1,b1,c1):
    if count1==n:
        if min(a1,b1,c1)==0:
            return float('inf')
        else:
            return abs(a-a1)+abs(b-b1)+abs(c-c1)+count2*10
    else:
        e1=d(count1+1,count2,a1,b1,c1)
        e2=d(count1+1,count2,a1+l[count1],b1,c1) if a1==0 else d(count1+1,count2+1,a1+l[count1],b1,c1)
        e3=d(count1+1,count2,a1,b1+l[count1],c1) if b1==0 else d(count1+1,count2+1,a1,b1+l[count1],c1)
        e4=d(count1+1,count2,a1,b1,c1+l[count1]) if c1==0 else d(count1+1,count2+1,a1,b1,c1+l[count1])
    return min(e1,e2,e3,e4)

print(d(0,0,0,0,0))

Solution (Python):


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


ans = []

for i in range(1 << 2*N):
    A_hon = 0
    B_hon = 0
    C_hon = 0
    A_zai = 0
    B_zai = 0
    C_zai = 0
    for k in range(N):
        if i & (1<<(2*k)) and i & (1<< (2*k)+1):
            C_hon += 1
            C_zai += l[k]
        elif i & (1<<(2*k)+1):
            B_hon += 1
            B_zai += l[k]
        elif i & (1<<(2*k)):
            A_hon += 1
            A_zai += l[k]
    if A_hon * B_hon * C_hon >0:
       ans += [10*(A_hon - 1)+ abs(A_zai - A)+10*(B_hon - 1)+ abs(B_zai - B)+ 10*(C_hon - 1)+ abs(C_zai - C)]
print(min(ans))

Solution (Python):


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


ans = float("inf")

for i in range(1 << 2*N):
    cost = float("inf")
    A_hon = 0
    B_hon = 0
    C_hon = 0
    A_zai = 0
    B_zai = 0
    C_zai = 0
    for k in range(N):
        if i & (1<<(2*k)) and i & (1<< (2*k)+1):
            C_hon += 1
            C_zai += l[k]
        elif i & (1<<(2*k)+1):
            B_hon += 1
            B_zai += l[k]
        elif i & (1<<(2*k)):
            A_hon += 1
            A_zai += l[k]
    if A_hon * B_hon * C_hon >0:
       cost = 10*(A_hon - 1)+ abs(A_zai - A)+10*(B_hon - 1)+ abs(B_zai - B)+ 10*(C_hon - 1)+ abs(C_zai - C)
    ans = min(cost, ans)
print(ans)

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

ans = float('inf')
for i in range(1<<N):
    for j in range(1<<N):
        a,b,c = [],[],[]
        cost = 0
        for keta in range(N):
            bit_i = i&(1<<keta)
            bit_j = j&(1<<keta)
            if bit_i==0 and bit_j==0:
                a.append(keta)
            elif bit_i==0 and bit_j>0:
                b.append(keta)
            elif bit_i>0 and bit_j==0:
                c.append(keta)
            else:
                pass
        if a==[] or b==[] or c==[]:
            continue
        cost += 10*(len(a)+len(b)+len(c)-3)
        for l,val in zip([a,b,c],
                         [A,B,C]):
            s = sum([L[li] for li in l])
            cost += abs(s-val)
        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 = 100000000
def rec(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 = rec(i+1,a,b,c)
    ret1 = rec(i+1,a+l[i],b,c)+10
    ret2 = rec(i+1,a,b+l[i],c)+10
    ret3 = rec(i+1,a,b,c+l[i])+10
    return min(ret0,ret1,ret2,ret3)
print(rec(0,0,0,0))

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

abc = [a, b, c]

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

print(res)

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


def dfs(n, a, b, c):
    global N, A, B, C, l
    if n == N:
        return abs(a - A) + abs(b - B) + abs(c - C) if min(a, b, c) > 0 else 10 ** 100
    alpha = dfs(n + 1, a, b, c)
    beta = dfs(n + 1, a + l[n], b, c) + 10
    gamma = dfs(n + 1, a, b + l[n], c) + 10
    delta = dfs(n + 1, a, b, c + l[n]) + 10
    return min(alpha, beta, gamma, delta)


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