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):
from itertools import product
N, A, B, C = map(int, input().split())
L = [int(input()) for i in range(N)]
INF = 10 ** 30
def calc(p):
    if sum(list(set(p))) < 6:
        return INF
    a, b, c = 0, 0, 0
    ac, bc, cc = 0, 0, 0
    for i, v in enumerate(p):
        if v == 1:
            a += L[i]
            ac += 1
        elif v == 2:
            b += L[i]
            bc += 1
        elif v == 3:
            c += L[i]
            cc += 1
    return abs(A-a) + abs(B-b) + abs(C-c) + (ac+bc+cc-3) * 10

ans = INF
for p in product(range(4), repeat=N):
    ans = min(ans, calc(p))
print(ans)

Solution (Python):
from itertools import product
import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)

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

ans = 10 ** 18
for alloc in product(range(4), repeat=N):
    La = 0
    Lb = 0
    Lc = 0
    use_a = 0
    use_b = 0
    use_c = 0
    for i, f in enumerate(alloc):
        if f == 0:
            La += L[i]
            use_a += 1
        elif f == 1:
            Lb += L[i]
            use_b += 1
        elif f == 2:
            Lc += L[i]
            use_c += 1
        else:
            pass

    if not all([use_a, use_b, use_c]):
        continue

    score = 0
    score += 10 * (use_a - 1)
    score += 10 * (use_b - 1)
    score += 10 * (use_c - 1)
    score += abs(A - La)
    score += abs(B - Lb)
    score += abs(C - Lc)
    ans = min(ans, score)

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):
from itertools import product

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

ans = 10**18
for state in product(range(4), repeat=N):
    K = [[] for _ in range(4)]
    for i, l in enumerate(L):
        K[state[i]].append(l)

    cost = 0
    for d, D in zip((A, B, C), K):
        if len(D) == 0:
            cost += 10**18
        cost += (len(D) - 1) * 10
        cost += abs(d - sum(D))
    ans = min(ans, cost)

print(ans)

Solution (Python):
import sys
def input(): return sys.stdin.readline().strip()


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

    """
    解答方針は正しかったが、選んだ竹のA,B,Cへの割り振り方の実装にてこずったので、
    潔く模範解答を写経することにする。
    こういう全列挙は再帰を使うと超あっさり書けるのは聞いてたけど、まだ実践力が足らぬ。。
    再帰を木構造に書き表せたら自然にdfsと結びつくのか！
    """

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





if __name__ == "__main__":
    main()

Solution (Python):
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    sys.setrecursionlimit(10**7)
    INF = 10**4
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]
    NI = lambda : int(sys.stdin.readline())

    N,A,B,C = LI()
    l = [NI() for _ in range(N)]
    x = [0] * 3
    ans = INF
    for p in itertools.combinations(range(N),3):
        q = []
        for i in range(N):
            if not i in p: q.append(i)
        for i in range(4**(N-3)):
            for j in range(3): x[j] = l[p[j]]
            mp = 0
            r = i
            for j in range(N-3):
                k = r % 4
                if k > 0:
                    x[k-1] += l[q[j]]
                    mp += 10
                r //= 4
            for a,b,c in itertools.permutations(x):
                ans = min(ans,mp+abs(A-a)+abs(B-b)+abs(C-c))

    print(ans)



if __name__ == '__main__':
    main()

Solution (Python):
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    sys.setrecursionlimit(10**7)
    INF = 10**4
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]
    NI = lambda : int(sys.stdin.readline())

    N,A,B,C = LI()
    l = [NI() for _ in range(N)]
    m = [i for i in range(N)]
    x = [0, 0, 0]
    ans = INF
    for p in itertools.combinations(m,3):
        q = []
        for i in range(N):
            if not i in p: q.append(i)
        for i in range(4**(N-3)):
            for j in range(3): x[j] = l[p[j]]
            mp = 0
            r = i
            for j in range(N-3):
                k = r % 4
                if k > 0:
                    x[k-1] += l[q[j]]
                    mp += 10
                r //= 4
            for a,b,c in itertools.permutations(x):
                ans = min(ans,mp+abs(A-a)+abs(B-b)+abs(C-c))

    print(ans)



