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, *l = map(int, open(0).read().split())
ans = 10 ** 9


def dfs(a, b, c, cur, cost):
    if cur == N:
        if not a or not b or not c:
            return
        global ans
        ans = min(ans, abs(a - A) + abs(b - B) + abs(c - C) + cost - 30)
    else:
        dfs(a, b, c, cur + 1, cost)
        dfs(a + l[cur], b, c, cur + 1, cost + 10)
        dfs(a, b + l[cur], c, cur + 1, cost + 10)
        dfs(a, b, c + l[cur], cur + 1, cost + 10)


def solve():
    dfs(0, 0, 0, 0, 0)
    print(ans)


if __name__ == '__main__':
    solve()

Solution (Python):

import sys
import itertools

def solve(N: int, A: int, B: int, C: int, l: "List[int]"):
    
    all = list(itertools.product([0,1,2,3], repeat=N))
    answer = 10**9
    for a in all:
        a_sum = 0
        a_count = 0
        b_sum = 0
        b_count = 0
        c_sum = 0
        c_count = 0

        for i in range(len(a)):
            if a[i] == 0:
                continue
            elif a[i] == 1:
                a_sum += l[i]
                a_count +=1
            elif a[i] == 2:
                b_sum += l[i]
                b_count +=1
            elif a[i] == 3:
                c_sum += l[i]
                c_count +=1
        
        if a_count == 0 or b_count == 0 or c_count==0 :
            continue

        MP = (a_count-1)*10+(b_count-1)*10+(c_count-1)*10+abs(A-a_sum)+abs(B-b_sum)+abs(C-c_sum)

        answer = min(MP,answer)
    print(answer)
    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 = [int(input()) for i in range(N)]
inf = 99999999999

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

  a_pattern = dfs(cursor+1, a + L[cursor], b, c) + 10
  b_pattern = dfs(cursor+1, a, b + L[cursor], c) + 10
  c_pattern = dfs(cursor+1, a, b, c + L[cursor]) + 10
  no_pattern = dfs(cursor+1, a, b, c)
  return min(a_pattern, b_pattern, c_pattern, no_pattern)


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

Solution (Python):
n,A,B,C = map(int,input().split())
l = list(int(input())for _ in range(n))
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 10**9
    x = dfs(depth+1,a,b,c)
    y = dfs(depth+1,a+l[depth],b,c) +10
    z = dfs(depth+1,a,b+l[depth],c) +10
    w = dfs(depth+1,a,b,c+l[depth]) +10
    return min(x,y,z,w)
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 sys
from copy import copy
sys.setrecursionlimit(10**7)

n, a, b, c = map(int, input().split())
k = sorted([a, b, c])
l = [int(input()) for _ in range(n)]
ans = float("inf")

def dfs(i, num, nk):
    global ans
    if i == n:
        if not all(nk): return
        nk.sort()
        cost = (num - 3) * 10
        for j in range(3): cost += abs(k[j] - nk[j])
        ans = min(ans, cost)
        return
    
    nxt = l[i]
    dfs(i+1, num, nk)
    for j in range(3):
        nnk = copy(nk)
        nnk[j] += nxt
        dfs(i+1, num+1, nnk)

dfs(0, 0, [0] * 3)
print(ans)

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

def rec(i: int, a: int, b: int, c: int) -> int:
    if i == n:
        if a == 0 or b == 0 or c == 0:
            return float("inf")

        return abs(a-A)+abs(b-B)+abs(c-C)

    res = rec(i+1, a, b, c)

    res = min(res, rec(i+1, a+l[i], b, c) + (0 if a == 0 else 10))
    res = min(res, rec(i+1, a, b+l[i], c) + (0 if b == 0 else 10))
    res = min(res, rec(i+1, a, b, c+l[i]) + (0 if c == 0 else 10))

    return res

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

Solution (Python):
n,a,b,c=[int(i) for i in input().split()]
ts = [a,b,c]
ls = []
for _ in range(n):
    ls.append(int(input()))
r = 10**18
for i in range(4**n):
    xs = [[] for _ in range(4)]
    for j in range(n):
        k, i = i%4, i//4
        xs[k].append(ls[j])
    ir = 0
    for l in range(3):
        if len(xs[l]) == 0:
            ir = 10 ** 18
            break
        ir += 10*(len(xs[l])-1) + abs(ts[l]-sum(xs[l]))
    r = min(r, ir)
