Problem:
 Score : 300 points    Problem Statement You have N bamboos. The lengths (in centimeters) of these are l_1, l_2, ..., l_N , respectively.  Your objective is to use some of these bamboos (possibly all) to obtain three bamboos of length A, B, C . For that, you can use the following three kinds of magics any number:   Extension Magic: Consumes 1  MP (magic point). Choose one bamboo and increase its length by 1 .  Shortening Magic: Consumes 1 MP. Choose one bamboo of length at least 2 and decrease its length by 1 .  Composition Magic: Consumes 10 MP. Choose two bamboos and combine them into one bamboo. The length of this new bamboo is equal to the sum of the lengths of the two bamboos combined. (Afterwards, further magics can be used on this bamboo.)   At least how much MP is needed to achieve the objective?      Constraints  3 \leq N \leq 8  1 \leq C < B < A \leq 1000  1 \leq l_i \leq 1000  All values in input are integers.         Input Input is given from Standard Input in the following format:  N  A  B  C  l_1  l_2  :  l_N       Output Print the minimum amount of MP needed to achieve the objective.        Sample Input 1 5 100 90 80
98
40
30
21
80      Sample Output 1 23  We are obtaining three bamboos of lengths 100, 90, 80 from five bamboos 98, 40, 30, 21, 80 . We already have a bamboo of length 80 , and we can obtain bamboos of lengths 100, 90 by using the magics as follows at the total cost of 23 MP, which is optimal.   Use Extension Magic twice on the bamboo of length 98 to obtain a bamboo of length 100 . (MP consumed: 2 )  Use Composition Magic on the bamboos of lengths 40, 30 to obtain a bamboo of length 70 . (MP consumed: 10 )  Use Shortening Magic once on the bamboo of length 21 to obtain a bamboo of length 20 . (MP consumed: 1 )  Use Composition Magic on the bamboo of length 70 obtained in step 2 and the bamboo of length 20 obtained in step 3 to obtain a bamboo of length 90 . (MP consumed: 10 )        Sample Input 2 8 100 90 80
100
100
90
90
90
80
80
80      Sample Output 2 0  If we already have all bamboos of the desired lengths, the amount of MP needed is 0 . As seen here, we do not necessarily need to use all the bamboos.       Sample Input 3 8 1000 800 100
300
333
400
444
500
555
600
666      Sample Output 3 243

Solution (Python):
N, A, B, C = map(int, input().split())
l = [int(input()) for i in range(N)]
 
INF = float('inf')
ans = INF
 
def dfs(a, b, c, n, cost):
    global ans
    if n == N:
        cost += abs(A - a) + abs(B - b) + abs(C - c) - 30 if min(a, b, c) > 0 else INF
        ans = min(ans, cost)
        return
    dfs(a, b, c, n+1, cost)
    dfs(a+l[n], b, c, n+1, cost+10)
    dfs(a, b+l[n], c, n+1, cost+10)
    dfs(a, b, c+l[n], n+1, cost+10)
 
dfs(0, 0, 0, 0, 0)
print(ans)

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

INF = 10 ** 9

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

    ret1 = dfs(a, b, c, n + 1)
    ret2 = dfs(a + L_list[n], b, c, n + 1) + 10
    ret3 = dfs(a, b + L_list[n], c, n + 1) + 10
    ret4 = dfs(a, b, c + L_list[n], n + 1) + 10
    return min(ret1, ret2, ret3, ret4)


ans = dfs(0, 0, 0, 0)
print(ans)
'''
8 100 90 80
100
100
90
90
90
80
80
80

0


8 1000 800 100
300
333
400
444
500
555
600
666

243

'''

Solution (Python):
import itertools

n, *abc = map(int, input().split())
llst = [int(input()) for _ in range(n)]

