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

import sys
import itertools

def solve(N: int, A: int, B: int, C: int, L: "List[int]"):

    def dfs(cur,a,b,c):
        if cur == N:
            return abs(a-A)+abs(b-B)+abs(c-C) - 30 if min(a,b,c) > 0 else 100000

        cur_l = L[cur]
        nex = cur + 1

        ret0 = dfs(nex,a,b,c)
        ret1 = dfs(nex,a+cur_l,b,c)+10
        ret2 = dfs(nex,a,b+cur_l,c)+10
        ret3 = dfs(nex,a,b,c+cur_l)+10

        return min(ret0,ret1,ret2,ret3)

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

    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
n,goalA,goalB,goalC = list(map(int, input().split()))
l = [int(input()) for _ in range(n)]
cost = []
isum = 0

def dfs(depth: int, a: int, b: int, c:int, isum:int):
    if depth == n:
        if min(a,b,c) > 0:
            cost.append(abs(a - goalA) + abs(b - goalB) + abs(c - goalC) +isum - 30)
            return
        else:
            cost.append(10**8)
            return
    
    dfs(depth+1, a, b, c, isum)
    dfs(depth+1, a + l[depth], b, c, isum + 10) 
    dfs(depth+1, a , b + l[depth], c, isum+10) 
    dfs(depth+1, a , b, c + l[depth], isum + 10) 
    

dfs(0,0,0,0,isum)
print(min(cost))

Solution (Python):




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


def dfs(i, a_len, b_len, c_len):
    
    if i == N:
        if a_len == 0 or b_len == 0 or c_len == 0:  
            return float('inf')
        else:
            return abs(a_len - A) + abs(b_len - B) + abs(c_len - C) - 30

    br1 = dfs(i + 1, a_len + L[i], b_len, c_len) + 10  
    br2 = dfs(i + 1, a_len, b_len + L[i], c_len) + 10  
    br3 = dfs(i + 1, a_len, b_len, c_len + L[i]) + 10  
    br4 = dfs(i + 1, a_len, b_len, c_len)  

    return min(br1, br2, br3, br4)


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

Solution (Python):
import itertools
n,a,b,c=map(int,input().split())
ans=float("inf")
l=[]
for i in range(n):l.append(int(input()))
for i in itertools.product([1,2,3,0],repeat=n):
    if i.count(1) and i.count(2) and i.count(3):
        m=[0,0,0,0]
        for j in range(n):
            m[i[j]]+=l[j]
        ans=min(ans,abs(m[1]-a)+abs(m[2]-b)+abs(m[3]-c)+(n-i.count(0)-3)*10)
print(ans)

Solution (Python):
from sys import exit, stdin
N, A, B, C = [int(_) for _ in stdin.readline().rstrip().split()]
L = [int(stdin.readline().rstrip()) for _ in range(N)]
INF = 10**9

def rec(i, a, b, c):
    if i == N:
        if min(a, b, c) > 0:
            return abs(a-A)+abs(b-B)+abs(c-C)-30
        else:
            return INF
    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):
from __future__ import print_function
import numpy as np
import sys
import math,string,itertools,fractions,heapq,collections,re,array,bisect,copy,functools


import random

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
























 








n,a,b,c=map(int,input().split())
l=np.zeros(n,dtype=int)

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




    
minx = sys.maxsize

for bit in range(0,1<<2*n):
    da=0
    db=0
    dc=0
    cnt=0
    for i in range(0,n):
        if (bit>>2*i)&(0b11) ==0:
            continue
        elif (bit>>2*i)&(0b11) == 1:
            da+=l[i]
            cnt+=10
        elif (bit>>2*i)&(0b11) == 2:
            db+=l[i]
            cnt+=10
        elif (bit>>2*i)&(0b11) == 3:
            dc+=l[i]
            cnt+=10
    if da*db*dc==0:
        continue
    else:
        cnt+=abs(da-a)+abs(db-b)+abs(dc-c)-30
        
        
        if  cnt < minx:
            minx = cnt

