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 collections import Counter,defaultdict,deque
import sys
import bisect
import math
import itertools
import string
import queue
import copy


from itertools import permutations, combinations
from heapq import heappop, heappush

sys.setrecursionlimit(10**8)
mod = 10**9+7

def inp(): 
    return int(input())
def inpm(): 
    return map(int,input().split())
def inpl(): 
    return list(map(int, input().split()))
def inpls(): 
    return list(input().split())
def inplm(n): 
    return list(int(input()) for _ in range(n))
def inpll(n): 
    return sorted([list(map(int, input().split())) for _ in range(n)])

    
def main():
    ans=10**10
    n,a,b,c=inpm()
    l=inplm(n)
    for an in range(1,n-1):
        for use_a in combinations(range(n),an):
            tank = []
            for j in range(n):
                if j not in use_a:
                    tank.append(j)
            for bn in range(1,n-an):
                for use_b in combinations(tank,bn):
                    tank1=[]
                    for j in tank:
                        if j not in use_b:
                            tank1.append(j)
                    for cn in range(1,n-an-bn+1):
                        for use_c in combinations(tank1,cn):
                            a_pre=0
                            b_pre=0
                            c_pre=0
                            for i in use_a:
                                a_pre+=l[i]
                            for i in use_b:
                                b_pre+=l[i]
                            for i in use_c:
                                c_pre+=l[i]
                            cnt=0
                            cnt+= (len(use_a)+len(use_b)+len(use_c)-3)*10 + abs(a_pre-a)+abs(c_pre-c)+abs(b_pre-b)
                            if ans>cnt:
                                ans=cnt
                            
    print(ans) 

if __name__ == "__main__":
    main()

Solution (Python):
from itertools import product
N,A,B,C = map(int,input().split())
L = [int(input()) for _ in range(N)]
cntmin = abs(A-L[0])+abs(B-L[1])+abs(C-L[2])
for x in product(range(4),repeat=N):
    cnt = 0
    a = x.count(1)
    b = x.count(2)
    c = x.count(3)
    if a*b*c==0:continue
    A1 = []
    B1 = []
    C1 = []
    for i in range(N):
        if x[i]==1:
            A1.append(L[i])
        elif x[i]==2:
            B1.append(L[i])
        elif x[i]==3:
            C1.append(L[i])
    cntA = 10*(len(A1)-1)
    La = 0
    for l in A1:
        La += l
    cntA += abs(A-La)
    cntB = 10*(len(B1)-1)
    Lb = 0
    for l in B1:
        Lb += l
    cntB += abs(B-Lb)
    cntC = 10*(len(C1)-1)
    Lc = 0
    for l in C1:
        Lc += l
    cntC += abs(C-Lc)
    cntmin = min(cntmin,cntA+cntB+cntC)
        
print(cntmin)

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

def f(x,y,z,arr,cnt):
    global a
    global b
    global c
    global ans
    if arr:
        f(x + arr[0], y, z, arr[1:],cnt+1)
        f(x, y + arr[0], z, arr[1:],cnt+1)
        f(x, y, z + arr[0], arr[1:],cnt+1)
        f(x, y, z, arr[1:],cnt)
    else:
        if x > 0 and y > 0 and z > 0:
            ans.append(abs(a-x) + abs(b-y) + abs(c-z) + 10 * (cnt - 3))
f(0,0,0,l,0)
print(min(ans))

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

def f(x,y,z,arr,ca,cb,cc):
    global a
    global b
    global c
    global ans
    if arr:
        f(x + arr[0], y, z, arr[1:],ca+1,cb,cc)
        f(x, y + arr[0], z, arr[1:],ca,cb+1,cc)
        f(x, y, z + arr[0], arr[1:],ca,cb,cc+1)
        f(x, y, z, arr[1:],ca,cb,cc)
    else:
        if x > 0 and y > 0 and z > 0:
            ans.append(abs(a-x) + abs(b-y) + abs(c-z) + 10 * (ca + cb + cc - 3))
f(0,0,0,l,0,0,0)
print(min(ans))

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

