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 itertools

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

ans = []
for X in itertools.product(range(4), repeat=N):
    P = [[] for _ in range(4)]
    for i, x in enumerate(X):
        P[x].append(l[i])
    if min(map(len, P[1:]))==0:
        continue
    
    tmp = 0
    for Q, D in zip(P[1:], [A,B,C]):
        tmp+=10*(len(Q)-1)+abs(D-sum(Q))
    ans.append(tmp)    
print(sorted(ans)[0])

Solution (Python):
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**2) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

n,a,b,c = li()
l = [ni() for _ in range(n)]

ans = 10**18

for i in range(4**n):
    cur = i
    cnt = {i: [] for i in range(4)}
    
    for bi in l:
        cnt[cur%4].append(bi)
        cur //= 4
    
    
    if len(cnt[1]) * len(cnt[2]) * len(cnt[3]) == 0:
        continue
    
    ans = min(ans, abs(a-sum(cnt[1]))\
                  + abs(b-sum(cnt[2]))\
                  + abs(c-sum(cnt[3]))\
                  + 10*(n - len(cnt[0]) -3))
    
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 a*b*c==0:
            return INF
        else:
            return abs(a-A) + abs(b-B) + abs(c-C) - 30
    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 bfs(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:
        ret0=bfs(cur+1,a,b,c)
        ret1=bfs(cur+1,a+l[cur],b,c)+10
        ret2=bfs(cur+1,a,b+l[cur],c)+10
        ret3=bfs(cur+1,a,b,c+l[cur])+10
        return min(ret0,ret1,ret2,ret3)
print(bfs(0,0,0,0))

Solution (Python):
from itertools import product
N, A, B, C = [int(i) for i in input().split()]
l_ls = list()
for i in range(N):
    l_ls.append(int(input()))
best = 1e9
for combi in product(range(4),repeat=N):
    a = [l for l, label in zip(l_ls, combi) if label == 1]
    b = [l for l, label in zip(l_ls, combi) if label == 2]
    c = [l for l, label in zip(l_ls, combi) if label == 3]
    dis = abs(A - sum(a)) + abs(B - sum(b)) + abs(C - sum(c)) \
          + (len(a) + len(b) + len(c) - 3) * 10
    if len(a) * len(b) * len(c) == 0:
        continue
    if best > dis:
        best = dis
print(best)

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

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

print(kadomatsu(0, 0, 0, 0))

Solution (Python):
from itertools import product
n,a,b,c = (int(i) for i in input().split())
x = [int(input()) for i in range(n)]
ans = float("inf")
def f(q):
    if any(i not in q for i in range(3)): return ans
    else:
        z,num = [0,0,0],0
        for i in range(n):
            if q[i]-3:
                if z[q[i]]: num+=10
                z[q[i]]+=x[i]
        return num+abs(z[0]-a)+abs(z[1]-b)+abs(z[2]-c)
for i in product((0,1,2,3),repeat=n): ans = min(ans,f(i))
print(ans)

Solution (Python):

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

ls = [int(input()) for _ in range(n)]

inf = 10000

def dfs(i,a,b,c):
    if i == n:
        return \
            inf if min(a,b,c) == 0 \
            else abs(A-a) + abs(B-b)+abs(C-c)-30
    
    i1 = i+1
    lsi = ls[i]

    r0 = dfs(i1, a,b,c)
    r1 = dfs(i1, a+lsi,b,c) + 10
    r2 = dfs(i1, a,b+lsi,c) + 10
    r3 = dfs(i1, a,b,c+lsi) + 10

    return min(r0,r1,r2,r3)

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

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

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

Solution (Python):
import numpy as np
    
INF = float('inf')

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

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

def dfs(cur, a, b, c):
    
    if cur == n:
        return abs(a-A) + abs(b-B) + abs(c-C) - 30 if min(a,b,c) > 0 else 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 _ in range(N):
    l.append(int(input()))






def to_bit(num,digit):
    bit = [0] * digit
    for i in range(digit):
        bit[i] = num % 4
        num = int((num - (num % 4)) / 4)
    return bit

min_cost = 5000

for i in range(4 ** N):
    bit = to_bit(i,len(l))
    
    cost = 0

    
    sum_ = 0
    counts = 0
    for j in range(len(l)):
        if bit[j] == 0:
            sum_ += l[j]
            counts += 1
    if counts > 0:
        cost += abs(sum_ - A) + 10 * (counts - 1)
    else:
        cost += 1000000

    
    sum_ = 0
    counts = 0
    for j in range(len(l)):
        if bit[j] == 1:
            sum_ += l[j]
            counts += 1
    if counts > 0:
        cost += abs(sum_ - B) + 10 * (counts - 1)
    else:
        cost += 1000000

    
    sum_ = 0
    counts = 0
    for j in range(len(l)):
        if bit[j] == 2:
            sum_ += l[j]
            counts += 1
    if counts > 0:
        cost += abs(sum_ - C) + 10 * (counts - 1)
    else:
        cost += 1000000

    if cost < min_cost:
        min_cost = cost
print(min_cost)

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

INF = 10 ** 9

def dfs(idx, a, b, c) -> int:
	if idx == n:
		return abs(A - a) + abs(B - b) + abs(C - c) - 30 if min(a, b, c) > 0 else INF
	res0 = dfs(idx + 1, a, b, c)
	res1 = dfs(idx + 1, a + l[idx], b, c) + 10
	res2 = dfs(idx + 1, a, b + l[idx], c) + 10
	res3 = dfs(idx + 1, a, b, c + l[idx]) + 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 _ in range(n)]
