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 combinations

def f(in_A,in_B,in_C,A,B,C,ex_l,counter):
    if (counter == len(ex_l)):
        return abs(in_A-A)+abs(in_B-B)+abs(in_C-C)
    else:
        tmp = ex_l[counter]
        counter +=1
        a1 = f(in_A+tmp,in_B,in_C,A,B,C,ex_l,counter)+10
        a2 = f(in_A,in_B+tmp,in_C,A,B,C,ex_l,counter)+10
        a3 = f(in_A,in_B,in_C+tmp,A,B,C,ex_l,counter)+10
        a4 = f(in_A,in_B,in_C,A,B,C,ex_l,counter)
    return min(a1,a2,a3,a4)

N,A,B,C=map(int,input().split())
l = []
for i in range(N):
    l.append(int(input()))
in_l = list(map(list,combinations(l,3)))
ex_l = [l[:] for _ in range(len(in_l))]
for i in range(len(in_l)):
    ex_l[i].remove(in_l[i][0])
    ex_l[i].remove(in_l[i][1])
    ex_l[i].remove(in_l[i][2])

mp = []
for i in range(len(in_l)):
    mp.append(f(in_l[i][0],in_l[i][1],in_l[i][2],A,B,C,ex_l[i],0))
    mp.append(f(in_l[i][0],in_l[i][1],in_l[i][2],A,C,B,ex_l[i],0))
    mp.append(f(in_l[i][0],in_l[i][1],in_l[i][2],B,A,C,ex_l[i],0))
    mp.append(f(in_l[i][0],in_l[i][1],in_l[i][2],B,C,A,ex_l[i],0))
    mp.append(f(in_l[i][0],in_l[i][1],in_l[i][2],C,A,B,ex_l[i],0))
    mp.append(f(in_l[i][0],in_l[i][1],in_l[i][2],C,B,A,ex_l[i],0))
mp = min(mp)
print(mp)

Solution (Python):
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 float('inf')

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

    return min(ret1,ret2,ret3,ret4)



if __name__ == "__main__":
    N,A,B,C = map(int,input().split())
    l = [int(input()) for _ in range(N)]
    print(dfs(0,0,0,0))

Solution (Python):
from itertools import combinations

def f(in_A,in_B,in_C,A,B,C,ex_l,counter):
    if (counter == len(ex_l)):
        return abs(in_A-A)+abs(in_B-B)+abs(in_C-C)
    else:
        tmp = ex_l[counter]
        counter +=1
        a1 = f(in_A+tmp,in_B,in_C,A,B,C,ex_l,counter)+10
        a2 = f(in_A,in_B+tmp,in_C,A,B,C,ex_l,counter)+10
        a3 = f(in_A,in_B,in_C+tmp,A,B,C,ex_l,counter)+10
        a4 = f(in_A,in_B,in_C,A,B,C,ex_l,counter)
    return min(a1,a2,a3,a4)

N,A,B,C=map(int,input().split())
l = []
for i in range(N):
    l.append(int(input()))
in_l = list(map(list,combinations(l,3)))
ex_l = [l[:] for _ in range(len(in_l))]
for i in range(len(in_l)):
    ex_l[i].remove(in_l[i][0])
    ex_l[i].remove(in_l[i][1])
    ex_l[i].remove(in_l[i][2])

mp = 1000000
for i in range(len(in_l)):
    tmp = f(in_l[i][0],in_l[i][1],in_l[i][2],A,B,C,ex_l[i],0)
    if(mp>tmp):
        mp = tmp
for i in range(len(in_l)):
    tmp = f(in_l[i][0],in_l[i][1],in_l[i][2],A,C,B,ex_l[i],0)
    if(mp>tmp):
        mp = tmp
for i in range(len(in_l)):
    tmp = f(in_l[i][0],in_l[i][1],in_l[i][2],B,A,C,ex_l[i],0)
    if(mp>tmp):
        mp = tmp
for i in range(len(in_l)):
    tmp = f(in_l[i][0],in_l[i][1],in_l[i][2],B,C,A,ex_l[i],0)
    if(mp>tmp):
        mp = tmp
for i in range(len(in_l)):
    tmp = f(in_l[i][0],in_l[i][1],in_l[i][2],C,A,B,ex_l[i],0)
    if(mp>tmp):
        mp = tmp
for i in range(len(in_l)):
    tmp = f(in_l[i][0],in_l[i][1],in_l[i][2],C,B,A,ex_l[i],0)
    if(mp>tmp):
        mp = tmp