def search(t,a,b,c):
    if t == n:
        return abs(A-a)+abs(B-b)+abs(C-c) -30 if min(a,b,c) > 0 else 10**9
        
        
    ret0 = search(t+1,a,b,c)
    ret1 = search(t+1,a+list1[t],b,c) +10
    ret2 = search(t+1,a,b+list1[t],c) +10
    ret3 = search(t+1,a,b,c+list1[t]) +10
    return min(ret0,ret1,ret2,ret3)

print(search(0,0,0,0))

Solution (Python):
import itertools


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

for pattern in itertools.product(range(4), repeat=N):
    if not {1, 2, 3} <= set(pattern):
        continue

    trees = [0] * 3
    synthesis_cost = -30
    for num, length in zip(pattern, L):
        if num == 0:
            continue
        trees[num-1] += length
        synthesis_cost += 10

    fix_cost = 0
    for tree_length, target_length in zip(trees, [A, B, C]):
        fix_cost += abs(target_length - tree_length)

    total_cost = synthesis_cost + fix_cost
    ans = min(ans, total_cost)

print(ans)

Solution (Python):
import itertools


n, a, b, c = map(int, input().split())
L = [int(input())for _ in range(n)]
L_perm = list(itertools.permutations(L))
ans = 10**9
for l in L_perm:
    for i in range(1, n-1):
        A_tree = l[:i]
        synthesis_A = 10 * (i-1)
        fix_A = abs(a-sum(A_tree))
        for j in range(i+1, n):
            B_tree = l[i:j]
            synthesis_B = 10 * (j-i-1)
            fix_B = abs(b-sum(B_tree))
            for k in range(j+1, n+1):
                C_tree = l[j:k]
                synthesis_C = 10 * (k-j-1)
                fix_C = abs(c-sum(C_tree))

                total = fix_A + fix_B + fix_C + synthesis_A + synthesis_B + synthesis_C
                ans = min(ans, total)

print(ans)

Solution (Python):

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

big=10**9

def dp(cur,a,b,c):
  if cur==n:
    if a==0 or b==0 or c==0:
      return big
    else:
      return abs(A-a)+abs(B-b)+abs(C-c)-30
  else:
    ret0 = dp(cur+1,a,b,c)
    ret1 = dp(cur+1,a+l[cur],b,c)+10
    ret2 = dp(cur+1,a,b+l[cur],c)+10
    ret3 = dp(cur+1,a,b,c+l[cur])+10
    return min([ret0, ret1, ret2, ret3])

ret=dp(0,0,0,0)
print(ret)

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


def ketame(n, i):
  return n // (4 ** i) % 4


mps = []
import itertools
for pro in itertools.product([0, 'a', 'b', 'c'], repeat=n):
  if pro.count('a') == 0 or pro.count('b') == 0 or pro.count('c') == 0:
    continue
  merge = {'a': 0, 'b': 0, 'c': 0}
  for i in range(n):
    if pro[i] != 0:
      merge[pro[i]] += l[i]
  mp = (max(pro.count('a') - 1, 0) + max(pro.count('b') - 1, 0) +
        max(pro.count('c') - 1, 0)) * 10
  mp += abs(merge['a'] - a)
  mp += abs(merge['b'] - b)
  mp += abs(merge['c'] - c)
  mps.append(mp)

print(min(mps))

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

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

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

Solution (Python):
def base_change(numbers,base,keta):
    s=""
    while numbers>0:
        a,b=divmod(numbers,base)
        if b==0:
            s+="0"
            numbers=a
        elif b!=0:
            s+=str(b)
            numbers=a
    if len(s)<keta:
        s+="0"*(keta-len(s))
    elif len(s)>keta:
        print("桁がどうやら足りないようです。桁をもう少し数が収まるように大きく取ってください　。")
    s=s[::-1]
    return s