print(r)

Solution (Python):
import sys
sys.setrecursionlimit(10**7)

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):
from itertools import combinations, permutations


def f(x, lst, rem):
    c = int(1e9+7)
    tpl = ()
    for v in range(len(lst) - rem + 1):
        for t in combinations(lst, v + 1):
            if abs(x - sum(t)) + 10 * v < c:
                c = abs(x - sum(t)) + 10 * v
                tpl = t
    for v in tpl:
        lst.remove(v)
    return c, lst


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

    ans = int(1e9+7)
    for ABC in permutations((A, B, C), 3):
        lst = L[:]
        accum = 0
        for i, v in enumerate(ABC):
            c, lst = f(v, lst, 3 - i)
            accum += c
        ans = min(ans, accum)
    print(ans)


main()

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

ans = 100000000
for i in itertools.product([0, 1, 2, 3], repeat = n):
    x = 0
    y = 0
    z = 0
    for j in range(n):
        if i[j] == 1:
            x += take[j]
        elif i[j] == 2:
            y += take[j]
        elif i[j] == 3:
            z += take[j]

    if x * y * z == 0:
        continue

    x, y, z = sorted([x, y, z], reverse = True)
    tmp = 0
    tmp += (i.count(1) + i.count(2) + i.count(3) - 3)*10
    tmp += abs(x-a) + abs(y-b) + abs(z-c)
    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 dfs(depth,a,b,c):
  if depth == N:
    return abs(a - A) + abs(b - B) + abs(c - C) 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 if a else dfs(depth+1,a+L[depth],b,c)
  ret2 = dfs(depth+1,a,b+L[depth],c) + 10 if b else dfs(depth+1,a,b+L[depth],c)
  ret3 = dfs(depth+1,a,b,c+L[depth]) + 10 if c else dfs(depth+1,a,b,c+L[depth])
  return min(ret0,ret1,ret2,ret3)
print(dfs(0,0,0,0))

Solution (Python):
from itertools import combinations, permutations


def f(x, lst, rem):
    d = int(1e9+7)
    tpl = ()
    for v in range(len(lst) - rem + 1):
        for t in combinations(lst, v + 1):
            if abs(x - sum(t)) + 10 * v < d:
                d = abs(x - sum(t)) + 10 * v
                tpl = t
    for v in tpl:
        lst.remove(v)
    return d, lst


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

    ans = int(1e9+7)
    for ABC in permutations((A, B, C), 3):
        lst = L[:]
        accum = 0
        for i, v in enumerate(ABC):
            d, lst = f(v, lst, 3 - i)
            accum += d
        ans = min(ans, accum)
    print(ans)


main()

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


def f(x, lst, rem):
    d = int(1e9+7)
    tpl = ()
    for v in range(len(lst) - rem + 1):
        for t in combinations(lst, v + 1):
            if abs(x - sum(t)) + 10 * v < d:
                d = abs(x - sum(t)) + 10 * v
                tpl = t
    for v in tpl:
        lst.remove(v)
    return d, lst


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

    ans = int(1e9+7)
    for ABC in permutations((A, B, C), 3):
        lst = L[:]
        accum = 0
        for i, v in enumerate(ABC):
            d, lst = f(v, lst, 3 - i)
            accum += d
        ans = min(ans, accum)
    print(ans)


main()

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


def f(x, lst, rem):
    d = abs(x - min(lst))
    min_lst = min(lst)
    tpl = None
    for v in range(len(lst) - rem + 1):
        for t in combinations(lst, v + 1):
            if abs(x - sum(t)) + 10 * v < d:
                d = abs(x - sum(t)) + 10 * v
                tpl = t
    if tpl is None:
        lst.remove(min_lst)
    else:
        for v in tpl:
            lst.remove(v)
    return d, lst


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

    ans = int(1e9+7)
    for ABC in permutations((A, B, C), 3):
        lst = L[:]
        accum = 0
        for i, v in enumerate(ABC):
            d, lst = f(v, lst, 3 - i)
            accum += d
        ans = min(ans, accum)
    print(ans)