print(mp)

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

Solution (Python):
N, A, B, C = map(int, input().split())
lst = [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 + lst[cur], b, c) + 10
    ret2 = dfs(cur + 1, a, b + lst[cur], c) + 10
    ret3 = dfs(cur + 1, a, b, c + lst[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 = []
for i in range(N):
    L.append(int(input()))
    
import itertools
m = 4
bit_list = list(itertools.product([i for i in range(m)], repeat=N))
ans = 10 ** 18
for bit in bit_list:
    retA = 0
    retB = 0
    retC = 0
    checkA = False
    checkB = False
    checkC = False
    cost = 0
    for i,j in enumerate(bit):
        if j == 1:
            retA += L[i]
            if checkA:
                cost += 10
            checkA = True
        if j == 2:
            retB += L[i]
            if checkB:
                cost += 10
            checkB = True
        if j == 3:
            retC += L[i]
            if checkC:
                cost += 10
            checkC = True
    if min(retA,retB,retC) != 0:
        ans = min(ans, abs(A-retA)+abs(B-retB)+abs(C-retC)+cost)
print(ans)

Solution (Python):
from itertools import product
N, A, B, C = map(int, input().split())
l = [int(input()) for _ in range(N)]
ans = min(
    sum(
        abs(sum(x for k, x in zip(p, l) if k == r) - y) + 10 * (p.count(r) - 1)
        for r, y in enumerate((A, B, C), 1)
    )
    for p in product(range(4), repeat=N)
    if {1, 2, 3} <= set(p)
)
print(ans)

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


def f(idx, mp, a, b, c):
    if idx == N:
        if a==0 or b==0 or c==0:
            return float('inf')
        else:
            d = abs(A - a) + abs(B - b) + abs(C - c)
            return mp + d

    l = L[idx]
    
    if a == 0:
        f1 = f(idx + 1, mp, l, b, c)
    else:
        f1 = f(idx + 1, mp + 10, a + l, b, c)

    
    if b == 0:
        f2 = f(idx + 1, mp, a, l, c)
    else:
        f2 = f(idx + 1, mp + 10, a, b + l, c)

    
    if c == 0:
        f3 = f(idx + 1, mp, a, b, l)
    else:
        f3 = f(idx + 1, mp + 10, a, b, c + l)

    
    f4 = f(idx + 1, mp, a, b, c)

    return min(f1, f2, f3, f4)


ans = f(0, 0, 0, 0, 0)
print(ans)

Solution (Python):
N,A,B,C = map(int,input().split())
l = []
cand = []
for i in range(N):
    l.append(int(input()))
for i in range(4**N):
    a,b,c = [],[],[]
    for j in range(N):
        tmp = i // (4**j) % 4
        if tmp == 1:
            a.append(l[j])
        elif tmp == 2:
            b.append(l[j])
        elif tmp == 3:
            c.append(l[j])
    if a != [] and b != [] and c != []:
        al,bl,cl = sum(a),sum(b),sum(c)
        cand.append(abs(al-A)+abs(bl-B)+abs(cl-C)+10*(len(a)+len(b)+len(c)-3))
print(min(cand))

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

take = [int(input()) for i 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+take[n], b, c) + 10
  ret1 = dfs(n+1, a, b+take[n], c) + 10
  ret2 = dfs(n+1, a, b, c+take[n]) + 10
  ret3 = dfs(n+1, a, b, c)
  return min(ret0, ret1, ret2, ret3)

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

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

    ans = editorial(N, A, B, C, ls)
    print(ans)


def editorial(N, A, B, C, ls):
    def dfs(cur, a, b, c):
        if cur == N:
            if min([a, b, c]) > 0:
                
                tmp = abs(a - A) + abs(b - B) + abs(c - C) - 30
            else:
                tmp = float("inf")
            
            return tmp

        
        d = ls[cur]
        ret0 = dfs(cur + 1, a, b, c)
        ret1 = dfs(cur + 1, a + d, b, c) + 10
        ret2 = dfs(cur + 1, a, b + d, c) + 10
        ret3 = dfs(cur + 1, a, b, c + d) + 10

        return min([ret0, ret1, ret2, ret3])

    ans = dfs(0, 0, 0, 0)
    return ans


if __name__ == '__main__':
    main()

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

Solution (Python):

import itertools
import math
import copy

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

pattern = [list(itertools.combinations(range(x), 3)) for x in range(3, N+1)]

def stretch_shortening(sticks, A, B, C):
    min_cost = 1e10
    
    for i1, i2, i3 in pattern[len(sticks) - 3]:
        cost = abs(sticks[i1] - A) + abs(sticks[i2] - B) + abs(sticks[i3] - C)
        if min_cost > cost:
            min_cost = cost
    return min_cost

temp_set = set()
def connect(sticks, A, B, C):
    
    min_cost = stretch_shortening(sticks, A, B, C) + (N - len(sticks)) * 10
    
    if len(sticks) <= 3:
        return min_cost
    
    for i1, i2 in itertools.combinations(range(len(sticks)), 2):
        if sticks[i1] + sticks[i2] > sticks[-1] and sticks[-1] >= C:
            continue
        if sticks[i1] <= 10 or sticks[i2] <= 10:
            continue
        
        copied_sticks = copy.copy(sticks)
        copied_sticks[i1] += copied_sticks[i2]
        copied_sticks.pop(i2)
        copied_sticks = sorted(copied_sticks)
        
        len_set = len(temp_set)
        temp_set.add(''.join(["{:05d}".format(x) for x in copied_sticks]))
        if len_set == len(temp_set):
            continue
        
        min_cost_connect = connect(copied_sticks, A, B, C)

        if min_cost_connect < min_cost:
            min_cost = min_cost_connect
    
    return min_cost

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

min_cost = connect(sticks, C, B, A) 



print(min_cost)

Solution (Python):
import numpy as np

def calc(i,x):
    if i ==0:
        return int((x % 4 == 1) or (x % 4 == 2))
    else:
        return int((x % (2**(i+1)) >= 2**i) and (x % 2 == 0))

def main():
    N,A,B,C = map(int, input().split())
    
    l = [int(input()) for i in range(N)]
    
    num = 4**N
    cre = 5000
    
    for i in range(num):
        num_A = 0
        num_B = 0
        num_C = 0
        new_cre_A = 0
        new_cre_B = 0
        new_cre_C = 0
        s = i
        for m in range(N):
            num_A += int(s % 4 == 1)
            num_B += int(s % 4 == 2)
            num_C += int(s % 4 == 3)
            s = s // 4
            
        
        
        s = i
        if num_A > 0 and num_B > 0 and num_C > 0:
            for j in range(N):
                new_cre_A += l[j]*int(s % 4 == 1)
                new_cre_B += l[j]*int(s % 4 == 2)
                new_cre_C += l[j]*int(s % 4 == 3)
                s = s // 4
                
            new_cre_A = abs(new_cre_A - A) + (num_A-1) * 10
            new_cre_B = abs(new_cre_B - B) + (num_B-1) * 10
            new_cre_C = abs(new_cre_C - C) + (num_C-1) * 10
            new_cre_A = new_cre_A + new_cre_B + new_cre_C
            
            
            if cre > new_cre_A:
                cre = new_cre_A

    print(cre)
    
main()

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

l = []


for i in range(0,N):
  inp = int(input())
  l.append(inp)


def select_bamboo(choice,mp,ID,a,b,c,aa,bb,cc):
  if ID > N-1 and aa>= 1 and bb >= 1 and cc >= 1:
    return mp + abs(A-a) + abs(B-b) + abs(C-c)
  elif ID > N-1:
    return 10**12
  
  if choice == "a":
    a += l[ID]
    if aa>=1:
      mp += 10
    aa += 1
    reta = min(select_bamboo("a",mp,ID+1,a,b,c,aa,bb,cc), select_bamboo("b",mp,ID+1,a,b,c,aa,bb,cc), select_bamboo("c",mp,ID+1,a,b,c,aa,bb,cc), select_bamboo("x",mp,ID+1,a,b,c,aa,bb,cc))
    return reta
  if choice == "b":
    b += l[ID]
    if bb>=1:
      mp += 10
    bb += 1
    retb = min(select_bamboo("a",mp,ID+1,a,b,c,aa,bb,cc),select_bamboo("b",mp,ID+1,a,b,c,aa,bb,cc),select_bamboo("c",mp,ID+1,a,b,c,aa,bb,cc),select_bamboo("x",mp,ID+1,a,b,c,aa,bb,cc))
    return retb
  if choice == "c":
    c += l[ID]
    if cc>=1:
      mp += 10
    cc += 1
    retc = min(select_bamboo("a",mp,ID+1,a,b,c,aa,bb,cc),select_bamboo("b",mp,ID+1,a,b,c,aa,bb,cc),select_bamboo("c",mp,ID+1,a,b,c,aa,bb,cc),select_bamboo("x",mp,ID+1,a,b,c,aa,bb,cc))
    return retc
  if choice == "x":
    retx = min(select_bamboo("a",mp,ID+1,a,b,c,aa,bb,cc),select_bamboo("b",mp,ID+1,a,b,c,aa,bb,cc),select_bamboo("c",mp,ID+1,a,b,c,aa,bb,cc),select_bamboo("x",mp,ID+1,a,b,c,aa,bb,cc))
    return retx
  

print(min(select_bamboo("a",0,0,0,0,0,0,0,0),select_bamboo("b",0,0,0,0,0,0,0,0),select_bamboo("c",0,0,0,0,0,0,0,0),select_bamboo("x",0,0,0,0,0,0,0,0)))

Solution (Python):
N,*target = map(int,(input().split()))
l = [int(input()) for i in range(N)]

cnt = 0
out = None
for i in range(4**N):
    j = [(i//(4**k) % 4) for k in range(N)]
    num = [j.count(i) for i in range(3)]
    if num[0]==0 or num[1]==0 or num[2]==0:
        continue
    score = sum([abs(target[k]-sum([l[tmp] for tmp in range(N) if j[tmp]==k])) for k in range(3)]) + (sum(num)-3)*10
    if out == None:
        out = score
    else:
        out = min(out,score)
print(out)

Solution (Python):
N, A, B, C = map(int, input().split())
L = [int(input()) for _ 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
  ret0 = dfs(n + 1, a, b, c) 
  ret1 = dfs(n + 1, a + L[n], b, c) + 10 
  ret2 = dfs(n + 1, a, b + L[n], c) + 10 
  ret3 = dfs(n + 1, a, b, c + L[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 _ in range(N)]

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

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

def dfs(depth, t, ans):
    if depth == n:
        sum = calc(t)
        if sum < ans:
            return sum
        else:
            return ans

    for j in range(4):
        tt = t.copy()
        tt.append(j)
        ans = dfs(depth + 1, tt, ans)

    return ans

def calc(t):
    ttt = [ [0, 0] ] * 3

    for i, v in enumerate(t):
        if v < 3:
            ttt[v] = [ttt[v][0] + 1, ttt[v][1] + l[i]]

    if ttt[0][0] == 0 or ttt[1][0] == 0 or ttt[2][0] == 0:
        return 1000000000

    sum = 0
    abc = [a, b, c, 0]
    for i, v in enumerate(ttt):
        sum += ((v[0] - 1) * 10) + abs(v[1] - abc[i])

    return sum


ans = dfs(0, [], 1000000000)
print(ans)

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

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

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

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

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

Solution (Python):


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

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

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

Solution (Python):


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

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

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

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

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

ans = 1e9
for idxs in product(range(4), repeat=n):
    la, lb, lc = [], [], []
    for idx, l in zip(idxs, ls):
        if idx == 0:
            la.append(l)
        elif idx == 1:
            lb.append(l)
        elif idx == 2:
            lc.append(l)
    if len(la) and len(lb) and len(lc):
        mpa = max(len(la) - 1, 0) * 10 + abs(sum(la) - a)
        mpb = max(len(lb) - 1, 0) * 10 + abs(sum(lb) - b)
        mpc = max(len(lc) - 1, 0) * 10 + abs(sum(lc) - c)
        ans = min(ans, mpa + mpb + mpc)
print(ans)

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


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

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

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

import itertools
m = 4
n = N
bit_list = list(itertools.product([i for i in range(m)], repeat=n))

res = 10 ** 18
for b in bit_list:
    tmp1 = 0
    tmp2 = 0
    tmp3 = 0
    check1 = False
    check2 = False
    check3 = False
    cost = 0
    for i in range(n):
        if b[i] == 1:
            tmp1 += L[i]
            if check1:
                cost += 10
            check1 = True       
            
        if b[i] == 2:
            tmp2 += L[i]
            if check2:
                cost += 10
            check2 = True
            
        if b[i] == 3:
            tmp3 += L[i]
            if check3:
                cost += 10
            check3 = True
    if min(tmp1, tmp2, tmp3) != 0:
        res = min(res, abs(tmp1 - A)+abs(tmp2 - B)+abs(tmp3 - C)+cost)
print(res)

Solution (Python):
N,A,B,C=map(int,input().split())
L=[int(input()) for i in range(N)]
answ=10**9
for i in range(1<<N):
    for j in range(1<<N):
        M=[0]*4
        num=[0]*4
        for k in range(N):
            s=(1<<k)&i
            t=(1<<k)&j
            if s:
                if t:
                    continue
                else:
                    M[1]+=L[k]
                    num[1]+=1
            else:
                if t:
                    M[2]+=L[k]
                    num[2]+=1
                else:
                    M[3]+=L[k]
                    num[3]+=1
        ans=abs(A-M[1])+abs(B-M[2])+abs(C-M[3])
        for l in range(1,4):
            if num[l]==0:
                ans=10**9
            elif num[l]!=0:
                ans+=(num[l]-1)*10
        

        answ=min(ans,answ)
print(answ)

Solution (Python):
class ans:
  def __init__(self):
    self.N, self.A, self.B, self.C = map(int,input().split())
    self.L = [int(input()) for _ in range(self.N)]
    print(self.dfs(0, 0, 0, 0))
    exit()

  def dfs(self, x, a, b, c):
    if(x == self.N):
      if(min(a, b, c) > 0):
        return abs(a-self.A)+abs(b-self.B)+abs(c-self.C) - 30
      else:
        return 1e9+7
    ret0 = self.dfs(x+1, a, b, c)
    ret1 = self.dfs(x+1, a+self.L[x], b, c) + 10
    ret2 = self.dfs(x+1, a, b+self.L[x], c) + 10
    ret3 = self.dfs(x+1, a, b, c+self.L[x]) + 10
    return min(ret0, ret1, ret2, ret3)

ans()

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

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

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

Solution (Python):
data = input().split()
N = int(data[0])
A = int(data[1])
B = int(data[2])
C = int(data[3])

i = 0
kd = []
while i < N:
  kd.append(int(input()))
  i += 1


cur = 0 
sc = 0
def ans(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 1000000
  ret0 = ans(cur+1,a,b,c)
  ret1 =  ans(cur+1,a+kd[cur],b,c) + 10
  ret2 = ans(cur + 1,a,b+kd[cur],c) +10 
  ret3 = ans(cur+1,a,b,c+kd[cur])+ 10
  return min(ret0,ret1,ret2,ret3)

print(ans(0,0,0,0))

Solution (Python):
n, A, B, C = (int(i) for i in 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):


INF = 10**9

def solve():
    N, A, B, C = [int(x) for x in input().split(" ")]
    ls = []
    for _ in range(N):
        ls.append(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 INF
        ret0 = _dfs(depth+1, a, b, c)  
        ret1 = _dfs(depth+1, a+ls[depth], b, c) + 10  
        ret2 = _dfs(depth+1, a, b+ls[depth], c) + 10  
        ret3 = _dfs(depth+1, a, b, c+ls[depth]) + 10  
        
        return min(ret0, ret1, ret2, ret3)

    print(_dfs(0,0,0,0))


if __name__ == "__main__":
    solve()

Solution (Python):


INF = 10**9

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


def solve():
    N, A, B, C = [int(x) for x in input().split(" ")]
    ls = []
    for _ in range(N):
        ls.append(int(input()))
    
    print(dfs(0,0,0,0, A, B, C, N, ls))
    
    


if __name__ == "__main__":
    solve()

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


INF = 10**9


def dfs(n_bamboo, a, b, c):
    if n_bamboo == N:
        if a != 0 and b != 0 and c != 0:
            
            return abs(A - a) + abs(B - b) + abs(C - c) - 30
        else:
            return INF
    else:
        return1 = dfs(n_bamboo+1, a, b, c)
        return2 = dfs(n_bamboo+1, a + L[n_bamboo], b, c)+10
        return3 = dfs(n_bamboo+1, a, b+L[n_bamboo], c)+10
        return4 = dfs(n_bamboo+1, a, b, c+L[n_bamboo])+10
        return min([return1, return2, return3, return4])


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

Solution (Python):
N,a,b,c = map(int, input().split())
L = [int(input()) for i in range(N)]
m = []
def m_dep(s):
    if len(s) == N:
        if '1' in s and '2' in s and '3' in s:
            m.append(calc([int(i) for i in s]))
    else:
        for i in range(4):
            m_dep(s + str(i))

def calc(l):
    A=0
    B=0
    C=0
    M=0
    for i,n in enumerate(l):
        if n==1:
            if A!=0:
                M+=10
            A+=L[i]
        if n==2:
            if B!=0:
                M+=10
            B+=L[i]
        if n==3:
            if C!=0:
                M+=10
            C+=L[i]
    M += abs(a-A)+abs(b-B)+abs(c-C)
    return(M)


m_dep('')
print(min(m))

Solution (Python):
from itertools import product


def solve(string):
    n, a, b, c, *l = map(int, string.split())
    ans = 3000
    for p in product(range(4), repeat=n):
        if not ({0, 1, 2} <= set(p)):
            continue
        sums = [0, 0, 0, 0]
        for _p, _l in zip(p, l):
            sums[_p] += _l
        mp12 = sum(abs(_s - _abc) for _s, _abc in zip(sums, [a, b, c]))
        mp3 = sum(1 for _p in p if _p < 3) - 3
        ans = min(ans, mp12 + mp3 * 10)
    return str(ans)


if __name__ == '__main__':
    n, a, b, c = map(int, input().split())
    print(solve('{} {} {} {}\n'.format(n, a, b, c) + '\n'.join([input() for _ in range(n)])))

Solution (Python):
from itertools import product


def solve(string):
    n, a, b, c, *l = map(int, string.split())
    ans = 3000
    for p in product(range(4), repeat=n):
        if not ({0, 1, 2} <= set(p)):
            continue
        sums = [0, 0, 0, 0]
        for _p, _l in zip(p, l):
            sums[_p] += _l
        mp12 = sum(abs(_s - _abc) for _s, _abc in zip(sums[:3], [a, b, c]))
        mp3 = sum(1 for _p in p if _p < 3) - 3
        ans = min(ans, mp12 + mp3 * 10)
    return str(ans)


if __name__ == '__main__':
    n, a, b, c = map(int, input().split())
    print(solve('{} {} {} {}\n'.format(n, a, b, c) + '\n'.join([input() for _ in range(n)])))

Solution (Python):
from itertools import permutations, combinations


def solve(string):
    n, a, b, c, *l = map(int, string.split())
    ans = 3000
    for _l in permutations(l):
        for i, j, k in combinations(range(1, len(l) + 1), 3):
            tmp = 10 * (i - 1) + abs(a - sum(_l[:i]))
            if tmp > ans:
                continue
            tmp += 10 * (j - i - 1) + abs(b - sum(_l[i:j]))
            if tmp > ans:
                continue
            tmp += 10 * (k - j - 1) + abs(c - sum(_l[j:k]))
            if tmp < ans:
                ans = tmp
    return str(ans)


if __name__ == '__main__':
    n, a, b, c = map(int, input().split())
    print(solve('{} {} {} {}\n'.format(n, a, b, c) + '\n'.join([input() for _ in range(n)])))

Solution (Python):
from itertools import permutations, combinations


def solve(string):
    n, a, b, c, *l = map(int, string.split())
    ans = 3000
    for _l in permutations(l):
        for i, j, k in combinations(range(1, len(l) + 1), 3):
            tmp = 10 * (i - 1) + abs(a - sum(_l[:i])) + 10 * (j - i - 1) + abs(
                b - sum(_l[i:j])) + 10 * (k - j - 1) + abs(c - sum(_l[j:k]))
            if tmp < ans:
                ans = tmp
    return str(ans)


if __name__ == '__main__':
    n, a, b, c = map(int, input().split())
    print(solve('{} {} {} {}\n'.format(n, a, b, c) + '\n'.join([input() for _ in range(n)])))

Solution (Python):
from itertools import permutations


def solve(string):
    n, a, b, c, *l = map(int, string.split())
    ans = 3000

    for _l in permutations(l):
        for i in range(1, len(l) - 1):
            tmp_a = 10 * (i - 1) + abs(a - sum(_l[:i]))
            if tmp_a >= ans:
                continue
            for j in range(i + 1, len(l)):
                tmp_b = 10 * (j - i - 1) + abs(b - sum(_l[i:j]))
                if tmp_a + tmp_b >= ans:
                    continue
                for k in range(j + 1, len(l) + 1):
                    tmp = tmp_a + tmp_b + 10 * (k - j - 1) + abs(c - sum(_l[j:k]))
                    if tmp < ans:
                        ans = tmp
    return str(ans)


if __name__ == '__main__':
    n, a, b, c = map(int, input().split())
    print(solve('{} {} {} {}\n'.format(n, a, b, c) + '\n'.join([input() for _ in range(n)])))