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):
many, A, B, C = map(int, input().split())
bamboos = [int(input()) for i in range(many)]







infinity = 1e10

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

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

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


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

mp = []
for i in range(4**N):
    Test = [0,0,0]
    MP = 0
    for j in range(N):
        n = i%4
        if n < 3:
            MP += 10 if Test[n] else 0
            Test[n] += l[j]
        i >>= 2
    for i in range(3):
        MP += abs(L[i]-Test[i]) if Test[i] else 3000
    mp.append(MP)

print(min(mp))

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

if __name__ == '__main__':
    main()

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

def solve(w,x,y,z):
    if w == N:
        if min(x,y,z)>0:
            return abs(x-A) + abs(y-B) + abs(z-C) -30
        else: 
            return 10e4
    can0 = solve(w + 1,x,y,z)
    can1 = solve(w + 1,x + l[w],y,z) + 10
    can2 = solve(w + 1,x,y + l[w],z) + 10
    can3 = solve(w + 1,x,y,z + l[w]) + 10
    return min(can0,can1,can2,can3)

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

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

N, A, B, C = map(int, input().split())
L = [int(input()) for _ in range(N)]
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)
        else:
            return 10 ** 9
    
    res0 = dfs(i+1, a, b, c)
    if a == 0:
        res1 = dfs(i+1, a+L[i], b, c)
    else:
        res1 = dfs(i+1, a+L[i], b, c) + 10
    if b == 0:
        res2 = dfs(i+1, a, b+L[i], c)
    else:
        res2 = dfs(i+1, a, b+L[i], c) + 10
    if c == 0:
        res3 = dfs(i+1, a, b, c+L[i])
    else:
        res3 = dfs(i+1, a, b, c+L[i]) + 10

    return min(res0, res1, res2, res3)

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(cnt,la,lb,lc,cost):
    if cnt == N:
        if la == 0 or lb == 0 or lc == 0:
            return 10**10
        else:
            return cost+abs(A-la)+abs(B-lb)+abs(C-lc)-30
    else:
        a = dfs(cnt+1,la+L[cnt],lb,lc,cost+10)
        b = dfs(cnt+1,la,lb+L[cnt],lc,cost+10)
        c = dfs(cnt+1,la,lb,lc+L[cnt],cost+10)
        d = dfs(cnt+1,la,lb,lc,cost) 
        return min(a,b,c,d)

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

Solution (Python):
n, *x = map(int, input().split())
L = [int(input()) for _ in range(n)]

def dfs(i, a, b, c):
    if i == len(L):
        if min(a, b, c) > 0:
            return abs(x[0] - a) + abs(x[1] - b) + abs(x[2] - c) - 30
        else:
            return 10 ** 9

    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 sys
import collections

def main():
	N, A, B, C = map(int, input().split(" "))
	ls = []
	for _ in range(N):
		tmp = int(input())
		ls.append(tmp)
	
	ptn = []
	for i in range(4**N):
		tmp = []
		for j in range(N):
			tmp.append(i >> (j*2) & 3)
		ptn.append(tmp)
	
	gattai = 0
	mp = -1
	for i, val in enumerate(ptn):
		a_count = 0
		b_count = 0
		c_count = 0
		a_ni = 0
		b_ni = 0
		c_ni = 0
		for j, vol in enumerate(val):
			if vol == 1:
				a_ni += ls[j]
				a_count += 1
			if vol == 2:
				b_ni += ls[j]
				b_count += 1
			if vol == 3:
				c_ni += ls[j]
				c_count += 1
		
		if a_count == 0 or b_count == 0 or c_count == 0:
			continue
		mp_tmp = abs(a_ni - A) + abs(b_ni - B) + abs(c_ni - C) + ((a_count + b_count + c_count -3) * 10)
		if mp == -1 or mp > mp_tmp:
			mp = mp_tmp
			
			
	print(mp)
	
	
if __name__ == "__main__":
	main()

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

