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())
D = [A,B,C]
def nex(time):
  for i in range(n):
    if time[i] < 3:
      time[i] += 1
      break
    else:
      time[i] = 0
  else:
    time = 'over'
  return time
boo = []
for _ in range(n):
  boo.append(int(input()))
can = []
time = [0 for _ in range(n)]
while time != 'over':
  d = [0,0,0]
  tmp = (n-3) * 10
  for i in range(n):
    if time[i] < 3:
      d[time[i]] += boo[i]
    else:
      tmp -= 10
  for k in range(3):
    tmp += abs(D[k] - d[k])
  if d[0] * d[1] * d[2] != 0:
    can.append(tmp)
  time = nex(time)
print(min(can))

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):
import bisect
n,a,b,c=map(int,input().split())
l=[int(input()) for x in range(n)]
l.sort()
total=4**n
ans=999999999
for i in range(total):
    fora=[]
    forb=[]
    forc=[]
    nonused=[]
    temp_i = i
    for j in range(n):
        temp=temp_i%4
        if(temp==0):
            fora.append(l[j])
        elif(temp==1):
            forb.append(l[j])
        elif(temp==2):
            forc.append(l[j])
        else:
            nonused.append(l[j])
        temp_i = temp_i // 4
    if(len(fora)==0 or len(forb)==0 or len(forc)==0):
        continue
    else:
        mp=0
        mp+=10*(len(fora)-1)
        len_fora=sum(fora)
        mp+=abs(a-len_fora)

        mp+=10*(len(forb)-1)
        len_forb=sum(forb)
        mp+=abs(b-len_forb)

        mp+=10*(len(forc)-1)
        len_forc=sum(forc)
        mp+=abs(c-len_forc)

        if(ans > mp):
            ans = mp
print(ans)

Solution (Python):
import numpy as np
import copy
N, A, B, C = map(int,input().split())
t = sorted([A,B,C])
A = t[0]
B = t[1]
C = t[2]
l = []
for i in range(N):
    l.append(int(input()))
dic = {}

def calc(ls):
    dic[tuple(ls)] = 0
    Asublist = [np.abs(i-A) for i in ls]
    res = min(Asublist)
    idx = Asublist.index(res)
    ls.remove(ls[idx])

    Bsublist = [np.abs(i-B) for i in ls]
    tmp = min(Bsublist)
    res += tmp
    idx = Bsublist.index(tmp)
    ls.remove(ls[idx])

    Csublist = [np.abs(i-C) for i in ls]
    tmp = min(Csublist)
    res += tmp
    idx = Csublist.index(tmp)
    ls.remove(ls[idx])

    return res + 10*(N - (len(ls)+3))

def rec(ls, num, maxcomb, minval):
    if num > maxcomb:
        return minval
    for i in range(0, len(ls)-1):
        for j in range(i+1, len(ls)):
            tmp = [ls[i]+ls[j]]
            tmp = tmp + [ls[k] for k in range(len(ls)) if k!=i and k!=j]
            if tuple(tmp) in dic.keys():
                continue
            tmp2 = calc(copy.deepcopy(tmp))
            if minval > tmp2:
                minval = tmp2
            if num < maxcomb:
                tmp3 = rec(copy.deepcopy(tmp), num+1, maxcomb, minval)
                if minval > tmp3:
                    minval = tmp3
    return minval
minval = rec(l, 1, N-3, calc(copy.deepcopy(l)))
print(minval)

Solution (Python):
INF = 10 ** 5

A = [0] * 3
N,A[0],A[1],A[2] = map(int,input().split())

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

can = [0] * (4 ** N)

for x in range(4 ** N):
    union = [-1] * 4
    bef = [0] * 4 
    xx = x
    for i in range(N):
        where = xx % 4
        xx //= 4
        bef[where] += l[i]
        union[where] += 1
    
    if -1 in union[:3]:
        can[x] = INF
        continue
    deffer = 0
    for j in range(3):
        deffer += abs(bef[j]-A[j])
    can[x] = deffer + sum(union[:3]) * 10