main()

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:
        return abs(a - A) + abs(b - B) + abs(c - C) - 30\
            if 0 < min(a, b, c) else int(1e9+7)

    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
    ret4 = dfs(cur + 1, a, b, c)

    return min(ret1, ret2, ret3, ret4)


def main():
    print(dfs(0, 0, 0, 0))


main()

Solution (Python):
def dfs(cur, a, b, c):
    if cur == N:
        return abs(a - A) + abs(b - B) + abs(c - C) - 30\
            if 0 < min(a, b, c) else int(1e9+7)

    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
    ret4 = dfs(cur + 1, a, b, c)

    return min(ret1, ret2, ret3, ret4)


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

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

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

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


def f(x, lst, rem):
    d = abs(x - min(lst))
    min_lst = min(lst)
    tpl = None
    for v in range(1, len(lst) - rem + 2):
        for t in combinations(lst, v):
            if abs(x - sum(t)) + 10 * (v - 1) < d:
                d = abs(x - sum(t)) + 10 * (v - 1)
                tpl = t
    if tpl is None:
        lst.remove(min_lst)
    else:
        for v in tpl:
            lst.remove(v)
    return d, lst


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

    ans = int(1e9+7)
    for ABC in permutations((A, B, C), 3):
        lst = L[:]
        accum = 0
        for i, v in enumerate(ABC):
            d, lst = f(v, lst, 3 - i)
            accum += d
        ans = min(ans, accum)
    print(ans)


main()

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


def f(x, lst, rem):
    d = abs(x - min(lst))
    min_lst = min(lst)
    tpl = None
    for v in range(1, len(lst) - rem + 2):
        for t in combinations(lst, v):
            if abs(x - sum(t)) + 10 * (v - 1) < d:
                d = abs(x - sum(t)) + 10 * (v - 1)
                tpl = t
    if tpl is None:
        lst.remove(min_lst)
    else:
        for v in tpl:
            lst.remove(v)
    return d, lst


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

    ans = int(1e9+7)
    for ABC in permutations((A, B, C), 3):
        lst = L[:]
        accum = 0
        for i, v in enumerate(ABC):
            d, lst = f(v, lst, 3 - i)
            accum += d
        ans = min(ans, accum)
    print(ans)


