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

Solution (Python):


from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
from itertools import *
import bisect
from heapq import *
from math import *
from copy import *
from collections import deque
from collections import Counter as counter  
from itertools import combinations as comb  
from itertools import permutations as permutate
from bisect import bisect_left as bl



from bisect import bisect_right as br
from bisect import bisect







BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


def print(*args, **kwargs):
    """Prints the values to a stream, or to sys.stdout by default."""
    sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", "\n"))
    if kwargs.pop("flush", False):
        file.flush()


if sys.version_info[0] < 3:
    sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
    sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)





from types import GeneratorType


def iterative(f, stack=[]):
    def wrapped_func(*args, **kwargs):
        if stack: return f(*args, **kwargs)
        to = f(*args, **kwargs)
        while True:
            if type(to) is GeneratorType:
                stack.append(to)
                to = next(to)
                continue
            stack.pop()
            if not stack: break
            to = stack[-1].send(to)
        return to

    return wrapped_func







mod = 1000000007


def inp(): return sys.stdin.readline().rstrip("\r\n")  


def out(var): sys.stdout.write(str(var))  


def lis(): return list(map(int, inp().split()))


def stringlis(): return list(map(str, inp().split()))


def sep(): return map(int, inp().split())


def strsep(): return map(str, inp().split())


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


def nextline(): out("\n")  


def testcase(t):
    for p in range(t):
        solve()


def printlist(a):
    for p in range(0, len(a)):
        out(str(a[p]) + ' ')

import heapq as hp
from math import ceil
def solve():
    n,a,b,c=sep()
    ar=[]
    for _ in range(n):
        ar.append(int(inp()))
    ar+=([0]*(8-len(ar)))
    m=99999999999999999999999999
    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):
                                    temp=[a1,a2,a3,a4,a5,a6,a7,a8]
                                    s1=0
                                    s2=0
                                    s3=0
                                    c1=0
                                    c2=0
                                    c3=0
                                    for i in range(8):
                                        if temp[i]==0:
                                            pass
                                        elif temp[i]==1:
                                            s1+=ar[i]
                                            c1+=1
                                        elif temp[i]==2:
                                            s2+=ar[i]
                                            c2+=1
                                        else:
                                            s3+=ar[i]
                                            c3+=1
                                        if s1==0 or s2==0 or s3==0:
                                            continue
                                        else:
                                            k=10*(c1-1) + abs(a-s1) + 10*(c2-1) + abs(b-s2) + 10*(c3-1) + abs(c-s3)
                                            m=min(m,k)
    print(m)


    







solve()

