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

from itertools import product

ans = 10**10
for pro in product([0,1,2,3], repeat= N):
    cost,lena,lenb,lenc = -30,0,0,0
    for i in range(N):
        if pro[i] == 1:
            lena += L[i]
            cost += 10
        elif pro[i] == 2:
            lenb += L[i]
            cost += 10
        elif pro[i] == 3:
            lenc += L[i]
            cost += 10
    if lena == 0 or lenb == 0 or lenc == 0:
        continue
    cost += abs(A-lena) + abs(B-lenb) + abs(C-lenc)
    ans = min(ans,cost)
print(ans)

Solution (Python):
from itertools import combinations


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

dp_a = [-1] * (2 ** n)
dp_b = [-1] * (2 ** n)
dp_c = [-1] * (2 ** n)

for i in range(1, n-1):
    for j in combinations(range(n), i):
        indices = [1 << k for k in j]
        index = sum(indices)
        if i == 1:
            dp_a[index] = abs(l[j[0]] - a)
            dp_b[index] = abs(l[j[0]] - b)
            dp_c[index] = abs(l[j[0]] - c)
        else:
            dp_a[index] = min([dp_a[index - indices[k]] for k in range(i)])
            dp_a[index] = min(dp_a[index], abs(sum([l[k] for k in j]) - a) + 10 * (i-1))

            dp_b[index] = min([dp_b[index - indices[k]] for k in range(i)])
            dp_b[index] = min(dp_b[index], abs(sum([l[k] for k in j]) - b) + 10 * (i - 1))

            dp_c[index] = min([dp_c[index - indices[k]] for k in range(i)])
            dp_c[index] = min(dp_c[index], abs(sum([l[k] for k in j]) - c) + 10 * (i - 1))

ans = 10 ** 10
for i in range(1, 2 ** n):
    for j in range(1, 2 ** n):
        if i | j != i + j:
            continue
        for k in range(1, 2 ** n):
            if i + j + k == 2 ** n - 1 and i | j | k == 2 ** n - 1:
                ans = min(ans, dp_a[i] + dp_b[j] + dp_c[k])

print(ans)

Solution (Python):
N,A,B,C=map(int,input().split())
l=[int(input()) for i in range(N)]
D=0
a=0
b=0
c=0
mp=10**5

for i in range(4**N):
  a=0
  b=0
  c=0
  x=0
  y=0
  z=0
  for j in range(N):
    if(i&1<<2*j and i&1<<(2*j+1)):
      
      a+=l[j]
      x+=1
    elif(i&1<<2*j and ~(i&1<<(2*j+1))):
      b+=l[j]
      y+=1
    elif(~(i&1<<2*j) and (i&1<<(2*j+1))):
      c+=l[j]
      z+=1
  if(a==0 or b==0 or c==0):
    continue

  mp=min(mp,abs(A-a)+abs(B-b)+abs(C-c)+(x+y+z-3)*10)
  if(mp==110):
    print("a={}".format(a)+"b={}".format(b)+"c={}".format(c))
print(mp)

Solution (Python):


from itertools import product
nim, a, b, c = map(int, input().split())
array = []
for i in range(nim):
  array.append(int(input()))

abc = [0,a,b,c]
ch = [0,1,2,3]
cal = []

for i in product(ch,repeat=nim):
  calc = 0
  su = [0,0,0,0]
  ct = [0,0,0,0]
  temp = list(i)
  for j in range(nim):
    su[temp[j]] += array[j]
    ct[temp[j]] += 1
  if (ct[1]==0)|(ct[2]==0)|(ct[3]==0):
    continue
  for k in range(1,4):
    calc += abs(abc[k]-su[k])
    calc += max((ct[k]-1),0)*10
  cal.append(calc)
    
print(min(cal))

Solution (Python):

import sys
import itertools

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))


N, A, B, C = lr()
bamboo = [ir() for _ in range(N)]
answer = 10 ** 5
for pattern in itertools.product(range(4), repeat=N):
    use = [0] * 3
    mp = 0
    for p, bam in zip(pattern, bamboo):
        if p == 3:
            continue
        if use[p] != 0:
            mp += 10
        use[p] += bam
    if use.count(0) != 0:
        continue
    mp += abs(A-use[0]) + abs(B-use[1]) + abs(C-use[2])
    if mp < answer:
        answer = mp