ans = 100000000
for i in range(4**n):
  x,y,z=0,0,0
  s,t,u=0,0,0
  for j in range(n):
    if i % 4 == 0:
      x += l[j]
      s += 1
    elif i % 4 == 1:
      y += l[j]
      t += 1
    elif i % 4  == 2:
      z += l[j]
      u += 1
    i //= 4
  if s == 0 or t == 0 or u == 0:
    continue
  k = (s + t + u - 3) * 10
  k += abs(a - x)
  k += abs(b - y)
  k += abs(c - z)
  ans = min(ans, k)
print(ans)

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

def dfs(depth,x,y,z):
  if depth==n:
    return abs(x-a)+abs(y-b)+abs(z-c)-30 if min(x,y,z)>0 else np.inf
  
  A = dfs(depth+1,x+l[depth],y,z)+10
  B = dfs(depth+1,x,y+l[depth],z)+10
  C = dfs(depth+1,x,y,z+l[depth])+10
  X = dfs(depth+1,x,y,z)
  
  return min(A,B,C,X)
print(dfs(0,0,0,0))

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

for i in range(4**n):
    aa=0
    bb=0
    cc=0
    cnt = 0
    for j in range(n):
        flag = ((i//(4**j))%4)
        if flag == 0:
            aa += l[j]
            cnt += 1
        if flag == 1:
            bb += l[j]
            cnt += 1          
        if flag == 2:
            cc += l[j]
            cnt += 1
    if aa==0 or bb==0 or cc == 0:
        continue
    aans = abs(aa-a)+abs(bb-b)+abs(cc-c)+10*(cnt-3)
    ans = min(ans,aans)
print(ans)

Solution (Python):


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

ans = float('inf')
for i in range(4**N):
    a,b,c = [],[],[]
    cost = 0
    for keta in range(N):
        v = (i//(4**keta))%4
        if v==0:   a.append(L[keta])
        elif v==1: b.append(L[keta])
        elif v==2: c.append(L[keta])
        else: pass
    
    if len(a)*len(b)*len(c)==0: continue
    cost += 10*(len(a)+len(b)+len(c)-3)
    cost += abs(sum(a)-A) + abs(sum(b)-B) + abs(sum(c)-C)
    ans = min(ans, cost)

print(ans)

Solution (Python):
import itertools

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

cost = 10**10
for s in itertools.product(' ABC', repeat=n):
  al, bl, cl = [], [], []
  for i, _s in enumerate(s):
    if _s == 'A':
      al.append(ll[i])
    elif _s == 'B':
      bl.append(ll[i])
    elif _s == 'C':
      cl.append(ll[i])
  if not al or not bl or not cl:
    continue
  cost = min(cost, 
             10 * (len(al) - 1) + abs(a - sum(al)) + 
             10 * (len(bl) - 1) + abs(b - sum(bl)) + 
             10 * (len(cl) - 1) + abs(c - sum(cl)))
print(cost)

Solution (Python):
tl = [0] * 3
n, tl[0], tl[1], tl[2] = map(int, input().split())
l = []
for i in range(n):
	l.append(int(input()))
ans = -1
arr = None
for mask in range(1 << (2 * n)):
	grp = [[], [], []]
	for i in range(n):
		ty = (mask >> (i * 2)) & 3
		if ty > 0:
			grp[ty - 1].append(l[i])
	res = 0
	for i in range(3):
		res += abs(sum(grp[i]) - tl[i]) + 10 * (len(grp[i]) - 1)
		if len(grp[i]) == 0:
			res = ans
			break
	if ans == -1 or res < ans:
		ans = res
		arr = grp
print(ans)

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

Solution (Python):


"""
Created on 2019/2/24
Solved on 2019/2/24
@author: shinjisu
"""



def getInt(): return int(input())


def getIntList(): return [int(x) for x in input().split()]


def zeros(n): return [0]*n


def getIntLines(n): return [int(input()) for i in range(n)]


def getIntMat(n):
    mat = []
    for i in range(n):
        mat.append(getIntList())
    return mat


def zeros2(n, m): return [zeros(m)]*n


ALPHABET = [chr(i+ord('a')) for i in range(26)]
DIGIT = [chr(i+ord('0')) for i in range(10)]
N1097 = 10**9 + 7
INF = 10**10


def dmp(x, cmt=''):
    global debug
    if debug:
        if cmt != '':
            print(cmt, ':  ', end='')
        print(x)
    return x


def gen2Bits(n):  
    bits = zeros(n)
    for i in range(4**n):
        for k in range(n):
            bits[k] = i % 4
            i //= 4
        yield bits



def loop(level, a, b, c, mp):
    global N, L, A, B, C, minMP
    if level == N:
        if min(a, b, c) == 0:
            return INF
        mp += abs(A-a) + abs(B-b) + abs(C-c) - 30  
        dmp((minMP, mp), 'min, MP')
        minMP = min(minMP,mp)
        return minMP
    else:
        mp0 = loop(level+1, a, b, c, mp)
        mp1 = loop(level+1, a+L[level], b, c, mp+10)
        mp2 = loop(level+1, a, b+L[level], c, mp+10)
        mp3 = loop(level+1, a, b, c+L[level], mp+10)
        dmp((mp0, mp1, mp2, mp3))
        return min(mp0, mp1, mp2, mp3)


def probC():
    global N, L, A, B, C, minMP
    
    N, A, B, C = getIntList()
    L = []
    for i in range(N):
        L.append(getInt())
    dmp((N, A, B, C ))
    dmp(L)
    d = zeros(3)
    d = A, B, C
    dmp(d)
    minMP = INF
    return loop(0, 0, 0, 0, 0)


def probC_v2():
    N, A, B, C = getIntList()
    L = []
    for i in range(N):
        L.append(getInt())
    dmp((N, A, B, C ))
    dmp(L)
    d = zeros(3)
    d = A, B, C
    dmp(d)
    minMP = INF
    for f in gen2Bits(N):
        dmp(f)
        mp = 0
        lg = zeros(4)
        for i in range(N):
            if lg[f[i]] > 0 and f[i] < 3:
                mp += 10
            lg[f[i]] += L[i]
            
        
        if min(lg[:3]) == 0:  
            continue
        for i in range(3):
            mp += abs(lg[i]-d[i])
        dmp((minMP, mp), 'min, MP')
        minMP = min(minMP, mp)
    return minMP


def probC_v1():
    N, A, B, C = getIntList()
    L = []
    for i in range(N):
        L.append(getInt())
    dmp((N, A, B, C ))
    dmp(L)
    d = zeros(3)
    d[0] = A
    d[1] = B
    d[2] = C
    dmp(d)
    minMP = INF
    for f in gen2Bits(N):
        dmp(f)
        mp = 0
        lg = zeros(4)
        for i in range(N):
            if lg[f[i]] > 0 and f[i] < 3:
                mp += 10
            lg[f[i]] += L[i]
            
        
        allUsed = True
        for i in range(3):  
            if lg[i] == 0:
                allUsed = False
                break
            mp += abs(lg[i]-d[i])
        if not allUsed:
            continue
        dmp((minMP, mp))
        minMP = min(minMP, mp)
    return minMP


debug = False  
print(probC())


def gcd(x, y):  
    m = max(x, y)
    n = min(x, y)
    while m % n != 0:
        w = m % n
        m = n
        n = w
    return n


def probD():
    N, M = getIntList()
    A = getIntList()
    dmp((N, M))
    dmp(A)
    return num


def probA():
    S = input()
    dmp(S)
    y = int(S[0:4])
    m = int(S[5:7])
    d = int(S[8:10])
    dmp((y,m,d))
    HEI = 'Heisei'
    if y <= 2018:
        return HEI
    if m <= 4:
        return HEI
    else:
        return 'TBD'


def probB():
    N = getInt()
    dmp(N)
    sm = 0.0
    for i in range(N):
        x, u = [a for a in input().split()]
        dmp((x,u))
        if u == 'JPY':
            x = int(x)
        else:
            x = float(x) * 380000.0
        sm += x
    return sm

Solution (Python):

import itertools
import collections
from bisect import bisect



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

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

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


if __name__ == '__main__':
    main()

Solution (Python):
N,*A=map(int,input().split());a=[[0]*4];r=[0,1,2]
for i in range(N):l=int(input());a+=[[p[j]+l*(i==j)for j in r]+[p[3]+10]for i in r for p in a]
print(min(sum(abs(p[i]-A[i])for i in r)-30+p[3]for p in a if min(p[:3])>0))

Solution (Python):
N,*A=map(int,input().split());a=[[0]*4];r=[0,1,2]
for i in range(N):l=int(input());a=[[p[j]+l*(i==j)for j in r]+[p[3]+(i<3)*10]for i in r+[3]for p in a]
print(min(sum(abs(p[i]-A[i])for i in r)-30+p[3]for p in a if min(p[:3])>0))

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


def dfs(cur, a, b, c):
    if cur == N:
        if min(a, b, c) == 0:  
            return float('INF')
        
        return sum([abs(_ - __) for _, __ in zip([a, b, c], [A, B, C])])
    sub_cost_A = dfs(cur+1, a+L[cur], b, c) + 10
    sub_cost_B = dfs(cur+1, a, b+L[cur], c)+10
    sub_cost_C = dfs(cur+1, a, b, c+L[cur])+10
    sub_cost_X = dfs(cur + 1, a, b, c)  
    return min(sub_cost_A, sub_cost_B, sub_cost_C, sub_cost_X)


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

Solution (Python):
N,A,B,C=map(int,input().split());a=[[0,0,0,-30]]
for i in range(N):l=int(input());a=[[p[j]+l*(i==j)for j in[0,1,2]]+[p[3]+(i<3)*10]for i in range(4)for p in a]
print(min(abs(p[0]-A)+abs(p[1]-B)+abs(p[2]-C)+p[3]for p in a if min(p[:3])>0))

Solution (Python):
N, A, B, C = map(int,input().split())
ans = [[0, 0, 0, -30]]
for i in range(N):
    l = int(input())
    ans = [[p[j] + l * (i == j) for j in [0, 1, 2]] + [p[3] + (i < 3) * 10] for p in ans for i in range(4)]
print(min(abs(p[0] - A) + abs(p[1] - B) + abs(p[2] - C) + p[3] for p in ans if min(p[:3]) > 0))

Solution (Python):
N, A, B, C = map(int,input().split())
ans = [[0, 0, 0, 0]]
for i in range(N):
    l = int(input())
    ans = [[p[j] + l * (i == j) for j in [0, 1, 2]] + [p[3]+(i < 3)] for p in ans for i in range(4)]
print(min(abs(p[0] - A) + abs(p[1] - B) + abs(p[2] - C) + p[3] * 10 - 30 for p in ans if min(p[:3]) > 0))

Solution (Python):
n,a,b,c=map(int,input().split())
l=[int(input()) for _ in range(n)]
def cal(l,a):
    return abs(sum(l)-a)+10*(len(l)-1)
def ten_to_n(X, n):
    X_dumy = X
    out = ''
    while X_dumy>0:
        out = str(X_dumy%n)+out
        X_dumy = int(X_dumy/n)
    return out




res=[]
for i in range(4**n):
    s=str(ten_to_n(i+4**n,4))[1:]
    if s.count("0")==0 or s.count("1")==0 or s.count("2")==0:
        continue
    
    ta,tb,tc=[],[],[]
    for j in range(n):
        if s[j]=="0":
            ta.append(l[j])
        if s[j]=="1":
            tb.append(l[j])
        if s[j]=="2":
            tc.append(l[j])
    res.append(cal(ta,a)+cal(tb,b)+cal(tc,c))
print(min(res))

Solution (Python):
N, A, B, C = map(int,input().split())
ls = [int(input()) for i in range(N)]
def func(pos, a, b, c):return abs(a - A) + abs(b - B) + abs(c - C) - 30 + 9999 * max(0, 1 - a * b * c) if pos == N else min(func(pos + 1, a + ls[pos] * (i == 0), b + ls[pos] * (i == 1), c + ls[pos] * (i == 2)) + 10 * (i < 3) for i in range(4))
print(func(0, 0, 0, 0))

Solution (Python):
N, A, B, C = map(int,input().split())
ls = [int(input()) for i in range(N)]
def func(pos, a, b, c):return abs(a - A) + abs(b - B) + abs(c - C) - 30 + 10000 * max(0, 1 - a * b * c) if pos == N else min(func(pos + 1, a + ls[pos] * (i == 0), b + ls[pos] * (i == 1), c + ls[pos] * (i == 2)) + 10 * (i < 3) for i in range(4))
print(func(0, 0, 0, 0))

Solution (Python):
n, a, b, c = map(int, input().split())
length = []
inf = 10**9 + 7

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


def DFS(cur, x, y, z):
    if cur == n:
        if min(x, y, z) > 0:
            return abs(x - a) + abs(y - b) + abs(z - c) - 30
        else:
            return inf

    part0 = DFS(cur + 1, x, y, z)
    part1 = DFS(cur + 1, x + length[cur], y, z) + 10
    part2 = DFS(cur + 1, x, y + length[cur], z) + 10
    part3 = DFS(cur + 1, x, y, z + length[cur]) + 10

    return min(part0, part1, part2, part3)


print(DFS(0, 0, 0, 0))

Solution (Python):
INF = 10 ** 18

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

def func(pos, a, b, c):return abs(a - A) + abs(b - B) + abs(c - C) - 30 + INF * max(0, 1 - a * b * c) if pos == N else min(func(pos + 1, a + ls[pos] * (i == 0), b + ls[pos] * (i == 1), c + ls[pos] * (i == 2)) + 10 * (i < 3) for i in range(4))
print(func(0, 0, 0, 0))

Solution (Python):
n, a, b, c = map(int, input().split())
length = []
inf = 10**9 + 7

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


def DFS(cur, x, y, z):
    if cur == n:
        return abs(x - a) + abs(y - b) + abs(z - c) - 30 if min(x, y,
                                                                z) else inf

    part0 = DFS(cur + 1, x, y, z)
    part1 = DFS(cur + 1, x + length[cur], y, z) + 10
    part2 = DFS(cur + 1, x, y + length[cur], z) + 10
    part3 = DFS(cur + 1, x, y, z + length[cur]) + 10

    return min(part0, part1, part2, part3)


print(DFS(0, 0, 0, 0))

Solution (Python):
PENALTY = 3000


def func(n, a, b, c):
    if n == N:
        return (abs(a - A) if a > 0 else PENALTY) +\
               (abs(b - B) if b > 0 else PENALTY) +\
               (abs(c - C) if c > 0 else PENALTY)
    else:
        r0 = func(n + 1, a, b, c)
        r1 = func(n + 1, a + l[n], b, c) + 10
        r2 = func(n + 1, a, b + l[n], c) + 10
        r3 = func(n + 1, a, b, c + l[n]) + 10
        return min(r0, r1, r2, r3)


N, A, B, C = map(int, input().split())
l = [int(input()) for _ in range(N)]
ans = func(0, 0, 0, 0)
print(ans - 30)

Solution (Python):


from collections import deque
import itertools as it
import sys
import math

sys.setrecursionlimit(1000000)

INF = 10 ** 18

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

ans = 10 ** 12

for i in range(4 ** N):
  S = [0, 0, 0, 0]
  hoge = i
  cost = -30
  for j in range(N):
    S[hoge % 4] += l[j]
    if hoge % 4 < 3:
      cost += 10
    hoge /= 4
  if S[0] * S[1] * S[2] == 0:
    continue
  ans = min(ans, cost + abs(S[0] - A) + abs(S[1] - B) + abs(S[2] - C))

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, x, y, z):
    if cur == n:
        if min(x, y, z) > 0:
            return abs(x - a) + abs(y - b) + abs(z - c) - 30 
        else:
            return 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))