main()

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):
def p_c():
    n, A, B, C = map(int, input().split())
    li = [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
        ret1 = dfs(cur + 1, a, b, c)
        ret2 = dfs(cur + 1, a + li[cur], b, c) + 10
        ret3 = dfs(cur + 1, a, b + li[cur], c) + 10
        ret4 = dfs(cur + 1, a, b, c + li[cur]) + 10
        return min(ret1, ret2, ret3, ret4)

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

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

bit = 1<<n
ans = 10**10

def chs(L,x):
    L2 = []
    for i in range(len(L)):
        if not 1&(x>>i):
            L2.append(L[i])
    return L2

def calc(L,x,ai):
    res = 0
    la = 0
    for i in range(len(L)):
        if 1&(x>>i):
            la+=L[i]
            res += 10
    if x:
        res -= 10
    res += abs(la-ai)
    return res

for ind in range(1,bit):
    cost = 0
    cost += calc(l,ind,a)
    l2 = chs(l,ind)

    bit2 = 1<<len(l2)
    

    for ind2 in range(1,bit2):
        cost2 = cost
        cost2 += calc(l2,ind2,b)
        l3 = chs(l2,ind2)
        
        bit3 = 1<<len(l3)
       
        
        for ind3 in range(1,bit3):
            cost3 = cost2
            cost3 += calc(l3,ind3,c)
            if ans>cost3:
                
                ans = cost3

print(ans)

Solution (Python):


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

def zet(l,m,n):
    return abs(A-l)+abs(B-m)+abs(C-n)

def saiki(x,y,z,n,count,ans):
    if n==N:
        if x==0 or y==0 or z==0:
            return 10**18
        else:
            return min(zet(x,y,z),zet(x,z,y),zet(y,x,z),zet(y,z,x),zet(z,x,y),zet(z,y,x))+count*10
    else:
        ans=min(saiki(x+l[n],y,z,n+1,count+1 if x!=0 else count,ans),ans)
        ans=min(saiki(x,y+l[n],z,n+1,count+1 if y!=0 else count,ans),ans)
        ans=min(saiki(x,y,z+l[n],n+1,count+1 if z!=0 else count,ans),ans)
        ans=min(saiki(x,y,z,n+1,count,ans),ans)
    return ans

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

print(saiki(0,0,0,0,0,10**18))

Solution (Python):
import numpy as np
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 np.inf
  
  ret0 = dfs(cur + 1, a, b, c)
  ret1 = dfs(cur + 1, a + l[cur], b, c) + 10
  ret2 = dfs(cur + 1, a, b + l[cur], c) + 10
  ret3 = dfs(cur + 1, a, b, c + l[cur]) + 10
  return min(ret0, ret1, ret2, ret3)

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

Solution (Python):
def examC():
    N, A, B, C = LI()
    L = [I() for _ in range(N)]
    loop = 4**N; ans = 10**9
    for i in range(loop):
        cur = 0
        curA = 0; curB = 0; curC = 0
        for j in range(N):
            judge = (i//(4**j))%4
            if judge==1:
                if curA>0:
                    cur +=10
                curA += L[j]
            elif judge==2:
                if curB>0:
                    cur +=10
                curB += L[j]
            elif judge==3:
                if curC>0:
                    cur +=10
                curC += L[j]
        if curA==0 or curB==0 or curC==0:
            continue
        cur += abs(curA-A)
        cur += abs(curB-B)
        cur += abs(curC-C)
        ans = min(cur,ans)
    print(ans)

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

if __name__ == '__main__':
    examC()

Solution (Python):

from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations
import sys
import bisect
import string
import math
import time
from fractions import gcd



def I(): return int(input())


def MI(): return map(int, input().split())


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


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


def input(): return sys.stdin.readline().rstrip()


def show(*inp, end='\n'):
    if show_flg:
        print(*inp, end=end)


YN = ['Yes', 'No']
MOD = 10**9+7
inf = float('inf')
IINF = 10**19
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)


show_flg = False


A = 0
B = 0
C = 0
N = 0


def dfs(cur, L, a, b, c):
    
    if cur == N:
        return abs(A - a) + abs(B - b) + abs(C - c) if min(a, b, c) > 0 else IINF

    ret0 = dfs(cur + 1, L, a, b, c)
    ret1 = dfs(cur + 1, L, a + L[cur], b, c) + (0 if a == 0 else 10)
    ret2 = dfs(cur + 1, L, a, b + L[cur], c) + (0 if b == 0 else 10)
    ret3 = dfs(cur + 1, L, a, b, c + L[cur]) + (0 if c == 0 else 10)

    return min(ret0, ret1, ret2, ret3)


def main():
    global A
    global B
    global C
    global N
    N, A, B, C = MI()
    L = [0] * N

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

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


if __name__ == '__main__':
    main()

Solution (Python):

from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations
import sys
import bisect
import string
import math
import time
from fractions import gcd



def I(): return int(input())


def MI(): return map(int, input().split())


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


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


def input(): return sys.stdin.readline().rstrip()


def show(*inp, end='\n'):
    if show_flg:
        print(*inp, end=end)


YN = ['Yes', 'No']
MOD = 10**9+7
inf = float('inf')
IINF = 10**19
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)


show_flg = False


A = 0
B = 0
C = 0
N = 0


def dfs(cur, L, a, b, c):
    
    if cur == N:
        return abs(A - a) + abs(B - b) + abs(C - c) if min(a, b, c) > 0 else IINF

    ret0 = dfs(cur + 1, L, a, b, c)
    ret1 = dfs(cur + 1, L, a + L[cur], b, c) + (0 if a == 0 else 10)
    ret2 = dfs(cur + 1, L, a, b + L[cur], c) + (0 if b == 0 else 10)
    ret3 = dfs(cur + 1, L, a, b, c + L[cur]) + (0 if c == 0 else 10)

    return min(ret0, ret1, ret2, ret3)


def main():
    global A
    global B
    global C
    global N
    N, A, B, C = MI()
    L = [0] * N

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

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


if __name__ == '__main__':
    main()

Solution (Python):
import itertools

N, A, B, C = map(int, input().split())
L = [int(input()) for i in range(N)]
class_list = ["A", "B", "C", "D"]

a_list = list(itertools.product(class_list, repeat=N))