n,A,B,C=map(int,input().split())
lists=[int(input()) for i in range(n)]   
mini=10**9
for i in range(4**(n)):
    alist=[0,0,0,0]
    counterlist=[0,0,0,0]
    S=base_change(i,4,n)
    for i in range(n):
        for j in range(3):
            if S[i]==str(j):
                alist[j]+=lists[i]
                counterlist[j]+=1
    if all(counterlist[j]>0 for j in range(3)):
        mini=min(mini,10*(sum(counterlist[:3])-3)+abs(alist[0]-A)+abs(alist[1]-B)+abs(alist[2]-C))
print(mini)

Solution (Python):
def find_index(l,x,default=-1):
    return l.index(x) if x in l else default

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)


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

    ans = 10**9
    for i in range(4**n):
        a = 0
        b = 0
        c = 0
        no_use = 0
        temp = 0
        status = base_10_to_n(i,4)
        status = status.zfill(n)
        status = list(map(int,list(status)))
        for j in range(n):
            if status[j] == 0:
                a += l[j]
                temp += 10
            if status[j] == 1:
                b += l[j]
                temp += 10
            if status[j] == 2:
                c += l[j]
                temp += 10
            if status[j] == 3:
                no_use += l[j]
        if a==0 or b==0 or c==0:
            continue
        temp += abs(A-a)+abs(B-b)+abs(C-c)-30
        ans = min(ans,temp)
    print(ans)


if __name__ == '__main__':
    main()

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


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
    Sa = dfs(i+1, a+l[i], b, c) + 10
    Sb = dfs(i+1, a, b+l[i], c) + 10
    Sc = dfs(i+1, a, b, c+l[i]) + 10
    Sx = dfs(i+1, a, b, c) 

    return min(Sa, Sb, Sc, Sx)

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

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





def dfs(cur, len_a, len_b, len_c):
    if cur == N:
        if min(len_a, len_b, len_c) > 0:
            
            return abs(len_a - A) + abs(len_b - B) + abs(len_c - C) - 30
        else:
            
            return INF
    
    not_use = dfs(cur + 1, len_a, len_b, len_c)
    
    use_for_a = dfs(cur + 1, len_a + L[cur], len_b, len_c) + 10
    
    use_for_b = dfs(cur + 1, len_a, len_b + L[cur], len_c) + 10
    
    use_for_c = dfs(cur + 1, len_a, len_b, len_c + L[cur]) + 10

    return min(not_use, use_for_a, use_for_b, use_for_c)


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()))
bit=[]
for i in range(1,4**n):
  bl=[]
  num=i
  while num!=0:
    bl.append(num%4)
    num=num//4
  while len(bl)<n:
    bl.append(0)
  bit.append(bl)
min=10**10
for i in bit:
  bit1=i
  l1=0
  l2=0
  l3=0
  c1,c2,c3=0,0,0
  for j in range(n):
    if bit1[j]==0:
      l1+=l[j]
      c1+=1
    elif bit1[j]==1:
      l2+=l[j]
      c2+=1
    elif bit1[j]==2: 
      l3+=l[j]
      c3+=1
  if l1==0 or l2==0 or l3==0:
    pass
  else:
    ans=0
    ans+=(c1+c2+c3-3)*10
    ans+=abs(a-l1)+abs(b-l2)+abs(c-l3)
    if min>ans:
      min=ans
print(min)

Solution (Python):
def TtoF(a):
  r = ''
  while a >= 1:
    r += str(a%4)
    a = a // 4
  return r[::-1]
n,a,b,c = map(int,input().split())
l = [int(input()) for i in range(n)]
m = float('INF')
for i in range(1,4**n):
  cur = TtoF(i)
  cur = '0'*(n-len(cur)) + cur
  abc = [[] for i in range(3)]
  for j in range(n):
    if cur[j] != '3':
      abc[int(cur[j])].append(l[j])
  if [] in abc:
    continue
  else:
    m = min(m,10*(len(abc[0])-1)+abs(sum(abc[0])-a)+10*(len(abc[1])-1)+abs(sum(abc[1])-b)+10*(len(abc[2])-1)+abs(sum(abc[2])-c))
print(m)

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

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

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