if __name__ == '__main__':
    main()

Solution (Python):

 
import sys












from itertools import product, permutations  







 
 
def main():
    mod = 1000000007                
    inf = float('inf')              
    
    sys.setrecursionlimit(10**6)    
    def input(): return sys.stdin.readline().rstrip()
    def ii():    return int(input())
    def mi():    return map(int, input().split())
    def mi_0():  return map(lambda x: int(x)-1, input().split())
    def lmi():   return list(map(int, input().split()))
    def lmi_0(): return list(map(lambda x: int(x)-1, input().split()))
    def li():    return list(input())
 

    def calc_min_mp(L, a, b, c):
        ans = inf
        quaternary = product(range(4), repeat=n)
        for pattern in quaternary:
            if pattern.count(1) and pattern.count(2) and pattern.count(3):
                needed_mp =  (n - pattern.count(0) - 3) * 10    
                base_abc = [0, 0, 0]    
                for i, num in enumerate(pattern):
                    if num != 0:
                        base_abc[num - 1] += L[i]
                needed_mp += abs(a - base_abc[0]) + abs(b - base_abc[1]) + abs(c - base_abc[2])
                ans = min(ans, needed_mp)
        return ans
 
    
    
    n, a, b, c = mi()
    L = [ii() for _ in range(n)]
 
    print(calc_min_mp(L, a, b, c))
 
 
if __name__ == "__main__":
    main()

Solution (Python):
import sys
from itertools import product

sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


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


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

    
    
    
    
    ans = INF
    for pattern in P:
        mp = 0
        a = 0
        b = 0
        c = 0
        for i, p in enumerate(pattern):
            if p == 0:
                continue
            elif p == 1:
                if a != 0:
                    mp += 10
                a += L[i]
            elif p == 2:
                if b != 0:
                    mp += 10
                b += L[i]
            else:
                if c != 0:
                    mp += 10
                c += L[i]

        if a == 0 or b == 0 or c == 0:
            continue

        mp += abs(A - a)
        mp += abs(B - b)
        mp += abs(C - c)

        if mp < ans:
            ans = mp

    print(ans)


if __name__ == "__main__":
    main()

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