print(min(can))

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

def dfs(depth,a,b,c):
    if depth == n:
        if min(a,b,c) > 0:
            return abs(a-A)+abs(b-B)+abs(c-C) - 30
        else:
            return 10**9
        
    numA = dfs(depth+1,a+l[depth],b,c) + 10
    numB = dfs(depth+1,a,b+l[depth],c) + 10
    numC = dfs(depth+1,a,b,c+l[depth]) + 10
    numX = dfs(depth+1,a,b,c)
    return min(numA,numB,numC,numX)
print(dfs(0,0,0,0))

Solution (Python):

n,A,B,C = map(int,input().split())
D = [A,B,C]
l = []
for _ in range(n):
  l.append(int(input()))
l.sort(reverse=True)
def nex(time):
  for i in range(n-1,-1,-1):
    if time[i] < 3:
      time[i] += 1
      break
    else:
      time[i] = 0
  else:
    time = 'over'
  return time

can = []
time = [0 for _ in range(n)]
while time != 'over':
  ans = (n-3) * 10
  d = [0,0,0]
  for i in range(n):
    t = time[i]
    if t == 3:
      ans -= 10
    else:
      d[t] += l[i]
  if all(d[k] != 0 for k in range(3)):
    for k in range(3):
      ans += abs(D[k] - d[k])
    can.append([ans,d])
  time = nex(time)
print(min(can,key=lambda x:x[0])[0])

Solution (Python):
import bisect

def solve():
  n,a,b,c = (int(i) for i in input().split())
  l = [int(input()) for _ in range(n)]
  ans = 10**15
  for bita in range(2**n):
    for bitb in range(2**n):
      if bita&bitb: continue
      for bitc in range(2**n):
        if bitc&bitb:continue
        if bitc&bita:continue
        if bita == 0 or bitb == 0 or bitc == 0: continue
        lenkado = [0]*3
        stime = [0]*3
        for k in range(n):
            if 1&(bita>>k): 
              lenkado[0] += l[k]
              stime[0] += 1
            elif 1&(bitb>>k): 
              lenkado[1] += l[k]
              stime[1] += 1
            elif 1&(bitc>>k): 
              lenkado[2] += l[k]
              stime[2] += 1
        tmp = abs(a-lenkado[0])+abs(b-lenkado[1]) + abs(c-lenkado[2])
        for i in range(3):
          tmp += max(0,stime[i]-1)*10
        ans = min(ans,tmp)
  print(ans)     
solve()

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


def dfs1(i, p1=[], p2=[], p3=[], ans=[]):
    
    if i == N:
        if len(p1) != 0 and len(p2) != 0 and len(p3)!= 0:
            ans.append([p1, p2, p3])
        return ans
    l = L[i]
    ans = dfs1(i+1, p1+[l], p2, p3, ans)
    ans = dfs1(i+1, p1, p2+[l], p3, ans)
    ans = dfs1(i+1, p1, p2, p3+[l], ans)
    ans = dfs1(i+1, p1, p2, p3, ans)
    return ans

def dfs2(R, L=[], ans=[]):
    if len(L) == 3:
        ans.append(L)
        return ans
    for r in R:
        if not r in L:
            ans = dfs2(R, L+[r], ans)
    return ans

indexes = dfs2([0, 1, 2])

ans = int(1E18)
for (p1, p2, p3) in dfs1(0):
    S = [sum(p1), sum(p2), sum(p3)]
    for i1, i2, i3 in indexes:
        p = abs(A-S[i1]) + abs(B-S[i2]) + abs(C-S[i3]) + (len(p1)+len(p2)+len(p3) - 3)*10
        ans = min(ans, p)

print(ans)

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

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

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

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

Solution (Python):
from itertools import combinations
from itertools import permutations