ans = 10**100
for x in itertools.product([0,1,2,3], repeat=n):
    if not 0 in x or not 1 in x or not 2 in x: continue
    bum = [0,0,0]
    res = 0
    for i,j in enumerate(x):
        if j<3:
            if bum[j]!=0:
                res+=10
            bum[j]+=l[i]
    res += abs(a-bum[0])+abs(b-bum[1])+abs(c-bum[2])
    ans = min(ans, res)
print(ans)

Solution (Python):
N,A,B,C = map(int,input().split())
L = []
for i in range(N):
  L.append(int(input()))
def Base_10_to_n(X,n):
  if (int(X/n)):
    return Base_10_to_n(int(X/n),n)+str(X%n)
  return str(X%n)
n = 4
ans = float('inf')
for X in range(n**N):
  STR = Base_10_to_n(X,n).zfill(N)
  cur = 0
  TAKEA = 0
  TAKEB = 0
  TAKEC = 0
  for i in range(len(STR)):
    if STR[i] == '1':
      TAKEA += L[i]
      cur += 10
    elif STR[i] == '2':
      TAKEB += L[i]
      cur += 10
    elif STR[i] == '3':
      TAKEC += L[i]
      cur += 10
  if TAKEA != 0 and TAKEB != 0 and TAKEC != 0:
    cur += abs(TAKEA-A)+abs(TAKEB-B)+abs(TAKEC-C)
    cur -= 30
    if cur <= ans:
      ans = cur
print(ans)

Solution (Python):
from itertools import product

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

answer = 1e9
for X in product('ABCD', repeat=N):
  total_length={'A':0,'B':0,'C':0,'D':0}
  total_nums={'A':0,'B':0,'C':0,'D':0}
  for i, x in enumerate(X):
    total_length[x]+=L[i]
    total_nums[x]+=1
  na = total_nums['A']
  nb = total_nums['B']
  nc = total_nums['C']
  la = total_length['A']
  lb = total_length['B']
  lc = total_length['C']
  
  if na and nb and nc:
    a = (na+nb+nc -3)*10 + abs(la-A) + abs(lb-B) + abs(lc-C)
    answer = min(a,answer)
    
print(answer)

Solution (Python):
from functools import lru_cache

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

inf = 10**9



@lru_cache(maxsize=1000)
def f(i, _a, _b, _c):
    
    if i == n:
        return abs(a - _a) + abs(b - _b) + abs(c - _c) if min(_a, _b, _c) > 0 else inf

    
    val1 = f(i + 1, _a, _b, _c)
    
    val2 = f(i + 1, _a + _l[i], _b, _c) + (0 if _a == 0 else 10)
    val3 = f(i + 1, _a, _b + _l[i], _c) + (0 if _b == 0 else 10)
    val4 = f(i + 1, _a, _b, _c + _l[i]) + (0 if _c == 0 else 10)
    return min(val1, val2, val3, val4)


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

Solution (Python):
from functools import lru_cache

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

inf = 10**9



@lru_cache(maxsize=10000)
def f(i, _a, _b, _c):
    
    if i == n:
        return abs(a - _a) + abs(b - _b) + abs(c - _c) if min(_a, _b, _c) > 0 else inf

    
    val1 = f(i + 1, _a, _b, _c)
    
    val2 = f(i + 1, _a + _l[i], _b, _c) + (0 if _a == 0 else 10)
    val3 = f(i + 1, _a, _b + _l[i], _c) + (0 if _b == 0 else 10)
    val4 = f(i + 1, _a, _b, _c + _l[i]) + (0 if _c == 0 else 10)
    return min(val1, val2, val3, val4)


