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):


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



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


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


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


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


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


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


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


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


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


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


debug = False  
print(probC())


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


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


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


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

Solution (Python):

now_ans = 100000


def kotae(A, a_tree, a_tree_sum):
    ans = (a_tree - 1) * 10 + abs(a_tree_sum - A)
    if a_tree == 0:
        ans = 100000
    return ans

def calc_final(A, B, C, a_tree, b_tree, c_tree, a_tree_sum, b_tree_sum, c_tree_sum):
    global now_ans

    ans = 0
    ans += kotae(A, a_tree, a_tree_sum)
    ans += kotae(B, b_tree, b_tree_sum)
    ans += kotae(C, c_tree, c_tree_sum)
    if ans < now_ans:
        now_ans = ans



def calc_data(tree_data, A, B, C, a_tree, b_tree, c_tree, index, a_tree_sum, b_tree_sum, c_tree_sum ):
    if a_tree_sum >= A and b_tree_sum >= B and c_tree_sum >= C:
        calc_final(A, B, C, a_tree, b_tree, c_tree, a_tree_sum, b_tree_sum, c_tree_sum)
        return
    if index == len(tree_data) - 1:
        calc_final(A, B, C, a_tree, b_tree, c_tree, a_tree_sum, b_tree_sum, c_tree_sum)
        return
    if a_tree_sum < A:
        calc_data(tree_data, A, B, C, a_tree + 1, b_tree, c_tree, index + 1, a_tree_sum + tree_data[index + 1], b_tree_sum, c_tree_sum)
    if b_tree_sum < B:
        calc_data(tree_data, A, B, C, a_tree, b_tree + 1, c_tree, index + 1, a_tree_sum, b_tree_sum + tree_data[index + 1], c_tree_sum)

    if c_tree_sum < C:
        calc_data(tree_data, A, B, C, a_tree, b_tree, c_tree + 1, index + 1, a_tree_sum, b_tree_sum, c_tree_sum + tree_data[index + 1])

    calc_data(tree_data, A, B, C, a_tree, b_tree, c_tree, index + 1, a_tree_sum, b_tree_sum, c_tree_sum)


def main():
    global now_ans
    num, A, B, C = list(map(int, input().split()))
    tree_data = [int(input()) for i in range(num)]
    tree_data.sort(reverse=True)


    calc_data(tree_data, A, B, C, 1, 0, 0, 0, tree_data[0], 0, 0)
    calc_data(tree_data, A, B, C, 0, 1, 0, 0, 0, tree_data[0], 0)
    calc_data(tree_data, A, B, C, 0, 0, 1, 0, 0, 0, tree_data[0])
    calc_data(tree_data, A, B, C, 0, 0, 0, 0, 0, 0, 0)



    print(now_ans)

if __name__ == '__main__':
    main()

Solution (Python):
import itertools
import numpy as np
import copy


ansl = []
N, A, B, C = list(map(int, input().split()))

a = []
for i in range(N):
    a.append(int(input()))

def get_ptn(la):
    ptn = []
    for i in range(1, len(la) + 1):
        ptn.append(list(itertools.combinations(la, i)))
    return ptn

takes = list(itertools.permutations([A, B, C]))

for take in takes:
    
    
    try:
        ta = copy.deepcopy(a)
        ans = 0
        for k in range(3):
            
            ptn = get_ptn(ta)

            
            cost = []
            tmp = []
            for i in range(len(ptn)):
                tmp.append([abs(take[k] - sum(j)) for j in ptn[i]])
                cost.append(min(tmp[i]) + 10 * i)
            ans += min(cost)
            ac = np.argmin(cost)
            
            
            
            at = np.argmin(tmp[ac])
            

            for _ in ptn[ac][at]:
                ta.remove(_)

        ansl.append(ans)
    except:
        pass

print(min(ansl))

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

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

ans = []
def f(i, anum, alen, bnum, blen, cnum, clen):
    if i == n:
        if alen * blen * clen == 0:
            return
        mp = abs(anum-a)+10*(alen-1) + abs(bnum-b)+10*(blen-1) + abs(cnum-c)+10*(clen-1)
        ans.append(mp)
    else:
        f(i+1, anum+l[i], alen+1, bnum, blen, cnum, clen)
        f(i+1, anum, alen, bnum+l[i], blen+1, cnum, clen)
        f(i+1, anum, alen, bnum, blen, cnum+l[i], clen+1)
        f(i+1, anum, alen, bnum, blen, cnum, clen)

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

