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):
import sys
 
stdin = sys.stdin
 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()
nas = lambda: stdin.readline().split()

from itertools import permutations
from itertools import combinations

n, a, b, c = na()
l = [ni() for i in range(n)]
inf = 1 << 60

def dfs(i, x, y, z):
    if i == n:
        return abs(x - a) + abs(y - b) + abs(z - c) - 30 if min(x, y, z) > 0 else inf
    q = dfs(i + 1, x, y, z)
    w = dfs(i + 1, x + l[i], y, z) + 10
    e = dfs(i + 1, x, y + l[i], z) + 10
    r = dfs(i + 1, x, y, z + l[i]) + 10
    return min(q, w, e, r)

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 ** 18


def dfs(i, a, b, c):
    if i == n:
        if a < 1 or b < 1 or c < 1:
            return inf
        return abs(A - a) + abs(B - b) + abs(C - c)
    res = dfs(i + 1, a, b, c)
    res = min([res, dfs(i + 1, a + l[i], b, c) + (10 if a > 0 else 0)])
    res = min([res, dfs(i + 1, a, b + l[i], c) + (10 if b > 0 else 0)])
    res = min([res, dfs(i + 1, a, b, c + l[i]) + (10 if c > 0 else 0)])
    return res


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

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


def dfs(i, a, b, c):
    if i == n:
        if 1 > a or 1 > b or 1 > c:
            return 10 ** 18
        return abs(A - a) + abs(B - b) + abs(C - c)
    res = dfs(i + 1, a, b, c)
    res = min([res, dfs(i + 1, a + l[i], b, c) + (10 if a > 0 else 0)])
    res = min([res, dfs(i + 1, a, b + l[i], c) + (10 if b > 0 else 0)])
    res = min([res, dfs(i + 1, a, b, c + l[i]) + (10 if c > 0 else 0)])
    return res


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

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


def dfs(i, a, b, c):
    if n == i:
        if 1 > a or 1 > b or 1 > c:
            return 10 ** 18
        return abs(A - a) + abs(B - b) + abs(C - c)

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


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

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


def dfs(i, a, b, c):
    if i == n:
        if a == 0 or b == 0 or c == 0:
            return 10 ** 18
        return abs(A - a) + abs(B - b) + abs(C - c)
    res = dfs(i + 1, a, b, c)
    res = min([res, dfs(i + 1, a + l[i], b, c) + (10 if a > 0 else 0)])
    res = min([res, dfs(i + 1, a, b + l[i], c) + (10 if b > 0 else 0)])
    res = min([res, dfs(i + 1, a, b, c + l[i]) + (10 if c > 0 else 0)])
    return res


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 ** 18


def dfs(i, a, b, c):
    if n == i:
        if 0 == a or 0 == b or 0 == c:
            return inf
        return abs(A - a) + abs(B - b) + abs(C - c)

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


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

Solution (Python):
n,A,B,C=map(int,input().split())
bam=[]
for i in range(n):
    bam.append(int(input()))
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 + bam[cur], b, c) + 10 
    ret2 = dfs(cur + 1, a, b + bam[cur], c) + 10
    ret3 = dfs(cur + 1, a, b, c + bam[cur]) + 10
    
    return min(ret0, ret1, ret2, ret3)

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

Solution (Python):

import math
import heapq
import bisect
import numpy as np


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

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

ans = 10**9

for i in range(1<<(2*N)):
    a = b = c = 0
    tmp = 0
    for j in range(N):
        flg = 0
        if i&(1<<(2*j)):
            flg += 1
        if i&(1<<(2*j+1)):
            flg += 2

        if flg == 1:
            if a:
                tmp += 10
            a += L[j]
        elif flg == 2:
            if b:
                tmp += 10
            b += L[j]
        elif flg == 3:
            if c:
                tmp += 10
            c += L[j]

    if a*b*c==0:
        continue

    tmp += abs(A-a)+abs(B-b)+abs(C-c)
    ans = min(ans,tmp)