Solution (Python):
import itertools
n,a,b,c = map(int,input().split())

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

assign = [0, 1, 2, 3]

ans = float('inf')
for assignl in itertools.product(assign, repeat=n):
    mp = -30
    abc = [0,0,0]
    for idx, i in enumerate(assignl):
        if i > 0:
            mp += 10
            abc[i-1] += ll[idx]
    if 0 in abc:
        continue
    else:
        mp += abs(abc[0] - a)
        mp += abs(abc[1] - b)
        mp += abs(abc[2] - c)
    if mp < ans:
        ans = mp
        
print(ans)

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

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

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

iter = [[]]
for i in range(n):
  temp = []
  for num in range(0, 4):
    for lis in iter:
      temp.append(lis + [num])
  else:
    iter = temp

min_cost = 1000000000000000
for lis in iter:
  length = [0, 0, 0, 0]
  cost = -30
  if 1 in lis and 2 in lis and 3 in lis:
    for i in range(n):
      length[lis[i]] += L[i]
      if lis[i] != 0: 
        cost += 10
    cost += abs(length[1] - a)
    cost += abs(length[2] - b)
    cost += abs(length[3] - c)
    min_cost = min(min_cost, cost)
  else:
    continue
print(min_cost)

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


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

    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

        no_compound_pattern = dfs(cursor+1, a, b, c)
        compound_a_pattern = dfs(cursor+1, a + l[cursor], b, c) + 10
        compound_b_pattern = dfs(cursor+1, a, b + l[cursor], c) + 10
        compound_c_pattern = dfs(cursor+1, a, b, c + l[cursor]) + 10

        return min(no_compound_pattern, compound_a_pattern, compound_b_pattern, compound_c_pattern)

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


if __name__ == '__main__':
    main()

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


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

    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

        no_compound_pattern = dfs(cursor+1, a, b, c)
        compound_a_pattern = dfs(cursor+1, a + l[cursor], b, c) + 10
        compound_b_pattern = dfs(cursor+1, a, b + l[cursor], c) + 10
        compound_c_pattern = dfs(cursor+1, a, b, c + l[cursor]) + 10

        return min(no_compound_pattern, compound_a_pattern, compound_b_pattern, compound_c_pattern)

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


if __name__ == '__main__':
    main()

Solution (Python):
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    n,a,b,c=map(int,input().split())
    L=[int(input()) for _ in range(n)]

    def dfs(v,Idx)->int:
        
        if(v==n):
            score=0
            Idx=tuple(Idx)
            A,B,C=[],[],[]
            for i,k in enumerate(Idx):
                if(k==0): A.append(L[i])
                elif(k==1): B.append(L[i])
                elif(k==2): C.append(L[i])
            if((not A) or (not B) or (not C)): return INF 
            score+=(len(A)+len(B)+len(C)-3)*10

            SA=sum(A)
            SB=sum(B)
            SC=sum(C)
            score+=abs(SA-a)
            score+=abs(SB-b)
            score+=abs(SC-c)
            return score
            
        res=INF
        for i in range(4):
            Idx[v]=i
            res=min(res,dfs(v+1,Idx))
        return res

    print(dfs(0,[0]*n))
resolve()

Solution (Python):
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    n,a,b,c=map(int,input().split())
    L=[int(input()) for _ in range(n)]

    def dfs(v,Idx)->int:
        
        if(v==n):
            score=0
            Idx=tuple(Idx)
            A,B,C=[],[],[]
            for i,k in enumerate(Idx):
                if(k==0): A.append(L[i])
                elif(k==1): B.append(L[i])
                elif(k==2): C.append(L[i])
            if((not A) or (not B) or (not C)): return INF 
            score+=(len(A)+len(B)+len(C)-3)*10

            SA=sum(A)
            SB=sum(B)
            SC=sum(C)
            score+=abs(SA-a)
            score+=abs(SB-b)
            score+=abs(SC-c)

            return score

        res=INF
        for i in range(4):
            res=min(res,dfs(v+1,Idx+[i]))
        return res

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