Solution (Python):
import itertools
n,a,b,c=(map(int,input().split()))
l=[int(input()) for i in range(n)]
ans=10**9
for k in itertools.product(range(4),repeat=n):
    A=[[] for i in range(4)]
    for i in range(n):
        A[k[i]]+=[l[i]]
    if A[1] and A[2] and A[3]:
        tmp=10*(n-len(A[0])-3)
        tmp+=abs(a-sum(A[1]))
        tmp+=abs(b-sum(A[2]))
        tmp+=abs(c-sum(A[3]))
        ans=min(tmp,ans)
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())
takes = []
for i in range(N):
    takes.append(int(input()))


takes = takes+[0,0,0,0,0,0,0]
takes = takes[:8]
g = [A, B, C]
combis = []
for i in range(4):
    for j in range(4):
        for k in range(4):
            for l in range(4):
                for m in range(4):
                    for n in range(4):
                        for o in range(4):
                            for p in range(4):
                                
                                combis.append([i,j,k,l,m,n,o,p])

min_diff = 10**10
for combi in combis:
    A,B,C = 0,0,0
    for idx, el in enumerate(combi):
        if el == 0:
            A += takes[idx]
        elif el == 1:
            B += takes[idx]
        elif el == 2:
            C += takes[idx]

        if A == 0 or B == 0 or C == 0:
            continue

        A_diff = abs(g[0] - A) + (len([_ for _ in combi if _ == 0]) - 1)*10
        B_diff = abs(g[1] - B) + (len([_ for _ in combi if _ == 1]) - 1)*10
        C_diff = abs(g[2] - C) + (len([_ for _ in combi if _ == 2]) - 1)*10
        diff = A_diff + B_diff + C_diff
        min_diff = min(diff, min_diff)