print(answer)

Solution (Python):

import sys
import itertools

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))


N, A, B, C = lr()
bamboo = [ir() for _ in range(N)]
answer = 10 **  5
for pattern in itertools.product(range(4), repeat=N):
    mp = 0
    Alist = []; Blist = []; Clist = []
    for i, p in enumerate(pattern):
        if p == 0:
            Alist.append(bamboo[i])
        elif p == 1:
            Blist.append(bamboo[i])
        elif p == 2:
            Clist.append(bamboo[i])
    if len(Alist) == 0 or len(Blist) == 0 or len(Clist) == 0:
        continue
    mp += 10 * (len(Alist) + len(Blist) + len(Clist) - 3)
    mp += abs(A - sum(Alist)) + abs(B - sum(Blist)) + abs(C - sum(Clist))
    if mp < answer:
        answer = mp

print(answer)

Solution (Python):
N,*ABC=map(int,input().split())
M=3
ans=10**9
L=[]
for i in range(N):
  l=int(input())
  if l in ABC:
    ABC.remove(l)
    N-=1
    M-=1
  else:
    L.append(l)
if M==0:
  print(0)
  exit(0)
for x in range((M+1)**N):
  _=0
  abc=[0]*(M+1)
  for i in range(N):
    if x%(M+1)!=M and abc[x%(M+1)]>0:
      abc[x%(M+1)]+=L[i]
      _+=10
    else:
      abc[x%(M+1)]+=L[i]
    x=x//(M+1)
  if min(abc[:M])==0:
    continue
  for i in range(M):
    _+=abs(ABC[i]-abc[i])
  ans=min(ans,_)
  
print(ans)

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

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

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)

min_mp = 10**10

for i in range(4**n):
    mp = 0
    li = [0]*4
    count_li = [0]*4
    s = Base_10_to_n(i,4).zfill(n)

    for j,k in enumerate(s):
        li[int(k)] += l[j]
        count_li[int(k)] += 1

    if 0 in li[:-1]:
        continue

    mp += abs(a-li[0])
    mp += abs(b-li[1])
    mp += abs(c-li[2])

    for item in count_li[:3]:
        if item >= 2:
            mp += 10*(item-1)
            
    min_mp = min(min_mp,mp)

print(min_mp)

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

def dfs(cur,a,b,c):
    if cur == N:
        if min(a,b,c) > 0:
            return abs(a-A) + abs(b-B) + abs(c-C) -10*3 
        else:
            return 10**9
    c0 = dfs(cur+1,a,b,c)
    c1 = dfs(cur+1,a+l[cur],b,c) +10
    c2 = dfs(cur+1,a,b+l[cur],c) +10
    c3 = dfs(cur+1,a,b,c+l[cur]) +10
    return min(c0,c1,c2,c3)

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

Solution (Python):
n, *abc = [int(x) for x in input().split()]
l = [int(input()) for i in range(n)]
A = 0; B = 1; C = 2;

minmp = 10 ** 9
for k in range(2 ** (2 * n)):
    mat = [[], [], [], []]  
    
    
    
    
    
    for i in range(n):
        mat[k & (0b11)].append(l[i])
        k >>= 2

    if len(mat[0]) == 0 or len(mat[1]) == 0 or len(mat[2]) == 0:
        continue

    mp = 0
    for j in range(3):
        mp += 10 * (len(mat[j]) - 1)
        mp += 1 * abs(abc[j] - sum(mat[j]))

    minmp = min(minmp, mp)

print(minmp)

Solution (Python):
n, *abc = [int(x) for x in input().split()]
l = [int(input()) for i in range(n)]
A = 0; B = 1; C = 2;

minmp = 10 ** 9
for k in range(2 ** (2 * n)):
    mat = [[], [], [], []]  
    
    
    
    
    
    for i in range(n):
        mat[(k >> 2 * i) & (0b11)].append(l[i])

    if len(mat[0]) == 0 or len(mat[1]) == 0 or len(mat[2]) == 0:
        continue
        
    mp = 0
    for j in range(3):
        mp += 10 * (len(mat[j]) - 1)
        mp += 1 * abs(abc[j] - sum(mat[j]))

    minmp = min(minmp, mp)