print(ans)

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

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

def search(l,a,b,c,d):
    if l == N:
        if a == 0 or b == 0 or c == 0:
            return float('inf')

        return (abs(a-A)+abs(b-B)+abs(c-C)+10*(d))

    if a == 0:
        mp1 = search(l+1,a+L[l],b,c,d)
    else:
        mp1 = search(l+1,a+L[l],b,c,d+1)

    if b == 0:
        mp2 = search(l+1,a,b+L[l],c,d)
    else:
        mp2 = search(l+1,a,b+L[l],c,d+1)

    if c == 0:
        mp3 = search(l+1,a,b,c+L[l],d)
    else:
        mp3 = search(l+1,a,b,c+L[l],d+1)

    mp4 = search(l+1,a,b,c,d)

    return min([mp1,mp2,mp3,mp4])

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

Solution (Python):
import itertools

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

li = [0,1,2,3]

ans = 10**9
for elm in  itertools.product(li,repeat=n):
    
    if not(1 in elm) or not(2 in elm) or not(3 in elm):
        continue

    whch = [[],[],[]]
    ans_mm = 0
    for j in range(len(elm)):
        if elm[j] == 1:
            whch[0].append(l[j])
        elif elm[j] == 2:
            whch[1].append(l[j])
        elif elm[j] == 3:
            whch[2].append(l[j])

    ans_mm += ((len(whch[0])-1) + (len(whch[1])-1) + (len(whch[2])-1)) * 10
    ans_mm += abs(sum(whch[0])-a)
    ans_mm += abs(sum(whch[1])-b)
    ans_mm += abs(sum(whch[2])-c)

    ans = min(ans,ans_mm)

print(ans)

Solution (Python):
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,pprint,fractions
sys.setrecursionlimit(10**8)
mod = 10**9+7
mod2 = 998244353
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

n,a,b,c = inpl()
l = inpln(n)
x = [a,b,c]
res = INF

for flag in itertools.product(range(4), repeat=n):
    cnt = 0
    xx = [[]for i in range(4)]
    for i,bit in enumerate(flag):
        xx[bit].append(i)
    for i in range(3):
        if xx[i] == []:
            break
        cnt += (len(xx[i]) - 1)*10
        sum = 0
        for j in xx[i]:
            sum += l[j]
        cnt += abs(sum-x[i])
    else:
        res = min(res,cnt)
print(res)

Solution (Python):
import sys
from itertools import product

inf = float('inf')

n, *target = map(int, sys.stdin.readline().split())
target = [None] + target
*l,  = map(int, sys.stdin.read().split())

def main():
    costs = []
    for p in product(range(4), repeat=n):
        tmp = [[] for _ in range(4)]
        p = list(p)
        for i in range(n):
            tmp[p[i]].append(l[i])

        cost = [0] * 4
        for i in range(1, 4):
            if not tmp[i]:
                break
            cost[i] += 10 * (len(tmp[i]) - 1)
            cost[i] += abs(target[i] - sum(tmp[i]))
        else:
            costs.append(sum(cost))

    return min(costs)

if __name__ == '__main__':
    ans = main()
    print(ans)

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

ans = 10**18
for pattern in product(range(4), repeat=N):
  MP = 0
  take = [0] * 3 
  for index, length in zip(pattern, l):
    if index == 3:
      continue
    if take[index] != 0: 
      MP += 10
    take[index] += length
  
  if take[0] != 0 and take[1] != 0 and take[2] != 0:
    MP += abs(A - take[0])
    MP += abs(B - take[1])
    MP += abs(C - take[2])
    ans = min(MP, ans)

print(ans)