print(min_diff)

Solution (Python):
from itertools import permutations

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

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


def dfs(dep, a, b, c, not_use)->int:
    if dep == N:
        return abs(A-a)+abs(B-b)+abs(C-c) - 30 if min(a, b, c) > 0 else float('inf')
    else:
        d1 = dfs(dep+1, a+l[dep], b, c, not_use)+10
        d2 = dfs(dep+1, a, b+l[dep], c, not_use)+10
        d3 = dfs(dep+1, a, b, c+l[dep], not_use)+10
        d4 = dfs(dep+1, a, b, c, not_use+l[dep])

        return min(d1, d2, d3, d4)


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

Solution (Python):

import copy
import itertools



def solve(t, l, c):
    min_cost = 100000
    cand = []

    ptn = 1 << len(l)
    
    
    for i in range(1, ptn):
        answers = []
        for j in range(0, len(l)):
            if (i >> j) & 1:
                answers.append(l[j])
        temp_cost = abs(t - sum(answers)) + (10 * (len(answers) - 1) if len(answers) > 1 else 0)
        if temp_cost < min_cost:
            min_cost = temp_cost
            cand = answers

    
    for d in cand:
        del l[l.index(d)]
    return min_cost, l


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

    answer = 1000000
    for targets in itertools.permutations([A, B, C]):
        is_done = [False] * 3
        L2 = copy.copy(L)
        total_cost = 0
        for i in range(len(targets)):
            if targets[i] in L2:
                del L2[L2.index(targets[i])]
                is_done[i] = True

        for i in range(3):
            if is_done[i]:
                continue

            cost, L2 = solve(targets[i], L2, 0)
            total_cost += cost

        answer = min(answer, total_cost)

    print(answer)


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, x, y, z):
    if cur == n:
        if min(x, y, z) > 0:
            return abs(x - a) + abs(y - b) + abs(z - c) - 30
        else:
            return 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))