res = float('inf')
for i in ans:
    res = min(res, i)

print(res)

Solution (Python):
n, a, b, c = map(int,input().split())
l = [0]*8
for i in range(n): l[i] = int(input())
ans = float("inf")
for i in range(65536):
    tmp = [[] for i in range(3)]
    for j in range(8):
        shift = (i >> j*2) & 3
        if shift == 0:
            continue
        elif shift == 1: 
            tmp[0].append(l[j])
        elif shift == 2: 
            tmp[1].append(l[j])
        elif shift == 3: 
            tmp[2].append(l[j])
    if tmp[0] and tmp[1] and tmp[2]: 
        tmpans = 0
        tmpans += (abs(a-sum(tmp[0]))+10*(len(tmp[0])-1))
        tmpans += (abs(b-sum(tmp[1]))+10*(len(tmp[1])-1))
        tmpans += (abs(c-sum(tmp[2]))+10*(len(tmp[2])-1))
        ans = min(ans,tmpans)
print(ans)

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

ans=10**12

for i in p(range(4),repeat=N):
  if not (i.count(1)>=1 and i.count(2)>=1 and i.count(3)>=1):continue
  a,b,c=0,0,0
  cost=0
  tt=N-i.count(0)

  for (k,j) in enumerate(i):
    if j==1:a+=l[k]
    elif j==2:b+=l[k]
    elif j==3:c+=l[k]
  
  cost=abs(a-A)+abs(b-B)+abs(c-C)
  cost+=10*(tt-3)

  ans=min(ans,cost)

print(ans)

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

for i in range(N):
    l.append(int(input()))


def gen_pat(n, pat):
    if n==0:
        return [pat]
    out = []
    for s in list("ABC0"):
        out += gen_pat(n-1, pat+s)
    return out

def cal(l, x):
    return 10*(len(l)-1)+abs(sum(l)-x)

cost = 10000000000000
for p in gen_pat(len(l), ""):
    a = [l[i] for i in range(len(l)) if p[i]=="A"]
    b = [l[i] for i in range(len(l)) if p[i]=="B"]
    c = [l[i] for i in range(len(l)) if p[i]=="C"]
    if len(a)==0 or len(b)==0 or len(c)==0:
        continue

    cost = min(cost,
               cal(a, A)+cal(b, B)+cal(c, C))

print(cost)

Solution (Python):
import copy

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



def search(abc, l):
    mp = 0
    for x in abc:
        if(x in l): l[l.index(x)] = 0
        else:
            while(True):
                k = 1e9
                d = -1
                for i in range(n):
                    if(l[i] == 0): continue

                    if(abs(x - l[i]) < k):
                        k = abs(x - l[i])
                        d = i

                if(k <= 10):
                    mp += k
                    l[d] = 0
                    break
                else:
                    k = 1e9
                    d = n*[0]
                    d[0] = 1
                    while(sum(d) != n):
                        tree = 0
                        for i in range(n): tree += d[i] * l[i]
                        cost = abs(x - tree)
                        if(tree == 0): cost = 1e9
                        if(sum(d) > 1): cost += (sum(d)-1) * 10
                        if(cost < k):
                            k = cost
                            tmp2 = copy.deepcopy(d)

                        for i in range(n):
                            if(d[i] == 0):
                                d[i] = 1
                                break
                            else: d[i] = 0

                    mp += k
                    for i in range(n):
                        if(tmp2[i] == 1): l[i] = 0
                    break

    return mp

ans = []
ans.append(search([a, b, c], copy.deepcopy(l)))
ans.append(search([a, c, b], copy.deepcopy(l)))
ans.append(search([b, a, c], copy.deepcopy(l)))
ans.append(search([b, c, a], copy.deepcopy(l)))
ans.append(search([c, a, b], copy.deepcopy(l)))
ans.append(search([c, b, a], copy.deepcopy(l)))


print(min(ans))

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


ans=1000000000
a=[]
b=[]
c=[]
COU=0
D=1