Solution (Python):
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    n,a,b,c=map(int,input().split())
    L=[int(input()) for _ in range(n)]
    ans=INF
    from itertools import product
    for I in product(range(4),repeat=n):
        score=0
        I=tuple(I)
        A,B,C=[],[],[]
        for i,k in enumerate(I):
            if(k==0): A.append(L[i])
            elif(k==1): B.append(L[i])
            elif(k==2): C.append(L[i])
        if((not A) or (not B) or (not C)): continue 
        score+=(len(A)+len(B)+len(C)-3)*10

        SA=sum(A)
        SB=sum(B)
        SC=sum(C)
        score+=abs(SA-a)
        score+=abs(SB-b)
        score+=abs(SC-c)
        ans=min(ans,score)
    print(ans)
resolve()

Solution (Python):
n,A,B,C=map(int,input().split())
l=[int(input()) for _ in range(n)]
inf = 10**9
def dfs(t,a,b,c):
    if t == n:
        if min(a,b,c)>0: return abs(a-A)+abs(b-B)+abs(c-C)-30
        else: return inf
    ret0 = dfs(t+1,a,b,c)
    ret1 = dfs(t+1,a+l[t],b,c)+10
    ret2 = dfs(t+1,a,b+l[t],c)+10
    ret3 = dfs(t+1,a,b,c+l[t])+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(bamboo_number,a,b,c):
    
    
    if bamboo_number == N:
        
        if min(a,b,c) > 0:
            return abs(a-A)+abs(b-B)+abs(c-C) - 30 
        else:
            return INF
        
    
    
    
    ret1 = dfs(bamboo_number+1,a,b,c);
    ret2 = dfs(bamboo_number+1,a+l[bamboo_number],b,c) + 10
    ret3 = dfs(bamboo_number+1,a,b+l[bamboo_number],c) + 10
    ret4 = dfs(bamboo_number+1,a,b,c+l[bamboo_number]) + 10

    
    return min(ret1, ret2, ret3, ret4)

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

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



mi = -1
for i in range(4**n):
	j = i
	p = []
	m = n-1
	while m >= 0:
		p.append(j//(4**m))
		j = j%(4**m)
		m -= 1
	abc = [0 for k in range(3)]
	cnt = -30
	for k in range(n):
		if p[k] != 3:
			cnt += 10
			abc[p[k]] += l[k]
	if 0 not in abc:
		for k in range(3):
			cnt += abs(ABC[k] - abc[k])
		if mi == -1:
			mi = cnt
		else:
			mi = min(cnt, mi)
print(mi)

Solution (Python):
n,A,B,C=map(int,input().split())
L=[]
r=10000
for i in range(n):
	l=int(input())
	L.append(l)
def k(i,a,b,c,s):
	global r
	if i==n:
		if a!=A and b!=B and c!=C:
			s+=abs(a)+abs(b)+abs(c)-30
			r=min(r,s)
	else:
		k(i+1,a-L[i],b,c,s+10)
		k(i+1,a,b-L[i],c,s+10)
		k(i+1,a,b,c-L[i],s+10)
		k(i+1,a,b,c,s)
a=A
b=B
c=C
k(0,a,b,c,0)
print(r)

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

ans=[]
for i in range(4**N):
  A=[]
  B=[]
  C=[]
  p=i
  for j in range(N):
    if p%4==1:
      A.append(l[j])
    elif p%4==2:
      B.append(l[j])
    elif p%4==3:
      C.append(l[j])
    p=(p-p%4)//4
  if len(A)>0 and len(B)>0 and len(C)>0:
    ans.append(abs(sum(A)-a)+10*(len(A)-1)+abs(sum(B)-b)+10*(len(B)-1)+abs(sum(C)-c)+10*(len(C)-1))
    
print(min(ans))

Solution (Python):
n,a,b,c=map(int,input().split())
l=[]
for i in range(n):
	x=int(input())
	l.append(x)
def tet(x):
	s=""
	for i in range(n):
		if x%4==0:
			s+="0"
		elif x%4==1:
			s+="1"
		elif x%4==2:
			s+="2"
		else:
			s+="3"
		x//=4
	return s[::-1]
ans=10**25
for i in range(4**n):
	q=tet(i)
	A=B=C=0
	AA=BB=CC=0
	for j in range(n):
		if q[j]=="0":
			A+=l[j]
			AA+=1
		elif q[j]=="1":
			B+=l[j]
			BB+=1
		elif q[j]=="2":
			C+=l[j]
			CC+=1
	if AA==0 or BB==0 or CC==0:
		continue
	cnt=0
	cnt+=(AA+BB+CC-3)*10
	cnt+=abs(A-a)+abs(B-b)+abs(C-c)
	ans=min(cnt,ans)
print(ans)

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

ans = 10000
for dist in itertools.product(range(4),repeat=N):
    n = [0]*3
    l = [0]*3
    for i in range(N):
        k = dist[i]
        if k < 3:
            n[k] += 1
            l[k] += L[i]
    for k in range(3):
        if n[k] == 0:
            break
    else:
        p = (sum(n)-3)*10 + sum(abs(ABC[k]-l[k]) for k in range(3))
        if p < ans:
            ans = p
print(ans)

Solution (Python):

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


def dfs(times, a, b, c):  
    if times == N:
        return abs(a - A) + abs(b - B) + abs(c - C) - 30 if min(a, b, c) > 0 else INF
    
    no_pattern = dfs(times + 1, a, b, c)
    a_pattern = dfs(times + 1, a + L[times], b, c) + 10
    b_pattern = dfs(times + 1, a, b + L[times], c) + 10
    c_pattern = dfs(times + 1, a, b, c + L[times]) + 10

    return min(no_pattern, a_pattern, b_pattern, c_pattern)


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 = [int(x) for x in input().split()]
L = [int(input()) for _ in range(N)]
INF = 10**8
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)
ans = dfs(0, 0, 0, 0)
print(ans)

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

