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


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

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

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


def calc(q):
    score = 0
    x = y = z = 0
    xflag = yflag = zflag = False
    for i in range(n):
        if q[i] == 0:
            x += l[i]
            if xflag:
                score += 10
            xflag = True
        elif q[i] == 1:
            y += l[i]
            if yflag:
                score += 10
            yflag = True
        elif q[i] == 2:
            z += l[i]
            if zflag:
                score += 10
            zflag = True
    if xflag and yflag and zflag:
        return abs(a-x) + abs(b-y) + abs(c-z) +score
    else:
        return 100000000000000


ans = 100000000000000
def dfs(q):
    global ans
    if len(q) >= n:
        res = calc(q)
        ans = min(ans,res)
        return
    else:
        for i in range(4):
            q.append(i)
            dfs(q)
            q.pop(-1)
    return ans


dfs([])

print(dfs([]))

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

bamboos = []
for _ in range(N):
    l = int(input())
    bamboos.append(l)
    
import copy

def dfs(i, bs):
    if i == len(bamboos):
        
        if not bs['A'] or not bs['B'] or not bs['C']:
            return []
        return [bs]
    ret = []
    tmp = copy.deepcopy(bs)
    tmp['A'].append(bamboos[i])
    ret.extend(dfs(i+1, tmp))
    tmp = copy.deepcopy(bs)
    tmp['B'].append(bamboos[i])
    ret.extend(dfs(i+1, tmp))
    tmp = copy.deepcopy(bs)
    tmp['C'].append(bamboos[i])
    ret.extend(dfs(i+1, tmp))
    tmp = copy.deepcopy(bs)
    tmp['D'].append(bamboos[i])
    ret.extend(dfs(i+1, tmp))
    
    return ret

ABC = {'A': A, 'B': B, 'C': C}
bs_comb = dfs(0, {'A':[],'B':[],'C':[],'D':[]})

import sys
ans = sys.maxsize
for bs in bs_comb:
    cost = 0
    for abc in ['A', 'B', 'C']:
        cost += (len(bs[abc]) - 1) * 10
        cost += abs(sum(bs[abc]) - ABC[abc])
    ans = min(ans, cost)
print(ans)

Solution (Python):
n,A,B,C=map(int,input().split())
l=[int(input()) for _ in range(n)]
t=10**9
def dfs(level,a,b,c):
  if level==n:
    return abs(a-A)+abs(b-B)+abs(c-C)-30 if min(a,b,c)>0 else t
  k1=dfs(level+1,a,b,c)
  k2=dfs(level+1,a+l[level],b,c)+10
  k3=dfs(level+1,a,b+l[level],c)+10
  k4=dfs(level+1,a,b,c+l[level])+10
  return min(k1,k2,k3,k4)
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 search(cou, x, y, z):
    if cou == n:
        return abs(x-a) + abs(y-b) + abs(z-c) - 30 if min(x, y, z) > 0 else INF
    ret0 = search(cou+1, x, y, z)    
    ret1 = search(cou+1, x+L[cou], y, z) + 10
    ret2 = search(cou+1, x, y+L[cou], z) + 10
    ret3 = search(cou+1, x, y, z+L[cou]) + 10
    return min(ret0, ret1, ret2, ret3)

print(search(0, 0, 0, 0))

Solution (Python):

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


def scorecalc(a,target):
    if len(a) == 0:
        ans = 10**9
    else:
        ans = 10*(len(a)-1)
        ans += abs(sum(a)-target)
    return ans

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

mnm = 10**9

for i in range(1,4**n+1):
    test = Base_10_to_n(i,4)
    test = list(test)
    for k in range(n-len(test)):
        test.insert(0,"0")
    fora = []
    forb = []
    forc = []
    for k in range(n):
        if test[k] == "0":
            fora.append(l[k])
        elif test[k] == "1":
            forb.append(l[k])
        elif test[k] == "2":
            forc.append(l[k])
    temp = scorecalc(fora,a) + scorecalc(forb,b) + scorecalc(forc,c)
    if temp < mnm:
        mnm = temp
    
print(mnm)

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

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

Solution (Python):
import itertools

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

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

ans = 999999999
count = 0

products = list(itertools.product([0, 1, 2, 3], repeat=N))

