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
input = sys.stdin.readline


NABC=list(map(int, input().split()))
l = [int(input()) for _ in range(NABC[0])]
l.sort(reverse=True)
done = [0]*NABC[0]

ans = 1e+8
for i in range(4**NABC[0]):
    A=0
    B=0
    C=0
    A_count=-1
    B_count=-1
    C_count=-1
    i_tmp = i
    for j in range(NABC[0]):
        bit = i_tmp%4
        i_tmp = i_tmp//4
        if bit == 0: 
            A += l[j]
            A_count += 1
        if bit == 1:
            B += l[j]
            B_count += 1
        if bit == 2:
            C += l[j]
            C_count += 1
    if A_count==-1 or B_count==-1 or C_count==-1: continue
    tmp = abs(A-NABC[1])+abs(B-NABC[2])+abs(C-NABC[3])+max(0,A_count*10)+max(0,B_count*10)+max(0,C_count*10)
    if tmp < ans:
        ans = tmp
print(ans)

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

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

Solution (Python):
def main():
    N, A, B, C = map(int, input().split())
    l = [int(input()) for i in range(N)]
    
    ans = int(1e10)
    for i in range(4**N):
        tmp = [0, 0, 0, 0]
        count = [0, 0, 0, 0]
        for j in range(N):
            tmp[i&3] += l[j]
            count[i&3] += 1
            i >>= 2
        if tmp[0] > 0 and tmp[1] > 0 and tmp[2] > 0:
            mp = abs(tmp[0] - A) + abs(tmp[1] - B) + abs(tmp[2] - C) + (count[0]+count[1]+count[2]-3) * 10
            
            ans = min(ans, mp)
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
def dfs(depth):
    if depth == N:
        ans.append(cal(v))
        return
    for i in range(4):
        v[depth] = i
        dfs(depth+1) 

def cal(v):
    tmp = [0] * 3 
    for i in range(N):
        if v[i] == 0:
            tmp[0] += l[i]
        elif v[i] == 1:
            tmp[1] += l[i]
        elif v[i] == 2:
            tmp[2] += l[i]
    cnt = 0
    if v.count(0) > 1:
        cnt += v.count(0) - 1
    if v.count(1) > 1:
        cnt += v.count(1) - 1
    if v.count(2) > 1:
        cnt += v.count(2) - 1

    if v.count(0) == 0 or v.count(1) == 0 or v.count(2) == 0:
        return float('inf')
    diff = cnt * 10
    for i in range(3):
        diff += abs(A[i]-tmp[i])
    
    return diff
        
    
    

N, *A= map(int, input().split())
l = [int(input()) for _ in range(N)]
v = [-1] * N

ans = []

dfs(0)
print(min(ans))

Solution (Python):

import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)






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

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

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

Solution (Python):
import sys
import math
from fractions import gcd





def lcm(a, b):
    return a * b // gcd(a, b)

def combination_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

def permutations_count(n, r):
    return math.factorial(n) // math.factorial(n - r)

big_prime = 1000000007

"""


 =  sys.stdin.readline().rstrip()
 =  list(sys.stdin.readline().rstrip())


 =  int(sys.stdin.readline())
 =  map(int, sys.stdin.readline().split())
 =  list(map(int, sys.stdin.readline().split()))
 =  [list(map(int,list(sys.stdin.readline().split()))) for i in range(N)]
"""

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

def dfs(idx, a, b, c):
    if idx >= N:
        return abs(A - a) + abs(B - b) + abs(C - c) - 30 if min(a, b, c) > 0 else INF
    n_t = dfs(idx + 1, a, b, c)
    a_t = dfs(idx + 1, a + l[idx], b, c) + 10
    b_t = dfs(idx + 1, a, b + l[idx], c) + 10
    c_t = dfs(idx + 1, a, b, c + l[idx]) + 10
    return min(n_t, a_t, b_t, c_t)

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