Solution (Python):
N,A,B,C=map(int,input().split())
l=[int(input()) for i in range(N)]
ans=10**9
for i in range(4**N):
  tmp=[0]*N
  s=[0,0,0]
  for j in range(N):
    k = (i // (4**j))%4
    tmp[j]=k
    if k>0:
      s[k-1]+=1
  if s[0]==0 or s[1]==0 or s[2]==0:
    continue
  else:
    ans_tmp=0
    ans_tmp=10*(max(s[0]-1,0)+max(s[1]-1,0) +max(s[2]-1,0))
    s[0],s[1],s[2]=0,0,0
    for j in range(N):
      if tmp[j]==0:
        continue
      s[tmp[j]-1]+=l[j]
    ans_tmp+=abs(s[0]-A)+abs(s[1]-B)+abs(s[2]-C)
    ans=min(ans,ans_tmp)
print(ans)

Solution (Python):
n,A,B,C=map(int,input().split())
L=[int(input()) for i in range(n)]
r=10**5
def dsf(i,a,b,c,p):
    global r
    if i==n:
        if a!=0 and b!=0 and c!=0:
            p+=abs(A-a)+abs(B-b)+abs(C-c)-30
            r=min(r,p)
        return
    dsf(i+1,a+L[i],b,c,p+10)
    dsf(i+1,a,b+L[i],c,p+10)
    dsf(i+1,a,b,c+L[i],p+10)
    dsf(i+1,a,b,c,p)
dsf(0,0,0,0,0)
print(r)

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

ans = 10**10

for pat in range(4**N):
    a = b = c = 0
    mp = 0
    for i in range(N):
        k = (pat >> (2*i)) & 0b11
        if k == 0:
            pass            
        if k == 1:
            a += l[i]
            mp += 10
        elif k == 2:
            b += l[i]
            mp += 10
        elif k == 3:
            c += l[i]
            mp += 10
    if a != 0 and b != 0 and c != 0:
        mp += -30 + abs(A-a) + abs(B-b) + abs(C-c)
        if mp < ans:
            ans = mp

print(ans)

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


def dfs(pointer, a, b, c):
    if pointer == N:
        return abs(a - A) + abs(b - B) + abs(c - C) - 30 if min(min(a, b), c) > 0 else INF
    ret_0 = dfs(pointer + 1, a, b, c)
    ret_1 = dfs(pointer + 1, a + l[pointer], b, c) + 10
    ret_2 = dfs(pointer + 1, a, b + l[pointer], c) + 10
    ret_3 = dfs(pointer + 1, a, b, c + l[pointer]) + 10
    return min(ret_0, ret_1, ret_2, ret_3)


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

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


ans=0

from operator import mul
from functools import reduce
import itertools
def cmb(n,r):
    if n<r: return 0
    r = min(n-r,r)
    if r == 0: return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1,r + 1))
    return over // under




def mp(x,y,z): 
  ans=float("inf")
  rng=list(range(n))
  for ptn in itertools.combinations(rng,x):
    cost=0
    cost+=10*(x-1) 
    wa=0
    copy=rng[:]
    for i in ptn:
      wa+=l[i]
      copy.remove(i)
    
    cost+=abs(wa-a) 
    for ptn2 in itertools.combinations(copy,y):
      
      cost1=cost+10*(y-1) 
      wa=0
      copy2=copy[:]
      for i in ptn2:
        wa+=l[i]
        copy2.remove(i)
      cost1+=abs(wa-b)
      for ptn3 in itertools.combinations(copy2,z):
        cost2=cost1+10*(z-1)
        wa=0
        for i in ptn3:
          wa+=l[i]
        cost2+=abs(wa-c)
        ans=min(ans,cost2)
  return(ans)

final=float("inf")
for i in range(1,n-1):
  for j in range(1,n-i):
    for k in range(1,n+1-i-j):
      final=min(final,mp(i,j,k))
print(final)

Solution (Python):

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

costs = []
min_cost = float('inf')