ret = float('inf')

def mp(x, target) :
    ret = -10
    long = 0
    for i in range(N) :
        if x & 1 :
            ret += 10
            long += l[i]
        x >>= 1
    ret += abs(long - target)
    return ret

for i in range(1, 1 << N) :
    for j in range(1, 1 << N) :
        if i & j :
            continue
        for k in range(1, 1 << N) :
            if i & k or j & k :
                continue
            ret = min(ret, mp(i, A) + mp(j, B) + mp(k, C))

print(ret)

Solution (Python):

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

def dfs(dep,a,b,c):
    if dep == N:
        if min(a,b,c) > 0:
          
            return abs(a-A)+abs(b-B)+abs(c-C) - 30
        else:
            return 10**9
    
    numA = dfs(dep+1,a+l[dep],b,c) + 10
    numB = dfs(dep+1,a,b+l[dep],c) + 10
    numC = dfs(dep+1,a,b,c+l[dep]) + 10
    numX = dfs(dep+1,a,b,c)
    return min(numA,numB,numC,numX)
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)]
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)
    retA = DFS(cur+1, a+l[cur], b, c) + 10
    retB = DFS(cur+1, a, b+l[cur], c) + 10
    retC = DFS(cur+1, a, b, c+l[cur]) + 10
    return min(ret0, retA, retB, retC)

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

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

lst = []

min_mp = 100000000

def check_mp(lst):

        global a,b,c
        global L

        len_A=0
        len_B=0
        len_C=0

        cnt_A=0
        cnt_B=0
        cnt_C=0

        for i in range(len(lst)):
                if lst[i] == "a":
                        cnt_A += 1
                        len_A += L[i]
                if lst[i] == "b":
                        cnt_B += 1
                        len_B += L[i]
                if lst[i] == "c":
                        cnt_C += 1
                        len_C += L[i]

        if cnt_A==0 or cnt_B==0 or cnt_C==0:
                return 10000000000




        difA = abs(len_A - A)
        difB = abs(len_B - B)
        difC = abs(len_C - C)

        diff = difA+difB+difC

        synth_mp = 10 * (cnt_A+cnt_B+cnt_C - 3)

        return diff + synth_mp