print(minmp)

Solution (Python):
n,a,b,c = map(int, input().split())
a,b,c = sorted([a,b,c])
l = [int(input()) for i in range(n)]
ans = 10 ** 18
for i in range(4 ** n):
  LEN = [0,0,0,0]
  num = [0,0,0,0]
  for j,v in enumerate(l):
    index = (i >> (j * 2)) & 3
    LEN[index] += v
    num[index] += 1
  if num[0] and num[1] and num[2]:
    mp = (sum(num[:3])-3) * 10
    d = sorted(LEN[:3])
    mp += abs(d[0]-a) + abs(d[1]-b) + abs(d[2]-c)
    ans = min(ans, mp)
print(ans)

Solution (Python):
N, A, B, C = [int(i) for i in input().split()]
L = [int(input()) for _ in range(N)]
ans = float("inf")
for i in range(2 ** (N * 2)):
    a = b = c = 0
    cnt = 0
    for j in range(N):
        bit = i & 3
        if bit == 1:
            a += L[j]
        elif bit == 2:
            b += L[j]
        elif bit == 3:
            c += L[j]
        if bit:
            cnt += 1
        i >>= 2
    if a and b and c:
        ans = min(ans, (cnt - 3) * 10 + abs(A - a) + abs(B - b) + abs(C - c))
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(i, x, y, z):
    if i == n:
        cand = abs(a - x) + abs(b - y) + abs(c - z) - 30 if min(x, y, z) > 0 else INF
        return cand

    temp0 = dfs(i + 1, x + l[i], y, z) + 10
    temp1 = dfs(i + 1, x, y + l[i], z) + 10
    temp2 = dfs(i + 1, x, y, z + l[i]) + 10
    temp3 = dfs(i + 1, x, y, z)
    return min(temp0, temp1, temp2, temp3)

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

Solution (Python):
n,*a=map(int,input().split())
l=[int(input()) for i in range(n)]
ans=float("inf")
for i in range(4**n):
  table=[0 for g in range(3) ]
  temp=i
  pre=0

  for j in range(n):

    if temp%4==3:temp//=4;continue
    if table[temp%4]:
      table[temp%4]+=l[j]
      pre+=10
    else:table[temp%4]=l[j]
    temp//=4
  if 0 in table:continue
  for g in range(3):
    pre+=abs(a[g]-table[g])
  ans=min(ans,pre)
print(ans)

Solution (Python):
import sys
inf = float('inf')

n, A, B, C, *l = map(int, sys.stdin.read().split())

def cost(a, b, c, cnt, i):
    if i == n:
        if not (a and b and c): return inf
        return abs(A - a) + abs(B - b) + abs(C - c) + 10 * (cnt - 3)
    return min(cost(a, b, c, cnt, i+1), 
               cost(a+l[i], b, c, cnt+1, i+1),
               cost(a, b+l[i], c, cnt+1, i+1),
               cost(a, b, c+l[i], cnt+1, i+1))

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

if __name__ ==  '__main__':
    main()

Solution (Python):
import sys
from functools import lru_cache
inf = float('inf')

n, A, B, C, *l = map(int, sys.stdin.read().split())

@lru_cache(None)
def cost(a, b, c, cnt, i):
    if i == n:
        if not (a and b and c): return inf
        return abs(A - a) + abs(B - b) + abs(C - c) + 10 * (cnt - 3)
    return min(cost(a, b, c, cnt, i+1), 
               cost(a+l[i], b, c, cnt+1, i+1),
               cost(a, b+l[i], c, cnt+1, i+1),
               cost(a, b, c+l[i], cnt+1, i+1))

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

if __name__ ==  '__main__':
    main()

Solution (Python):
import sys
from itertools import product

n, *abc = map(int, sys.stdin.readline().split())
*l, = map(int, sys.stdin.read().split())

def main():
    cand = []
    for p in product([0, 1, 2, 3], repeat=n):
        group = [[] for _ in range(4)]
        for i in range(n):
            group[p[i]].append(l[i])
        cost = 0
        for i in range(1, 4):
            if not group[i]: 
                break
            cost += 10 * (len(group[i]) - 1)
            group[i] = sum(group[i])
        else:
            group = group[1:]
            for i in range(3):
                cost += abs(abc[i] - group[i])
            cand.append(cost)
        
    print(min(cand))