def get_candidates(n, buf):
    if n == N:
        cands_A = [l for i, l in zip(buf, L) if i == 0]
        cands_B = [l for i, l in zip(buf, L) if i == 1]
        cands_C = [l for i, l in zip(buf, L) if i == 2]

        if len(cands_A) > 0 and len(cands_B) > 0 and len(cands_C) > 0:
            cost = 0
            cost += (len(cands_A) - 1) * 10
            cost += abs(sum(cands_A) - A)

            cost += (len(cands_B) - 1) * 10
            cost += abs(sum(cands_B) - B)

            cost += (len(cands_C) - 1) * 10
            cost += abs(sum(cands_C) - C)

            costs.append(cost)
            
            


    else:
        for i in range(4):
            buf.append(i)
            get_candidates(n+1, list(buf))
            buf.pop()

get_candidates(0, [])

print(min(costs))

Solution (Python):
import sys
import itertools
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
def input():
    return sys.stdin.readline().rstrip()

def main():
    N,A,B,C=map(int,input().split())
    l=[int(input()) for _ in range(N)]
    ps=itertools.permutations(l)
    ans=INF
    for p in ps:
        S=[0]*(N+1)
        for i in range(N):
            S[i+1]=S[i]+p[i]
        for i in range(N-2):
            for j in range(N-1-(i+1)):
                for k in range(N-(i+1)-(j+1)):
                    tmp=0
                    a=S[i+1]
                    b=S[i+1+j+1]-S[i+1]
                    c=S[i+1+j+1+k+1]-S[i+1+j+1]
                    tmp=(i+j+k)*10+abs(A-a)+abs(B-b)+abs(C-c)
                    ans=min(ans,tmp)
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
import sys
import itertools
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
def input():
    return sys.stdin.readline().rstrip()