ans = 10 ** 10
for abc_lst in itertools.product(range(4), repeat = n):
    tmp = 0
    labc = [[0, 0] for _ in range(3)]
    for i in range(n):
        pos = abc_lst[i]
        if pos == 3:
            continue
        labc[pos][0] += llst[i]
        labc[pos][1] += 1
    if [0, 0] in labc:
        continue
    for i in range(3):
        tmp += abs(abc[i] - labc[i][0])
        tmp += 10 * (labc[i][1] - 1)
    ans = min(ans, tmp)
print(ans)

Solution (Python):
from itertools import product

def main():
    N, A, B, C = map(int, input().split())
    L = list(int(input()) for _ in range(N))
    ans = 0
    
    
    
    ans = float('inf')
    for item in product(range(4), repeat=N):
        length_A = 0
        cnt_A = 0
        score_A = 0
        length_B = 0
        cnt_B = 0
        score_B = 0
        length_C = 0
        cnt_C = 0
        score_C = 0
        for i,j in enumerate(item):
            if j == 0:
                length_A += L[i]
                cnt_A += 1
            if j == 1:
                length_B += L[i]
                cnt_B += 1
            if j == 2:
                length_C += L[i]
                cnt_C += 1
        if cnt_A == 0 or cnt_B == 0 or cnt_C == 0:
            continue
        score_A += abs(A - length_A) + 10 * (cnt_A - 1)
        score_B += abs(B - length_B) + 10 * (cnt_B - 1)
        score_C += abs(C - length_C) + 10 * (cnt_C - 1)
        ans = min(ans, score_A + score_B + score_C)
    print(ans)

if __name__ == "__main__":
    main()

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

import itertools

Target = itertools.product([0, 1, 2, 3], repeat=N)

INF = 10 ** 9

ans = INF
for T in Target:
    A_sum = 0
    B_sum = 0
    C_sum = 0
    A_cost = 0
    B_cost = 0
    C_cost = 0
    for i in range(N):
        if T[i] == 0: continue
        if T[i] == 1:
            if A_sum == 0:
                A_sum += L_list[i]
            else:
                A_sum += L_list[i]
                A_cost += 10
        if T[i] == 2:
            if B_sum == 0:
                B_sum += L_list[i]
            else:
                B_sum += L_list[i]
                B_cost += 10
        if T[i] == 3:
            if C_sum == 0:
                C_sum += L_list[i]
            else:
                C_sum += L_list[i]
                C_cost += 10

    if min(A_sum, B_sum, C_sum) == 0: continue

    ans = min(ans, abs(A - A_sum)+A_cost + abs(B - B_sum)+B_cost + abs(C - C_sum)+C_cost)
print(ans)

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

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

import itertools

bumboo=["A","B","C","D"]

prod = list(itertools.product(bumboo,repeat=n))

temp = []

for i in prod:
    tempa = 0
    tempb = 0
    tempc = 0
    for j in range(n):
        if i[j] =="A":
            tempa += l[j]
        elif i[j] =="B":
            tempb += l[j]
        elif i[j] =="C":
            tempc += l[j]
    if tempa*tempb*tempc == 0:
        pass
    else:
        temp.append(abs(tempa-a)+abs(tempb-b)+abs(tempc-c)+10*(n-3-i.count("D")))
print(min(temp))

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

def rec(i,a,b,c):
  if i==N:
    if a==0 or b==0 or c==0:
      return 10**12
    return abs(a-A)+abs(b-B)+abs(c-C)

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

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

  return res

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