Solution (Python):
N, A, B, C = map(int,input().split())
ABC = [0, A,B,C]
L = [int(input()) for _ in range(N)]
import itertools
ans = 100000000
for flags in itertools.product(range(4), repeat=N):
    tmp = [[0, 0] for _ in range(4)]
    for i in range(N):
        tmp[flags[i]][0] += L[i]
        tmp[flags[i]][1] += 1
    mp = 0
    for i in range(1, 4):
        if tmp[i][1] == 0:
            mp = 100000000
            break
        mp += abs(ABC[i] - tmp[i][0])
        mp += (tmp[i][1] - 1) * 10
    if mp < ans:
        ans = mp
print(ans)

Solution (Python):
import math
import copy
import sys
import fractions
import numpy as np
from functools import reduce



def getInputNum():
    inputNum = int(input())
    return inputNum

def getInputListInt():
    outputoData = []
    inputData = input().split()
    outputData = [int(n) for n in inputData]

    return outputData

def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList

def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList



n, A, B, C = map(int, input().split())
data = getSomeInputInt(n)

def dfs(cnt, a, b, c):
    if cnt == n:
        return abs(a-A)+abs(b-B)+abs(c-C)-30 if min(a,b,c) > 0 else sys.maxsize

    nothing = dfs(cnt+1, a, b, c)
    ap = dfs(cnt+1, a+data[cnt], b, c) + 10
    bp = dfs(cnt+1, a, b+data[cnt], c) + 10
    cp = dfs(cnt+1, a, b, c + data[cnt]) + 10

    return min(nothing, ap, bp, cp)

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

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N, A, B, C = MAP()
l = [INT() 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 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):
def calc_cost( data_list, target ):
    num = len(data_list)
    if num == 0:
        return -1
    length = sum( data_list )
    cost = (num - 1) * 10 
    cost += abs( target - length)
    return cost 

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

for _ in range(N):
    data.append(int(input()))

ans = -1
for n in range(4**N):
    pat = n 
    data_a = []
    data_b = []
    data_c = []

    for i in range( N ):
        l = pat % 4
        if l == 0:
            pass
        elif l == 1:
            data_a.append(data[i])
        elif l == 2:
            data_b.append(data[i])
        elif l == 3:
            data_c.append(data[i])
        pat //= 4

    cost_a = calc_cost( data_a, A)
    cost_b = calc_cost( data_b, B)
    cost_c = calc_cost( data_c, C)

    if cost_a != -1 and cost_b != -1 and cost_c != -1:
        cost = cost_a+cost_b+cost_c
        if ans == -1:
            ans = cost
        else:
            if ans > cost:
                ans = cost

print(ans)

Solution (Python):
import sys
import math
sys.setrecursionlimit(200000)
input = sys.stdin.readline

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

ans = 10 ** 18
def dfs(index, z, al):
    global ans
    if len(al) == N:
        if len(set(al) - {0}) < 3:
            return

        m = {
            1: 0,
            2: 0,
            3: 0,
        }
        mp = 0
        for i in range(N):
            v = al[i]
            if v == 0:
                continue
            if m[v] != 0:
                mp += 10
            m[v] += L[i]
        ans = min(ans, mp + abs(A - m[1]) + abs(B - m[2]) + abs(C - m[3]))
        return

    dfs(index + 1, 0, al + [0])
    dfs(index + 1, 1, al + [1])
    dfs(index + 1, 2, al + [2])
    dfs(index + 1, 3, al + [3])
    return

dfs(0, 0, [])

print(ans)

Solution (Python):
from itertools import product
N, a, b, c = map(int, input().split())
L = []
for i in range(N):
  L.append(int(input()))

abc = [0,a,b,c]
ch = [0,1,2,3]
cal = []

for i in product(ch,repeat=N):
  calc = 0
  su = [0,0,0,0]
  ct = [0,0,0,0]
  temp = list(i)
  for j in range(N):
    su[temp[j]] += L[j]
    ct[temp[j]] += 1
  if (ct[1]==0)|(ct[2]==0)|(ct[3]==0):
    continue
  for k in range(1,4):
    calc += abs(abc[k]-su[k])
    calc += max((ct[k]-1),0)*10
  cal.append(calc)
    
print(min(cal))

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