Solution (Python):
def func(n):
    if n == N:
        p = 0
        for c in count:
            if c == 0:
                return
            p += 10 * (c - 1)
        p += abs(length[0] - A)
        p += abs(length[1] - B)
        p += abs(length[2] - C)
        global ans
        if p < ans:
            ans = p
    else:
        count[0] += 1
        length[0] += l[n]
        func(n + 1)
        count[0] -= 1
        length[0] -= l[n]
        count[1] += 1
        length[1] += l[n]
        func(n + 1)
        count[1] -= 1
        length[1] -= l[n]
        count[2] += 1
        length[2] += l[n]
        func(n + 1)
        count[2] -= 1
        length[2] -= l[n]
        func(n + 1)


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

ans = 3000
count = [0, 0, 0]
length = [0, 0, 0]
func(0)
print(ans)

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

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 INF
    
    ans1 = dfs(cnt+1, a, b, c)
    ans2 = dfs(cnt+1, a + L[cnt], b, c) + 10
    ans3 = dfs(cnt+1, a, b + L[cnt], c) + 10
    ans4 = dfs(cnt+1, a, b, c + L[cnt]) + 10
    return min(ans1, ans2, ans3, ans4)

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

Solution (Python):
import itertools

n, a, b, c = map(int, input().split())
l = tuple(sorted(int(input()) for _ in range(n)))