A_sum = 0
B_sum = 0
C_sum = 0
A_count = 0
B_count = 0
C_count = 0

min_score = 10**9
for a in a_list:
    for idx, b in enumerate(a):
        
        if b == "A":
            A_sum += L[idx]
            A_count += 1
        elif b == "B":
            B_sum += L[idx]
            B_count += 1
        elif b == "C":
            C_sum += L[idx]
            C_count += 1
    if A_count > 0 and B_count > 0 and C_count > 0:
        score = (A_count-1+B_count-1+C_count-1) * 10 + abs(A-A_sum) + abs(B-B_sum) + abs(C-C_sum)
        
        min_score = min(min_score, score)
    A_sum = 0
    B_sum = 0
    C_sum = 0
    A_count = 0
    B_count = 0
    C_count = 0

print(min_score)

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 doit(index, fora, forb, forc):
    if index == len(l):
        if min(fora, forb, forc) > 0:
            return abs(fora - a) + abs(forb - b) + abs(forc - c)
        else:
            return 10000
    return min(doit(index + 1, fora, forb, forc),
               doit(index + 1, fora + l[index], forb, forc) + 10,
               doit(index + 1, fora, forb + l[index], forc) + 10,
               doit(index + 1, fora, forb, forc + l[index]) + 10)

print(doit(0, 0, 0, 0) - 30)

Solution (Python):
import itertools

N,A,B,C=map(int,input().split())
l=[int(input()) for i in range(N)]
PRD=list(itertools.product([0,1,2,3], repeat=N))

P=10**5
for i in range(len(PRD)):
    a,b,c=0,0,0
    for j in range(N):
        if PRD[i][j]==1:
            a+=l[j]
        elif PRD[i][j]==2:
            b+=l[j]
        elif PRD[i][j]==3:
            c+=l[j]
    if 0 in [a,b,c]:
        continue
    L=PRD[i]
    Q=abs(A-a)+abs(B-b)+abs(C-c)+(L.count(1)+L.count(2)+L.count(3)-3)*10
    if P>=Q:
        P=Q

print(P)

Solution (Python):
def dt(a):
    for x in range(1,9):
        if 4**x > a:
            y = x-1
            break
    n = ''
    for i in range(y+1):
        t = a // 4 ** (y-i)
        n += str(t)
        a -= t * 4 ** (y-i)
    return n

def MP(a,b):
    return (len(a)-1)*10 + abs(sum(a)-b)

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

for i in range(4**N):
    jj = dt(i)
    j = jj.zfill(N)
    for (k,x) in enumerate(j):
        if x == '0':
            continue
        elif x == '1':
            ls[0].append(H[k])
        elif x == '2':
            ls[1].append(H[k])
        elif x == '3':
            ls[2].append(H[k])
    else:
        if ls[0] and ls[1] and ls[2]:
            ans = min(ans, MP(ls[0],A)+MP(ls[1],B)+MP(ls[2],C))
        ls = [[],[],[]]

print(ans)

Solution (Python):
from itertools import permutations
from sys import setrecursionlimit
setrecursionlimit(1000000)

def doit(a, b, c, idx):
    if (a, b, c, idx) in memo:
        return memo[a, b, c, idx]
    if idx == len(unused):
        return abs(a - needA) + abs(b - needB) + abs(c - needC)
    memo[a, b, c, idx] = min(
        doit(a, b, c, idx + 1),
        doit(a + unused[idx], b, c, idx + 1) + 10,
        doit(a, b + unused[idx], c, idx + 1) + 10,
        doit(a, b, c + unused[idx], idx + 1) + 10
    )
    return memo[a, b, c, idx]

n, needA, needB, needC = map(int, input().split())
a = [int(input()) for _ in range(n)]
ret = float('inf')
for mask in range(1 << n):
    if bin(mask).count('1') != 3:
        continue
    abc = [a[i] for i in range(n) if (mask >> i) & 1]
    unused = [a[i] for i in range(n) if not (mask >> i) & 1]
    for p in permutations(abc):
        memo = {}
        ret = min(ret, doit(p[0], p[1], p[2], 0))
print(ret)

Solution (Python):
from sys import setrecursionlimit
setrecursionlimit(1000000)