def ten_to_n(X, n):
    if (int(X / n)):
        return ten_to_n(int(X / n), n) + str(X % n)
    return str(X % n)

ans = float("inf")
for i in range(4**N):
    I = ten_to_n(i, 4)
    a, b, c = 0, 0, 0
    temp = 0
    for j in range(min(len(I), N)):
        J = I[-j-1]
        if J == "1":
            a += L[j]
            temp += 10
        elif J == "2":
            b += L[j]
            temp += 10
        elif J == "3":
            c += L[j]
            temp += 10
    if a == 0 or b == 0 or c == 0:
        continue
    temp += abs(A - a) + abs(B - b) + abs(C - c) - 30
    ans = min(ans, temp)
print(ans)

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

Solution (Python):
from itertools import product
n,a,b,c=map(int,input().split())
abc=[a,b,c]
l=tuple(int(input()) for _ in range(n))

ans=10**18
for a in product(range(4), repeat=n):
    MP=0
    take=[[] for _ in range(4)]

    for i in range(n):
        take[a[i]].append(l[i])
    
    if take[0] and take[1] and take[2]:
        for i,t in enumerate(take[:-1]):
            MP+=max(0, 10*(len(t)-1))
            MP+=abs(abc[i] - sum(t))
    else:
        continue

    ans=min(ans, MP) 

print(ans)

Solution (Python):
from itertools import product
n, *A = map(int, input().split())
L = [int(input()) for _ in range(n)]
m_ = sum(abs(A[i]-L[i]) for i in range(3))
for P in product(range(4), repeat = n):
    C = [[] for _ in range(4)]
    m = 0
    for i in range(n):
        C[P[i]].append(L[i])
    C = C[1:]
    if [] in C: continue
    for i in range(3):
        m += (len(C[i])-1)*10+abs(sum(C[i])-A[i])
    m_ = min(m_, m)
print(m_)

Solution (Python):
def makelist(n, m):
    return [[0 for _ in range(m)] for _ in range(n)]

N, A, B, C = map(int, input().split())
l = [0]*(N+1)
for i in range(1, N+1):
    l[i] = int(input())


ans = int(1e18)
for n in range(1 << N * 2):
    use = [0]*(N+1)
    for i in range(1, N+1):
        use[i] = (n >> ((i-1)*2)) % 4

    hoge = [[], [], [], []]
    for i in range(1, N+1):
        hoge[use[i]].append(i)

    flag = False
    for e in hoge[:3]:
        if len(e) == 0:
            flag = True
    if flag:
        continue

    tmp = 0
    fuga = [A, B, C]
    for i in range(3):
        now = hoge[i]
        tmp += 10 * (len(now) - 1)
        tmp += abs(fuga[i] - sum(map(lambda x: l[x], now)))

    ans = min(ans, tmp)

print(ans)

Solution (Python):
from itertools import product, combinations

n, *abc = list(map(int, input().split()))
ll = [int(input()) for _ in range(n)]




ans = float("inf")
for n_use in range(3, n+1) : 
    for l_use in combinations(ll, n_use) : 
        for dist in product(*[range(3) for _ in range(n_use)]) : 
            if len(set(dist)) != 3 :
                continue

            labc = [0, 0, 0]
            for i, l in zip(dist, l_use) :
                labc[i] += l
    
            diff = [abs(labc[i]-abc[i]) for i in range(3)]
            ans = min(ans, 10*(n_use-3)+sum(diff))

print(ans)

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

    cur = dict()
    cur[(0, 0, 0)] = 0
    for i in range(N):
        temp = dict()
        for t in cur:
            for j in range(3):
                cost = cur[t]
                lv = list(t)
                merge = False
                if lv[j] != 0:
                    merge = True
                lv[j] += L[i]
                newt = tuple(lv)
                if newt not in temp:
                    temp[newt] = cost + 10 * (merge == True)
                else:
                    temp[newt] = min(temp[newt], cost + 10 * (merge == True))
        for t in temp:
            if t not in cur:
                cur[t] = temp[t]
            else:
                cur[t] = min(cur[t], temp[t])

    ans = float('inf')
    for t in cur:
        lv = list(t)
        if lv[0] > 0 and lv[1] > 0 and lv[2] > 0:
            v = abs(lv[0]-A) + abs(lv[1]-B) + abs(lv[2]-C) + cur[t]
            ans = min(ans, v)

    return ans