print(minx)

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(dep, a,b,c):
    if dep == n:
        return abs(a-A) + abs(b-B) + abs(c-C) if min(a,b,c) > 0 else 10**8
    re0 = dfs(dep+1, a,b,c)
    re1 = dfs(dep+1, a+l[dep], b,c) + (10 if a != 0 else 0)
    re2 = dfs(dep+1, a, b+l[dep],c) + (10 if b != 0 else 0)
    re3 = dfs(dep+1, a, b, c+l[dep]) + (10 if c != 0 else 0)
    return min(re0, re1, re2, re3)

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):
from collections import defaultdict
import sys,heapq,bisect,math,itertools,string,queue,datetime
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inpl(): return list(map(int, input().split()))
def inpl_str(): return list(input().split())

N,A,B,C = inpl()
ll = [int(input()) for i in range(N)]

def saiki(cnt,a,b,c,MP):
    if cnt == N:
        if a==0 or b==0 or c==0:
            return INF
        MP += abs(a-A) + abs(b-B) + abs(c-C)
        return MP
    else:
        l = ll[cnt]
        if a == 0:
            tmpa = saiki(cnt+1,a+l,b,c,MP)
        else:
            tmpa = saiki(cnt+1,a+l,b,c,MP+10)
        if b == 0:
            tmpb = saiki(cnt+1,a,b+l,c,MP)
        else:
            tmpb = saiki(cnt+1,a,b+l,c,MP+10)
        if c == 0:
            tmpc = saiki(cnt+1,a,b,c+l,MP)
        else:
            tmpc = saiki(cnt+1,a,b,c+l,MP+10)
        tmpd = saiki(cnt+1,a,b,c,MP)
        return min(tmpa,tmpb,tmpc,tmpd)

print(saiki(0,0,0,0,0))

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

    
def dfs(i, a, b, c):
    if i == N:
        if (a == 0) or (b == 0) or (c == 0):
            return INF
        return abs(a-A) + abs(b-B) + abs(c-C)
    p = 0 if a == 0 else 10
    q = 0 if b == 0 else 10
    r = 0 if c == 0 else 10
    res1 = dfs(i+1, a, b, c)
    res2 = dfs(i+1, a+L[i], b, c) + p
    res3 = dfs(i+1, a, b+L[i], c) + q
    res4 = dfs(i+1, a, b, c+L[i]) + r
    return min(res1, res2, res3, res4)


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()))
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 + llist[cur], b, c) + 10
    ret2 = dfs(cur + 1, a, b + llist[cur], c) + 10
    ret3 = dfs(cur + 1, a, b, c + llist[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)]
case, ans = [], 10**10

for i in range(4**n):
    A, B, C = 0, 0, 0
    tmp_ans, tmp = 0, i
    cnt = 0
    for j in range(n):
        digit = tmp//4**(n-j-1)
        if digit == 1:
            A += l[j]
            cnt += 1
        if digit == 2:
            B += l[j]
            cnt += 1
        if digit == 3:
            C += l[j]
            cnt += 1
        tmp %= 4**(n-j-1)
    if A != 0 and B != 0 and C != 0:
        tmp_ans += abs(A-a)+abs(B-b)+abs(C-c)
        tmp_ans += max(0, (cnt-3)*10)
        ans = min(ans, tmp_ans)

print(ans)

Solution (Python):
import itertools

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

index_list = list(itertools.product(range(4), repeat=N))

min_mp_used = 100000000
for i in range(len(index_list)):
    tmp_index = index_list[i]
    kadomatsu = [0] * 3
    count_stick = 0
    for m in range(N):
        for j in range(3):
            if tmp_index[m] == j:
                kadomatsu[j] += l[m]
                count_stick += 1
    if kadomatsu.count(0):
        continue
    mp_used = 0
    for k in range(3):
        mp_used += abs(ans_kadomatsu[k] - kadomatsu[k])
    mp_used += 10 * (count_stick - 3)
    if min_mp_used > mp_used:
        min_mp_used = mp_used
print(min_mp_used)

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(a, b, c, k):
    if k == N:
        if min(a, b, c) > 0:
            return abs((a - A)) + abs(b - B) + abs(c - C) - 30
        else:
            return INF
    tmp = INF
    tmp = min(tmp, dfs(a, b, c, k + 1))
    tmp = min(tmp, dfs(a + L[k], b, c, k + 1) + 10)
    tmp = min(tmp, dfs(a, b + L[k], c, k + 1) + 10)
    tmp = min(tmp, dfs(a, b, c + L[k], k + 1) + 10)

    return tmp


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

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