for a1 in range(4):
    for a2 in range(4):
        for a3 in range(4):
            for a4 in range(4):
                for a5 in range(4):
                    for a6 in range(4):
                        for a7 in range(4):
                            for a8 in range(4):
                                for x in range(8):
                                    LIS=[a1,a2,a3,a4,a5,a6,a7,a8]
                                    if LIS[x]==0:
                                        a.append(L[x])
                                    elif LIS[x]==1:
                                        b.append(L[x])
                                    elif LIS[x]==2:
                                        c.append(L[x])
                                
                                A1=max((len(a)-a.count(0)-1),0)*10
                                B1=max((len(b)-b.count(0)-1),0)*10
                                C1=max((len(c)-c.count(0)-1),0)*10
                                A2=sum(a)
                                B2=sum(b)
                                C2=sum(c)
                                ARR=[A2,B2,C2]
                                ARR.sort(reverse=True)
                                cnt=A1+B1+C1+abs(A-ARR[0])+abs(B-ARR[1])+abs(C-ARR[2])
                                
                                if cnt<ans and ARR[2]!=0:
                                    ans=cnt
                                    
                                    
                                    
                                    
                                a=[]
                                b=[]
                                c=[]
                                if a7==3:
                                    D+=1
                                
print(ans)

Solution (Python):
from itertools import permutations

def get_int():
    return int(input())

def get_int_list():
    return list(map(int, input().split()))

while(True):
    try:
        n, a, b, c = get_int_list()
        l = [get_int() for _ in range(n)]
        ans = 1000000000000
        for take in permutations(l, n):
            
            take = list(take)
            cnt = 0
            i = 3
            while True:
                if i >= n:
                    break
                if abs(take[2] - a) < 10:
                    break
                if take[2] > a:
                    break
                if abs(take[2] + take[i] - a) >= abs(take[2] - a):
                    break
                if take[i] <= 10:
                    break
                take[2] += take[i]
                i += 1
                cnt += 10
            cnt += abs(take[2] - a)
            

            while True:
                if i >= n:
                    break
                if abs(take[1] - b) < 10:
                    break
                if take[1] > b:
                    break
                if abs(take[1] + take[i] - b) >= abs(take[1] - b):
                    break
                if take[i] <= 10:
                    break
                take[1] += take[i]
                i += 1
                cnt += 10
            cnt += abs(take[1] - b)
            

            while True:
                if i >= n:
                    break
                if abs(take[0] - c) < 10:
                    break
                if take[0] > c:
                    break
                if abs(take[0] + take[i] - c) >= abs(take[0] - c):
                    break
                if take[i] <= 10:
                    break
                take[0] += take[i]
                i += 1
                cnt += 10
            cnt += abs(take[0] - c)
            

            ans = min(ans, cnt)
            
            
        print(ans)
    except EOFError:
        exit()