if __name__ == '__main__':
    print(main())

Solution (Python):
n, a, b, c = map(int, input().split())
l = [int(input()) for _ in range(n)]
INF = 10**9
def count(a_sum, b_sum, c_sum, cur):
  if cur == n:
    return (abs(a_sum -a) + abs(b_sum -b) +abs(c_sum -c)) -30 if min(a_sum, b_sum, c_sum) > 0 else INF
  else:
    ans1 = count(a_sum, b_sum, c_sum, cur+1)
    ans2 = count(a_sum+l[cur], b_sum, c_sum, cur+1) + 10 
    ans3 = count(a_sum, b_sum+l[cur], c_sum, cur+1) + 10
    ans4 = count(a_sum, b_sum, c_sum+l[cur], cur+1) + 10
    return min(ans1, ans2, ans3, ans4)

print(count(0, 0, 0, 0))

Solution (Python):
n,A,B,C,=map(int,input().split())
l=[int(input()) for i in range(n)]
INF=1e9

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

Solution (Python):
import itertools
n, a, b, c = map(int, input().split())
abc = sorted([a, b, c])
ll = [int(input()) for _ in range(n)]
ls = [0, 1, 2, 3]
m_cost = 10 ** 9
for i in itertools.product(ls, repeat=n):
    t = [[0, 0] for _ in range(4)]
    if 1 in i and 2 in i and 3 in i:
        for j in range(n):
            t[i[j]][0] += ll[j]
            t[i[j]][1] += 1
        st = sorted([t[1][0], t[2][0], t[3][0]])
        c = (t[1][1] + t[2][1] + t[3][1] - 3) * 10
        c += abs(abc[0] - st[0]) + abs(abc[1] - st[1]) + abs(abc[2] - st[2])
        m_cost = min(m_cost, c)
print(m_cost)

Solution (Python):

import sys

INF = 10**9


def dfs(cur, ia, ib, ic):
    if(cur == N):
        return abs(ia-A) + abs(ib-B) + abs(ic-C) - 30 if min(ia,ib,ic) > 0 else INF

    ret0 = dfs(cur+1, ia, ib, ic)
    ret1 = dfs(cur+1, ia+l[cur], ib, ic) + 10
    ret2 = dfs(cur+1, ia, ib+l[cur], ic) + 10
    ret3 = dfs(cur+1, ia, ib, ic+l[cur]) + 10
    return min(ret0, ret1, ret2, ret3)


def solve(N: int, A: int, B: int, C: int, l: "List[int]"):
    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()
    global N, A, B, C, l
    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

INF = 10**9


def dfs(cur, l, ia, ib, ic):
    if(cur == N):
        return abs(ia-A) + abs(ib-B) + abs(ic-C) - 30 if min(ia,ib,ic) > 0 else INF

    ret0 = dfs(cur+1, l, ia, ib, ic)
    ret1 = dfs(cur+1, l, ia+l[cur], ib, ic) + 10
    ret2 = dfs(cur+1, l, ia, ib+l[cur], ic) + 10
    ret3 = dfs(cur+1, l, ia, ib, ic+l[cur]) + 10
    return min(ret0, ret1, ret2, ret3)


def solve(N: int, A: int, B: int, C: int, l: "List[int]"):
    print(dfs(0,l,0,0,0))

    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    global N, A, B, C, l
    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
import math
from collections import defaultdict


sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]


mod = 10**9 + 7


def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N):
    read_all = [tuple(map(int, input().split())) for _ in range(N)]
    return map(list,zip(*read_all))



N,A,B,C = II()
l = [I() for _ in range(N)]
inf = float('inf')

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