def find(X, L, remain=3):
    dis_min = abs(X-min(L))
    stock = min(L)
    for n in range(1, len(L)-remain+2):
        for x in combinations(L, n):
            if abs(X - sum(x)) + 10*(n-1) < dis_min:
                dis_min = abs(X - sum(x)) + 10*(n-1)
                stock = x
    if type(stock) == type(1):
        L.remove(stock)
    else:
        for x in stock:
            L.remove(x)
    return dis_min, stock, L

if __name__ == '__main__':
    N, A, B, C = map(int, input().split())
    L_ini = [int(input()) for _ in range(N)]
    
    S_min = 10**12
    for ABC in permutations([A, B, C], 3):
        L = L_ini.copy()
        S_dis = 0
        for i, X in enumerate(ABC):
            dis, stock, L = find(X, L, 3-i)
            S_dis += dis
        S_min = min(S_min, S_dis)
    print(S_min)

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



def dfs(depth,a,b,c,):
  
  if depth == n:
    if min(a,b,c) > 0:
      return abs(a-A) + abs(b-B) + abs(c-C) - 30
    else:
      return 10**9
  
  
  
  
  numA = dfs(depth+1,a+l[depth],b,c) + 10
  
  numB = dfs(depth+1,a,b+l[depth],c) + 10
  
  numC = dfs(depth+1,a,b,c+l[depth]) + 10
  
  numX = dfs(depth+1,a,b,c)
  return min(numA,numB,numC,numX)
print(dfs(0,0,0,0))

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations
import sys
import string
from bisect import bisect_left