if __name__ ==  '__main__':
    main()

Solution (Python):










mod = 10**9 + 7

def readInts():
  return list(map(int,input().split()))
def main():
    n,A,B,C = readInts()
    L = [] * n
    for i in range(n):
        l = int(input())
        L.append(l)

    def rec(i,a,b,c):
        if i == n:
            if not a or not b or not c:
                return float('inf')
            return abs(a-A) + abs(b-B) + abs(c - C)
            

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

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

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


if __name__ == '__main__':
  main()

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


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

    
    ret0 = dfs(depth + 1, a, b, c)

    
    
    ret1 = dfs(depth + 1, a + l[depth], b, c) + 10
    ret2 = dfs(depth + 1, a, b + l[depth], c) + 10
    ret3 = dfs(depth + 1, a, b, c + l[depth]) + 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(count,a,b,c):
    if count==n:
        if min(a,b,c)>0:
            return abs(a-A)+abs(b-B)+abs(c-C)-30
        else:
            return 10**9+1
    x = dfs(count+1, a, b, c)
    y = dfs(count+1, a+l[count], b, c)+10
    z = dfs(count+1, a, b+l[count], c)+10
    w = dfs(count+1, a, b, c+l[count])+10
    return min(x,y,z,w)



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

Solution (Python):
def f(i):
    global ans
    if i == n:
        w, x, y, z = 0, 0, 0, 0
        for i in range(n):
            if s[i] == 1:
                x += l[i]
            elif s[i] == 2:
                y += l[i]
            elif s[i] == 3:
                z += l[i]
            else:
                w += 1
        if x > 0 and y > 0 and z > 0:
            mp = abs(a - x) + abs(b - y) + abs(c - z) + 10 * (n - w - 3)
            ans = min(ans, mp)
        return
    s[i] = 0
    f(i + 1)
    s[i] = 1
    f(i + 1)
    s[i] = 2
    f(i + 1)
    s[i] = 3
    f(i + 1)

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

Solution (Python):
import sys
input = lambda: sys.stdin.readline().rstrip()

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

def dfs():
    stack = [(0,0,0,0,0)]
    ans = float("inf")

    while stack:
        current, c_a, c_b, c_c, score = stack.pop()

        if current == N:
            if c_a > 0 and c_b > 0 and c_c > 0:
                ans = min(ans, abs(A - c_a) + abs(B - c_b) + abs(C - c_c) + score - 30)
            continue

        stack.append((current+1, c_a, c_b, c_c, score))
        stack.append((current+1, c_a+l[current], c_b, c_c, score+10))
        stack.append((current+1, c_a, c_b+l[current], c_c, score+10))
        stack.append((current+1, c_a, c_b, c_c+l[current], score+10))

    return ans
print(dfs())

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