ans = float('inf')
for bit in range(4 ** n):
    kinds = []
    while bit > 0:
        kinds.append(bit % 4)
        bit = bit // 4
    while len(kinds) != n:
        kinds.append(0)
    
    sizes = [0, 0, 0, 0]
    counts = [0, 0, 0, 0]
    for i in range(n):
        sizes[kinds[i]] += l[i]
        counts[kinds[i]] += 1

    _, p, q, r = sizes
    _, s, t, u = counts

    if s == 0 or t == 0 or u == 0: continue
    cost = abs(p - a) + abs(q - b) + abs(r - c) + 10 * max(0, s - 1) + 10 * max(0, t - 1) + 10 * max(0, u - 1)
    ans = min(ans, cost)

print(ans)

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

Solution (Python):
n,A,B,C=map(int,input().split())
l=[int(input()) for i in range(n)]
def calc(r,a,b,c):
    if r == n:
        return abs(a-A) + abs(b-B) + abs(c-C) - 30 if min(a,b,c) > 0 else 10**9
    else:
        root1 = calc(r+1,a,b,c)
        root2 = calc(r+1,a+l[r],b,c) +10
        root3 = calc(r+1,a,b+l[r],c) +10
        root4 = calc(r+1,a,b,c+l[r]) +10
        return min(root1,root2,root3,root4)