Solution (Python):
import sys
sys.setrecursionlimit(10**7)
n,A,B,C = map(int,input().split())
L_ls = [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 float('inf')
        else:
            return abs(a-A) + abs(b-B) + abs(c-C)
    
    Min = dfs(i+1,a,b,c)

    if a == 0:
        Min = min(Min,dfs(i+1,a+L_ls[i],b,c))
    else:
        Min = min(Min,dfs(i+1,a+L_ls[i],b,c)+10)
    
    if b == 0:
        Min = min(Min,dfs(i+1,a,b+L_ls[i],c))
    else:
        Min = min(Min,dfs(i+1,a,b+L_ls[i],c)+10)
    
    if c == 0:
        Min = min(Min,dfs(i+1,a,b,c+L_ls[i]))
    else:
        Min = min(Min,dfs(i+1,a,b,c+L_ls[i])+10)
    
    return Min
print(dfs(0,0,0,0))

Solution (Python):
mod = 1000000007
eps = 10**-9


def main():
    import sys
    input = sys.stdin.readline

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

    ans = 10**9
    for i in range(4 ** N):
        AA = []
        BB = []
        CC = []
        ii = i
        for j in range(N):
            k = ii % 4
            if k == 0:
                AA.append(L[j])
            elif k == 1:
                BB.append(L[j])
            elif k == 2:
                CC.append(L[j])
            ii >>= 2
        if len(AA) * len(BB) * len(CC) == 0:
            continue
        ans_new = abs(sum(AA) - A) + 10*(len(AA)-1) + abs(sum(BB) - B) + 10*(len(BB)-1) + abs(sum(CC) - C) + 10*(len(CC)-1)
        ans = min(ans, ans_new)
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
n,a,b,c=map(int,input().split())
l=[int(input()) for _ in range(n)]
ans=10**18
for i in range(4**n):
  la,lb,lc,ca,cb,cc=0,0,0,0,0,0
  for j in range(n):
    if (i>>2*j)&1 and not (i>>2*j+1)&1:
      ca+=1
      la+=l[j]
    if not (i>>2*j)&1 and (i>>2*j+1)&1:
      cb+=1
      lb+=l[j]
    if (i>>2*j)&1 and (i>>2*j+1)&1:
      cc+=1
      lc+=l[j]
  if ca*cb*cc:ans=min((ca+cb+cc-3)*10+abs(la-a)+abs(lb-b)+abs(lc-c),ans)
print(ans)

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

l = [0] * N
for i in range(N):
    l[i] = int(input())

import numpy
mpp = 10**10
L = [0] * 3
t = [-10] * 3

for i in range(1, 4**N):
    b = numpy.base_repr(i, 4, N)[-N:]
    L = [A, B, C]
    t = [-10] * 3
    if "1" not in b or "2" not in b or "3" not in b:
        continue
    
    for j in range(N):
        for k in range(3):
            if b[j] == str(k + 1):
                L[k] -= l[j]
                t[k] += 10
    mpp = min(mpp, sum(t) + sum([abs(x) for x in L]))

print(mpp)

Solution (Python):
import bisect, collections, copy, heapq, itertools, math, string, sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = float('inf')
def I(): return int(input())
def F(): return float(input())
def SS(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LSS(): return input().split()

def resolve():
    N, A, B, C = LI()
    ABC = (A, B, C)
    l = [I() for _ in range(N)]

    
    ans = INF
    for i in itertools.product(range(4), repeat=N):
        a_cnt = i.count(0)
        b_cnt = i.count(1)
        c_cnt = i.count(2)
        if a_cnt > 0 and b_cnt > 0 and c_cnt > 0:
            
            mp = 10 * (a_cnt + b_cnt + c_cnt - 3)
            
            len = [0] * 3
            for j in range(N):
                if 0 <= i[j] < 3:
                    len[i[j]] += l[j]
            mp += sum([abs(j - k) for j, k in zip(ABC, len)])
            ans = min(mp, ans)

    print(ans)

if __name__ == '__main__':
    resolve()

Solution (Python):
import sys
input = lambda: sys.stdin.readline().rstrip()
input_nums = lambda: list(map(int, input().split()))
INF = 10**9

def main():
    N, A, B, C = input_nums()
    ll = [int(input()) for _ in range(N)]

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

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


if __name__ == '__main__':
    main()

Solution (Python):
n,a,b,c=map(int,input().split())
l = []
for i in range(n):
  l.append(int(input()))
m = [a,b,c]
ans = 4000
for i in range(4**n):
  p = [0]*n
  k = i
  for j in range(n):
    p[j] = k % 4
    k //= 4
  cost = [-1,-1,-1]
  q = [[] for _ in range(4)]
  for j in range(n):
    q[p[j]].append(l[j])
  for j in range(3):
    if len(q[j+1]) != 0:
      cost[j] = 10 * (len(q[j+1]) - 1) + abs(sum(q[j+1])-m[j])
  if cost[0] != -1 and cost[1] != -1 and cost[2] != -1:
    ans = min(ans, cost[0] + cost[1] + cost[2])
print(ans)

Solution (Python):
import sys
import math
import copy
from heapq import heappush, heappop, heapify
from functools import cmp_to_key
from bisect import bisect_left, bisect_right
from collections import defaultdict, deque, Counter



input = sys.stdin.readline
getS = lambda: input().strip()
getN = lambda: int(input())
getList = lambda: list(map(int, input().split()))
getZList = lambda: [int(x) - 1 for x in input().split()]

INF = float("inf")
MOD = 10**9 + 7
divide = lambda x: pow(x, MOD-2, MOD)

class BIT():
    
    def __init__(self, n):
        self.n = n
        self.BIT = [0] * (n + 2)

    
    def query(self, idx):
        res_sum = 0
        while idx > 0:
            res_sum += self.BIT[idx]
            idx -= idx & (-idx)
        return res_sum

    
    def update(self, idx, x):
        
        while idx <= self.n:
            
            self.BIT[idx] += x
            idx += idx & (-idx)
        return

    def show(self):
        print(self.BIT)

def cost(id, div, x):
    n = len(div)
    use = []
    for i in range(n):
        if id % 2 == 1:
            use.append(div[i])
        id //= 2

    return (len(use) - 1) * 10 + abs(x - sum(use))


def eachtake(div, x):
    n = len(div)
    ret = INF

    for id in range(1, 1 << n):
        tmp = cost(id, div, x)
        ret = min(tmp, ret)

    return ret


def eachpat(pat, a, b, c, li, n):
    div = [[] for i in range(3)]
    for i in range(n):
        if pat % 4 != 3:
            div[pat%4].append(li[i])
        pat //= 4

    for i in range(3):
        if not div[i]:
            return INF

    ret = 0
    ret += eachtake(div[0], a)
    ret += eachtake(div[1], b)
    ret += eachtake(div[2], c)
    
    return ret


def solve():
    n, a, b, c = getList()
    li = []
    for i in range(n):
        li.append(getN())

    ans = INF
    for pat in range(4 ** n):
        tmp = eachpat(pat, a, b, c, li, n)
        ans = min(ans, tmp)

    print(ans)

def main():
    n = getN()
    for _ in range(n):
        solve()

    return
if __name__ == "__main__":
    
    solve()

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

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

res = 10**9

for i in range(4**n):
    flag = i
    a,b,c,tmp = 0,0,0,0
    for j in range(n):
        flag,now = divmod(flag,4)
        if now == 1:
            if a != 0:
                tmp += 10
            a += L[j]
        elif now == 2:
            if b != 0:
                tmp += 10
            b += L[j]
        elif now == 3:
            if c != 0:
                tmp += 10
            c += L[j]
    if a*b*c == 0:
        continue

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

    res = min(res,tmp)

print(res)

Solution (Python):

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


def dfs(idx, a, b, c, cnt):
    if idx == N:
        if min(a, b, c) > 0:
            return abs(a - A) + abs(b - B) + abs(c - C) + cnt - 30
        else:
            return 10 ** 9

    res = 10 ** 9
    res = min(res, dfs(idx + 1, a, b, c, cnt))
    res = min(res, dfs(idx + 1, a + X[idx], b, c, cnt + 10))
    res = min(res, dfs(idx + 1, a, b + X[idx], c, cnt + 10))
    res = min(res, dfs(idx + 1, a, b, c + X[idx], cnt + 10))
    return res


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

Solution (Python):
def li():
    return [int(x) for x in input().split()]
INF = 10**18
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)

N, A, B, C = li()
l = [int(input()) for x in range(N)]

def dfs(index, a, b, c, none):
    
    if index == N:
        if min(a, b, c) > 0:
            return abs(a - A) + abs(b - B) + abs(c - C)
        else:
            return INF
    
    v1 = dfs(index+1, a + l[index], b, c, none) + (10 if a > 0 else 0)
    v2 = dfs(index+1, a, b + l[index], c, none) + (10 if b > 0 else 0)
    v3 = dfs(index+1, a, b, c + l[index], none) + (10 if c > 0 else 0)
    v4 = dfs(index+1, a, b, c, none+1)
    return min(v1, v2, v3, v4)

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

Solution (Python):
from itertools import product
N, *P = map(int, input().split())
L = []
for _ in range(N):
    L.append(int(input()))
res = []
for p in product((0,1,2,3),repeat=N):
    tmp = [0]*4
    cnt = [0]*4
    for i in range(N):
        tmp[p[i]] += L[i]
        cnt[p[i]] += 1
    if cnt[1]>=1 and cnt[2]>=1 and cnt[3]>=1:
        res.append(sum(abs(tmp[i]-P[i-1]) + (cnt[i]-1)*10 for i in range(1,4)))
print(min(res))

Solution (Python):
N,A,B,C=map(int,input().split())
l=[int(input()) for _ in range(N)]
dist=[A,B,C]
ans=float("inf")
for i in range(pow(4,N)):
    bit=[0]*N
    num=i
    x=[[],[],[]]
    cnt=0
    for j in range(N-1,-1,-1):
        bit[j]=num//pow(4,j)
        num=num%pow(4,j)
    
    if (bit.count(1)>0)and(bit.count(2)>0)and(bit.count(3)>0):
        
        for j in range(N):
            if bit[j]>0:
                x[bit[j]-1].append(l[j])
        for j in range(3):
            cnt=cnt+(len(x[j])-1)*10
            num=sum(x[j])
            cnt=cnt+abs(num-dist[j])
        ans=min(cnt,ans)
print(ans)

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

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

def dfs(i, a, b, c):
    if i == N:
        if min(a,b,c) == 0:
            return float("INF")
        else:
            return abs(a-A) + abs(b-B) + abs(c-C)
    else:
        ans0 = dfs(i+1, a, b, c) 
        ans1 = dfs(i+1, a+l[i], b, c) + (10 if a!=0 else 0)
        ans2 = dfs(i+1, a, b+l[i], c) + (10 if b!=0 else 0)
        ans3 = dfs(i+1, a, b, c+l[i]) + (10 if c!=0 else 0)
    return min(ans0, ans1, ans2, ans3)

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

Solution (Python):
import sys
from functools import lru_cache

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, A, B, C = map(int, readline().split())
    (*L,) = map(int, read().split())

    @lru_cache(maxsize=None)
    def rec(i, a, b, c):
        if i == N:
            return abs(a - A) + abs(b - B) + abs(c - C) - 30 if min(a, b, c) > 0 else INF

        res0 = rec(i + 1, a, b, c)
        res1 = rec(i + 1, a + L[i], b, c) + 10
        res2 = rec(i + 1, a, b + L[i], c) + 10
        res3 = rec(i + 1, a, b, c + L[i]) + 10

        return min(res0, res1, res2, res3)

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


if __name__ == '__main__':
    main()

Solution (Python):
import sys
from itertools import product

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, A, B, C = map(int, readline().split())
    (*L,) = map(int, read().split())

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

        res0 = rec(i + 1, a, b, c)
        res1 = rec(i + 1, a + L[i], b, c) + 10
        res2 = rec(i + 1, a, b + L[i], c) + 10
        res3 = rec(i + 1, a, b, c + L[i]) + 10

        return min(res0, res1, res2, res3)

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


if __name__ == '__main__':
    main()

Solution (Python):
import sys
from itertools import product

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, *A = map(int, readline().split())
    (*L,) = map(int, read().split())

    ans = INF
    for p in product(range(4), repeat=N):
        num = [0] * 3
        length = [0] * 3
        for i, j in enumerate(p):
            if j < 3:
                num[j] += 1
                length[j] += L[i]
        res = 0
        for j in range(3):
            if num[j] == 0:
                res = INF
                break
            else:
                res += (num[j] - 1) * 10 + abs(length[j] - A[j])
        if ans > res:
            ans = res

    print(ans)
    return


if __name__ == '__main__':
    main()

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

L = [int(input()) for i in range(N)]



ans = 10 ** 10
import itertools
for prod in itertools.product(range(4), repeat = N):
  bamboo = [0] * 4
  mp = 0
  for i in range(len(prod)):
    if prod[i] == 0:
      continue
    if bamboo[prod[i]] != 0:
      mp += 10
    bamboo[prod[i]] += L[i]
  if 0 in bamboo[1:]:
    continue
  mp += abs(A - bamboo[1]) + abs(B - bamboo[2]) + abs(C - bamboo[3])
  if ans > mp:
    ans = mp
    
print(ans)

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

INF = float('inf')

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

    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 i in range(N)]