INF = float('inf')
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LIM(): return list(map(lambda x:int(x) - 1, sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def LIRM(n): return [LIM() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
mod = 1000000007


n, a, b, c = LI()
L = IR(n)


def recur(ai, bi, ci, i):
    if i == n:
        return abs(a - ai) + abs(b -bi) + abs(c - ci) - 30 if ai > 0 and bi > 0 and ci > 0 else INF
    l1 = recur(ai + L[i], bi, ci, i+1) + 10
    l2 = recur(ai, bi + L[i], ci, i+1) + 10
    l3 = recur(ai, bi, ci + L[i], i+1) + 10
    l4 = recur(ai, bi, ci, i+1)
    return min(l1, l2, l3, l4)



print(recur(0, 0, 0, 0))

Solution (Python):



N, A, B, C = map(int, input().split())
L = [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) - 30if min(a,b,c) > 0 else 1e9
    r0 = dfs(cnt+1,a,b,c)
    r1 = dfs(cnt+1,a+L[cnt],b,c) + 10
    r2 = dfs(cnt+1,a,b+L[cnt],c) + 10
    r3 = dfs(cnt+1,a,b,c+L[cnt]) + 10
    return min(r0,r1,r2,r3)

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

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

def calc(x):
    a = []
    b = []
    c = []
    res = 0
    for n in range(N):
        m = (((3<<(2*n)) & x) >> (2*n))
        if m == 1:
            a.append(L[n])
        elif m == 2:
            b.append(L[n])
        elif m == 3:
            c.append(L[n])
    for l, t in zip([a, b, c], [A, B, C]):
        if len(l) == 0:
            return INF
        res += 10*max(len(l)-1, 0) + abs(sum(l) - t)
    return res

ans = INF
for x in range(pow(4, N)):
    ans = min(ans, calc(x))
print(ans)

Solution (Python):
n, a, b, c = map(int, input().split())
bamb = [int(input()) for _ in range(n)]
objective = [a, b, c]
ans = float("inf")
for i in range(4 ** n):
    tmp = 0
    material = [[] for _ in range(3)]
    for j in range(n):
        flag = (i >> (2 * j)) & 3
        if flag == 0:
            material[0].append(bamb[j])
        elif flag == 1:
            material[1].append(bamb[j])
        elif flag == 2:
            material[2].append(bamb[j])
    if len(material[0]) > 0 and len(material[1]) > 0 and len(material[2]) > 0:
        for k in range(3):
            tmp += (len(material[k]) - 1) * 10
            tmp += abs(sum(material[k]) - objective[k])
        ans = min(ans, tmp)
print(ans)

Solution (Python):
n, a, b, c = map(int, input().split())
bamb = [int(input()) for _ in range(n)]
objective = [a, b, c]
ans = float("inf")
for i in range(4 ** n):
    tmp = 0
    material = [[] for _ in range(3)]
    for j in range(n):
        flag = (i >> (2 * j)) & 3
        if flag == 0:
            material[0].append(bamb[j])
        elif flag == 1:
            material[1].append(bamb[j])
        elif flag == 2:
            material[2].append(bamb[j])
    if len(material[0]) > 0 and len(material[1]) > 0 and len(material[2]) > 0:
        for k in range(3):
            tmp += (len(material[k]) - 1) * 10
            tmp += abs(sum(material[k]) - objective[k])
        ans = min(ans, tmp)
print(ans)

Solution (Python):


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

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

    ret0 = f(idx + 1, a, b, c)
    ret1 = f(idx + 1, a + l[idx], b, c) + 10
    ret2 = f(idx + 1, a, b + l[idx], c) + 10
    ret3 = f(idx + 1, a, b, c + l[idx]) + 10
    return min(ret0, ret1, ret2, ret3)

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

Solution (Python):
n,a,b,c = map(int,input().split())
l = []
for i in range(n):
    l0 = int(input())
    l.append(l0)
bitl = []
for i in range(4**n):
    bit = [0 for i in range(n)]
    p = i
    q = 1
    for j in range(n):
        bit[j] = (p//q) % 4
        q *= 4
    bitl.append(bit)
mpl = []
for i in range(4**n):
    bit = bitl[i]
    takel = [[],[],[]]
    for j in range(n):
        if bit[j] > 0:
            takel[bit[j]-1].append(l[j])
    mp = 0
    al = len(takel[0])
    bl = len(takel[1])
    cl = len(takel[2])
    if al > 0:
        take = takel[0][0]
        for j in range(al-1):
            take += takel[0][j+1]
            mp += 10
        mp += abs(a - take)
        if bl > 0:
            take = takel[1][0]
            for j in range(bl-1):
                take += takel[1][j+1]
                mp += 10
            mp += abs(b - take)
            if cl > 0:
                take = takel[2][0]
                for j in range(cl-1):
                    take += takel[2][j+1]
                    mp += 10
                mp += abs(c - take)
                mpl.append(mp)
print(min(mpl))

Solution (Python):
INF = 10**10

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

def dfs(num,ai,bi,ci):
  if num==n:
    ret = abs(a-ai) + abs(b-bi) + abs(c-ci) - 30
    return ret if min(ai,bi,ci) >0 else INF
  t0 = dfs(num+1,ai,        bi,        ci)
  ta = dfs(num+1,ai+ll[num],bi,        ci)         +10
  tb = dfs(num+1,ai        ,bi+ll[num],ci)         +10 
  tc = dfs(num+1,ai        ,bi,        ci+ll[num]) +10
  return min(t0,ta,tb,tc)

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

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

costs = []


def add(xs, cost, index):
    if index < n:
        for i in range(4):
            new_xs = xs.copy()
            new_cost = cost
            if i < 3:
                if new_xs[i] > 0:
                    new_cost += 10
                new_xs[i] += ls[index]
            add(new_xs, new_cost, index + 1)
    else:
        xs.sort()
        if xs[0] > 0:
            cost += abs(c - xs[0]) + abs(b - xs[1]) + abs(a - xs[2])
            costs.append(cost)


add([0] * 3, 0, 0)
print(min(costs))

Solution (Python):
from itertools import product

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

def sum_ks(k_1, k_2, k_3):
    return abs(k_1-a) + abs(k_2-b) + abs(k_3-c)

min_ = 999999
for per in product([0, 1, 2, 3], repeat=n):
    if 1 in per and 2 in per and 3 in per:
        count = 0
        k_1 = 0
        k_2 = 0
        k_3 = 0
        for i in range(n):
            if per[i] == 0:
                count += 1
            if per[i] == 1:
                k_1 += l[i]
            if per[i] == 2:
                k_2 += l[i]
            if per[i] == 3:
                k_3 += l[i]
        cost = 10 * (n - 3 - count)
        min_ = min(min_, cost+sum_ks(k_1, k_2, k_3))
print(min_)

Solution (Python):
N, A, B, C = map(int, input().split())
ls = []
for i in range(N):
  l = int(input())
  ls.append(l)
rs = []
for i in range(4**N):
  hA = 0
  hB = 0
  hC = 0
  n = 0
  for j in range(N):
    if i%4 == 1:
      hA += ls[j]
      n += 1
    if i%4 == 2:
      hB += ls[j]
      n += 1
    if i%4 == 3:
      hC += ls[j]
      n += 1
    i = i//4
  if not(hA > 0 and hB > 0 and hC > 0):
    continue
  r = (n-3)*10+abs(hA-A)+abs(hB-B)+abs(hC-C)
  rs.append(r)
r = min(rs)
print(r)

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 = map(int, input().split())
l = [int(input()) for i in range(n)]
goals = ['A', 'B', 'C', 'N']
from copy import copy
p = []

def search(i, goal, pre_abc):
  abc = copy(pre_abc)
  if goal == 'A':
    abc[0] += l[i]
  elif goal == 'B':
    abc[1] += l[i]
  elif goal == 'C':
    abc[2] += l[i]
  else:
    abc[3] += 1
  if i == len(l) - 1:
    if abc[0]!=0 and abc[1]!=0 and abc[2]!=0:
      p.append(abc)
  else:
    for goal in goals:
      search(i+1, goal, abc)
      
def mp(abc):
  return abs(abc[0]-a) + abs(abc[1]-b) + abs(abc[2]-c) + 10 * (n-3-abc[3])
      
def solve():
  for goal in goals:
    search(0, goal, [0,0,0,0])
  print(min(list(map(mp, p))))
  
solve()

Solution (Python):
N,A,B,C=[int(s) for s in input().split()]
ls=[int(input()) for i in range(N)]
def four(n):
  val_n=n
  val_ls=[0 for s in range(N)]
  for i in range(N):
    val_ls[i]=val_n%4
    val_n=val_n//4
  return val_ls
def mp(ls,n):
  if ls[0]==0:
    return 10000
  else:
    return 10*(ls[0]-1)+abs(ls[1]-n)
ans=30000
for i in range(4**N):
  take_ls=[[0,0],[0,0],[0,0],[0,0]] 
  go_ls=four(i)
  for j in range(N):
    take_ls[go_ls[j]][0]+=1
    take_ls[go_ls[j]][1]+=ls[j]
  mp_use=mp(take_ls[0],A)+mp(take_ls[1],B)+mp(take_ls[2],C)
  if mp_use<ans:
    ans=mp_use
print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 5 + 5)

INF = 10 ** 9

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


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

    r0 = dfs(current + 1, a, b, c)
    r1 = dfs(current + 1, a + L[current], b, c) + 10
    r2 = dfs(current + 1, a, b + L[current], c) + 10
    r3 = dfs(current + 1, a, b, c + L[current]) + 10

    return min(r0, r1, r2, r3)


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

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


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

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

def dfs(counter, A, B, C):
    if counter == len(l):
        return abs(A-a)+abs(B-b)+abs(C-c)-30 if min(A,B,C) > 0 else inf
    p_1 = dfs(counter+1, A+l[counter], B, C) + 10
    p_2 = dfs(counter+1, A, B+l[counter], C) + 10
    p_3 = dfs(counter+1, A, B, C+l[counter]) + 10
    p_4 = dfs(counter+1, A, B, C)
    return min(p_1,p_2,p_3,p_4)

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)]
MP = []
for i in range(4**n):
    i_bit = bin(i)[2:].zfill(2*n)
    co_a = 0
    co_b = 0
    co_c = 0
    p_a = 0
    p_b = 0
    p_c = 0
    for j in range(n):
        if i_bit[-1-2*j:-3-2*j:-1] == '00':
            co_a += L[j]
            p_a += 1
        elif i_bit[-1-2*j:-3-2*j:-1] == '01':
            co_b += L[j]
            p_b += 1
        elif i_bit[-1-2*j:-3-2*j:-1] == '10':
            co_c += L[j]
            p_c += 1
        else:
            continue
    if co_a*co_b*co_c!=0 and p_a<=a and p_b<=b and p_c<=c:
        MP += [10*(p_a+p_b+p_c-3)+abs(a-co_a)+abs(b-co_b)+abs(c-co_c)]
print(min(MP))

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)

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