def nxt():
        global N
        global lst
        global min_mp

        if len(lst) == N:
                mp = check_mp(lst)

                if mp < min_mp:
                        min_mp = mp
                return

        lst.append("a")
        nxt()
        lst.pop(-1)

        lst.append("b")
        nxt()
        lst.pop(-1)

        lst.append("c")
        nxt()
        lst.pop(-1)

        lst.append("N")
        nxt()
        lst.pop(-1)

        return


nxt()

print(min_mp)

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

ans = 10 ** 10
for i in range(4**N):
    a = 0
    l = [0, 0, 0]
    for n in range(N):
        x = i % 4
        i //= 4
        if x == 3:
            continue
        if l[x] > 0:
            a += 10
        l[x] += L[n]

    if l[0] == 0 or l[1] == 0 or l[2] == 0:
        continue

    a += abs(l[0]-A) + abs(l[1]-B) + abs(l[2]-C)
    ans = min(a, ans)

print(ans)

Solution (Python):
def slove():
    import sys
    import 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)]
    mt = 10 ** 10
    for v in itertools.product([i for i in range(4)], repeat=n):
        nl = [[] for _ in range(4)]
        for i, vv in enumerate(v):
            nl[vv].append(l[i])
        if len(nl[1]) != 0 and len(nl[2]) != 0 and len(nl[3]) != 0:
            t = 0
            t += ((len(nl[1]) - 1) * 10 + abs(a - sum(nl[1])))
            t += ((len(nl[2]) - 1) * 10 + abs(b - sum(nl[2])))
            t += ((len(nl[3]) - 1) * 10 + abs(c - sum(nl[3])))
            mt = min(t, mt)
    print(mt)


if __name__ == '__main__':
    slove()

Solution (Python):
def slove():
    import sys
    import 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)]
    mt = 10 ** 10
    for v in itertools.product([i for i in range(4)], repeat=n):
        nl = [[] for _ in range(4)]
        for i, vv in enumerate(v):
            nl[vv].append(l[i])
        if len(nl[1]) != 0 and len(nl[2]) != 0 and len(nl[3]) != 0:
            t = 0
            t += ((len(nl[1]) - 1) * 10 + abs(a - sum(nl[1])))
            t += ((len(nl[2]) - 1) * 10 + abs(b - sum(nl[2])))
            t += ((len(nl[3]) - 1) * 10 + abs(c - sum(nl[3])))
            mt = min(t, mt)
    print(mt)


if __name__ == '__main__':
    slove()

Solution (Python):
n,a,b,c=map(int,input().split())
l=[]
for i in range(n):
    l.append(int(input()))
op=[[0],[1],[2],[3]]
for i in range(n-1):
    tmp=[]
    for j in op:
        tmp.append(j+[0])
        tmp.append(j+[1])
        tmp.append(j+[2])
        tmp.append(j+[3])
    op=tmp
ans=1000000000
for i in op:
    ta=0
    tb=0
    tc=0
    mp=0
    for j in range(len(i)):
        if i[j]==0:
            if ta!=0:
                mp+=10
            ta+=l[j]        
        elif i[j]==1:
            if tb!=0:
                mp+=10
            tb+=l[j]
        elif i[j]==2:
            if tc!=0:
                mp+=10
            tc+=l[j]
    mp+=abs(a-ta)+abs(b-tb)+abs(c-tc)
    if ta==0 or tb==0 or tc==0:
        mp=1000000000
    ans=min(ans,mp)
print(ans)

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

ans = float('inf')
for mask in range(pow(4, N)):
    cost = 0
    for i, l in enumerate([A, B, C]):
        grp = []
        for digit in range(N):
            if mask // pow(4, digit) % 4 == i:
                grp.append(L[digit])

        if not grp:
            cost = float('inf')

        cost += (len(grp) - 1) * 10
        cost += abs(l - sum(grp))
    ans = min(ans, cost)

print(ans)