def main():
    N,A,B,C=map(int,input().split())
    l=[int(input()) for _ in range(N)]
    ps=itertools.permutations(l)
    ans=INF
    for p in ps:
        for i in range(N-2):
            for j in range(N-1-(i+1)):
                for k in range(N-(i+1)-(j+1)):
                    tmp=0
                    a=sum(p[:i+1])
                    b=sum(p[i+1:i+1+j+1])
                    c=sum(p[i+1+j+1:i+1+j+1+k+1])
                    tmp=(i+j+k)*10+abs(A-a)+abs(B-b)+abs(C-c)
                    ans=min(ans,tmp)
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
n,a,b,c=map(int,input().split())
t=[int(input()) for i in range(n)]
def getStatus(num):
  arr=[]
  for i in range(n)[::-1]:
    arr.append(num//(4**i))
    num%=4**i
  return arr
ans=10**10
for i in range(4**n):
  S=getStatus(i)
  L=[0,0,0]
  chk=0
  if 1 not in S or 2 not in S or 3 not in S:continue
  for j in range(n):
    if S[j]==0:continue
    if L[S[j]-1]!=0:chk+=10
    L[S[j]-1]+=t[j]
  chk+=abs(a-L[0])+abs(b-L[1])+abs(c-L[2])
  if chk<ans:ans=chk
print(ans)

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

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

    memo = 0
    ans = float("inf")

    for a, b, c in list(itertools.permutations([A, B, C], 3)):
        Ncopy = N
        a_cost = float("inf")
        b_cost = float("inf")
        c_cost = float("inf")

        lcopy = copy.deepcopy(l)

        for i in range(1, 2 ** Ncopy):
            tmp = -10
            a_len = 0
            for j in range(Ncopy):
                if i >> j & 1:
                    tmp += 10
                    a_len += lcopy[j]
            tmp += abs(a_len - a)
            a_cost = min(a_cost, tmp)
            if a_cost == tmp:
                memo = i

        for j in range(Ncopy - 1, -1, -1):
            if memo >> j & 1:
                lcopy.pop(j)
                Ncopy -= 1

        for i in range(1, 2 ** Ncopy):
            tmp = -10
            b_len = 0
            for j in range(Ncopy):
                if i >> j & 1:
                    tmp += 10
                    b_len += lcopy[j]
            tmp += abs(b_len - b)
            b_cost = min(b_cost, tmp)
            if b_cost == tmp:
                memo = i

        for j in range(Ncopy - 1, -1, -1):
            if memo >> j & 1:
                lcopy.pop(j)
                Ncopy -= 1


        if len(lcopy) == 0:
            continue


        for i in range(1, 2 ** Ncopy):
            tmp = -10
            c_len = 0
            for j in range(Ncopy):
                if i >> j & 1:
                    tmp += 10
                    c_len += lcopy[j]
            tmp += abs(c_len - c)
            c_cost = min(c_cost, tmp)
            if c_cost == tmp:
                memo = i
        for j in range(Ncopy - 1, -1, -1):
            if memo >> j & 1:
                lcopy.pop(j)
                Ncopy -= 1

        ans = min(ans, a_cost + b_cost + c_cost)


    print(ans)









    

if __name__ == '__main__':
    main()

Solution (Python):
n, a, b, c = map(int, input().split())
Bmb = [int(input()) for _ in range(n)]
ans = 10**9
for i in range(4**n):
    aa, bb, cc = 0, 0, 0
    cnt = 0
    for j in range(n):
        k = ( i >> (2*j) ) & 3
        if k == 1:
            if aa > 0:
                cnt += 10
            aa += Bmb[j]
        elif k == 2:
            if bb > 0:
                cnt += 10
            bb += Bmb[j]
        elif k == 3:
            if cc > 0:
                cnt += 10
            cc += Bmb[j]
    if aa == 0 or bb == 0 or cc == 0:
        continue
    cnt += abs(a-aa) + abs(b-bb) + abs(c-cc)
    ans = min(ans, cnt)
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
N,A,B,C=map(int,input().split())
l=[0]*N
for i in range(N):
    l[i]=int(input())
ans=float("inf")
for i in range(1<<2*N):
    a=[]
    b=[]
    c=[]
    for j in range(0,2*N,2):
        
        if 1 & i>>j:
            if 1 & i>>(j+1):
                a.append(l[j//2])
            else :
                b.append(l[j//2])
        else:
            if 1 & i>>(j+1):
                c.append(l[j//2])

    
    if len(a)==0 or len(b)==0 or len(c)==0:
        continue
    mp=abs(sum(a)-A)+abs(sum(b)-B)+abs(sum(c)-C)+10*(len(a)+len(b)+len(c)-3)
    ans=min(ans,mp)
print(ans)

Solution (Python):


list = [int(i) for i in input().rstrip().split(" ")]

matsu_count = list[0]
require = sorted(list[1:])

matsu_list = sorted([int(input().rstrip()) for i in range(matsu_count)])

def get_mp(matsu:list):
    ans = 0
    for i in range(3):
        ans += abs(matsu[i] - require[i])
    return ans
mp = {}

def get_ans(matsu:list):
    if len(matsu) == 3:
        return get_mp(matsu)
    key = "
    if key in mp:
        return mp[key]
    ans = 80000
    for i in range(len(matsu) - 3 + 1):
        for j in range(i + 1, len(matsu) - 2 + 1):
            for k in range(j + 1, len(matsu) - 1 + 1):
                ans = min(ans, get_mp([matsu[i],matsu[j], matsu[k]]))
    if ans > 0:
        for i in range(len(matsu)):
            for j in range(i + 1, len(matsu)):
                sub_matsu = []
                for k in range(len(matsu)):
                    if k == i or k == j:
                        continue
                    else:
                        sub_matsu.append(matsu[k])
                sub_matsu.append(matsu[i] + matsu[j])
                ans = min(ans, get_ans(sorted(sub_matsu)) + 10)

    mp[key] = ans
    return ans

ans = get_ans(matsu_list)
print(ans)

Solution (Python):
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 float("inf")
    ret0 = dfs(cursor + 1, a, b, c)
    ret1 = dfs(cursor + 1, a + l[cursor], b, c) + 10
    ret2 = dfs(cursor + 1, a, b + l[cursor], c) + 10
    ret3 = dfs(cursor + 1, a, b, c + l[cursor]) + 10
    return min(ret0, ret1, ret2, ret3)


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

Solution (Python):
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 float("inf")
    ret0 = dfs(cursor + 1, a, b, c)
    ret1 = dfs(cursor + 1, a + l[cursor], b, c) + 10
    ret2 = dfs(cursor + 1, a, b + l[cursor], c) + 10
    ret3 = dfs(cursor + 1, a, b, c + l[cursor]) + 10
    return min(ret0, ret1, ret2, ret3)


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

Solution (Python):
import itertools


n_bamboo, b1, b2, b3 = [int(i) for i in input().split()]
bamboos = [int(input()) for _ in range(n_bamboo)]

ans = float("inf")


for bit in itertools.product((0, 1, 2, 3), repeat=n_bamboo):
    l1, l2, l3 = 0, 0, 0
    c1, c2, c3 = 0, 0, 0
    
    for b, bamboo in zip(bit, bamboos):
        if b == 1:
            if l1 > 0:
                c1 += 10
            l1 += bamboo
        elif b == 2:
            if l2 > 0:
                c2 += 10
            l2 += bamboo
        elif b == 3:
            if l3 > 0:
                c3 += 10
            l3 += bamboo
    
    if l1 * l2 * l3 == 0:
        continue
    
    c1 += abs(b1 - l1)
    c2 += abs(b2 - l2)
    c3 += abs(b3 - l3)
    
    ans = min(ans, c1 + c2 + c3)
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(cnt,a,b,c):
  if cnt==n:
    if min(a,b,c)>0:
      return abs(A-a)+abs(B-b)+abs(C-c)-30
    else:
      return  INF
  else:
    res0=dfs(cnt+1,a,b,c)
    res1=dfs(cnt+1,a+L[cnt],b,c)+10
    res2=dfs(cnt+1,a,b+L[cnt],c)+10
    res3=dfs(cnt+1,a,b,c+L[cnt])+10
    return min(res0,res1,res2,res3)

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

Solution (Python):
from itertools import product
N, *ABC = map(int, input().split())
L = [int(input()) for _ in range(N)]
ans = float("inf")
for prod in product(range(4), repeat=N):
    abc = [0] * 3
    mp = -30
    for p, l in zip(prod, L):
        if p != 3:
            abc[p] += l
            mp += 10
    if abc[0] and abc[1] and abc[2]:
        mp += sum(map(lambda x,y: abs(x-y), ABC, abc))
        ans = min(ans, mp)
print(ans)

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


ans = 10000
for x in itertools.product(range(4), repeat=N):
    a, b, c, mp = 0, 0, 0, 0
    for i in range(N):
        if x[i] == 1:
            if a != 0:
                mp += 10
            a += L[i]

        if x[i] == 2:
            if b != 0:
                mp += 10
            b += L[i]

        if x[i] == 3:
            if c != 0:
                mp += 10
            c += L[i]

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

    mp += abs(A-a)+abs(B-b)+abs(C-c)
    ans = min(ans, mp)
print(ans)

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

ans = 10**18
for pattern in itertools.product(range(4), repeat=n):
    MP = 0
    take = [0] * 3
    for index, length in zip(pattern, L):
        if index == 3:
            continue
        if take[index] != 0:
            MP += 10
        take[index] += length

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

    MP += abs(a - take[0])
    MP += abs(b - take[1])
    MP += abs(c - take[2])

    ans = min(ans, MP)

print(ans)

Solution (Python):
from itertools import product

N,*T = map(int,input().split())
L = [int(input()) for i in range(N)]
ans = 3000

for x in product([0,1,2,3], repeat=N):
    cost = 0
    l = [0]*4
    for i,v in enumerate(x):
        l[v] += L[i]
        if v<3:
            cost += 10

    if 0 in l[:3]:
        continue
    cost -= 30
    for i in range(3):
        cost += abs(l[i]-T[i])
    ans = min(ans, cost)

print(ans)

Solution (Python):
N,*T = map(int,input().split())
L = [int(input()) for i in range(N)]
ans = 3000

for x in range(4**N):
    l = [0]*4
    t = [0]*4
    n = 4
    i = 0

    for i in range(N):
        l[x%n] += L[i]
        t[x%n] += 1
        x//=n

    if 0 in l[:3]:
        continue
    cost = 0
    for i in range(3):
        cost += (t[i]-1)*10
        cost += abs(l[i]-T[i])
    ans = min(ans, cost)

print(ans)

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(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
    non_ans=dfs(times+1,A,B,C)
    a_ans=dfs(times+1,A+l[times],B,C)+10
    b_ans=dfs(times+1,A,B+l[times],C)+10
    c_ans=dfs(times+1,A,B,C+l[times])+10
    return min(non_ans,a_ans,b_ans,c_ans)

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

Solution (Python):
import sys
input = sys.stdin.readline
N,A,B,C=map(int,input().rstrip().split())
l=[int(input().rstrip()) for _ in range(N)]
inf=10**9+7
def resolve():
    print(rect(0,0,0,0))

def rect(i,a,b,c):
    if i==N:
        if(a==0 or b==0 or c==0):
            return inf
        return abs(A-a)+abs(B-b)+abs(C-c)



    ans=rect(i+1,a,b,c)

    costa=10 if a!=0 else 0
    costb=10 if b!=0 else 0
    costc=10 if c!=0 else 0

    ans=min(ans,rect(i+1,a+l[i],b,c)+costa)
    ans=min(ans,rect(i+1,a,b+l[i],c)+costb)
    ans=min(ans,rect(i+1,a,b,c+l[i])+costc)


    return ans


import sys
from io import StringIO
import unittest

class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)
    def test_入力例_1(self):
        input = """5 100 90 80
98
40
30
21
80"""
        output = """23"""
        self.assertIO(input, output)
    def test_入力例_2(self):
        input = """8 100 90 80
100
100
90
90
90
80
80
80"""
        output = """0"""
        self.assertIO(input, output)
    def test_入力例_3(self):
        input = """8 1000 800 100
300
333
400
444
500
555
600
666"""
        output = """243"""
        self.assertIO(input, output)

if __name__ == "__main__":
    
    resolve()

Solution (Python):
import itertools
import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

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



N, A, B, C = list(map(int, sys.stdin.buffer.readline().split()))
L = [int(sys.stdin.buffer.readline()) for _ in range(N)]


ans = INF
for choices in itertools.product(range(4), repeat=N):
    a = []
    b = []
    c = []

    for i, choice in enumerate(choices):
        if choice:
            [a, b, c][choice - 1].append(L[i])

    if a and b and c:
        mp = 0
        mp += (len(a) - 1) * 10 + abs(A - sum(a))
        mp += (len(b) - 1) * 10 + abs(B - sum(b))
        mp += (len(c) - 1) * 10 + abs(C - sum(c))
        ans = min(mp, ans)
print(ans)

Solution (Python):
n, a, b, c = [int(it) for it in input().split()]
ls = [int(input()) for it in range(n)]
def dfs(cur, tmpA, tmpB, tmpC):
    if cur == n:
        return abs(a - tmpA) + abs(b - tmpB) + abs(c - tmpC) if tmpA > 0 and tmpB > 0 and tmpC else 10**20
    return min(
        dfs(cur + 1, tmpA + ls[cur], tmpB, tmpC) + (10 if tmpA > 0 else 0),
        dfs(cur + 1, tmpA, tmpB + ls[cur], tmpC) + (10 if tmpB > 0 else 0),
        dfs(cur + 1, tmpA, tmpB, tmpC + ls[cur]) + (10 if tmpC > 0 else 0),
        dfs(cur + 1, tmpA, tmpB, tmpC)
    )

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

Solution (Python):
n, a, b, c = [int(it) for it in input().split()]
ls = [int(input()) for it in range(n)]
def dfs(cur, tmpA, tmpB, tmpC):
    if cur == n:
        return abs(a - tmpA) + abs(b - tmpB) + abs(c - tmpC) if tmpA > 0 and tmpB > 0 and tmpC else 10**20
    return min(
        dfs(cur + 1, tmpA + ls[cur], tmpB, tmpC) + (10 if tmpA > 0 else 0),
        dfs(cur + 1, tmpA, tmpB + ls[cur], tmpC) + (10 if tmpB > 0 else 0),
        dfs(cur + 1, tmpA, tmpB, tmpC + ls[cur]) + (10 if tmpC > 0 else 0),
        dfs(cur + 1, tmpA, tmpB, tmpC)
    )

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

Solution (Python):
import random as rng
import itertools as it
import collections as col
import heapq as hq
import sys
import copy as cp
sys.setrecursionlimit(10**9)


def dump_impl(*objects):
    print(*objects, file=sys.stderr)


def dump_dummy(*objects):
    pass


dump = dump_impl if "DEBUG" in sys.argv else dump_dummy

N, A, B, C = map(int, input().split())
l = [int(input()) for i in range(N)]
ans = 10**20
for use in it.product({0, 1, 2, 3}, repeat=N):
    ABC = {0: [], 1: [], 2: [], 3: []}
    for i, u in enumerate(use):
        dump(i, u, use)
        ABC[u].append(l[i])
    if any([len(ABC[u]) == 0 for u in {1, 2, 3}]):
        continue
    ans = min(ans, sum([abs(sum(ABC[i]) - x) + (len(ABC[i]) - 1)
                        * 10 for i, x in zip([1, 2, 3], [A, B, C])]))
print(ans)

Solution (Python):
import random as rng
import itertools as it
import collections as col
import heapq as hq
import sys
import copy as cp
sys.setrecursionlimit(10**9)


def dump_impl(*objects):
    print(*objects, file=sys.stderr)


def dump_dummy(*objects):
    pass


dump = dump_impl if "DEBUG" in sys.argv else dump_dummy

N, A, B, C = map(int, input().split())
l = [int(input()) for i in range(N)]
ans = 10**20
for use in it.product({0, 1, 2, 3}, repeat=N):
    ABC = {0: [], 1: [], 2: [], 3: []}
    for i, u in enumerate(use):
        dump(i, u, use)
        ABC[u].append(l[i])
    if any([len(ABC[u]) == 0 for u in {1, 2, 3}]):
        continue
    ans = min(ans, sum([abs(sum(ABC[i]) - x) + (len(ABC[i]) - 1)
                        * 10 for i, x in zip([1, 2, 3], [A, B, C])]))
print(ans)

Solution (Python):
import sys
from collections import Counter
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

n, *abc = map(int, readline().split())
lst = list(map(int, read().split()))
s = 10 ** 9 + 7
for i in range(4 ** n):
  tlst = [0] * n
  ti = i
  for j in range(n):
    tlst[j] = ti % 4
    ti //= 4
  c = Counter(tlst)
  if not (c[1] and c[2] and c[3]):
    continue
  else:
    cost = [0] * 3
    for idx, j in enumerate(tlst):
      if j == 0:
        continue
      cost[j - 1] += lst[idx]
    s = min(s, sum(abs(cost[j] - abc[j]) + 10 * (c[j + 1] - 1) for j in range(3)))
print(s)