Solution (Python):
from itertools import*
n,a,b,c,*l=map(int,open(0).read().split())
a=a,b,c
m=10**18
for s in product(*[range(4)]*n):
  if all(i in s for i in(0,1,2)):
    t,c=[0]*4,[0]*4
    for i,v in zip(s,l):
      t[i]+=v
      c[i]+=1
    m=min(m,sum(abs(x-y)+~-z*10for x,y,z in zip(a,t,c)))
print(m)

Solution (Python):
from itertools import*
n,a,b,c,*l=map(int,open(0).read().split())
a=a,b,c
m=10**18
for s in product(*[range(4)]*n):
  if all(i in s for i in(0,1,2)):
    t,c=[0]*4,[0]*4
    for i,v in zip(s,l):
      t[i]+=v
      c[i]+=1
    m=min(m,sum(abs(x-y)+~-z*10for x,y,z in zip(a,t,c)))
print(m)

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

import itertools

w = [0,1,2,3]

ss=1000000000000000
for ci in itertools.product(*([w]*N)):
  cc=[0,0,0,0]
  vc=[0,0,0]
  for i in range(N):
    cc[ci[i]]+=1
    if (ci[i]>0):
      vc[ci[i]-1]+=li[i]
  if ( cc[1]>0 and cc[2]>0 and cc[3]>0 ):
    _ss = abs(vc[0]-A)+abs(vc[1]-B)+abs(vc[2]-C)+(cc[1]+cc[2]+cc[3]-3)*10
    if (_ss<ss):
      ss = _ss
print ( ss )

Solution (Python):


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


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

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

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

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


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

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