def dfs(pos, a_tmp, b_tmp, c_tmp):
    if pos == n:
        if min(a_tmp, b_tmp, c_tmp) > 0:
            return abs(a-a_tmp)+abs(b-b_tmp)+abs(c-c_tmp)-30
        else:
            return INF
    res0 = dfs(pos+1, a_tmp, b_tmp, c_tmp)
    res1 = dfs(pos+1, a_tmp+l[pos], b_tmp, c_tmp)+10
    res2 = dfs(pos+1, a_tmp, b_tmp+l[pos], c_tmp)+10
    res3 = dfs(pos+1, a_tmp, b_tmp, c_tmp+l[pos])+10
    return min(res0, res1, res2, res3)


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

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from itertools import permutations, accumulate, combinations, combinations_with_replacement
from math import sqrt, ceil, floor, factorial
from bisect import bisect_left, bisect_right, insort_left, insort_right
from copy import deepcopy
from operator import itemgetter
from functools import reduce
from fractions import gcd
import sys
def input(): return sys.stdin.readline().rstrip()
def I(): return int(input())
def Is(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def TI(): return tuple(map(int, input().split()))
def IR(n): return [I() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def TIR(n): return [TI() for _ in range(n)]
def S(): return input()
def Ss(): return input().split()
def LS(): return list(input())
def SR(n): return [S() for _ in range(n)]
def SsR(n): return [Ss() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
sys.setrecursionlimit(10**6)
MOD = 10**9+7
INF = float('inf')


n, A, B, C = Is()
L = IR(n)

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

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

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


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


def resolve():
    INF = 10**18    

    
    def rec(i, a, b, c):
        
        if i == N:
            if not a or not b or not c:
                return INF
            return abs(a-A)+abs(b-B)+abs(c-C)
        
        
        res = rec(i+1, a, b, c)
        
        if a >= 1:
            res = min(res, rec(i+1, a+L[i], b, c)+10)
        else:
            res = min(res, rec(i+1, a+L[i], b, c))
        
        if b >= 1:
            res = min(res, rec(i+1, a, b+L[i], c)+10)
        else:
            res = min(res, rec(i+1, a, b+L[i], c))
        
        
        if c >= 1:
            res = min(res, rec(i+1, a, b, c+L[i])+10)
        else:
            res = min(res, rec(i+1, a, b, c+L[i]))

        return res

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

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

resolve()

Solution (Python):
import itertools

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

ans = float('inf')
for Lp in itertools.permutations(L):
    Lac = tuple(itertools.accumulate(([0] + list(Lp))))
    for a in range(1, N - 1):
        for b in range(a + 1, N):
            for c in range(b + 1, N + 1):
                A_MP = 10 * (a - 1) + abs(Lac[a] - Lac[0] - A)
                B_MP = 10 * (b - a - 1) + abs(Lac[b] - Lac[a] - B)
                C_MP = 10 * (c - b - 1) + abs(Lac[c] - Lac[b] - C)
                ans = min(ans, A_MP + B_MP + C_MP)
print(ans)

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

    comb = list(product([-1,0,1,2],repeat=N))

    mn = float('inf')
    for c in comb:
        sm = 0
        for i in range(3):
            elements = []
            for j in range(N):
                if c[j] == i:
                    elements.append(l[j])
            
            if len(elements) == 0:
                break
            sm += (len(elements) - 1) * 10
            sm += abs(models[i] - sum(elements))
        else:
            mn = min(mn, sm)
    
    print(mn)

if __name__ == '__main__':
    solve()

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

AMP=0
BMP=0
CMP=0
MP=10**5

def dfs(S,N,l,A,B,C):
    A_l=0
    B_l=0
    C_l=0
    A_cnt=0
    B_cnt=0
    C_cnt=0
    if len(S)==N:
        
        for n in range(N):
            if S[n]==0:
                pass
            elif S[n]==1:
                A_l+=l[n]
                A_cnt+=1
            elif S[n]==2:
                B_l+=l[n]
                B_cnt+=1
            else:
                C_l+=l[n]
                C_cnt+=1
        
        global MP
        if A_cnt>=1 and B_cnt>=1 and C_cnt>=1:
            MP=min(MP,(A_cnt-1+B_cnt-1+C_cnt-1)*10+abs(A-A_l)+abs(B-B_l)+abs(C-C_l))
        return
    else:
        for n in range(4):
            S.append(n)
            dfs(S,N,l,A,B,C)
            S.pop()

dfs([],N,l,A,B,C)
print(MP)

Solution (Python):
import itertools
N, A, B, C = map(int, input().split())
ln = [int(input()) for _ in range(N)]
bit = itertools.product([0, 1, 2, 3], repeat=N)
ans = 10000
for bi in bit:
    cnt = 0
    a = []
    b = []
    c = []
    for i in range(N):
        if bi[i] == 1:
            a.append(ln[i])
        elif bi[i] == 2:
            b.append(ln[i])
        elif bi[i] == 3:
            c.append(ln[i])
    if len(a) == 0 or len(b) == 0 or len(c) == 0:
        continue
    cnt += 10*(max(len(a)-1, 0)+max(len(b)-1, 0)+max(len(c)-1, 0))
    temp = [sum(a), sum(b), sum(c)]
    temp.sort()
    cnt += abs(temp[0]-C) + abs(temp[1]-B) + abs(temp[2]-A)
    ans = min(ans, cnt)
print(ans)

Solution (Python):
n, a, b, c=map(int, input().split())
l=[0]*n
ch=0
ans=0

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

ans+=abs(l[0]-a)
ans+=abs(l[1]-b)
ans+=abs(l[2]-c)

    
for i in range(4**n):
    ch=0
    mp=0
    s=[0]*4
    for j in range(n):
        for k in range(3):
            if (i>>2*j) % 2**2 ==k:
                if s[k]==0:
                    0
                else:
                    mp+=10
                s[k]+=l[j]
                    
    for p in range(3):
        if s[p]==0:
            
            ch=1
    if ch==1:
        0
        
    else:
        mp+=abs(int(s[0]-a))
        mp+=abs(int(s[1]-b))
        mp+=abs(int(s[2]-c))
        if mp<ans:
            ans=mp
            s_ans=s
            j_ans=j
    
    
print(ans)

Solution (Python):
def check(s,e,t):
    global ans
    if(not s or not e or not t):
        return
    ans=min(ans,(len(s)-1)*10+abs(A-sum(s))+(len(e)-1)*10+abs(B-sum(e))+(len(t)-1)*10+abs(C-sum(t)))

def dfs(s,e,t,i):
    if(i==N):
        check(s,e,t)
        return
    dfs(s,e,t,i+1)
    dfs(s+[l[i]],e,t,i+1)
    dfs(s,e+[l[i]],t,i+1)
    dfs(s,e,t+[l[i]],i+1)

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

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

Solution (Python):

from itertools import product
import sys
try:
    from typing import List
except ImportError:
    pass


def solve(N: int, A: int, B: int, C: int, l: "List[int]"):
    m = None
    for p in product(range(4), repeat=N):
        a = {i for i in range(N) if p[i] == 0}
        b = {i for i in range(N) if p[i] == 1}
        c = {i for i in range(N) if p[i] == 2}
        if not a or not b or not c:
            continue
        md = (
            abs(sum(l[ai] for ai in a) - A) + 10 * (len(a) - 1) +
            abs(sum(l[bi] for bi in b) - B) + 10 * (len(b) - 1) +
            abs(sum(l[ci] for ci in c) - C) + 10 * (len(c) - 1)
        )
        if m is None or m > md:
            m = md

    print(m)


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    A = int(next(tokens))  
    B = int(next(tokens))  
    C = int(next(tokens))  
    l = [int(next(tokens)) for _ in range(N)]  
    solve(N, A, B, C, l)


if __name__ == '__main__':
    main()

Solution (Python):
def calc_mp(ls, target):
    ret = 0
    ret += 10 * (len(ls) - 1)
    val = sum(ls)
    ret += abs(val - target)
    return ret


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

pattern = []

for i in range(4 ** n):
    tmp = [0] * n
    val = i
    for j in range(n):
        tmp[j] = val % 4
        val = val // 4
    pattern.append(tmp)

ret = 10 ** 100

for item in pattern:
    if 1 not in item or 2 not in item or 3 not in item:
        continue
    else:
        ones = []
        twos = []
        threes = []
        for i in range(n):
            if item[i] == 1:
                ones.append(l_ls[i])
            elif item[i] == 2:
                twos.append(l_ls[i])
            elif item[i] == 3:
                threes.append(l_ls[i])
        mp = 0
        mp += calc_mp(ones, a)
        mp += calc_mp(twos, b)
        mp += calc_mp(threes, c)
        ret = min(ret, mp)

print(ret)

Solution (Python):



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


class DFS:

    def __init__(self, l_list, N, A, B, C):
        self.l_list = l_list
        self.N = N
        self.A = A
        self.B = B
        self.C = C
        self.INF = float('INF')
        self.ans = float('INF')

    def get_ans(self):
        return self.ans

    def dfs(self, n, a, b, c):
        if n==self.N:
            if not a==0 and not b==0 and not c==0:
                return abs(a-self.A) + abs(b-self.B) + abs(c-self.C)
            else:
                return self.INF

        
        self.ans = min(self.ans, self.dfs(n+1, a, b, c))

        
        if a==0:
            self.ans = min(self.ans, self.dfs(n+1, a+self.l_list[n], b, c))
        else:
            self.ans = min(self.ans, self.dfs(n+1, a+self.l_list[n], b, c) + 10)

        if b==0:
            self.ans = min(self.ans, self.dfs(n+1, a, b+self.l_list[n], c))
        else:
            self.ans = min(self.ans, self.dfs(n+1, a, b+self.l_list[n], c) + 10)

        if c==0:
            self.ans = min(self.ans, self.dfs(n+1, a, b, c+self.l_list[n]))
        else:
            self.ans = min(self.ans, self.dfs(n+1, a, b, c+self.l_list[n]) + 10)

        return self.ans


dfs_obj = DFS(l_list, N, A, B, C)
dfs_obj.dfs(0, 0, 0, 0)


print(dfs_obj.get_ans())

Solution (Python):
n, a, b, c = list(map(int, input().split(' ')))
ln = [None] * n
for i in range(n):
    ln[i] = (int(input()))


def dfs(i, ap, bp, cp):
    pattern = [ap, bp, cp]
    if i >= n and [] not in pattern:
        mp = 0
        for p in pattern:
            mp += max([0, len(p) - 1]) * 10
        mp += abs(a - sum(pattern[0]))
        mp += abs(b - sum(pattern[1]))
        mp += abs(c - sum(pattern[2]))
        return mp
    elif i >= n:
        return float('inf')
    else:
        return min([
            dfs(i + 1, ap + [ln[i]], bp, cp),
            dfs(i + 1, ap, bp + [ln[i]], cp),
            dfs(i + 1, ap, bp, cp + [ln[i]]),
            dfs(i + 1, ap, bp, cp)
        ])


result = dfs(0, [], [], [])
print(result)

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

def cost(group,target):
    res = 10*(len(group)-1)
    return res + abs(sum(group)-target)

ans = []
for i in range(1<<(2*N)):
    groupA = []
    groupB = []
    groupC = []
    for j in range(N):
        s = i>>(2*j)
        if (s & 3) == 3:
            groupA.append(L[j])
        elif (s & 2) == 2:
            groupB.append(L[j])
        elif (s & 1) == 1:
            groupC.append(L[j])
    
    if len(groupA)==0 or len(groupB)==0 or len(groupC)==0:
        pass
    else:
        ans.append(cost(groupA,A)+cost(groupB,B)+cost(groupC,C))
print(min(ans))

Solution (Python):
def min_mp(bls, A, B, C, a_used=False, b_used=False, c_used=False):
    if sum([a_used, b_used, c_used]) == 3:
        return 0
    else:
        min_val = 10**9  
        non_used_num = 3 - sum([a_used, b_used, c_used])
        if len(bls) - 1 >= non_used_num:
            min_val = min([
                min_val,
                min_mp(bls[1:], A, B, C, a_used, b_used, c_used)
            ])
        if len(bls) > 0:
            if not a_used:
                min_val = min([
                    min_val,
                    abs(bls[0] - A) + min_mp(bls[1:], A, B, C, not a_used, b_used, c_used),
                    10 + min_mp(bls[1:], A - bls[0], B, C, a_used, b_used, c_used)
                ])
            if not b_used:
                min_val = min([
                    min_val,
                    abs(bls[0] - B) + min_mp(bls[1:], A, B, C, a_used, not b_used, c_used),
                    10 + min_mp(bls[1:], A, B - bls[0], C, a_used, b_used, c_used)
                ])
            if not c_used:
                min_val = min([
                    min_val,
                    abs(bls[0] - C) + min_mp(bls[1:], A, B, C, a_used, b_used, not c_used),
                    10 + min_mp(bls[1:], A, B, C - bls[0], a_used, b_used, c_used)
                ])
        return min_val


def main():
    N, A, B, C = list(map(int, input().split(' ')))
    bls = [int(input()) for _ in range(N)]  
    print(min_mp(bls, A, B, C))


if __name__ == '__main__':
    main()

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 = [int(input()) for _ in range(N)]
def dfs(i, a, b, c):
    if i == N:
        if a == 0 or b == 0 or c == 0:
            return float('inf')
        else:
            return abs(A-a)+abs(B-b)+abs(C-c)-30
    four = dfs(i+1, a, b, c)
    one = dfs(i+1, a+l[i], b, c) + 10
    two = dfs(i+1, a, b+l[i], c) + 10
    three = dfs(i+1, a, b, c+l[i]) + 10
    return min(one, two, three, four)
print(dfs(0, 0, 0, 0))

Solution (Python):
n,a,b,c = map(int, input().split())
l = [int(input()) for i in range(n)]
y=[0]*n
x=[]
def dfs(n,y,now):
    if now==n:
        z=y.copy()
        x.append(z)
        return
    for i in range(4):
        y[now]=i
        dfs(n,y,now+1)
dfs(n,y,0)
mi=1000000
for i in range(len(x)):
    al,bl,cl=0,0,0
    ac,bc,cc=0,0,0
    for j in range(n):
        if x[i][j]==1:
            al+=l[j]
            ac+=1
        if x[i][j]==2:
            bl+=l[j]
            bc+=1
        if x[i][j]==3:
            cl+=l[j]
            cc+=1
    if ac!=0 and bc!=0 and cc!=0:
        ans=abs(a-al)+abs(b-bl)+abs(c-cl)+10*(ac+bc+cc-3)
        mi=min(mi,ans)
print(mi)

Solution (Python):
n,a,b,c=map(int,input().split())
l=[]
for i in range(n):
  l.append(int(input()))
  
import itertools as it
pattern=list(it.product(range(4),repeat=n))

keep=10**100
for i in range(0,len(pattern)):
  kep1=0
  kep2=0
  kep3=0
  mp=0
  liis=pattern[i]
  for j in range(0,n):
    if liis[j]==1:
      if kep1==0:
        kep1=l[j]
      else:
        kep1+=l[j]
        mp+=10
    elif liis[j]==2:
      if kep2==0:
        kep2=l[j]
      else:
        kep2+=l[j]
        mp+=10
    elif liis[j]==3:
      if kep3==0:
        kep3=l[j]
      else:
        kep3+=l[j]
        mp+=10
  if kep1==0 or kep2==0 or kep3==0:
    continue
  mp+=abs(kep1-a)+abs(kep2-b)+abs(kep3-c)
  if mp<keep:
    keep=mp
print(keep)

Solution (Python):
N, A, B, C, *X = map(int, open(0).read().split())

def dfs(i, a, b, c):
    if i == N:
        if a * b * c == 0:
            return 10 ** 9 + 7
        return abs(A - a) + abs(B - b) + abs(C - c)

    v0 = dfs(i + 1, a, b, c)
    v1 = dfs(i + 1, a + X[i], b, c) + int(a > 0) * 10
    v2 = dfs(i + 1, a, b + X[i], c) + int(b > 0) * 10
    v3 = dfs(i + 1, a, b, c + X[i]) + int(c > 0) * 10

    return min(v0, v1, v2, v3)


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

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

res2=1000000000

for i in range(2**n-1):
	for j in range(2**n-1):
		x=[]
		y=[]
		z=[]
		for k in range(n):
			
			if i>>k&1:
				if j>>k&1:
					x.append(L[k])
				else:
					y.append(L[k])
			else:
				if j>>k&1:
					z.append(L[k])
		res1=abs(sum(x)-a)+abs(sum(y)-b)+abs(sum(z)-c)
		
		if len(x)==0 or len(y)==0 or len(z)==0:
			continue
	
		if len(x)>1:
			res1+=10*(len(x)-1)
		if len(y)>1:
			res1+=10*(len(y)-1)
		if len(z)>1:
			res1+=10*(len(z)-1)				
		res2=min(res1,res2)

print(res2)

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

from itertools import product
from collections import Counter

n, a, b, c, *l = map(int, read().split())
ans = float('inf')
for bit in product([0, 1, 2, 3], repeat=n):
    if 0 not in bit or 1 not in bit or 2 not in bit:
        continue
    counter = Counter()
    memo = 0
    for ll, num in zip(l, bit):
        if num == 3:
            continue
        if counter[num] != 0:
            memo += 10
        counter[num] += ll
    ans = min(ans, abs(counter[0] - a) + abs(counter[1] - b) + abs(counter[2] - c) + memo)
print(ans)

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

def dfs(cnt, x, y, z):
    if cnt == n:
        if x == 0 or y == 0 or z == 0:
            return float('INF')
        return abs(a - x) + abs(b - y) + abs(c - z) - 30
    r0 = dfs(cnt + 1, x, y, z)
    r1 = dfs(cnt + 1, x + ll[cnt], y, z) + 10
    r2 = dfs(cnt + 1, x, y + ll[cnt], z) + 10
    r3 = dfs(cnt + 1, x, y, z + ll[cnt]) + 10
    return min(r0, r1, r2, r3)

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

Solution (Python):
from itertools import product 
n,a,b,c = map(int,input().split())
l = [int(input()) for _ in range(n)]
tar = [a,b,c]
ans = float("inf")
for p in product(range(4),repeat=n):
    mp = 0
    length = [0]*3
    for i,j in enumerate(p):
        if j == 3: continue
        if length[j] > 0: mp += 10
        length[j] += l[i]
    for j in range(3):
        if length[j] == 0: break
        mp += abs(length[j]-tar[j])
    else:
        ans = min(ans, mp)
print(ans)

Solution (Python):

def dfs(d, A, B, C, f):
    global ans
    if d == n:
        if not 0 in [A, B, C]:
            ans = min(ans, abs(a - A) + abs(b - B) + abs(c - C) + f - 30)
        return
    dfs(d + 1, A, B, C, f)
    dfs(d + 1, A + l[d], B, C, f + 10)
    dfs(d + 1, A, B + l[d], C, f + 10)
    dfs(d + 1, A, B, C + l[d], f + 10)


n, a, b, c = map(int, input().split())
l = [int(input()) for _ in range(n)]
ans = 10 ** 10
dfs(0, 0, 0, 0, 0)
print(ans)

Solution (Python):
from itertools import product

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

ans = 10 ** 10
for bit in product([0, 1, 2, 3], repeat=n):
    tmp = [0] * 4
    mp = 0
    for i, j in enumerate(bit):
        if j != 3 and tmp[j] != 0:
            mp += 10
        tmp[j] += l[i]
    if any([i == 0 for i in tmp[:3]]):
        continue
    mp += abs(a - tmp[0]) + abs(b - tmp[1]) + abs(c - tmp[2])
    ans = min(ans, mp)
print(ans)

Solution (Python):
from itertools import product
from collections import Counter as ct

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

ans = 10 ** 10
for bit in product([0, 1, 2, 3], repeat=n):
    if len(set(bit) - {0}) != 3:
        continue
    tmpa = 0
    tmpb = 0
    tmpc = 0
    for i, j in enumerate(bit):
        if j == 1:
            tmpa += l[i]
        elif j == 2:
            tmpb += l[i]
        elif j == 3:
            tmpc += l[i]
    mp = abs(a - tmpa) + abs(b - tmpb) + abs(c - tmpc)
    tmp = ct(bit)
    for i in range(1, 4):
        mp += max(0, tmp[i] - 1) * 10
    ans = min(ans, mp)
print(ans)

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:
        
        if a == 0 or b == 0 or c == 0:
            return 10 ** 10
        return abs(A-a) + abs(B-b) + abs(C-c) - 30

    r1 = DFS(cnt+1, a, b, c)
    r2 = DFS(cnt+1, a+l[cnt], b, c) + 10
    r3 = DFS(cnt+1, a, b+l[cnt], c) + 10
    r4 = DFS(cnt+1, a, b, c+l[cnt]) + 10
    return min(r1, r2, r3, r4)

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

Solution (Python):

import sys
input = sys.stdin.readline









N,A,B,C = map(int,input().split())
L = [int(input()) for _ in range(N)]
 
from itertools import product
ans = float("inf")




for p in product(range(4),repeat=N):
    a = [L[i] for i in range(N) if p[i]==1]
    b = [L[i] for i in range(N) if p[i]==2]
    c = [L[i] for i in range(N) if p[i]==3]
 
    if a==[] or b==[] or c==[]:
        continue
    
    t = 0
    for x in [a,b,c]:
        
        if len(x)>=2:
            t += (len(x)-1)*10
    sumA = sum(a)
    sumB = sum(b)
    sumC = sum(c)
    
    t += abs(A-sumA) + abs(B-sumB) + abs(C-sumC)
 
    ans = min(ans,t)
 
print(ans)

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

def calc(a, b, c):
    la, lb, lc = len(a), len(b), len(c)
    if la + lb + lc < 3 or 0 in (la, lb, lc): return INF
    sa, sb, sc = sum(a), sum(b), sum(c)
    t = 0
    return abs(sa-A) + abs(sb-B) + abs(sc-C) + (la+lb+lc-3) * 10

def dfs(n, a, b, c):
    global ans
    if n == N:
        ans = min(ans, calc(a, b, c))
        return
    dfs(n+1, a, b, c)
    dfs(n+1, a+[l[n]], b, c)
    dfs(n+1, a, b+[l[n]], c)
    dfs(n+1, a, b, c+[l[n]])

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