for i in range(4**N):
    x = Base_10_to_n(i, 4)
    if '1' in x and '2' in x and '3' in x:
        x = x.zfill(N)
        idx = 0
        a = []
        b = []
        c = []
        for i in x:
            i = int(i)
            if i == 1:
                a.append(l[idx])
            elif i == 2:
                b.append(l[idx])
            elif i == 3:
                c.append(l[idx])
            idx += 1
        MP = (len(a)+len(b)+len(c)-3) * 10
        MP += abs(sum(a)-A) + abs(sum(b)-B) + abs(sum(c)-C)
        ans = min(ans, MP)
print(ans)

Solution (Python):
def slove():
    import sys, itertools
    input = sys.stdin.readline
    n, a, b, c = list(map(int, input().rstrip('\n').split()))
    l = [int(input().rstrip('\n')) for _ in range(n)]
    ls = [0, 1, 2, 3]
    m_cost = 10 ** 10

    for it in itertools.product(ls, repeat=n):
        if 1 in it and 2 in it and 3 in it:
            lst = [0] * 4
            cost = 0
            for i, v in enumerate(it):
                lst[v] += l[i]
                if v == 1 or v == 2 or v == 3:
                    cost += 10
            cost -= 30
            cost += (abs(lst[1] - a) + abs(lst[2] - b) + abs(lst[3] - c))
            m_cost = min(m_cost, cost)
    print(m_cost)