print(rec(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 = float("inf")
use_list = list(product(range(4), repeat=n))

for take_use in use_list:
    a, b, c = 0, 0, 0
    count_a, count_b, count_c = 0, 0, 0
    cost = 0
    for j in range(n):
        if take_use[j] == 3:
            a += l[j]
            count_a += 1
        elif take_use[j] == 2:
            b += l[j]
            count_b += 1
        elif take_use[j] == 1:
            c += l[j]
            count_c += 1
    if count_a == 0 or count_b == 0 or count_c == 0:
        continue
    cost += (count_a-1 + count_b-1 + count_c-1)*10
    cost += abs(A-a) + abs(B-b) + abs(C-c)
    ans = min(ans, cost)
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)


ans = dfs(0, 0, 0, 0)
print(ans)

Solution (Python):
def main():
    n, *target = map(int, input().split())
    l = [int(input()) for _ in range(n)]
    ans = float("inf")
    base = 4
    for i in range(pow(base, n)):
        now_l = [0] * base
        add = [0] * base
        now = i
        for j in range(n):
            now_l[now % base] += l[j]
            add[now % base] += 1
            now //= base
        if 0 in now_l[:base - 1]:
            continue
        now_ans = sum([abs(target[i] - now_l[i]) for i in range(3)]) + (sum(add[:3]) - 3) * 10
        ans = min(ans, now_ans)
    print(0 if all(t in l for t in target) else ans)


if __name__ == '__main__':
    main()

Solution (Python):
import math
from math import gcd,pi,sqrt
INF = float("inf")

import sys
sys.setrecursionlimit(10**6)
import itertools
from collections import Counter,deque
def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_str(N): return [s_list() for _ in range(N)]
def s_row_list(N): return [list(s_input()) for _ in range(N)]

def main():
    n, A,B,C = i_map()

    l = [i_input() for i in range(n)]

    def dfs(cur, a,b,c):
        if cur==n:
            return abs(a-A) + abs(b-B) + abs(c-C) - 30 if min(a,b,c) > 0 else 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))


if __name__=="__main__":
    main()

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

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

    ans = 10**9
    for prod in product([0, 1, 2, 3], repeat=n):
        cost = 10 * (n-3)
        lengths = [0, 0, 0]
        for i, x in enumerate(prod):
            if x < 3:
                lengths[x] += l[i]
            else:
                cost -= 10

        for i in lengths:
            if i == 0:
                break

        else:
            for i, x in enumerate(abc):
                cost += abs(x - lengths[i])
            if cost < ans:
                ans = cost

    print(ans)

main()

Solution (Python):
from itertools import permutations

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

    
    ans = 10**9
    for perm in permutations(l):
        cost = -30
        for i in range(1, n-1):
            cost1 = cost + 10 * len(perm[:i]) + abs(a - sum(perm[:i]))
            for j in range(i+1, n):
                cost2 = cost1 + 10 * len(perm[i:j]) + abs(b - sum(perm[i:j]))
                for k in range(j+1, n+1):
                    cost3 = cost2 + 10 * len(perm[j:k]) + abs(c - sum(perm[j:k]))
                    if cost3 < ans:
                        ans = cost3

    print(ans)

main()

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

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

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
     
patterns = []
def saiki(n, pat):
    if n == 0:
        patterns.append(pat)
        return
    for c in 'ABCX':
        pat += c
        saiki(n-1, pat)
        pat = pat[:-1]
def main():
    N, A, B, C = LI()
    L = []
    ans = inf
    for _ in range(N):
        L.append(I())

    saiki(len(L), '')
    for pat in patterns:
        if 'A' in pat and 'B' in pat and 'C' in pat:
            pass
        else:
            continue

        a, b, c = 0, 0, 0
        MP = 0
        for i, ch in enumerate(pat):
            if ch == 'X':
                continue

            if ch == 'A':
                if a > 0:
                    MP += 10
                a += L[i]
            if ch == 'B':
                if b > 0:
                    MP += 10
                b += L[i]
            if ch == 'C':
                if c > 0:
                    MP += 10
                c += L[i]
        tmp = min(
            abs(A-a) + abs(B-b) + abs(C-c),
            abs(A-a) + abs(B-c) + abs(C-b),
            abs(A-b) + abs(B-a) + abs(C-c),
            abs(A-b) + abs(B-c) + abs(C-a),
            abs(A-c) + abs(B-b) + abs(C-a),
            abs(A-c) + abs(B-a) + abs(C-b),
        )
        MP += tmp
        if MP < ans:
            ans_pat = pat
            ans = min(MP, ans)
    print(ans)

main()