print(solve(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):
INF=10**9
n,A,B,C=map(int,input().split())
l=[int(input()) for i in range(n)]
ans=INF
a=[0,0,0,0]
def yonsin(X):
    if(int(X/4)):
        return yonsin(int(X/4))+str(X%4)
    return str(X%4)


for i in range(4**n):
  s=yonsin(i)
  if len(s)<n:
    s='0'*(n-len(s))+s
  d=list(s)
  cost=0
  a=[0,0,0,0]
  if set(d)!={'1','2','3'} and set(d)!={'0','1','2','3'}:
    cost=INF
  else:  
    for j in range(n):          
      a[int(s[j])]+=l[j]
      if int(s[j])!=0:
        cost+=10
    cost+=abs(a[1]-A)+abs(a[2]-B)+abs(a[3]-C)-30
    

  ans=min(ans,cost)

print(ans)

Solution (Python):
import itertools

N, A, B, C = map(int, input().split())
l = [0] * N
for i in range(N):
  l[i] = int(input())
mplist = []
for r in itertools.product([0, 1, 2, 3], repeat = N):
  mp = 0
  use = 0
  le = [0] * 4
  for i in range(N):
    le[r[i]] += l[i]
    if r[i] >= 1:
      use += 1
  if le[1] == 0 or le[2] == 0 or le[3] == 0:
    continue
  mp = (use - 3) * 10 + abs(le[1] - A) + abs(le[2] - B) + abs(le[3] - C)
  mplist.append(mp)
print(min(mplist))

Solution (Python):

INF = float("inf")
MOD = int(1e9 + 7)
def int1(n):
    return int(n) - 1
def parse(args, *params):
    return tuple(p(v) for p, v in zip(params, next(args).split()))


def main(args):
    N, A, B, C = parse(args, int, int, int, int)
    L = tuple(int(next(args)) for _ in range(N))

    def solve(n, l, a, b, c, cost):
        if n == 0:
            if not all((a, b, c)): return INF
            return abs(A - a) + abs(B - b) + abs(C - c) + cost - 30
        r = []
        r += [solve(n - 1, l, a, b, c, cost)]
        r += [solve(n - 1, l, a + l[n - 1], b, c, cost + 10)]
        r += [solve(n - 1, l, a, b + l[n - 1], c, cost + 10)]
        r += [solve(n - 1, l, a, b, c + l[n - 1], cost + 10)]
        return min(r)

    print(solve(N, L, 0, 0, 0, 0))

import sys
if __name__ == '__main__':
    main(iter(sys.stdin.read().split("\n")))

Solution (Python):

n, A, B, C = map(int, input().split())
L = [int(input()) for _ in range(n)]
INF = float('inf')
mixMP = 10

def rec(idx, a, b, c):
  if idx == n:
    return abs(A-a) + abs(B-b) + abs(C-c)  - mixMP*3 if min(a,b,c)>0 else INF
  res1 = rec(idx+1,a,b,c) 
  res2 = rec(idx+1,a+L[idx],b,c) + mixMP 
  res3 = rec(idx+1,a,b+L[idx],c) + mixMP 
  res4 = rec(idx+1,a,b,c+L[idx]) + mixMP 
  return min(res1, res2, res3, res4)

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

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

b4 = ["0"]*(4**n)
def B4(X):
  if X == "0":
    return "0"
  elif b4[X] != "0":
    return b4[X]
  if (int(X/4)):
    b4[X] = B4(int(X/4))+str(X%4)
    return B4(int(X/4))+str(X%4)
  else:
    b4[X] = str(X%4)
  return str(X%4)
def f(x):
  return [int(j) for j in ["0"]*(n-len(list(x)))+list(x)]

m = float('inf')
for i in range(4**n):
  t = f(B4(i))
  cost = 0
  for j in range(3):
    cost += 10*(max(t.count(j)-1,0))
  length = [0,0,0,0]
  for j in range(n):
    length[t[j]] += l[j]
  if min(length[0:3]) == 0:
    continue
  cost += abs(a-length[0])+abs(b-length[1])+abs(c-length[2])
  
  
  m = min(m,cost)
print(m)

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

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

Solution (Python):
import itertools

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

case = list(itertools.product(range(4), repeat=N))
ans = 10 ** 7

for i in range(len(case)):
    if case[i].count(1) != 0 and case[i].count(2) != 0 and case[i].count(3) != 0:
        listA = []
        listB = []
        listC = []    
        for j in range(N):
            if case[i][j] == 1:
                listA.append(l[j])
            elif case[i][j] == 2:
                listB.append(l[j])
            elif case[i][j] == 3:
                listC.append(l[j])
        ansA = abs(sum(listA) - A) + 10 * (len(listA) - 1)
        ansB = abs(sum(listB) - B) + 10 * (len(listB) - 1)
        ansC = abs(sum(listC) - C) + 10 * (len(listC) - 1)


        anstmp = ansA + ansB + ansC

        if anstmp < ans :
            ans = anstmp
    else:
        pass

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

answer = 3000
container = [0]*n
for _ in range(4**n):
    abcx = [0,0,0,0]
    for i in range(n):
        container[i] += 1
        if container[i] == 4:
            container[i] = 0
        else:
            break
    for i in range(n):
        for j in range(4):
            if container[i] == j:
                if j == 3:
                    abcx[j] += 1
                else:
                    abcx[j] += l[i]
    if abcx[0] == 0 or abcx[1] == 0 or abcx[2] == 0:
        continue
    else:
        need_mp = (n-3-abcx[3])*10 + abs(abcx[0]-a) + abs(abcx[1]-b) + abs(abcx[2]-c)
        if need_mp < answer:
            answer = need_mp

print(answer)

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

    x = dfs(cur+1, a, b, c)
    y = dfs(cur+1, a+l[cur], b, c)+10
    z = dfs(cur+1, a, b+l[cur], c)+10
    w = dfs(cur+1, a, b, c+l[cur])+10
    return min(x,y,z,w)

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

Solution (Python):
import sys

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

x=[int(input()) for _ in range(n)]
inf=10**9

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

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
    else:
        cost0 = dfs(cur+1,a,b,c)
        cost1 = dfs(cur+1,a+l[cur],b,c)+10
        cost2 = dfs(cur+1,a,b+l[cur],c)+10
        cost3 = dfs(cur+1,a,b,c+l[cur])+10
        return min(cost0,cost1,cost2,cost3)

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

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

def kado(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
  cal1=kado(cur+1,a,b,c)
  cal2=kado(cur+1,a+lis[cur],b,c)+10
  cal3=kado(cur+1,a,b+lis[cur],c)+10
  cal4=kado(cur+1,a,b,c+lis[cur])+10
  return min(cal1,cal2,cal3,cal4)
print(kado(0,0,0,0))

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

MP = []


start = 0 
end = (4**N)*1 
for n in range(start, end):
  n4 = ""
  while n > 0:
    mod = n % 4
    n4 = str(mod) + n4
    n //= 4
  if ("1" not in n4) or ("2" not in n4) or ("3" not in n4):
    continue
  if len(n4) < N:
    n4 = "0"*(N-len(n4)) + n4
  
  
  IA = [i for i, x in enumerate(n4) if x == "1"]
  IB = [j for j, y in enumerate(n4) if y == "2"]
  IC = [k for k, z in enumerate(n4) if z == "3"]
  
  LA = 0
  mp_A = -10
  for i in IA:
    LA += L[i]
    mp_A += 10
  mp_A += abs(A - LA)
  
  LB = 0
  mp_B = -10
  for i in IB:
    LB += L[i]
    mp_B += 10
  mp_B += abs(B - LB)
  
  LC = 0
  mp_C = -10
  for i in IC:
    LC += L[i]
    mp_C += 10
  mp_C += abs(C - LC)
  
  mp = mp_A + mp_B + mp_C
  MP.append(mp)
  

ans = min(MP)
print(ans)

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

def dfs(x,a,b,c):
    if x == N:
        return (abs(a-A) + abs(b-B) + abs(c-C) -30) if min(a,b,c)> 0 else float('inf')
    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 = map(int,input().split())
l = [int(input()) for i in range(n)]
inf = 10 ** 9

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

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