INF = 10 ** 9

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

n, a, b, c = list(map(int, input().split()))

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

x = [0, 1, 2, 3]

mp_min = 10000

for sublist in itertools.product(x, repeat = n):
    flag = True
    mp = 0
    length = [0] * 3
    for j in range(n):
        d = sublist[j]
        if  d != 3:
            if length[d] == 0:
                length[d] = l[j]
            else:
                length[d] += l[j]
                mp += 10
    
    for k in range(3):
        if length[k] == 0:
            flag = False
            break
        else:
            mp += abs(length[k] - f[k])

    if flag:
        mp_min = min(mp_min, mp)

print(mp_min)

Solution (Python):
'''
自宅用PCでの解答
'''
import math

import itertools
import queue
import bisect
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit


ipt = stdin.readline
setrecursionlimit(10**7)
mod = 10**9+7
dir = [(-1,0),(0,-1),(1,0),(0,1)]
alp = "abcdefghijklmnopqrstuvwxyz"

def main():
    n,a,b,c = map(int,ipt().split())
    l = [int(ipt()) for i in range(n)]
    mi = 10**18

    for i in range(4**n):
        sa = 0
        sb = 0
        sc = 0
        na = 0
        nb = 0
        nc = 0
        for j in range(n):
            ni = i>>(2*j)
            if (ni&2):
                if ni&1:
                    sa += l[j]
                    na += 1
                else:
                    sb += l[j]
                    nb += 1
            elif ni&1:
                sc += l[j]
                nc += 1
        if na*nb*nc == 0:
            continue
        nm = abs(sa-a)+abs(sb-b)+abs(sc-c)+(na+nb+nc-3)*10
        if nm < mi:
            mi = nm

    print(mi)



    return None