Solution (Python):
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:
        if min(a,b,c) > 0:
            return abs(a-A) + abs(b-B) + abs(c-C) - 30
        else:
            return INF
            
    ret1 = dfs(i+1, a, b, c)
    ret2 = dfs(i+1, a+L[i], b, c) + 10
    ret3 = dfs(i+1, a, b+L[i], c) + 10
    ret4 = dfs(i+1, a, b, c+L[i]) + 10
    return min(ret1,ret2,ret3,ret4)
    
print(dfs(0,0,0,0))

Solution (Python):
from itertools import product


def solve(T, PTTN):
    lst = [l[i] for i, t in enumerate(PTTN) if t == T]
    ANS = 10 ** 5
    if lst:
        ANS = (len(lst) - 1) * 10
        ANS += abs(sum(lst) - tgt[T])
    return ANS


tgt = list(map(int, input().split()))
l = [int(input()) for _ in range(tgt[0])]

ans = 10 ** 5
for pttn in product([0, 1, 2, 3], repeat=tgt[0]):
    tmp = sum(solve(i + 1, pttn) for i in range(3))
    ans = min(ans, tmp)
print(ans)

Solution (Python):


import sys 
from itertools import product
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
N, A, B, C = map(int, readline().split())
l = list(map(int,read().split()))
INF = 10**31
ans = INF

for patterns in product(range(4), repeat=N):
    x = [0]*4
    MP = 0
    for i,take in enumerate(patterns):
        if take == 0:
            continue
        if x[take] != 0:
            MP += 10
        x[take] += l[i]
    if x[1] == 0 or x[2] == 0 or x[3] == 0:
        continue
    MP += abs(x[1]-A)
    MP += abs(x[2]-B)
    MP += abs(x[3]-C)
    ans = min(MP,ans)
print(ans)

Solution (Python):
from itertools import product


def solve(T, PTTN):
    lst = [l[i] for i, t in enumerate(PTTN) if t == T]
    if lst:
        ANS = (len(lst) - 1) * 10
        ANS += abs(sum(lst) - tgt[T])
        return ANS
    return 10 ** 5


tgt = list(map(int, input().split()))
l = [int(input()) for _ in range(tgt[0])]

ans = 10 ** 5
for pttn in product([0, 1, 2, 3], repeat=tgt[0]):
    tmp = sum(solve(i + 1, pttn) for i in range(3))
    ans = min(ans, tmp)
print(ans)

Solution (Python):
import abc
import itertools

def main():
    N, A, B, C = map(int, input().split())
    L = [int(input()) for _ in range(N)]
    i = 0
    mps = []
    for assign in itertools.product([0, 1, 2, 3], repeat=N):
        if 1 not in assign or 2 not in assign or 3 not in assign:
            continue
        mp = 0
        abc_length = {1: 0, 2: 0, 3: 0}
        for i, abc in enumerate(assign):
            if abc == 0:
                continue
            elif abc_length[abc] == 0:
                abc_length[abc] = L[i]
            else:
                mp += 10
                abc_length[abc] += L[i]

        for key, goal in zip([1, 2, 3], [A, B, C]):
            mp += abs(goal - abc_length[key])
        mps.append(mp)
    print(min(mps))


if __name__ == '__main__':
    main()

Solution (Python):
def dfs(ka, kb, kc, cnta, cntb, cntc, phase):
    global ans
    if phase == n:
        if 0 < cnta and 0 < cntb and 0 < cntc:
            ans = min(ans, abs(a-ka) + abs(b-kb) + abs(c-kc) + 10*(cnta+cntb+cntc-3))
        return
    dfs(ka+l[phase], kb, kc, cnta+1, cntb, cntc, phase+1)
    dfs(ka, kb+l[phase], kc, cnta, cntb+1, cntc, phase+1)
    dfs(ka, kb, kc+l[phase], cnta, cntb, cntc+1, phase+1)
    dfs(ka, kb, kc, cnta, cntb, cntc, phase+1)


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