print(calc(0,0,0,0))

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

ans=1e10
for p in product([0,1,2,3],repeat=N):
    m=[-1,-1,-1]
    mp=0
    for i in range(N):
        if p[i]!=3:
            if m[p[i]]==-1:
                m[p[i]]=l[i]
            else:
                mp+=10
                m[p[i]]+=l[i]
    mp+=abs(m[0]-A)+abs(m[1]-B)+abs(m[2]-C)
    if min(m)>=0:
        ans=min(mp,ans)
print(ans)

Solution (Python):
n,a,b,c=map(int,input().split())
l=[0]*n
for i in range(n):
  l[i]=int(input())
fouradic=[0]*n
ret=3000
while fouradic!=([3]*n):
  a_=0
  b_=0
  c_=0
  for i in range(n):
    if fouradic[i]<3:
      fouradic[i]+=1
      break
    fouradic[i]=0
  if not (fouradic.count(1) and fouradic.count(2) and fouradic.count(3)):
    continue
  ret_in=0
  ret_in+=10*(n-fouradic.count(0)-3)
  for i in range(n):
    if fouradic[i]==1:
      a_+=l[i]
    if fouradic[i]==2:
      b_+=l[i]
    if fouradic[i]==3:
      c_+=l[i]
  ret_in+=abs(a-a_)+abs(b-b_)+abs(c-c_)
  ret=min(ret,ret_in)
print(ret)