Solution (Python):
n,a,b,c = map(int,input().split())
nu = [a,b,c]
ans = [[[0,0],[0,0],[0,0],[0,0]] for i in range(4 ** n)]
lis = [int(input()) for i in range(n)]
def to4(n):
    if int(n//4):
        return to4(n//4) + str(n%4)
    return str(n%4)
for i in range(4 ** n):
    s = to4(i).zfill(n)
    for j in range(n):
        ans[i][int(s[j])][0] += lis[j]
        ans[i][int(s[j])][1] += 1
an = 10 ** 18
for i in range(4 ** n):
    if ans[i][0][1] != 0 and ans[i][1][1] != 0 and ans[i][2][1] != 0:
        a = 0
        for num in range(3):
            a += abs(ans[i][num][0] -nu[num])
            a += max(0,ans[i][num][1]-1) * 10
        an = min(an,a)
print(an)

Solution (Python):



import itertools

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

    cand = []
    ids = set(range(N))
    for a in range(1, N-1):
        for b in range(1, N-a):
            for c in range(1, N-a-b+1):
                cand.append((a, b, c))
    best_cost = -1
    for a, b, c in cand:
        la = itertools.combinations(ids, a)
        for a_ in la:
            lb = itertools.combinations(ids-set(a_), b)
            for b_ in lb:
                lc = itertools.combinations(ids-set(a_)-set(b_), c)
                for c_ in lc:
                    cost = (len(a_)-1)*10 + abs(sum(ll[e] for e in a_)-A)
                    cost += (len(b_)-1)*10 + abs(sum(ll[e] for e in b_)-B)
                    cost += (len(c_)-1)*10 + abs(sum(ll[e] for e in c_)-C)
                    if best_cost == -1 or cost < best_cost:
                        best_cost = cost
    print(best_cost)


if __name__ == '__main__':
    main()

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

def choose3(bamboos: list, a: int, b: int, c: int):
    bamboos.sort(reverse=True)
    l = len(bamboos)
    minmp = float('inf')
    
    for idx in range(l):
        for jdx in range(idx+1,l):
            for kdx in range(jdx+1,l):
                minmp = min(minmp,
                            abs(bamboos[idx]-a)\
                             + abs(bamboos[jdx]-b)\
                             + abs(bamboos[kdx]-c))
    return minmp


def dfs(bamboos: list, a: int, b: int, c: int, mp: int):

    l = len(bamboos)
    
    if l == 3:
        return mp + choose3(bamboos, a, b, c)
    
    else:
        minmp = mp + choose3(bamboos, a, b, c)
        
        for p1 in range(l):
            for p2 in range(p1+1,l):
                newbamboos = [bamboos[p1] + bamboos[p2]]
                
                for p in range(l):
                    if p == p1 or p == p2:
                        continue
                    else:
                        newbamboos.append(bamboos[p])
                
                minmp = min(minmp, dfs(newbamboos, a, b, c, mp+10))
                del newbamboos
                
        return minmp
    
n,a,b,c = li()
laaa = [ni() for _ in range(n)]

print(dfs(laaa, a, b, c, 0))

Solution (Python):
N,A,B,C=list(map(int,input().split()))
l=[]
for i in range(N):
    l.append(int(input()))
    
def three_value(l,N,i,s0,s1,s2,A,B,C,sig):
    if i!=N:
        if sig[i]:
            return min(three_value(l,N,i+1,s0+l[i],s1,s2,A,B,C,sig),three_value(l,N,i+1,s0,s1+l[i],s2,A,B,C,sig),three_value(l,N,i+1,s0,s1,s2+l[i],A,B,C,sig))
        else:
            return three_value(l,N,i+1,s0,s1,s2,A,B,C,sig)
    else:
        if s0 == 0 or s1==0 or s2==0:
            return 10**27
        else:
            return min(abs(s0-A)+abs(s1-B)+abs(s2-C),abs(s0-A)+abs(s2-B)+abs(s1-C),abs(s1-A)+abs(s0-B)+abs(s2-C),
                   abs(s1-A)+abs(s2-B)+abs(s0-C),abs(s2-A)+abs(s0-B)+abs(s1-C),abs(s2-A)+abs(s1-B)+abs(s0-C))
def two_value(l,N,i,A,B,C,sig,num):
    if i!=N:
        sig1 = list(sig)
        sig2=list(sig)
        sig1[i]=True
        return min(two_value(l,N,i+1,A,B,C,sig1,num+1),two_value(l,N,i+1,A,B,C,sig2,num))
    else:
        if num>2:
            return three_value(l,N,0,0,0,0,A,B,C,sig)+(num-3)*10
        else:
            return 10**27
sig=[False for i in range(N)]
print(two_value(l,N,0,A,B,C,sig,0))

Solution (Python):
def dfs(wants, remains):
    if len(wants) == 0:
        
        return 0

    
    min_cost = float("inf")
    if len(wants) < len(remains):  
        for i in range(1, len(remains)):
            
            
            
            c = dfs(wants, [remains[0] + remains[i]] +
                    remains[1:i] + remains[i+1:])
            min_cost = min(min_cost, 10+c)

    for i in range(len(wants)):  
        
        
        
        c = dfs(wants[:i] + wants[i+1:], remains[1:])
        min_cost = min(min_cost, abs(wants[i]-remains[0]) + c)

    if len(wants) < len(remains):
        
        
        c = dfs(wants, remains[1:])
        min_cost = min(min_cost, c)

    return min_cost


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

wants = [A, B, C]
print(dfs(wants, ls))

Solution (Python):

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

for i in range(4**N):
    r = 0
    r_list = [0]*4
    c_list = [0]*4
    
    for j in range(N):
        k = (i // (4**j)) % 4
        r_list[k] += l_list[j]
        c_list[k] += 1
        
    if c_list[0]*c_list[1]*c_list[2] != 0:
        r += 10*(c_list[0] - 1)
        r += 10*(c_list[1] - 1)
        r += 10*(c_list[2] - 1)
        CC, BB, AA = sorted(r_list[:3])
        r += abs(CC-C)
        r += abs(BB-B)
        r += abs(AA-A)
        res = min(res, r)
    
print(res)

Solution (Python):


"""
Created on Sun Feb 24 21:04:52 2019

@author: Riichi_Yokoyama
"""
import itertools
import copy
N,A,B,C = map(int, input().split())

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

def toku(C,B,A,l):
    l2 = copy.deepcopy(l)
    ans = 0
    needtomake = 3
    plist = []
    for i in range(len(l2)-needtomake + 1):
        plist.extend(list(itertools.combinations(l2, i+1))) 
    one = []
    for i in range(len(plist)):
        one.append(abs(C - sum(plist[i])) + 10*(len(plist[i])-1))
    mindex = one.index(min(one))
    ans += min(one)
    needtomake -= 1
    for i in range(len(plist[mindex])):
        l2.remove(plist[mindex][i])
    
    
    plist = []
    for i in range(len(l2)-needtomake + 1):
        plist.extend(list(itertools.combinations(l2, i+1)))
        
    one = []
    for i in range(len(plist)):
        one.append(abs(B - sum(plist[i])) + 10*(len(plist[i])-1))
        
    mindex = one.index(min(one))
    ans += min(one)
    needtomake -= 1
    for i in range(len(plist[mindex])):
        l2.remove(plist[mindex][i])

    
    plist = []
    for i in range(len(l2)-needtomake + 1):
        plist.extend(list(itertools.combinations(l2, i+1)))
        
    one = []
    for i in range(len(plist)):
        one.append(abs(A - sum(plist[i])) + 10*(len(plist[i])-1))
        
    mindex = one.index(min(one))
    ans += min(one)
    needtomake -= 1
    for i in range(len(plist[mindex])):
        l2.remove(plist[mindex][i])
    return ans

an = [0,0,0,0,0,0]
an[0] = toku(C,B,A,l)
an[1] = toku(C,A,B,l)
an[2] = toku(B,A,C,l)
an[3] = toku(B,C,A,l)
an[4] = toku(A,B,C,l)
an[5] = toku(A,C,B,l)

print(min(an))

Solution (Python):
import itertools

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

def score(x,NOW):
    L=len(NOW)
    ITE=list(itertools.product([0,1],repeat=L))

    ANS=10**15
    CANDI=[]

    for ite in ITE:
        if max(ite)==0:
            continue
        SUM=sum([NOW[i] for i in range(L) if ite[i]==1])        
        MAGIC=(ite.count(1)-1)*10
        

        if ANS>MAGIC+abs(x-SUM):
            ANS=MAGIC+abs(x-SUM)
            CANDI=ite

    return ANS,[NOW[i] for i in range(L) if CANDI[i]==0]

X=[[A,B,C],[A,C,B],[B,A,C],[B,C,A],[C,A,B],[C,B,A]]
ANS=10**15

for x in X:
    ANS1,L1=score(x[0],L)
    if len(L1)==0:
        continue
    ANS2,L2=score(x[1],L1)
    if len(L2)==0:
        continue
    ANS3,L3=score(x[2],L2)

    

    if ANS>ANS1+ANS2+ANS3:
        ANS=ANS1+ANS2+ANS3

print(ANS)

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

x = [1]*N

ans = []


def f(p):
    y1 = []
    y2 = []
    y3 = []
    for i in range(len(p)):
        if p[i] == 1:
            y1.append(l[i])
        elif p[i] == 2:
            y2.append(l[i])
        elif p[i] == 3:
            y3.append(l[i])
    if len(y1)*len(y2)*len(y3)!=0:
        return (len(y1)+len(y2)+len(y3)-3)*10 + abs(sum(y1)-A) + abs(sum(y2)-B) + abs(sum(y3)-C)
    else:
        return 'No'

for i1 in range(1,5):
    x[0]=i1
    for i2 in range(1,5):
        x[1]=i2
        for i3 in range(1,5):
            x[2]=i3
            if N==3:
                q=f(x)
                if q != 'No':
                    ans.append(q)
                continue
            else:
                for i4 in range(1,5):
                    x[3]=i4
                    if N==4:
                        q=f(x)
                        if q != 'No':
                            ans.append(q)
                        continue
                    else:
                        for i5 in range(1,5):
                            x[4]=i5
                            if N==5:
                                q=f(x)
                                if q != 'No':
                                    ans.append(q)
                                continue
                            else:
                                for i6 in range(1,5):
                                    x[5]=i6
                                    if N==6:
                                        q=f(x)
                                        if q != 'No':
                                            ans.append(q)
                                        continue
                                    else:
                                        for i7 in range(1,5):
                                            x[6]=i7
                                            if N==7:
                                                q=f(x)
                                                if q != 'No':
                                                    ans.append(q)
                                                continue
                                            else:
                                                for i8 in range(1,5):
                                                    x[7]=i8
                                                    q=f(x)
                                                    if q != 'No':
                                                        ans.append(q)

                                                    

print(min(ans))

Solution (Python):
from itertools import combinations as comb
from itertools import permutations as perm
from copy import copy


def inpl():
    return list(map(int, input().split()))


A = [0, 0, 0]
N, A[0], A[1], A[2] = inpl()
bamboo = [int(input()) for _ in range(N)]

ans = 10000 * 3
for tmp_a in perm(A, 3):
    tmp_bamboo = copy(bamboo)
    junjo_ans = 0
    for num, a in enumerate(tmp_a):
        tmp_ans = 1000 * 3
        for i in range(1, len(tmp_bamboo) - (2 - num) + 1):
            for Comb in comb(tmp_bamboo, i):
                if tmp_ans > abs(sum(Comb) - a) + 10 * (i - 1):
                    tmp_ans = abs(sum(Comb) - a) + 10 * (i - 1)
                    tmp_comb = copy(Comb)
        junjo_ans += tmp_ans
        for c in tmp_comb:
            tmp_bamboo.remove(c)
    ans = min(ans, junjo_ans)
print(ans)

Solution (Python):
def base4(x):
    if int(x/4):
        return base4(int(x/4)) + str(x%4)
    return str(x%4)

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

res = 99999999
for i in range(4**N):
    c = [[0,0,0],[0,0,0]]
    for j,b in enumerate(base4(i).zfill(N)):
        if int(b) != 0:
            c[0][int(b)-1] += l[j]
            c[1][int(b)-1] += 1
    if c[1][0] > 0 and c[1][1] > 0 and c[1][2] > 0:
        res = min(res,abs(c[0][0]-A)+abs(c[0][1]-B)+abs(c[0][2]-C)+(sum(c[1])-3)*10)

print(res)

Solution (Python):
n,a,b,c=map(int,input().split())
l=[int(input()) for i in range(n)]
choice=[]
for i in range(4**n):
    now=i
    cho=[]
    for j in range(n):
        cho.append(now%4)
        now//=4
    choice.append(cho)
ans=10**10
def calc(xl,x):
    if len(xl)==0:return  10**10
    cost=10*(len(xl)-1)
    cost+=abs(x-sum(xl))
    return cost
for x in choice:
    al=[]
    bl=[]
    cl=[]
    for i in range(len(x)):
        if x[i]==1:
            al.append(l[i])
        elif x[i]==2:
            bl.append(l[i])
        elif x[i]==3:
            cl.append(l[i])
    now=calc(al,a)+calc(bl,b)+calc(cl,c)
    if now<ans:ans=now
print(ans)

Solution (Python):
import itertools
N, a, b, c = [int(_) for _ in input().split()]
L = [int(input()) for _ in range(N)]
ans = 10**10
for K in itertools.product(range(4), repeat=N):
    A = [[] for _ in range(4)]
    for i in range(N):
        A[K[i]] += [L[i]]
    if len(A[1]) and len(A[2]) and len(A[3]):
        cost = 10 * (N - len(A[0]) - 3)
        cost += abs(a - sum(A[1]))
        cost += abs(b - sum(A[2]))
        cost += abs(c - sum(A[3]))
        ans = min(cost, ans)
print(ans)

Solution (Python):
N, A, B, C = map(int, input().split())
TotalLength = A + B + C
L = [int(input()) for _ in range(N)]
Ans = 1000000000000
for i in range(1, 4**N):
    X, fourN = i, []
    while X > 0:
        fourN.append(X % 4)
        X //= 4
    Length = [0, 0, 0]
    connect = 0
    for j in range(len(fourN)):
        if fourN[j] > 0:
            if Length[fourN[j]-1] > 0: connect += 1
            Length[fourN[j]-1] += L[j]
        if 0 not in Length: Ans = min(Ans, abs(Length[0]-A) + abs(Length[1]-B) + abs(Length[2]-C) + connect * 10)
print(Ans)

Solution (Python):
def keisan(lis):
    ans =abs(lis[0]-A)
    ans += abs(lis[1]-B)
    ans += abs(lis[2]-C)
    return ans

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

answ = 10000

for i in range(4**N):
    len =[0]*4
    wa = 0
    for j in range(N):
        amari = i % 4
        i = i//4
        if amari != 3 and len[amari]!=0:
            wa += 10
        len[amari] += l[j]

    if len[0]*len[1]*len[2] != 0:
        wa += keisan(len)
        if wa < answ:
            answ = wa
print(answ)