Solution (Python):
N,A,B,C = map(int,input().split())
l = [int(input()) for _ in range(N)]
ans = float("inf")
for i in range(4**N):
    a,b,c = [],[],[]
    cost = 0
    for j in range(N):
        r = (i//(4**j)) % 4
        if r == 1:
            a.append(l[j])
        elif r == 2:
            b.append(l[j])
        elif r == 3:
            c.append(l[j])
    if a and b and c:
        cost += sum(max((len(x)-1), 0)*10 for x in [a,b,c])
        a = sum(a)
        b = sum(b)
        c = sum(c)
        cost += sum(abs(x-y) for x,y in [(a,A),(b,B),(c,C)])
        ans = min(ans, cost)
print(ans)

Solution (Python):
N,A,B,C = map(int,input().split())
l = [int(input()) for _ in range(N)]
ans = float("inf")
for i in range(4**N):
    a,b,c = [],[],[]
    cost = 0
    for j in range(N):
        r = (i//(4**j)) % 4
        if r == 1:
            a.append(l[j])
        elif r == 2:
            b.append(l[j])
        elif r == 3:
            c.append(l[j])
    if a and b and c:
        cost += sum(max((len(x)-1), 0)*10 for x in [a,b,c])
        a = sum(a)
        b = sum(b)
        c = sum(c)
        cost += sum(abs(x-y) for x,y in [(a,A),(b,B),(c,C)])
        ans = min(ans, cost)
print(ans)

Solution (Python):
from itertools import product

def solve(NUM, LIST):
    lst = [l[i] for i, t in enumerate(LIST) if t == NUM]
    if len(lst) == 0:
        return 10 ** 5
    ANS = (len(lst) - 1) * 10
    ANS += abs(sum(lst) - tgt[NUM])
    return ANS

tgt = list(map(int, input().split()))
l = [int(input()) for _ in range(tgt[0])]

ans = 10 ** 5
for pttn in product([0, 1, 2, 3], repeat=tgt[0]):
    tmp = sum(solve(i + 1, pttn) for i in range(3))
    ans = min(ans, tmp)
print(ans)

Solution (Python):
from itertools import product

def solve(NUM, LIST):
    lst = [l[i] for i, li in enumerate(LIST) if li == NUM]
    if len(lst) == 0:
        return 10 ** 5
    ANS = (len(lst) - 1) * 10
    ANS += abs(sum(lst) - target[NUM])
    return ANS

target = list(map(int, input().split()))
l = [int(input()) for _ in range(target[0])]

ans = 10 ** 5
for pat in product([0, 1, 2, 3], repeat=target[0]):
    tmp = sum(solve(i + 1, pat) for i in range(3))
    ans = min(ans, tmp)
print(ans)

Solution (Python):
from itertools import product

def solve(NUM, LIST):
    lst = [i for i, li in enumerate(LIST) if li == NUM]
    if len(lst) == 0:
        return 10 ** 5
    ANS = (len(lst) - 1) * 10
    ANS += abs(sum(l[i] for i in lst) - target[NUM])
    return ANS

target = list(map(int, input().split()))
l = [int(input()) for _ in range(target[0])]

ans = 10 ** 5
for pat in product([0, 1, 2, 3], repeat=target[0]):
    tmp = sum(solve(i + 1, pat) for i in range(3))
    ans = min(ans, tmp)
print(ans)

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

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())
Q=[int(input()) for ii in range(n)]
inf=10**15
def dfs(now,a,b,c):
    if now==n:
        return abs(a-A)+abs(b-B)+abs(c-C) if 0 not in [a,b,c] else inf
    q1= dfs(now+1,a,b,c)
    q2= dfs(now+1, a+Q[now],b,c)+10
    q3= dfs(now+1, a, b+Q[now], c)+10
    q4 = dfs(now+1,a,b,c+Q[now])+10
    return min(q1,q2,q3,q4)

print(dfs(0,0,0,0)-30)

Solution (Python):
N, A, B, C = map(int, input().split())
L = [int(input()) for _ in range(N)]
def rec(itr, a, b, c, mp):
    if itr == N:
        if a == 0 or b == 0 or c == 0:
            return 10 ** 9
        return abs(a - A) + abs(b - B) + abs(c - C) + mp - 30
    res = rec(itr + 1, a, b, c, mp)
    res = min(res, rec(itr + 1, a + L[itr], b, c, mp) + 10)
    res = min(res, rec(itr + 1, a, b + L[itr], c, mp) + 10)
    res = min(res, rec(itr + 1, a, b, c + L[itr], mp) + 10)
    return res
print(rec(0, 0, 0, 0, 0))

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

def settify(j, n): 
  i = 0
  ans = [set() for _ in range(n)]
  while i < N:
    ans[j % n].add(i)
    i += 1
    j //= n
  return ans

def set_label(st): 
  rtn = 0
  for j in st:
    rtn += 2**j 
  return rtn

def cost(s, L): 
  if s == 0:
    return 10**20
  st = settify(s, 2)[1]
  
  l_first = 0
  for i in st:
    l_first += l[i] 
  return 10*(len(st)-1) + abs(l_first - L) 
  


cost = [ 
  [cost(s2, A) for s2 in range(2**N)]
, [cost(s2, B) for s2 in range(2**N)]
, [cost(s2, C) for s2 in range(2**N)] ]



mi = 10**20
for j in range(4**N):
  sets = settify(j, 4)
  q = [0, 0, 0]
  tmp = 0
  for p in range(3):
    tmp += cost[p][set_label(sets[p])]
  mi = min(mi, tmp)
  
print(mi)

Solution (Python):
n,a,b,c, = map(int,input().split())
l = list(int(input()) for i in range(n))
ans = 10 ** 6
for i in range(4 ** n):
    m = i
    j = 0
    x,xp,y,yp,z,zp = 0,0,0,0,0,0
    while m > 0:
        p = m % 4
        if p == 1:
            x += l[j]
            xp += 1
        elif p == 2:
            y += l[j]
            yp += 1
        elif p == 3:
            z += l[j]
            zp += 1
        else:
            pass
        m = m // 4
        j += 1
    if xp != 0 and yp != 0 and zp != 0:
        t = abs(a-x) + abs(b-y) + abs(c-z) + ((xp-1)*10) + ((yp-1)*10) + ((zp-1)*10)
        ans = min(ans,t)
print(ans)

Solution (Python):
from itertools import combinations, product
N,A,B,C=map(int, input().split())
l=[int(input()) for _ in range(N)]
ans = float("inf")
for tmp in product(range(4), repeat=N):
    cost = 0
    forA = []
    forB = []
    forC = []
    for i in range(N):
        if tmp[i] == 0:
            forA.append(l[i])
            cost += 10
        elif tmp[i] == 1:
            forB.append(l[i])
            cost += 10
        elif tmp[i] == 2:
            forC.append(l[i])
            cost += 10
    if not (forA and forB and forC):
        continue
    cost -= 30
    toA = sum(forA)
    toB = sum(forB)
    toC = sum(forC)
    cost += abs(toA - A) + abs(toB - B) + abs(toC - C)
    ans = min(ans, cost)

print(ans)

Solution (Python):
import itertools

n, a, b, c = map(int, input().split())
l = []
for i in range(n):
    l.append(int(input()))
mp = float("inf")
for i in itertools.product([0,1,2,3], repeat = n):
    if 0 not in i or 1 not in i or 2 not in i:
        continue
    la, lb, lc = 0, 0, 0
    temp = 0
    for j in range(3):
        temp += (i.count(j) - 1) * 10
    for j in range(n):
        if i[j] == 0:
            la += l[j]
        elif i[j] == 1:
            lb += l[j]
        elif i[j] == 2:
            lc += l[j]
    for j, k in [[la, a], [lb, b], [lc, c]]:
        temp += abs(j - k)
    mp = min(mp, temp)
print(mp)

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

from itertools import product

P=product(range(4),repeat=N)

ans=1000000000
for p in P:
    A_len=0
    B_len=0
    C_len=0
    temp=0
    for i in range(N):
        n=int(p[i])
        if n==3:
            if A_len!=0:
                temp+=10
            A_len+=l[i]
        elif n==2:
            if B_len!=0:
                temp+=10
            B_len+=l[i]
        elif n==1:
            if C_len!=0:
                temp+=10
            C_len+=l[i]

    if A_len!=0 and B_len!=0 and C_len!=0:
        temp+=abs(A-A_len)
        temp+=abs(B-B_len)
        temp+=abs(C-C_len)

        ans=min(ans,temp)

print(ans)

Solution (Python):
N,A,B,C = map(int,input().split())
l = [int(input()) for _ in range(N)]
ans = float('inf')
for i in range(2**N):
    da = [True]*N
    for j in range(N):
        if (i>>j) & 1:
            da[j] = False
    a = []
    lb = []
    for j in range(N):
        if da[j] == True:
            a.append(l[j])
        else:
            lb.append(l[j])
    
    if len(lb) >= 2:
        for j in range(2**len(lb)):
            db = [True] * len(lb)
            for k in range(len(lb)):
                if (j>>k) & 1:
                    db[k] = False
            b = []
            lc = []
            for k in range(len(lb)):
                if db[k] == True:
                    b.append(lb[k])
                else:
                    lc.append(lb[k])
            
            if len(lc) >= 1:
                for k in range(2**len(lc)):
                    dc = [True] * len(lc)
                    for m in range(len(lc)):
                        if (k>>m) & 1:
                            dc[m] = False
                    c = []
                    for m in range(len(lc)):
                        if dc[m] == True:
                            c.append(lc[m])
                    if len(a) > 0 and len(b) > 0 and len(c) > 0:
                        
                        ans = min(ans, 10*(len(a)-1)+abs(A-sum(a)) + 10*(len(b)-1)+abs(B-sum(b)) + 10*(len(c)-1)+abs(C-sum(c))          )
print(ans)

Solution (Python):
import math


def read_int():
    return int(input().strip())


def read_ints():
    return list(map(int, input().strip().split(' ')))


def build(l, A):
    
    N = len(l)
    mask_to_cost = [math.inf]*(1<<N)
    for m in range(1, 1<<N):
        i = 0
        cost = 0
        length = 0
        while i < N:
            if m&(1<<i):
                cost += 10
                length += l[N-i-1]
            i += 1
        if cost > 0:
            cost -= 10
        cost += abs(length-A)
        mask_to_cost[m] = cost
    for m in range(1<<N):
        s = m
        while s > 0:
            s = (s-1)&m
            mask_to_cost[m] = min(mask_to_cost[m], mask_to_cost[s])
    return mask_to_cost


def solve():
    N, A, B, C = read_ints()
    l = [read_int() for _ in range(N)]
    A_cost = build(l, A)
    B_cost = build(l, B)
    C_cost = build(l, C)
    min_cost = math.inf
    for a_mask in range(1, 1<<N):
        bc_mask = ((1<<N)-1)-a_mask
        b_mask = bc_mask
        while b_mask > 0:
            b_mask = (b_mask-1)&bc_mask
            c_mask = bc_mask-b_mask
            if b_mask and c_mask:
                min_cost = min(min_cost, A_cost[a_mask]+B_cost[b_mask]+C_cost[c_mask])
    return min_cost


if __name__ == '__main__':
    print(solve())

Solution (Python):
N,A,B,C=map(int,input().split())
L=[int(input()) for i in range(N)]
R=[0]*N
ans=10**9
def myadd(i):
    if R[i]==3:
        R[i]=0
        myadd(i+1)
    else:
        R[i]+=1
for i in range(4**N-1):
    myadd(0)
    if 1 in R and 2 in R and 3 in R:
        tmp=-30
        t1,t2,t3=0,0,0
        for j in range(N):
            if R[j]==0:
                continue
            if R[j]==1:
                t1+=L[j]
            elif R[j]==2:
                t2+=L[j]
            elif R[j]==3:
                t3+=L[j]
            tmp+=10
        tmp+=abs(A-t1)+abs(B-t2)+abs(C-t3)
        ans=min(tmp,ans)
print(ans)