if __name__ == '__main__':
    slove()

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


x=10**9

for i in list(itertools.product([0,1,2,3], repeat=n)):
    l2=[0]*3
    for j in range(n):
        if i[j]!=3:
            l2[i[j]]+=l1[j]
    l2=sorted(l2)
    if l2[0]!=0:
        l3=(len([_ for _ in i if _<3])-3)*10+abs(c-l2[0])+abs(b-l2[1])+abs(a-l2[2])
        if l3<x:
            x=l3

print(x)

Solution (Python):
import itertools

A = [0, 0, 0, 0]
N,A[0],A[1],A[2] = map(int,input().split())
bamboo_list = [int(input()) for n in range(N)]
ans = 10**15

for seq in itertools.product(range(4),repeat=N): 
  X = [0, 0, 0] 
  Y = [0, 0, 0] 
  
  for n in range(N):
    if seq[n] != 3:
      X[seq[n]] += bamboo_list[n]
      Y[seq[n]] += 1
  
  
  if Y[0]*Y[1]*Y[2] == 0:
    continue
  
  
  MP = (sum(Y)-3)*10
  for i in range(3):
    MP += abs(X[i]-A[i])
  
  
  
  if MP < ans:
    ans = MP

print(ans)

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


from itertools import product

answer = 10 ** 18
for pattern in product(range(4), repeat=N):
    X = [0] * 3
    MP = 0
    for take, length in zip(pattern, L):
        if take == 3:
            continue
        if X[take] != 0:
            MP += 10
        X[take] += length
    if X[0] == 0 or X[1] == 0 or X[2] == 0:
        continue
    MP += abs(A - X[0])
    MP += abs(B - X[1])
    MP += abs(C - X[2])
    answer = min(MP, answer)