def f(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 10 ** 9
    ans = f(a, b, c, n + 1)
    ans = min(ans, f(a + l[n], b, c, n + 1) + 10)
    ans = min(ans, f(a, b + l[n], c, n + 1) + 10)
    ans = min(ans, f(a, b, c + l[n], n + 1) + 10)
    return ans


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

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


def dfs(lances, d, len_a, len_b, len_c):
    if d == N:
        if min(len_a, len_b, len_c) == 0:
            return INF
        else:
            return abs(A-len_a) + abs(B-len_b) + abs(C-len_c) - 30
    use_no = dfs(lances, d+1, len_a, len_b, len_c)
    use_for_a = dfs(lances, d+1, len_a + lances[d], len_b, len_c) + 10
    use_for_b = dfs(lances, d+1, len_a, len_b + lances[d], len_c) + 10
    use_for_c = dfs(lances, d+1, len_a, len_b, len_c + lances[d]) + 10
    return min(use_no, use_for_a, use_for_b, use_for_c)


print(dfs(L, 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 ** 9

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 INF
    else:
        p = dfs(n+1, a, b, c)
        q = dfs(n+1, a+l[n], b, c) + 10
        r = dfs(n+1, a, b+l[n], c) + 10
        s = dfs(n+1, a, b, c+l[n]) + 10
        return min(p, q, r, s)

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

Solution (Python):
def grouping(data, n):
  return _grouping(data, n, 0)
  
    
def _grouping(data, n, c):
  if c==len(data):
    return [tuple([] for _ in range(n))]
  else:
    tg = data[c]
    b  = _grouping(data, n, c+1)
    a  = [] 
    for i in range(n):
      for t in b:
        a.append(tuple(e + [tg] if idx==i else e for idx, e in enumerate(t)))
    return a

n, a, b, c = map(int, input().split(" "))
reqs = (a, b, c)
ls = [int(input()) for _ in range(n)]
gr = filter(lambda x: len(x[0])!=0 and len(x[1])!=0 and len(x[2])!=0, grouping(ls, 4))
print(min(sum(10*(len(es)-1) + abs(sum(es)-r)for es, r in zip(t, reqs))for t in gr))

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

def dfs(cur, a, b, c):
  if cur == N:
    return abs(A - a) + abs(B - b) + abs(C - c) - 30 if min(a, b, c) > 0 else INF
  res0 = dfs(cur + 1, a, b, c)
  res1 = dfs(cur + 1, a + l[cur], b, c) + 10
  res2 = dfs(cur + 1, a, b + l[cur], c) + 10
  res3 = dfs(cur + 1, a, b, c + l[cur]) + 10
  return min(res0, res1, res2, res3)

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

Solution (Python):

def grouping(lst):
  if len(lst)==0:
    return [([],[],[],[])]
  else:
    elm = lst.pop()
    ans = []
    bf = grouping(lst)
    for i in range(4):
      for t in bf:
        ans.append(tuple(g + [elm] if idx==i else g for idx, g in enumerate(t)))
    return ans

n, a, b, c = map(int, input().split(" "))
reqs = (a, b, c)
ls = [int(input()) for _ in range(n)]
gr = filter(lambda x: len(x[0])!=0 and len(x[1])!=0 and len(x[2])!=0, grouping(ls))
print(min(sum(10*(len(es)-1) + abs(sum(es)-r)for es, r in zip(t, reqs))for t in gr))

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


def dfs(cur, a, b, c):
    if cur == N:
        return abs(a-A) + abs(b-B) + abs(c-C) - 30 if min(a, b, c) else 1000000000

    path_null = dfs(cur+1, a, b, c)
    path_a = dfs(cur+1, a+L[cur], b, c) + 10
    path_b = dfs(cur+1, a, b+L[cur], c) + 10
    path_c = dfs(cur+1, a, b, c+L[cur]) + 10

    return min(path_null, path_a, path_b, path_c)

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) 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())
length_array = [int(input()) for i in range(N)]


ans = 100000

for i in range(4**N):
    use_array = [[], [], []]
    for j in range(N):
        digit_j = (i & (3 << (j * 2))) >> (j * 2)
        if digit_j == 0:
            continue
        use_array[digit_j - 1].append(length_array[j])
    if len(use_array[0]) == 0 or len(use_array[1]) == 0 or len(
            use_array[2]) == 0:
        continue
    ans = min(ans,
              abs(sum(use_array[0]) - A) +
              (len(use_array[0]) - 1) * 10 + abs(sum(use_array[1]) - B) +
              (len(use_array[1]) - 1) * 10 + abs(sum(use_array[2]) - C) +
              (len(use_array[2]) - 1) * 10)
print(ans)

Solution (Python):
n, a, b, c = map(int, input().split())
l = sorted([int(input()) for i in range(n)])
ans = 10**10
for order in [[a, b, c],[b, c, a],[c, a, b],[a, c, b],[b, a, c],[c, b, a]]:
    mp = 0
    l_copy = l.copy()
    for goal in order:
        tem_mp = 10**10
        used = []
        for bit in range(1, 2 ** len(l_copy)):
            length = 0
            cost = 0
            count = 0
            tem_used = []
            for j in range(len(l_copy)):
                if (bit >> j) & 1:
                    length += l_copy[j]
                    count += 1
                    if count > 1:
                        cost += 10
                    tem_used.append(j)
            if tem_mp  > abs(goal - length) + cost:
                used = tem_used
                tem_mp = abs(goal - length) + cost
        mp += tem_mp
        for u in used[::-1]:
            l_copy.pop(u)
    ans = min(ans, mp)
print(ans)

Solution (Python):
a = [0]*3
n,a[0],a[1],a[2] = map(int,input().split())

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

def f(x):
  if len(x) == n:
    if not (0 in x and 1 in x and 2 in x):
      return 5000
    b = [0]*3
    c = -30
    for i in range(n):
      t = x[i]
      if t >= 0:
        b[t] += l[i]
        c += 10
    for i in range(3):
      c += abs(a[i]-b[i])
    return c
  else:
    return min(f(x+[-1]),f(x+[0]),f(x+[1]),f(x+[2]))
  
print(f([]))

Solution (Python):
a = [0]*4
n,a[1],a[2],a[3] = map(int,input().split())

l = []
for i in range(n):
  l += [int(input())]
  
Inf = 5000
  
def f(x):
  if len(x) == n:
    if not (1 in x and 2 in x and 3 in x):
      return Inf
    b = [0]*4
    c = -30
    for i in range(n):
      t = x[i]
      if t > 0:
        b[t] += l[i]
        c += 10
    for i in range(1,4):
      c += abs(a[i]-b[i])
    return c
  else:
    return min(f(x+[0]),f(x+[1]),f(x+[2]),f(x+[3]))
  
print(f([]))

Solution (Python):
INF=10**9

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

[n,A,B,C]=[int(i) for i in input().split()]
l=[]
for i in range(n):
    l.append(int(input()))

print(dfs(0,0,0,0,l,n))

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

def f(a,b,c,k):
    if k == N:
        if not a or not b or not c:
            return 10**9
        return abs(a-A)+abs(b-B)+abs(c-C)-30
    ans0 = f(a,b,c,k+1)
    ans1 = f(a+L[k],b,c,k+1)+10
    ans2 = f(a,b+L[k],c,k+1)+10
    ans3 = f(a,b,c+L[k],k+1)+10
    return min(ans0,ans1,ans2,ans3)

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

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


def solve(index, ma, mb, mc):
    if index == N:
        if len(ma) == 0 or len(mb) == 0 or len(mc) == 0:
            return INF
        cost = (len(ma) + len(mb) + len(mc) - 3) * 10
        cost += abs(sum(ma) - A)
        cost += abs(sum(mb) - B)
        cost += abs(sum(mc) - C)
        return cost
    t0 = solve(index + 1, ma, mb, mc)
    ta = solve(index + 1, ma + [L[index]], mb, mc)
    tb = solve(index + 1, ma, mb + [L[index]], mc)
    tc = solve(index + 1, ma, mb, mc + [L[index]])
    return min(t0, ta, tb, tc)


res = solve(0, [], [], [])
print(res)

Solution (Python):
N, A, B, C = [int(a) for a in input().split(" ")]
ls = [int(input()) for i in range(N)]
tab = [([float("inf")]*N, ref) for ref in (A, B, C)]
INF = float("inf")
from heapq import heappush, heappop, heapreplace
maxtarg = max(A, B, C)


def solve(ls):
    n = len(ls)
    qs = [[(-INF, -1)]*3 for i in range(3)]
    
    for ref, q in zip((A, B, C), qs):
        for i in range(n):
            val = -abs(ref-ls[i])
            if q[0][0] < val:
                heapreplace(q, (val, i))
    ans = INF
    
    for a, i in qs[0]:
        for b, j in qs[1]:
            for c, k in qs[2]:
                if i!=j and j!=k and k!=i:
                    ans = min(ans, -(a+b+c))
    return ans
dic = {}
def dfs(ls):
    key = tuple(sorted(ls))
    if key in dic:
        return dic[key]
    n = len(ls)
    if n < 3: return INF
    ans = solve(ls)
    for i in range(n-1):
        for j in range(i+1,n):
            tmp = ls[i]+ls[j]
            if min(maxtarg-ls[i], maxtarg-ls[j]) < 10:
                continue
            ls2 = ls[:i]+ls[i+1:]
            ls2[j-1] = tmp
            ans = min(ans, 10+dfs(ls2))
    dic[key] = ans
    return ans

print(dfs(ls))

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

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

def dfs(i, a, b, c):
    if i == N:
        if min(a, b, c) == 0:
            return 1000000000
        else:
            return sum([abs(A - a), abs(B - b), abs(C - c)]) - 30

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

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

Solution (Python):

import sys
from math import *
from itertools import *
from collections import *
from functools import *
try:
    from math import gcd
except Exception:
    from fractions import gcd


def solve(N: int, A: int, B: int, C: int, l: "List[int]"):
    e = [A, B, C]

    ret = float('inf')
    for i in range(4 ** N):
        ls = [0, 0, 0, 0]
        cs = [0, 0, 0, 0]
        for j, ll in enumerate(l):
            key = (i >> (2 * j)) & 0x03
            ls[key] += ll
            cs[key] += 10

        if cs[0] == 0 or cs[1] == 0 or cs[2] == 0:
            continue

        for i0, i1, i2 in permutations([0, 1, 2], 3):
            cc = cs[0] + abs(e[i0] - ls[0]) - 10
            cc += cs[1] + abs(e[i1] - ls[1]) - 10
            cc += cs[2] + abs(e[i2] - ls[2]) - 10
            if cc < ret:
                ret = cc
    return ret


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) ]  
    result = solve(N, A, B, C, l)
    print(result)