def permute(a, idx):
    if idx == len(a):
        permutations.append(a[::])
    for i in range(idx, len(a)):
        a[i], a[idx] = a[idx], a[i]
        permute(a, idx + 1)
        a[i], a[idx] = a[idx], a[i]

def doit(a, b, c, idx):
    if (a, b, c, idx) in memo:
        return memo[a, b, c, idx]
    if idx == len(unused):
        return abs(a - needA) + abs(b - needB) + abs(c - needC)
    memo[a, b, c, idx] = min(
        doit(a, b, c, idx + 1),
        doit(a + unused[idx], b, c, idx + 1) + 10,
        doit(a, b + unused[idx], c, idx + 1) + 10,
        doit(a, b, c + unused[idx], idx + 1) + 10
    )
    return memo[a, b, c, idx]

n, needA, needB, needC = map(int, input().split())
a = [int(input()) for _ in range(n)]
ret = float('inf')
for mask in range(1 << n):
    if bin(mask).count('1') != 3:
        continue
    abc = [a[i] for i in range(n) if (mask >> i) & 1]
    unused = [a[i] for i in range(n) if not (mask >> i) & 1]
    permutations = []
    permute(abc, 0)
    for p in permutations:
        memo = {}
        ret = min(ret, doit(p[0], p[1], p[2], 0))
print(ret)

Solution (Python):
import sys
import math
n, a, b, c = [int(i) for i in sys.stdin.readline().split()]
l_ls = []
INF = 10 ** 18
def get_4_sinsu(x):
    if x == 0:
        return "0"
    res = ""
    for i in range(math.floor(math.log(x, 4)), -1, -1):
        res += str(x // (4 ** i))
        x %= 4 ** i
    return res

for i in range(n):
    l = int(input())
    l_ls.append(l)
best = INF
for i in range(4 ** n):
    dic = {0 : [], 1 : [], 2 : []}
    _i = get_4_sinsu(i).zfill(n)
    cost = 0
    for k, j in enumerate(_i):
        if j == "3":
            continue
        if j == "0":
            dic[0].append(l_ls[k])
        elif j == "1":
            dic[1].append(l_ls[k])
        else:
            dic[2].append(l_ls[k])
    if any([len(j) == 0 for j in dic.values()]):
        continue
    res = abs(sum(dic[0]) - a) + abs(sum(dic[1]) - b) + abs(sum(dic[2]) - c) + max(0, len(dic[0]) - 1) * 10 + \
                                                                                max(0, len(dic[1]) - 1) * 10 + \
                                                                                 max(0, len(dic[2]) - 1) * 10
    best = min(best, res)
print(best)

Solution (Python):
INF = 1e+9
import sys
sys.setrecursionlimit(int(INF))

N, A, B, C = map(int, input().split())
l = list()
for n in range(N):
  l.append(int(input()))
  
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
    branch1 = dfs(depth+1, a+l[depth], b, c) + 10
    branch2 = dfs(depth+1, a, b+l[depth], c) + 10
    branch3 = dfs(depth+1, a, b, c+l[depth]) + 10
    branch4 = dfs(depth+1, a, b, c)
    return min(branch1, branch2, branch3, branch4)
print(dfs(0,0,0,0))

Solution (Python):
def MAP(): return list(map(int,input().split()))

n,A,B,C = MAP()
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):
import sys
input = sys.stdin.readline

N,A,B,C = [int(x) for x in input().split()]
ln = tuple([int(input()) for _ in range(N)])

def dp(A, B, C, a, b, c, n):
    global ln
    if n == N:
        if len(a) == 0 or len(b) == 0 or len(c) == 0:
            return float('INF')
        cost = (len(a) + len(b) + len(c) - 3) * 10
        return cost + abs(A - sum(a)) + abs(B - sum(b)) + abs(C - sum(c))
    ans = float('INF')
    ans = min(ans, dp(A, B, C, a[:], b[:], c[:], n+1))
    ans = min(ans, dp(A, B, C, a[:]+[ln[n]], b[:], c[:], n+1))
    ans = min(ans, dp(A, B, C, a[:], b[:]+[ln[n]], c[:], n+1))
    ans = min(ans, dp(A, B, C, a[:], b[:], c[:]+[ln[n]], n+1))
    return ans
print(dp(A, B, C, [], [], [], 0))