for p in products:
    scores_g = [[0, 0], [0, 0], [0, 0], [0, 0]]
    for i in range(len(p)):
        scores_g[p[i]][0] += l[i]
        scores_g[p[i]][1] += 1
    for g in range(3):
        if scores_g[g][1] == 0:
            count = 999999999
            continue
        else:
            count += abs(scores_g[g][0] - t[g]) + (scores_g[g][1] - 1) * 10
    if ans > count:
        ans = count
    count = 0

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

def dfs(cnt,a,b,c):
    if cnt == N:
        return abs(A-a)+abs(B-b)+abs(C-c) if min(a,b,c) > 0 else 10**9

    l  = dfs(cnt+1,a,b,c)
    la = dfs(cnt+1,a+list[cnt],b,c) + (10 if a != 0 else 0)
    lb = dfs(cnt+1,a,b+list[cnt],c) + (10 if b != 0 else 0)
    lc = dfs(cnt+1,a,b,c+list[cnt]) + (10 if c != 0 else 0)
    return min(l,la,lb,lc)

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

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

    print(solve(a, b, c, l))

def solve(a, b, c, l):
    m = float('inf')
    for x in range(1, 4 ** len(l)):
        m = min(m, eval_cost(x, a, b, c, l))
    return m

def eval_cost(x, a, b, c, l):
    cnt = [0, 0, 0]
    y = [0, 0, 0]

    for i in range(len(l)):
        n = (x >> (i * 2)) & 0x03
        if n < 3:
            cnt[n] += 1
            y[n] += l[i]

    if cnt.count(0) != 0:
        return float('inf')
    
    return (sum(cnt) - 3) * 10 + abs(y[0] - a) + abs(y[1] - b) + abs(y[2] - c)

main()

Solution (Python):
from itertools import product
n,a,b,c = map(int,input().split())
l = [int(input()) for i in range(n)]
x = list(product([0, 1, 2, 3], repeat=n))
ans = 10**10
for i in range(len(x)):
    if x[i].count(1) == 0 or x[i].count(2) == 0 or x[i].count(3) == 0:
        continue
    tmpa = 0
    tmpb = 0
    tmpc = 0
    for j in range(n):
        if x[i][j] == 1:
            tmpa += l[j]
        if x[i][j] == 2:
            tmpb += l[j]
        if x[i][j] == 3:
            tmpc += l[j]
    tmpans = abs(a-tmpa)+abs(b-tmpb)+abs(c-tmpc)
    tmpans += (x[i].count(1)-1)*10 + (x[i].count(2)-1)*10 + (x[i].count(3)-1)*10
    ans = min(ans,tmpans)
print(ans)

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