if __name__ == '__main__':
    main()

Solution (Python):
n,a,b,c,*l=map(int,open(0).read().split())
def dfs(x,y,z,r):
  if r==n:
    return abs(a-x)+abs(b-y)+abs(c-z)-30 if min(x,y,z)>0 else 10**9
  return min(
    dfs(x+l[r],y,z,r+1)+10,
    dfs(x,y+l[r],z,r+1)+10,
    dfs(x,y,z+l[r],r+1)+10,
    dfs(x,y,z,r+1)
  )
print(dfs(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)]

INF = 10**18
ans = INF
for subset in product(range(4), repeat=n):
    cost = 0
    ABC = [[]for _ in range(3)]
    for i, s in enumerate(subset):
        if not s:
            continue
        ABC[s-1].append(L[i])
    if any(not X for X in ABC):
        continue
    for x, X in zip((a, b, c), ABC):
        cost += (len(X)-1)*10
        cost += abs(x-sum(X))
    if cost < ans:
        ans = cost

print(ans)

Solution (Python):
n,a,b,c=map(int,input().split())
L=[int(input()) for _ in range(n)]
ans=10**9
for x in range(4**n):
    A,B,C=0,0,0
    cost=0
    for i in range(n):
        r=x%4
        x//=4
        if r==1:
            A+=L[i]
        elif r==2:
            B+=L[i]
        elif r==3:
            C+=L[i]
        if r>0:
            cost+=10
    if A==0 or B==0 or C==0:
        continue
    cost+=-30+abs(A-a)+abs(B-b)+abs(C-c)
    ans=min(ans,cost)