if __name__ == '__main__':
    main()

Solution (Python):
import math

N,A,B,C = list(map(int,input().split(" ")))
ls = []
for i in range(N):
    ls.append(int(input()))

def fun(rec,a,b,c):
    if rec == N:
        return abs(A-a) + abs(B-b) + abs(C-c)-30 if a*b*c != 0 else 10e20

    ans =  [fun(rec + 1 ,a,b,c) , fun(rec + 1 ,a+ls[rec],b,c) + 10 ,fun(rec + 1 ,a,b+ls[rec],c) + 10 ,fun(rec + 1 ,a,b,c+ls[rec])+10] 
    return min(ans)
print(fun(0,0,0,0))

Solution (Python):
N, A, B, C = map(int, input().split())
l = [int(input()) for i in range(N)]
def dfs(d, a, b, c):
  if d == N:
    res = abs(a - A) + abs(b - B) + abs(c - C) - 30
    return 1e9 if min(a, b, c) == 0 else res
  ret = [0] * 4
  ret[0] = dfs(d + 1, a, b, c)
  ret[1] = dfs(d + 1, a + l[d], b, c) + 10
  ret[2] = dfs(d + 1, a, b + l[d], c) + 10
  ret[3] = dfs(d + 1, a, b, c + l[d]) + 10
  return min(ret)