MAX = 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 MAX
  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 = [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 = []
for i in range(N):
	l.append(int(input()))
X = []
for i in range(4 ** N):
	num = []
	for j in range(N):
		num.append(i % 4)
		i //= 4
	a, b, c, x = 0, 0, 0, -30
	for k in range(N):
		if num[k] == 0:
			a += l[k]
			x += 10
		elif num[k] == 1:
			b += l[k]
			x += 10
		elif num[k] == 2:
			c += l[k]
			x += 10
	if a * b * c != 0:
		x += abs(A - a) + abs(B - b) + abs(C - c)
		X.append(x)
print(min(X))

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

def find(i, A, B, C):
	if i == n:
		return abs(A-a) + abs(B-b) + abs(C-c) if min(A, B, C) > 0 else 9999999999999999999999999999
	else:
		return min(
			find(i + 1, A       , B       , C       ),
			find(i + 1, A + l[i], B       , C       ) + (10 if A > 0 else 0),
			find(i + 1, A       , B + l[i], C       ) + (10 if B > 0 else 0),
			find(i + 1, A       , B       , C + l[i]) + (10 if C > 0 else 0),
		)

print(find(0, 0, 0, 0))

Solution (Python):
N,A,B,C = list(map(int,input().split()))
L = [int(input()) for i in range(N)]
MIN = 99999999999
def solve(a,b,c,an,bn,cn,k):
    global MIN
    if k == N:
        if not (an == 0 or bn == 0 or cn == 0):
            MIN = min(MIN,abs(A-a)+abs(B-b)+abs(C-c)+10*(an+bn+cn-3))
        return
    else:
        solve(a+L[k],b,c,an+1,bn,cn,k+1)
        solve(a,b+L[k],c,an,bn+1,cn,k+1)
        solve(a,b,c+L[k],an,bn,cn+1,k+1)
        solve(a,b,c,an,bn,cn,k+1)
solve(0,0,0,0,0,0,0)
print(MIN)

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

min_ans = 10 ** 10
for i in range(4 ** n):
	mm = [[], [], [],[]]
	tmp = i
	for i2 in range(n):
		g = tmp // (4 ** (n - i2 - 1))
		tmp = tmp % (4 ** (n - i2 - 1))
		mm[g%4].append(ls[i2])
	
	if len(mm[0]) * len(mm[1]) * len(mm[2]) == 0:
		continue

	
	ans = abs(sum(mm[0]) - a) + abs(sum(mm[1]) - b) + abs(sum(mm[2]) - c) + (len(mm[0]) - 1) * 10 + (len(mm[1]) - 1) * 10 + (len(mm[2]) - 1) * 10
	min_ans = min(ans, min_ans)

print(min_ans)

Solution (Python):

import sys


def solve(N: int, A: int, B: int, C: int, l: "List[int]"):
    result = 10000000000
    for i in range(2**(N*2) -1):
        bitBase = i | 2**(N*2)
        debug = False
        
        
        mergeCost = 0
        a = 0
        b = 0
        c = 0
        invalid = False
        debugStr = ""
        for j in range(N):
            flag = ((3 << (j * 2)) & bitBase) >> (j * 2)
            if flag == 1:
                if a != 0:
                    mergeCost+= 10
                if debug:
                    debugStr += "A"
                a += l[j]
            elif flag == 2:
                if b != 0:
                    mergeCost += 10
                if debug:
                    debugStr += "B"
                b += l[j]
            elif flag == 3:
                if c != 0:
                    mergeCost += 10 
                if debug:
                    debugStr += "C"               
                c+= l[j]
            else:
                if debug:
                    debugStr += "X"
        if a == 0 or b == 0 or c == 0:
            continue
        cost = mergeCost + abs(A-a) + abs(B - b) + abs(C - c)
        if debug:
            print(debugStr)
        
        
        result = min(cost,result)
    print(result)
    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


def solve(N: int, A: int, B: int, C: int, l: "List[int]"):
    result = 10000000000
    for i in range(2**(N*2) -1):
        bitBase = i | 2**(N*2)
        debug = False
        
        
        mergeCost = 0
        a = 0
        b = 0
        c = 0
        invalid = False
        debugStr = ""
        for j in range(N):
            flag = ((3 << (j * 2)) & bitBase) >> (j * 2)
            if flag == 1:
                if a != 0:
                    mergeCost+= 10
                if debug:
                    debugStr += "A"
                a += l[j]
            elif flag == 2:
                if b != 0:
                    mergeCost += 10
                if debug:
                    debugStr += "B"
                b += l[j]
            elif flag == 3:
                if c != 0:
                    mergeCost += 10 
                if debug:
                    debugStr += "C"               
                c+= l[j]
            else:
                if debug:
                    debugStr += "X"
        if a == 0 or b == 0 or c == 0:
            continue
        cost = mergeCost + abs(A-a) + abs(B - b) + abs(C - c)
        if debug:
            print(debugStr)
        
        
        result = min(cost,result)
    print(result)
    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):