m = 10 ** 6
for choice in range(1, 4 ** (N + 1)):
    bucket = [[] for _ in range(4)]
    
    for j in range(N):
        temp = (choice // 4 ** j) % 4
        bucket[temp].append(l[j])
        
    
    if bucket[0] and bucket[1] and bucket[2]:
        pass
    else:
        continue
    cost = 0
    for i in range(4):
        if i <= 2 and bucket[i]:
            if len(bucket[i]) >= 2:
                total = sum(bucket[i])
                cost += 10 * (len(bucket[i]) - 1)
            else:
                total = bucket[i][0]
            cost += abs(total - ABC[i])
    m = min(m, cost)
print(m)

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

ans=float('inf')
for i in range(0,4**N):
    a=0
    b=0
    c=0
    MP=0
    for j in range(0,16):
        if i>>(2*j) & 1==1 and i>>(2*j+1) &1==0:
            a+=L[j]
            MP+=10
        elif i>>(2*j) & 1==0 and i>>(2*j+1) &1==1:
            b+=L[j]
            MP+=10
        elif i>>(2*j) & 1==1 and i>>(2*j+1) &1==1:
            c+=L[j]
            MP+=10

    if a*b*c==0:
        continue
    else:
        MP-=30
        MP+=abs(a-A)+abs(b-B)+abs(c-C)
        ans=min(MP,ans)

print(ans)

Solution (Python):
n,a,b,c = map(int,input().split())
l = [int(input()) for _ in range(n)]
ans = 10**6
for i in range(4**n):
  pattern = [0]*n
  for j in range(n):
    pattern[j] = ((i >> j*2) & 3)
  bamboo = [0]*4
  for j in range(n):
    bamboo[pattern[j]] += l[j]
  if bamboo[1]*bamboo[2]*bamboo[3]==0:
    continue
  time = pattern.count(1)*10+pattern.count(2)*10+pattern.count(3)*10-30+abs(bamboo[1]-a)+abs(bamboo[2]-b)+abs(bamboo[3]-c)
  if ans > time:
    ans = time
print(ans)

Solution (Python):
from itertools import product

n, a, b, c = map(int, input().split())
kumiawase = list(product(range(4), repeat=n))
L = []
for _ in range(n):
    L.append(int(input()))


ans = 10 ** 20

for kumi in kumiawase:
    temp = 0
    L0 = []
    L1 = []
    L2 = []
    for j in range(n):
        if kumi[j] == 0:
            L0.append(L[j])
        if kumi[j] == 1:
            L1.append(L[j])
        if kumi[j] == 2:
            L2.append(L[j])
    n0 = len(L0)
    n1 = len(L1)
    n2 = len(L2)
    if n0 == 0 or n1 == 0 or n2 == 0:
        continue
    l0 = sum(L0)
    l1 = sum(L1)
    l2 = sum(L2)
    temp = 10 * (n0 - 1 + n1 - 1 + n2 - 1) + abs(l0 - a) + abs(l1 - b) + abs(l2 - c)
    ans = min(temp, ans)
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())
ans=10**10
for i in range(2**n):
  S=format(i,"10b")
  K=[]
  for j in range(n):
    if S[-1-j]=="1":
      K.append(j)
  k=len(K)
  if len(K)<3:
    continue
  for j in range(2**k):
    SS=format(j,"10b")
    AA=[]
    for l in range(k):
      if SS[-1-l]=="1":
        AA.append(K[l])
    a=len(AA)
    if a<1 or k-2<a:
      continue
    KK=sorted(list(set(K)-set(AA)))
    kk=k-a
    for l in range(2**kk):
      SSS=format(l,"10b")
      BB=[]
      for m in range(kk):
        if SSS[-1-m]=="1":
          BB.append(KK[m])
      b=len(BB)
      if b<1 or kk-1<b:
        continue
      CC=sorted(list(set(KK)-set(BB)))
      c=len(CC)
      AAA=sum(L[AA[x]] for x in range(a))
      BBB=sum(L[BB[x]] for x in range(b))
      CCC=sum(L[CC[x]] for x in range(c))
      aans=(a+b+c-3)*10+abs(AAA-A)+abs(BBB-B)+abs(CCC-C)
      if aans<ans:
        ans=aans
print(ans)

Solution (Python):
import sys
input = lambda: sys.stdin.readline().rstrip()

def dfs():
    stack = [(0, 0, 0, 0, 0)]
    ans = float("inf")
    while stack:
        c, c_a, c_b, c_c, score = stack.pop()

        if c == N:
            
            if c_a > 0 and c_b > 0 and c_c > 0:
                ans = min(ans, abs(A-c_a) + abs(B-c_b) + abs(C-c_c) + score - 30)
            continue
        
        stack.append((c+1, c_a, c_b, c_c, score))
        stack.append((c+1, c_a+l[c], c_b, c_c, score + 10))
        stack.append((c+1, c_a, c_b+l[c], c_c, score + 10))
        stack.append((c+1, c_a, c_b, c_c+l[c], score + 10))
    return ans
    

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

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

print(dfs())

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(i) for i in input().split())
l = [int(input()) for i in range(n)]
a = []
b = []
c = []

def furiwake(x):
    global a
    global b
    global c
    a = []
    b = []
    c = []
    for i in range(1, n+1):
        if dig4(x, i) == 0:
            a.append(l[i-1])
        if dig4(x, i) == 1:
            b.append(l[i-1])
        if dig4(x, i) == 2:
            c.append(l[i-1])