print(ans)

Solution (Python):
import sys
from itertools import product

input = sys.stdin.readline


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

    ans = float("inf")
    for comb in product(range(4), repeat=N):
        mp = -30
        a = b = c = 0
        for i, j in enumerate(comb):
            if j == 0:
                continue
            elif j == 1:
                a += L[i]
            elif j == 2:
                b += L[i]
            elif j == 3:
                c += L[i]
            mp += 10

        if min(a, b, c) == 0:
            continue

        mp += abs(A - a) + abs(B - b) + abs(C - c)
        if mp < ans:
            ans = mp

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
from itertools import product

N, *ABC = map(int, input().split())

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

ans = 10**9
for X in product((0, 1, 2, 3), repeat=N):
  cnt, K, temp = [0]*4, [0]*4, 0
  for i in range(N):
    cnt[X[i]] += 1
    K[X[i]] += L[i]
  if min(cnt[1:]) == 0:
    continue
  for i in range(3):
    temp += abs(ABC[i] - K[i+1])
    temp += 10 * max(cnt[i+1]-1, 0)
  ans = min(ans, temp)

print(ans)

Solution (Python):
n,a,b,c=map(int,input().split())
l=[]
for i in range(n):
    l.append(int(input()))
ans=100000
for i in range(4**n):
    mp=0
    f=0
    x=[]
    y=[]
    z=[]
    w=[]
    for j in range(n):
        if i%4==0:
            x.append(l[j])
        elif i%4==1:
            y.append(l[j])
        elif i%4==2:
            z.append(l[j])
        else:
            w.append(l[j])
        i=i//4
    if sum(x)==0 or sum(y)==0 or sum(z)==0:
        f=1
    
    X=sum(x)
    Y=sum(y)
    Z=sum(z)
    mp+=(len(x)+len(y)+len(z)-3)*10
    p=[X,Y,Z]
    q=[a,b,c]
    p=sorted(p)
    q=sorted(q)
    mp+=abs(p[0]-q[0])+abs(p[1]-q[1])+abs(p[2]-q[2])
    if f!=1:     
        ans=(min(mp,ans))
print(ans)

Solution (Python):
import itertools

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

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

ans = 10**10


for x in itertools.product([0, 1, 2, 3], repeat=N):
    
    bamboo = [0] * 4
    tmp = 0
    for i in range(N):
        
        if x[i] in [1, 2, 3] and bamboo[x[i]] > 0:
            tmp += 10
        bamboo[x[i]] += l[i]

    
    if 0 in bamboo[1:]:
        continue
    
    tmp += abs(bamboo[1] - A) + abs(bamboo[2] - B) + abs(bamboo[3] - C)

    ans = min(ans, tmp)