def base10to(n, b):
    if n // b:
        return base10to(n // b, b) + str(n % b)
    return str(n % b)


nabc = list(map(int, input().split()))
n = nabc[0]
a = nabc[1]
b = nabc[2]
c = nabc[3]
l_list = []
for i in range(n):
    l_list.append(int(input()))
min_MP = 3000
for i in range(4 ** n):
    mix = "00000000" + base10to(i, 4)
    if mix.count("1") * mix.count("2") * mix.count("3") == 0:
        continue
    MP = (mix.count("1")  + mix.count("2") + mix.count("3") - 3) * 10
    b_a = 0
    b_b = 0
    b_c = 0
    for j in range(n):
        if mix[- j - 1] == "1":
            b_a += l_list[j]
        elif mix[- j - 1] == "2":
            b_b += l_list[j]
        elif mix[- j - 1] == "3":
            b_c += l_list[j]
    MP += abs(b_a - a) + abs(b_b - b) + abs(b_c - c)
    if MP < min_MP:
        min_MP = MP
print(min_MP)

Solution (Python):
N, A, B, C = map(int, (input().split(' ')))
a_list = list(map(int, [input() for _ in range(N)]))
kado = [0] * 3
calc = []

def calc_mp(kado, score):
    calc.append(abs(A - kado[0]) + abs(B - kado[1]) + abs(C - kado[2]) + score)


def solve(kado, a_list, score):
    if all([k != 0 for k in kado]):
        calc_mp(kado, score)
    if a_list == []:
        return
    for i in range(4):
        if i == 3:
            solve(kado, a_list[1:], score)
        else:
            empty = [0, 0, 0]
            empty[i] += a_list[0]
            solve([i + j for i, j in zip(kado, empty)], a_list[1:], score + 10)

solve(kado, a_list, 0)
print(min(calc) - 30)

Solution (Python):
magic = 10**4
N,a,b,c = map(int,input().split())
src = [int(input()) for _ in range(N)]
count = [0]*N

for i in range(4**N):
    a_num = [0,0]
    b_num = [0,0]
    c_num = [0,0]
    tmpi = i
    for j in range(N)[::-1]:
        num = src[j]
        abc = tmpi//(4**j)
        tmpi %= 4**j
        if abc == 0:
            a_num[0] += num
            a_num[1] += 1
        elif abc == 1:
            b_num[0] += num
            b_num[1] += 1
        elif abc == 2:
            c_num[0] += num
            c_num[1] += 1
    if a_num[1] > 0 and b_num[1] > 0 and c_num[1] > 0:
        magic = min(abs(a-a_num[0]) + abs(b-b_num[0]) + abs(c-c_num[0]) + (a_num[1]+b_num[1]+c_num[1]-3)*10,magic)
        
print(magic)

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

def magic(cur,a,b,c):
  if cur==N:
    if a*b*c!=0:
      return abs(a-A)+abs(b-B)+abs(c-C)-30
    else:
      return 10**9
  r0=magic(cur+1,a,b,c)
  r1=magic(cur+1,a+l[cur],b,c)+10
  r2=magic(cur+1,a,b+l[cur],c)+10
  r3=magic(cur+1,a,b,c+l[cur])+10
  return min(r0,r1,r2,r3)

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

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

def dfs(depth):
    if depth == N:
        a, b, c = 0, 0, 0
        flag_a, flag_b, flag_c  = False, False, False
        cost = 0
        for i in range(N):
            if used[i] == 0:
                if flag_a:
                    cost += 10
                a += l[i]
                flag_a = True
            elif used[i] == 1:
                if flag_b:
                    cost += 10
                b += l[i]
                flag_b = True
            elif used[i] == 2:
                if flag_c:
                    cost += 10
                c += l[i]
                flag_c = True
            else:
                pass
        if min(a, b, c) > 0:
            cost += abs(a - A) + abs(b - B) + abs(c - C)
            return cost
        else:
            return 10**9

    for i in range(4):
        used[depth] = i
        cost_list.append(dfs(depth + 1))

    return 10**9

dfs(0)

print(min(cost_list))

Solution (Python):

from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LS()
    return l
sys.setrecursionlimit(1000000)
mod = 1000000007


def A():
    n = I()
    ans = float("inf")
    for i in range(n+1):
        m = 0
        k = i
        while k > 0:
            m += k%6
            k //= 6
        k = n-i
        while k > 0:
            m += k%9
            k //= 9
        if m < ans:
            ans = m
    print(ans)
    return


def B():
    n = I()
    x = [None for i in range(n)]
    y = [None for i in range(n)]
    h = [None for i in range(n)]
    for i in range(n):
        x[i],y[i],h[i] = LI()
    if n == 1:
        print(x[0],y[0],h[0])
        quit()
    for cy in range(101):
        for cx in range(101):
            for i in range(n):
                if h[i] > 0:break
            ch = h[i]+abs(x[i]-cx)+abs(y[i]-cy)
            f = 1
            for i in range(n):
                if max(ch-abs(x[i]-cx)-abs(y[i]-cy),0) != h[i]:f = 0
            if f:
                print(cx,cy,ch)
                quit()
    return


def C():
    def dfs(m,k):
        if m == n:
            lis.append(k)
        else:
            for i in range(4):
                dfs(m+1,k+[i])
    n,a,b,c = LI()
    an = [a,b,c]
    l = IR(n)
    lis = []
    dfs(0,[])
    ans = float("inf")
    for k in lis:
        s = [0,0,0,0]
        d = [0,0,0,0]
        for i in range(n):
            s[k[i]] += l[i]
            d[k[i]] += 1
        m = 0
        for i in range(3):
            m += 10*max(0,d[i]-1)
            if s[i] == 0:
                m = float("inf")
                break
            m += abs(an[i]-s[i])
        if m < ans:
            ans = m
    print(ans)
    return


def D():
    return


def E():
    return


def F():
    return


def G():
    return


def H():
    return


def I_():
    return


def J():
    return


if __name__ == "__main__":
    C()

Solution (Python):
INF = float('inf')
def dfs(a=0,b=0,c=0,d=0):
    if d==N:
        return abs(A-a)+abs(B-b)+abs(C-c)-30 if min(a,min(b,c)) else INF
    ans = dfs(a,b,c,d+1)
    ans = min(ans,dfs(a+l[d],b,c,d+1)+10)
    ans = min(ans,dfs(a,b+l[d],c,d+1)+10)
    ans = min(ans,dfs(a,b,c+l[d],d+1)+10)
    return ans

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

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

l = []

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

bit = {}
bitlen = {}

for i in range(1<<n):
    cnt = 0
    leng = 0
    for j in range(n):
        if((1<<j) & i): 
            cnt += 1
            leng += l[j]
    bit[i] = cnt
    bitlen[i] = leng


ans = float('inf')

for i in range(1,1<<n):
    for j in range(1,1<<n):
        if(i&j):continue
        for k in range(1,1<<n):
            if(i&k):continue
            if(j&k):continue
            
            t = 0
            t += (bit[i]-1)*10
            t += (bit[j]-1)*10
            t += (bit[k]-1)*10
            t += abs(bitlen[i]-a)
            t += abs(bitlen[j]-b)
            t += abs(bitlen[k]-c)
            if(ans > t):ans = t

print(ans)

Solution (Python):
import itertools

def calc_MP(take, num):
    return abs(sum(take) - num) + (len(take)-1) * 10

def main():
    n, a, b, c = [int(i) for i in input().split()]

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

    ans = float("inf")
    for i0, i1, i2, i3, i4, i5, i6, i7 in itertools.product(range(4), range(4), range(4),range(4), range(4), range(4),range(4), range(4)):
        i_list = [i0, i1, i2, i3, i4, i5, i6, i7]
        MP = 0
        take_A = []
        take_B = []
        take_C = []

        for i in range(n):
            if i_list[i] == 1:
                take_A.append(l[i])
            elif i_list[i] == 2:
                take_B.append(l[i])
            elif i_list[i] == 3:
                take_C.append(l[i])

        if len(take_A) != 0 and len(take_B) != 0 and len(take_C) != 0:
            MP += calc_MP(take_A, a)
            MP += calc_MP(take_B, b)
            MP += calc_MP(take_C, c)
        else:
            MP = float("inf")
        ans = min(ans, MP)

    print(ans)

main()

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

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


def convert(n):
    list_q = []
    while True:
        a = n % 4
        n //= 4
        list_q.insert(0, a)
        if n == 0:
            break
    list_q = [str(i) for i in list_q]
    return list_q


MP = -1
for i in range(21, 4**N):
    mp_a = 0
    mp_b = 0
    mp_c = 0
    use = [int(j) for j in ''.join(convert(i)).zfill(N)]
    a_n = use.count(1)
    b_n = use.count(2)
    c_n = use.count(3)
    if a_n == 0 or b_n == 0 or c_n == 0:
        pass
    else:
        mp_a += 10 * (a_n - 1)
        mp_b += 10 * (b_n - 1)
        mp_c += 10 * (c_n - 1)
        b_a = 0
        b_b = 0
        b_c = 0
        idx = 0
        for i in use:
            if i == 1:
                b_a += l[idx]
            elif i == 2:
                b_b += l[idx]
            elif i == 3:
                b_c += l[idx]
            idx += 1
        mp_a += abs(A-b_a)
        mp_b += abs(B-b_b)
        mp_c += abs(C-b_c)
        mp = mp_a + mp_b + mp_c
        if MP == -1 or mp < MP:
            MP = mp
print(MP)

Solution (Python):
n, a, b, c = [int(_) for _ in input().split()]
l = [int(input()) for _ in range(n)] + [0 for _ in range(8-n)]
ans = 1e9
for i in range(4**8):
    quas = format(i, '016b')
    d = [0, 0, 0, 0]
    m = 0
    for j in range(8):
        t = int(quas[2*j:2*(j+1)], 2)
        if d[t] != 0 and l[j] != 0 and t != 3 : m += 10
        d[t] += l[j]
    if not 0 in d[:3] : ans = min(ans, m + abs(a-d[0]) + abs(b-d[1]) + abs(c-d[2]))
print(ans)

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

def dfs(index, a, b, c):
    if index == N:
        return abs(a-A)+abs(b-B)+abs(c-C)-30 if min(a, b, c) > 0 else float('inf')
    
    val1 = dfs(index+1, a, b, c)
    val2 = dfs(index+1, a+l[index], b, c) + 10
    val3 = dfs(index+1, a, b+l[index], c) + 10
    val4 = dfs(index+1, a, b, c+l[index]) + 10
    return min(val1, val2, val3, val4)

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

Solution (Python):
def culc_mp(l,num):
  mp = (len(l)-1)*10
  mp += abs(sum(l)-num)
  return(mp)

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

min_mp = float('inf')
for i in range(4**n):
  a_l = []
  b_l = []
  c_l = []
  mp = 0
  for j in range(n):
    tmp  = (i >> j*2) & 3
    num = l[j]
    if tmp == 0:
      a_l.append(num)
    elif tmp == 1:
      b_l.append(num)
    elif tmp == 2:
      c_l.append(num)
    
  if len(a_l) * len(b_l) * len(c_l) == 0:
      continue
    
  mp += culc_mp(a_l,a)
  mp += culc_mp(b_l,b)
  mp += culc_mp(c_l,c)
  if mp < min_mp:
    min_mp = mp
    
print(min_mp)

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

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

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

Solution (Python):
import itertools

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

def mp(assigns):
    counts = [-1, -1, -1]
    totals = [0, 0, 0]
    for i in range(N):
        a = assigns[i]
        if a < 0:
            continue
        totals[a] += ls[i]
        counts[a] += 1
    if any([count < 0 for count in counts]):
        return A + B + C
    return sum([abs(totals[i] - target[i]) + 10 * counts[i] for i in range(3)])

min_mp = A + B + C
for p in itertools.product([-1, 0, 1, 2], repeat=N):
    min_mp = min(min_mp, mp(p))
print(min_mp)

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

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

def dfs(n, a = 0, b = 0, c = 0, mp = 0):
    if n == N:
        if a > 0 and b > 0 and c > 0:
            return abs(A-a) + abs(B-b) + abs(C-c) + mp
        else:
            return 9223372036854775807
    else:
        return min(
            dfs(n+1, a + ls[n], b, c, mp + (10 if a > 0 else 0)),
            dfs(n+1, a, b + ls[n], c, mp + (10 if b > 0 else 0)),
            dfs(n+1, a, b, c + ls[n], mp + (10 if c > 0 else 0)),
            dfs(n+1, a, b, c, mp),
        )

print(dfs(0))

Solution (Python):
def search(idx, a, b, c):
    if idx == N:
        if min(a, b, c) > 0:
            return abs(A-a) + abs(B-b) + abs(C-c) - 30
        else:
            return 10**20
            
    n0 = search(idx+1, a, b, c)
    n1 = search(idx+1, a+l[idx], b, c) + 10
    n2 = search(idx+1, a, b+l[idx], c) + 10
    n3 = search(idx+1, a, b, c+l[idx]) + 10
    
    return min(n0, n1, n2, n3)
    
N, A, B, C = list(map(int, input().split()))
l = [int(input()) for i in range(N)]

print(search(0, 0, 0, 0))

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

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

inf=3000

def mp(g,a,b,c,):
    if g==N:
        return abs(A-a)+abs(B-b)+abs(C-c)-30 if min(a,b,c)>0 else inf

    l0=mp(g+1,a+l[g],b,c)+10
    l1=mp(g+1,a,b+l[g],c)+10
    l2=mp(g+1,a,b,c+l[g])+10
    l3=mp(g+1,a,b,c)
    
    return min(l0,l1,l2,l3)

print(mp(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):
read = input














tmp = read()
tmp = list(map(int, tmp.split(' ')))
N,A,B,C = tmp
opt = list(map(int, map(lambda x: read(), range(N))))
def dfs(n,a,b,c):
    ret = 10**7
    if(n == N):
        if(min(a,b,c) == 0):
            return ret
        return abs(A - a) + abs(B - b) + abs(C - c) - 30
    cur = opt[n]
    ret = min(ret, dfs(n+1, a + cur, b, c) + 10)
    ret = min(ret, dfs(n+1, a, b + cur, c) + 10)
    ret = min(ret, dfs(n+1, a, b ,c + cur) + 10)
    ret = min(ret, dfs(n+1, a, b ,c ) + 0)
    return ret
print(dfs(0, 0, 0, 0))