print(answer)

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

def main():
    N,A,B,C = map(int, input().split())
    L = []
    for _ in range(N):
        L.append(int(input()))
    mi = 1000*N
    for i in range(4**N):
        mp = 0
        a = b = c = 0
        x = 0

        while i > 0:
            if i%4 == 1:
                if a > 0:
                    mp += 10
                a += L[x]
            if i%4 == 2:
                if b > 0:
                    mp += 10
                b += L[x]
            if i%4 == 3:
                if c > 0:
                    mp += 10
                c += L[x]
            i >>= 2
            x += 1
        if a > 0 and b > 0 and c > 0:
            mi = min(mi, mp + abs(A-a) + abs(B-b) + abs(C-c))

    print(mi)

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

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

INF = 10 ** 18
answer = INF

for pattern in itertools.product(range(4), repeat=N):
    X = [0] * 4
    MP = 0
    for take, length in zip(pattern, L):
        if take == 3:
            continue
        if X[take] != 0:
            MP += 10
        X[take] += length
    if X[0] == 0 or X[1] == 0 or X[2] == 0:
        continue
    MP += abs(X[0]-A)
    MP += abs(X[1]-B)
    MP += abs(X[2]-C)
    answer = min(MP, answer)
print(answer)

Solution (Python):
n, a, b, c = list(map(int, input().split()))
l = []
for i in range(n):
    l.append(int(input()))
min = 10**20
def dfs(depth, v):
    if depth >= n:
        if 0 in v and 1 in v and 2 in v:
            calc(v)
    else:
        for i in range(4):
            v[depth] = i
            dfs(depth + 1, v)

def calc(array):
    A = 0
    B = 0
    C = 0
    count = -3
    global min
    for i in range(n):
        if array[i] == 0:
            A += l[i]
            count += 1
        elif array[i] == 1:
            B += l[i]
            count += 1
        elif array[i] == 2:
            C += l[i]
            count += 1
    if min > abs(A - a) + abs(B - b) + abs(C - c) + count * 10 :
        min = abs(A - a) + abs(B - b) + abs(C - c) + count * 10

v = [0 for i in range(n)]
dfs(0,v)
print(min)

Solution (Python):
N, A, B, C = map(int, input().split())
l = [int(input()) for i in range(N)]
mp = []
syn = [str(i) for i in range(4)]
temp = []
for k in range(N - 1):
    for i in syn:
        for j in range(4):
            temp.append(i + str(j))
    syn.clear()
    syn.extend(temp)
    temp.clear()

syn_list = [i for i in syn if '1' in i and '2' in i and '3' in i]

for i in syn_list:
    A_l = 0
    B_l = 0
    C_l = 0
    for j in range(N):
        if i[j] == '1':
            A_l += l[j]
        elif i[j] == '2':
            B_l += l[j]
        elif i[j] == '3':
            C_l += l[j]
        else:
            continue
    mp.append(abs(A_l - A) + abs(B_l - B) + abs(C_l - C) + (i.count('1') + i.count('2') + i.count('3') - 3) * 10)
print(min(mp))

Solution (Python):
def main():
    import itertools
    import sys
    input = sys.stdin.readline
    N, A, B, C = map(int, input().split())
    D = [A,B,C]
    L = [int(input()) for i in range(N)]
    L = sorted(L)
    X = [[],[],[],[]]
    l = [0,1,2,3]
    p = itertools.product(l, repeat = N)
    q = itertools.product()
    ans = float("inf")
    for v in p:
        tmp = 0
        X = [[],[],[],[]]
        for i in range(len(v)):
            X[v[i]].append(L[i])
        if X[0] != [] and X[1] != [] and X[2] != []:
            for j in itertools.permutations(D,3):
                for k in range(3):
                    tmp += 10* (len(X[k])-1) + abs(j[k] - sum(X[k]))
                if tmp < ans:
                    ans = tmp
    print(ans)
    
    
main()