def dig4(n, k):
    return (n%(4**k))//(4**(k-1))

def cost():
    costa = 10 * (len(a)-1) + abs(sum(a)-A)
    costb = 10 * (len(b)-1) + abs(sum(b)-B)
    costc = 10 * (len(c)-1) + abs(sum(c)-C)
    return costa+costb+costc

ans = 1000000000
for i in range(1, 4**8):
    furiwake(i)
    if(len(a)*len(b)*len(c)):
        ans = min(ans, cost())
print(ans)

Solution (Python):
from itertools import product
n,*B=map(int,input().split())
A=[int(input()) for i in range(n)]
s=10**9
for P in product([0,1,2,3],repeat=n):
  if {0,1,2}<=set(P):
    m=[0,0,0]
    for x,y in zip(P,A):
      if x!=3:
        m[x]+=y
    t=sum(abs(m-y) for m,y in zip(m,B))
    l=sum(1 for j in P if j!=3)-3
    s=min(s,t+l*10)
print(s)

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

def base10to(n,b):
    if n//b:
        return base10to(n//b,b)+str(n%b)
    return str(n%b)

for i in range(n):
    L.append(int(input()))
ans=10**10
for i in range(4**n):
    S=base10to(i,4)
    S=S.zfill(n)
    A=[]
    B=[]
    C=[]
    for j in range(n):
        if S[j]=='0':
            continue
        elif S[j]=='1':
            A.append(L[j])
        elif S[j]=='2':
            B.append(L[j])
        else:
            C.append(L[j])
    if A and B and C:
        ans=min(ans,abs(a-sum(A))+(len(A)-1)*10+abs(b-sum(B))+(len(B)-1)*10+abs(c-sum(C))+(len(C)-1)*10)
print(ans)

Solution (Python):
INF = float("inf")

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


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


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

Solution (Python):
import itertools
N,A,B,C = map(int,input().split())
L = [int(input()) for _ in range(N)]
 
INF = 10 ** 18
answer = INF
for pattern in itertools.product(range(4), repeat = N):
  x = [0] * 4
  MP = 0
  for take, length in zip(pattern, L):
    if take == 3:
      continue
    if x[take] != 0:
      MP += 10
    x[take] += length
  if x[0] == 0 or x[1] == 0 or x[2] == 0:
    continue
  MP += abs(A-x[0])
  MP += abs(B-x[1])
  MP += abs(C-x[2])
  if answer > MP:
    answer = MP
print(answer)

Solution (Python):
import sys
input=sys.stdin.readline

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

ans=10**9
for i in range(4**n):
    F=[[] for i in range(3)] 
    now=i
    for j in range(n):
        r=now%(4**(j+1))
        chk=r//(4**(j))
        if chk!=3:
            F[chk].append(L[j])
        now-=r    
    if F[0] and F[1] and F[2]: 
        mp=0
        for k in range(3):
            f=F[k]
            mp+=abs(sum(f)-A[k])+10*(max(0,len(f)-1))
        ans=min(mp,ans)
print(ans)

Solution (Python):
import sys
input=sys.stdin.readline

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

Ans=10**9
for i in range(4**n):
    F=[[] for i in range(3)] 
    now=i
    for j in range(n):
        r=now%(4**(j+1))
        chk=r//(4**(j))
        if chk!=3:
            F[chk].append(L[j])
        now-=r    
    if F[0] and F[1] and F[2]: 
        ans=0
        for k in range(3):
            f=F[k] 
            Len=len(f)
            mp=10**9
            for l in range(1,2**Len):
                t=0
                cnt=0
                for q in range(Len):
                    if (l>>q)&1:
                        cnt+=1
                        t+=f[q]
                mp=min(abs(t-A[k])+10*(max(0,cnt-1)),mp)
            ans+=mp
        Ans=min(ans,Ans)
print(Ans)

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

import itertools
bambuu = [[0,0,0] for _ in range(4**N)]
for i,pro in enumerate(itertools.product(['A','B',"C",'D'], repeat=N)):
    lisA = [L[j] for j in range(N) if pro[j]=='A']
    lisB = [L[j] for j in range(N) if pro[j]=='B']
    lisC = [L[j] for j in range(N) if pro[j]=='C']
    sumA = [sum(lisA),(len(lisA)-1)*10]
    sumB = [sum(lisB),(len(lisB)-1)*10]
    sumC = [sum(lisC),(len(lisC)-1)*10]
    bambuu[i-1] = [sumA,sumB,sumC]

ans = 10000
for sA,sB,sC in bambuu:
    if sA[0]>0 and sB[0]>0 and sC[0]>0:
        mp_total = abs(A-sA[0])+abs(B-sB[0])+abs(C-sC[0])+sA[1]+sB[1]+sC[1]
        if mp_total < ans:
            
            ans = mp_total

print(ans)

Solution (Python):


import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

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)