print(f(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 f(i, _a, _b, _c):
    
    if i == n:
        return abs(a - _a) + abs(b - _b) + abs(c - _c) if min(_a, _b, _c) > 0 else inf

    
    val1 = f(i + 1, _a, _b, _c)
    
    val2 = f(i + 1, _a + _l[i], _b, _c) + (0 if _a == 0 else 10)
    val3 = f(i + 1, _a, _b + _l[i], _c) + (0 if _b == 0 else 10)
    val4 = f(i + 1, _a, _b, _c + _l[i]) + (0 if _c == 0 else 10)
    return min(val1, val2, val3, val4)


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

Solution (Python):
import itertools

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

cl = ('A', 'B', 'C', 'N')

result = 1e10
for pattern in itertools.product(*[cl]*n):
    wands = {'A': 0, 'B': 0, 'C': 0}
    point = 0
    for i, char in enumerate(pattern):
        if char != 'N':
            if wands[char] != 0:
                point += 10
            wands[char] += _l[i]
    if min(wands.values()) > 0:
        point += abs(wands['A'] - a) + abs(wands['B'] - b) + abs(wands['C'] - c)
        if point < result:
            result = point

print(result)

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

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

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

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

def make_length(cur,A_,B_,C_,cost):
  if cur == N:
    if min(A_,B_,C_) > 0:
      return abs(A-A_)+abs(B-B_)+abs(C-C_) + cost
    else:
      return 1000000
  a_use = make_length(cur+1,A_+l_[cur],B_,C_,cost+10)
  b_use = make_length(cur+1,A_,B_+l_[cur],C_,cost+10)
  c_use = make_length(cur+1,A_,B_,C_+l_[cur],cost+10)
  not_use = make_length(cur+1,A_,B_,C_,cost)
  return min(a_use,b_use,c_use,not_use)

print(make_length(0,0,0,0,-30))

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


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

    return min(ret0, ret1, ret2, ret3)
print(dfs(0, 0, 0, 0))

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

"""
C - Synthetic Kadomatsu
https://atcoder.jp/contests/abc119/tasks/abc119_c

"""
import sys
from itertools import product, groupby


def solve(N, A, B, C, L):
    ans = float('inf')
    for p in product('abc_', repeat=N):
        if not all([ch in p for ch in 'abc']):
            continue
        cost = 0
        t = sorted([(usage, l) for usage, l in zip(p, L)], key=lambda x:x[0])
        for k, g in groupby(t, key=lambda x: x[0]):
            if k not in 'abc':
                continue
            g = list(g)
            cost += 10 * (len(g) - 1)
            l = sum(i[1] for i in g)
            if k == 'a':
                cost += abs(A - l)
            elif k == 'b':
                cost += abs(B - l)
            elif k == 'c':
                cost += abs(C - l)
        ans = min(ans, cost)
    return ans


def main(args):
    N, A, B, C = map(int, input().split())
    L = [int(input()) for _ in range(N)]
    ans = solve(N, A, B, C, L)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

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

def dif(stick,abc):
    return abs(abc-sum(stick)) + 10*(len(stick)- 1)

mp = 300000

for i in range(0,1 << 2*n):
    bamb = [[] for i in range(0,4)]
    for j in range(0,2*n,2):
        if ((i >> j) & 3) == 3:
            bamb[0].append(l[j//2])
        elif ((i >> j)&2) == 2:
            bamb[1].append(l[j//2])
        elif ((i >> j)&1) == 1:
            bamb[2].append(l[j//2])
        elif((i >> j)&0) == 0:
            bamb[3].append(l[j//2])
    if bamb[0] == [] or bamb[1] == [] or bamb[2] == []:
        continue
    mp = min(mp,dif(bamb[0],a)+dif(bamb[1],b)+dif(bamb[2],c))
print(mp)

Solution (Python):
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict, deque, OrderedDict
from copy import deepcopy
from functools import lru_cache, reduce
from math import ceil, floor
from sys import setrecursionlimit

import heapq
import itertools
import operator


inf = float('inf')



N, A, B, C = 0, 0, 0, 0
L = []


def set_inputs():
    global N, A, B, C, L
    N, A, B, C = get_li()
    L = get_data(N, [int])
    return


def main():
    setrecursionlimit(100000)
    set_inputs()
    ans = inf
    for x in itertools.product(range(4), repeat=N):
        take = [[], [], [], []]
        for i in range(N):
            take[x[i]].append(L[i])
        if len(take[0]) == 0 or len(take[1]) == 0 or len(take[2]) == 0:
            continue
        cost = 0
        cost += abs(sum(take[0]) - A)
        cost += abs(sum(take[1]) - B)
        cost += abs(sum(take[2]) - C)
        cost += (len(take[0]) - 1) * 10
        cost += (len(take[1]) - 1) * 10
        cost += (len(take[2]) - 1) * 10
        ans = min(ans, cost)
    print(ans)
    return


def get_int():
    return int(input())


def get_float():
    return float(input())


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


def get_li():
    return [int(i) for i in input().split()]


def get_lf():
    return [float(f) for f in input().split()]


def get_lc():
    return list(input().strip())


def get_data(n, types):
    if len(types) == 1:
        return [types[0](input()) for _ in range(n)]
    return zip(*(
        [t(x) for t, x in zip(types, input().split())]
        for _ in range(n)
    ))


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(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 rec(index, a, b, c):

    
    if index >= N:
        if a != 0 and b != 0 and c != 0:
            
            return abs(A - a) + abs(B - b) + abs(C - c)
        else:
            return INF

    
    cost_mp_a = 10 if a != 0 else 0
    result_a = rec(index + 1, a + L[index], b, c) + cost_mp_a

    
    cost_mp_b = 10 if b != 0 else 0
    result_b = rec(index + 1, a, b + L[index], c) + cost_mp_b

    
    cost_mp_c = 10 if c != 0 else 0
    result_c = rec(index + 1, a, b, c + L[index]) + cost_mp_c

    
    result_none = rec(index + 1, a, b, c)

    return min(result_a, result_b, result_c, result_none)


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

Solution (Python):


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

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

print(dfs())

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

for i in range(4**n):
    ta, tb, tc = 0, 0, 0
    tmp = 0
    for j in range(n):
        k = (i>>(2*j)) & 3
        if k == 1:
            if ta > 0:
                tmp += 10
            ta += L[j]
        elif k == 2:
            if tb > 0:
                tmp += 10
            tb += L[j]
        elif k == 3:
            if tc > 0:
                tmp += 10
            tc += L[j]
    if ta * tb * tc == 0:
        continue
    tmp += abs(a - ta) + abs(b - tb) + abs(c - tc)
    ans = min(ans,tmp)

print(ans)

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

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

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

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







def dfs(l_index, aa, bb, cc):
    
    if l_index == n:
        return abs(a - aa) + abs(b - bb) + abs(c - cc) \
               - 30 if min(aa, bb, cc) > 0 else INF
    
    rtrn0 = dfs(l_index + 1, aa, bb, cc)
    rtrn1 = dfs(l_index + 1, aa + l[l_index], bb, cc) + 10
    rtrn2 = dfs(l_index + 1, aa, bb + l[l_index], cc) + 10
    rtrn3 = dfs(l_index + 1, aa, bb, cc + l[l_index]) + 10
    return min(rtrn0, rtrn1, rtrn2, rtrn3)


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

Solution (Python):
from sys import stdin
import itertools

def get_index(l,x):
    return [i for i, _x in enumerate(l) if _x == x]

N,A,B,C=[int(x) for x in stdin.readline().rstrip().split()]
data=[]
for i in range(N):
    data.append(int(input()))
target=[A,B,C]

ans=float("inf")
for j in (list(itertools.product(["none","A","B","C"],repeat=N))):
    if set(j) >= {"A","B","C"}:
        score=(j.count("A")-1)*10 + (j.count("B")-1)*10 + (j.count("C")-1)*10
        score_a=sum([data[k] for k in (get_index(j,"A"))])
        score_b=sum([data[l] for l in (get_index(j,"B"))])
        score_c=sum([data[m] for m in (get_index(j,"C"))])
        score=abs(A-score_a)+abs(B-score_b)+abs(C-score_c)+score
        if score < ans:
            ans=score
print(ans)

Solution (Python):

import sys
INF = float('inf')

def solve(N: int, A: int, B: int, C: int, l: "List[int]"):
    target = [A, B, C]
    def calc(group):
        ret = 0
        for i in range(3):
            elms = group[i]
            if len(elms) < 1:
                return INF
            ret += (len(elms) - 1) * 10
            total = 0
            for e in elms:
                total += l[e]
            ret += abs(total - target[i])
        return ret

    def rec(idx, group):
        if idx >= N:
            return calc(group)
        ret = INF
        for i in range(4):
            group[i].append(idx)
            ret = min(ret, rec(idx + 1, group))
            group[i].remove(idx)
        return ret

    ret = rec(0, [[] for i in range(4)])
    print(ret)
    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):
n,A,B,C=map(int,input().split())

l=[]

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

def base(X, n):
    X_dumy = X
    out = ''
    while X_dumy>0:
        out = str(X_dumy%n)+out
        X_dumy = int(X_dumy/n)
    return out
ans=10**9
for i in range(4**n):
    d=str(base(i,4)).rjust(n,'0')
    a=0
    b=0
    c=0
    ca=-10
    cb=-10
    cc=-10
    for j in range(n):
        if d[j]=="1":
            a+=l[j]
            ca+=10
        elif d[j]=="2":
            b+=l[j]
            cb+=10
        elif d[j]=="3":
            c+=l[j]
            cc+=10
    if ca<0:
        ca=0
    if cb<0:
        cb=0
    if cc<0:
        cc=0
    if a*b*c!=0:
        ans=min(ans,abs(A-a)+abs(B-b)+abs(C-c)+ca+cb+cc)
    
print(ans)

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

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

INF=3000

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

    ret0=mp(co+1,a,b,c)
    ret1=mp(co+1,a+l[co],b,c)+10
    ret2=mp(co+1,a,b+l[co],c)+10
    ret3=mp(co+1,a,b,c+l[co])+10

    return min(ret0,ret1,ret2,ret3)

print(mp(0,0,0,0))

Solution (Python):
def four(n):
    if (int(n/4)):
        return four(int(n/4))+str(n%4)
    return str(n%4)

def check(A,bit,l,k):
    num=-1
    sum=0
    for i in range(N):
        if bit[i]==k:
            num+=1
            sum+=l[i]
    return abs(A-sum)+num*10

N,A,B,C=map(int,input().split())
l=[]
MP=10**5
for i in range(N):
    li=int(input())
    l.append(li)
for i in range(4**N):
    bit=four(i).zfill(N)
    if not("1" in bit):
        continue
    if not("2" in bit):
        continue
    if not("3" in bit):
        continue
    mp=check(A,bit,l,"1")+check(B,bit,l,"2")+check(C,bit,l,"3")
    if mp<=MP:
        MP=mp
print(MP)

Solution (Python):
import numpy as np
N, A, B, C = list(map(int, input().split()))
l_li = []
for i in range(N):
    l_li.append(int(input()))
INF = 10 ** 9
def dfs(n, a, b, c):
  if n == N:
    if min(a, b, c) == 0:
      return INF
    else:
      return abs(a-A) + abs(b-B) + abs(c-C) -30
  res0 = dfs(n + 1, a, b, c)
  res1 = dfs(n + 1, a + l_li[n], b, c) + 10
  res2 = dfs(n + 1, a, b + l_li[n], c) + 10
  res3 = dfs(n + 1, a, b, c + l_li[n]) + 10
  return min(res0, res1, res2, res3)

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

Solution (Python):






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

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



















def cal_cost(L_use):
	a=0
	b=0
	c=0
	na=0
	nb=0
	nc=0
	for i in range(len(L_use)):
		if(L_use[i]==0):
			0
		if(L_use[i]==1):
			a+=L[i]
			na+=1
		if(L_use[i]==2):
			b+=L[i]
			nb+=1
		if(L_use[i]==3):
			c+=L[i]
			nc+=1
	if(na==0 or nb==0 or nc==0):
		return -1
	return abs(a-A)+abs(b-B)+abs(c-C)+10*(na+nb+nc-3)
	

L_use=[]
for i in range(4**N):
	k=i
	tmp=[]
	for j in range(N):
		tmp.append(k%4)
		k//=4
	L_use.append(tmp)
costs=[]
for u in L_use:
	c=cal_cost(u)
	if(c!=-1):
		costs.append(c)
print(min(costs))

Solution (Python):
from sys import stdin


INF = 10 ** 9

N, A, B, C = [int(x) for x in stdin.readline().rstrip().split()]


l_list = [int(stdin.readline().rstrip()) for _ 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 INF
        
    
    ret0 = dfs(cur+1, a, b, c) 
    ret1 = dfs(cur+1, a+l_list[cur], b, c) + 10 
    ret2 = dfs(cur+1, a, b+l_list[cur], c) + 10 
    ret3 = dfs(cur+1, a, b, c+l_list[cur]) + 10 

    return min(ret0, ret1, ret2, ret3)


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

Solution (Python):
import itertools
INF = 1000000007
n, a, b, c = map(int, input().split())
B = [a, b, c]
take = [int(input()) for i in range(n)]
sh = [i for i in itertools.product(range(4), repeat=n)]
res = INF
for C in sh:
    if 0 not in C or 1 not in C or 2 not in C:
        continue
    ex = [0] * 3
    ifres = 0
    for index, x in enumerate(C):
        if x != 3:
            if ex[x] != 0:
                ifres += 10
            ex[x] += take[index]
    for i in range(3):
        ifres += abs(B[i]-ex[i])
    if ifres < res:
        res = ifres
print(res)

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


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


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

Solution (Python):
import sys
sys.setrecursionlimit(1000000)
N,A,B,C = [int(x) for x in input().split()]
l = [int(input()) for _ in range(N)]

def calc(li):
    bam = [0]*4
    scr = [0]*4
    for index,i in enumerate(li):
        if(i == 0): continue
        scr[i] += 10 if bam[i]!=0 else 0
        bam[i] += l[index]
    scr[1] += abs(bam[1]-A)
    scr[2] += abs(bam[2]-B)
    scr[3] += abs(bam[3]-C)
    global ans
    ans = min(ans,sum(scr))

def dfs(num,li):
    if(num == N):
        exist = [0]*4
        mul = 1
        for i in li:
            exist[i]+=1
        for i in exist[1:]:
            mul *= i
        if(mul == 0): return 0        
        calc(li)
    else:
        for i in range(4):
            dfs(num+1,li+[i])


li1=[]
ans = 10**9
dfs(0,li1)
print(ans)

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

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


INF = 10 ** 5


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


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

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


def dfs(cur,a,b,c,cost):
    if cur == N:
        if min(a,b,c)>0 :
            return abs(A-a)+abs(B-b)+abs(C-c)+cost
        else: return 10**9
    
    return0=dfs(cur+1,a,b,c,cost)
    return1=dfs(cur+1,a+leng[cur],b,c,cost+10)
    return2=dfs(cur+1,a,b+leng[cur],c,cost+10)
    return3=dfs(cur+1,a,b,c+leng[cur],cost+10)
    
    return min(return0,return1,return2,return3)


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

Solution (Python):
import numpy as np

def main():
	n,a,b,c=map(int,input().split())
	l=[]
	for i in range(n):
		l.append(int(input()))
	
	key=[[0],[1],[2],[3]]
	key_4=key[:]
	for i in range(n-1):
		temp=[]
		for elem1 in key:
			for elem2 in key_4:
				temp.append(elem1+elem2)
		key=temp[:]

	res=10**9
	for elem in key:
		res=min(res,judge(elem,l,a,b,c))
	print(res)
	
def judge(key,l,a,b,c):
	num=[0,0,0,0]
	array=[0,0,0,0]
	for i in range(len(key)):
		array[key[i]]+=l[i]
		num[key[i]]+=1
	if(num[0]==0 or num[1]==0 or num[2]==0):
		return 10**9
	result=0
	result=abs(array[0]-a)+abs(array[1]-b)+abs(array[2]-c)
	result+=(num[0]+num[1]+num[2]-3)*10

	return result

def inputList():
	a=list(map(int,input().split()))
	return a

def multisort(li,index):
	return sorted(li,key=lambda x: x[index])

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

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