print(dfs(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 magic(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 = magic(cur+1, a, b, c)
    ret1 = magic(cur+1, a+L[cur], b, c) + 10
    ret2 = magic(cur+1, a, b+L[cur], c) + 10
    ret3 = magic(cur+1, a, b, c+L[cur]) + 10
    return min(ret0,ret1,ret2,ret3)
print(magic(0,0,0,0))

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

INF = 1e10

def solve(cur, a, b, c):
    if cur >= N:
        if min(a, b, c) == 0:
            return INF
        return -30 + sum(map(abs, [a - A, b - B, c - C]))
    return min(
        10 + solve(cur + 1, a + L[cur], b, c),
        10 + solve(cur + 1, a, b + L[cur], c),
        10 + solve(cur + 1, a, b, c + L[cur]),
        solve(cur + 1, a, b, c),
    )

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

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


INF = 10 ** 9

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

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

Solution (Python):
n,a,b,c, = map(int,input().split())
bb = [int(input()) for i in range(n)]
res = []
for j in range (4**n-1) :
  t = str(bin(j))
  tt = t[2:]
  ttt = "0" * (2*n-len(tt)) + tt
  tttt = tuple(ttt)
  ttttt = tuple(map (int,tttt))
  mp = 0
  lg = [0,0,0,0]
  for take in range(n) :
      use = ttttt[take*2]*2 + ttttt[take*2+1]
      mp += 10
      if lg[use] == 0:
          mp -= 10
      lg[use] += bb[take]
      lg[3] = 0 
  if lg[0]*lg[1]*lg[2] == 0 :
      mp=10000000
  mp += abs(a-lg[0])
  mp += abs(b-lg[1])
  mp += abs(c-lg[2])
  res.append(mp)
print (min(res))

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

patterns = [[0,0,0]]

def check(index, a, b, c, mp):
    
    if index == N:
        
        if a > 0 and b > 0 and c > 0:
            cost = abs(A - a) + abs(B - b) + abs(C - c) + mp
        else:
            cost = 1000 * 1000 * 10
        return cost

    cost1 = check(index + 1, a , b, c, mp)
    if a == 0:
        cost2 = check(index + 1, a + L[index] , b, c, mp)
    else:
        cost2 = check(index + 1, a + L[index] , b, c, mp + 10)

    if b == 0:
        cost3 = check(index + 1, a, b + L[index], c, mp)
    else:
        cost3 = check(index + 1, a, b + L[index], c, mp+ 10)

    if c == 0:
        cost4 = check(index + 1, a , b, c + L[index], mp)
    else:
        cost4 = check(index + 1, a , b, c + L[index], mp+10)

    return min([cost1, cost2, cost3, cost4])

print(check(0, 0, 0, 0, 0))

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

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

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

Solution (Python):


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

    INF = 10**10

    def dfs(depth, a, b, c):
        """ 各竹を、
        ・Aの合成魔法に使う
        ・Bの合成魔法に使う
        ・Cの合成魔法に使う
        ・使わない

        a:= 現在のAの長さ
        b:= 現在のBの長さ
        c:= 現在のCの長さ

        返り値は、MP
        """
        if depth == N:
            if min(a, b, c) > 0:
                return abs(a-A)+abs(b-B)+abs(c-C) - 30 
            else:
                return INF
        
        ans1 = dfs(depth+1, a, b, c)
        ans2 = dfs(depth+1, a+L[depth], b, c) + 10
        ans3 = dfs(depth+1, a, b+L[depth], c) + 10
        ans4 = dfs(depth+1, a, b, c+L[depth]) + 10

        return min(ans1, ans2, ans3, ans4)

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

if __name__ == "__main__":
    solve()