n,a,b,c = LI()
l = [I() for _ in range(n)]

s = a+b+c
ans = inf
for i in range(1<<n):
    lst = []
    t = 0
    for j in range(n):
        if i >> j & 1:
            t += 1
            lst.append(l[j])
    if t < 3:
        continue
    for j in range(3**t):
        tmp = '0'*t + Base_10_to_n(j,3)
        tmp = tmp[-t:]
        B = [0]*t
        for k in range(t):
            if str(tmp[k]) == '1':
                B[k] = 1
            elif str(tmp[k]) == '2':
                B[k] = 2
        X = [[],[],[]]
        for k in range(t):
            X[B[k]].append(lst[k])
        if len(X[0]) == 0:
            continue
        if len(X[1]) == 0:
            continue
        if len(X[2]) == 0:
            continue
        cost = abs(sum(X[0])-a)
        cost += abs(sum(X[1])-b)
        cost += abs(sum(X[2])-c)
        cost += 10*(len(X[0])-1)
        cost += 10*(len(X[1])-1)
        cost += 10*(len(X[2])-1)
        ans = min(ans,cost)
print(ans)

Solution (Python):
def main():
    from itertools import product
    n, a, b, c, *l = map(int, open(0).read().split())
    k = ['A', 'B', 'C', 'D']
    cost = []
    for abcd in product(k, repeat=n):
        s, t, u = [], [], []
        for p, q in zip(abcd, l):
            if p == 'A':
                s.append(q)
            elif p == 'B':
                t.append(q)
            elif p == 'C':
                u.append(q)
        tmp = 0
        for p, q in zip([a, b, c], [s, t, u]):
            tmp += abs(p - sum(q)) + 10 * (len(q) - 1)

        if len(s) > 0 and len(t) > 0 and len(u) > 0:
            cost.append(tmp)

    ans = min(cost)
    print(ans)


if __name__ == '__main__':
    main()

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):
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    
    from itertools import combinations, permutations, accumulate, groupby
    
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    from math import floor, ceil
    

    
    

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


    res = 10**20
    
    for s in range(1<<(2*n)):
        l1 = []
        l2 = []
        l3 = []
        mp = 0
        
        for i in range(0, 2*n, 2):
            num = (s>>i) & 1 + ((s>>(i+1)) & 1)*2
            if num==1:
                l1.append(l[i//2])
            elif num==2:
                l2.append(l[i//2])
            elif num==3:
                l3.append(l[i//2])
        d, e, f = len(l1), len(l2), len(l3)
        if d==0 or e==0 or f==0:
            continue
        mp += (d+e+f-3)*10
        mp += abs(sum(l1)-a) + abs(sum(l2)-b) + abs(sum(l3)-c)
        res = min(res, mp)
    print(res)

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

from collections import deque

ans = float('inf')

def dfs(i, abc, cost):
    global ans
    stack = deque([[i, abc, cost]])
    while stack:
        i, abc, cost = stack.pop()
        a, b, c = abc
        if i == n:
            if a * b * c:
                ans = min(ans, abs(A-a) + abs(B-b) + abs(C-c) + cost*10)
            continue
        for e in (0, 1, 2, 3):
            n_cost = cost
            n_abc = abc[:]
            if e < 3:
                if abc[e]:
                    n_cost += 1
                n_abc[e] += l[i]
            stack.append([i + 1, n_abc, n_cost])

dfs(0, [0,0,0], 0)

print(ans)