print(ans)

Solution (Python):
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]

def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

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

def dfs(cnt,aa,bb,cc):
  if cnt==n:
    if aa==0 or bb==0 or cc==0:
      return inf
    return abs(aa-a)+abs(bb-b)+abs(cc-c)-30

  aaa=dfs(cnt+1,aa+l[cnt],bb,cc)+10
  bbb=dfs(cnt+1,aa,bb+l[cnt],cc)+10
  ccc=dfs(cnt+1,aa,bb,cc+l[cnt])+10
  ddd=dfs(cnt+1,aa,bb,cc)
  return min(aaa,bbb,ccc,ddd)

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

Solution (Python):
n, a, b, c = map(int,input().split())
l = []
for i in range(n):
    l.append(int(input()))
ans = 10**9    
for ci in range(4**n):
    ci_ = [-1] * n
    cnt = [0] * 3
    ca, cb, cc = 0, 0, 0
    for cj in range(n):
        cx = ci & 3
        if cx == 0:
            ca += l[cj]
            cnt[0] += 1 
        elif cx == 1:
            cb += l[cj]
            cnt[1] += 1 
        elif cx == 2:
            cc += l[cj]
            cnt[2] += 1 
        ci >>= 2
    if ca == 0 or cb == 0 or cc == 0:
        continue
    
    cost = 0
    for i in range(3):
        if cnt[i] > 1:
            cost += 10 * (cnt[i]-1)
    cost += abs(a-ca) + abs(b-cb) + abs(c-cc)
    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()))
ans = 10**9    
for ci in range(4**n):
    ci_ = [-1] * n
    cnt = [0] * 3
    ca, cb, cc = 0, 0, 0
    for cj in range(n):
        cx = ci & 3
        if cx == 0:
            ca += l[cj]
            cnt[0] += 1 
        elif cx == 1:
            cb += l[cj]
            cnt[1] += 1 
        elif cx == 2:
            cc += l[cj]
            cnt[2] += 1 
        ci >>= 2
    if ca == 0 or cb == 0 or cc == 0:
        continue
    
    cost = 0
    for i in range(3):
        if cnt[i] > 1:
            cost += 10 * (cnt[i]-1)
    cost += abs(a-ca) + abs(b-cb) + abs(c-cc)
    ans = min(ans, cost)
print(ans)

Solution (Python):
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]

def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()


  




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

def f(lst,ind):
  global answer
  if ind==n:
    ac=lst.count(1)
    bc=lst.count(2)
    cc=lst.count(3)

    if ac==0 or bc==0 or cc==0:
      return min(answer,inf)

    _a=_b=_c=0
    for i,x in enumerate(lst):
      if x==1:
        _a+=l[i]
      elif x==2:
        _b+=l[i]
      elif x==3:
        _c+=l[i]
    
    
    

    sm=0
    sm+=max(0,ac-1)*10
    sm+=max(0,bc-1)*10
    sm+=max(0,cc-1)*10
    sm+=abs(a-_a)
    sm+=abs(b-_b)
    sm+=abs(c-_c)

    return min(answer,sm)

  else:
    for i in range(4):
      lst[ind]=i
      answer=min(answer,f(lst,ind+1))

  return answer

lst=[-1]*n
print(f(lst,0))

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

choiceList = []
for i in range(4 ** N):
    choice = [-1 for i in range(N)]
    for j in range (N):
        choice[j] = i // (4 ** j) % 4
    choiceList.append(choice)

minCost = 10 ** 9
for choice in choiceList:
    cost = 10 ** 9
    aList = []
    bList = []
    cList = []
    for i in range(N):
        if choice[i] == 1:
            aList.append(l[i])
        elif choice[i] == 2:
            bList.append(l[i])
        elif choice[i] == 3:
            cList.append(l[i])
    if len(aList) == 0 or len(bList) == 0 or len(cList) == 0:
        cost = 10 ** 9
    else:
        aCost = (len(aList) - 1) * 10 + abs(sum(aList) - A)
        bCost = (len(bList) - 1) * 10 + abs(sum(bList) - B)
        cCost = (len(cList) - 1) * 10 + abs(sum(cList) - C)
        cost = aCost + bCost + cCost
    if minCost > cost:
        minCost = cost

print(minCost)