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



Sample input:
5 100 90 80
98
40
30
21
80


Sample output:
23

Submission status: Accepted

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

ans=float('inf')
for i in range(4**n):
  G=[[] for _ in range(4)]
  for j in range(n):
    G[(i//(4**j))%4].append(L[j])
  if len(G[0])==0 or len(G[1])==0 or len(G[2])==0: continue
  tmp=0
  for j in range(3):
    tmp+=max(0,len(G[j])-1)*10
    if j==0: tmp+=abs(a-sum(G[j]))
    if j==1: tmp+=abs(b-sum(G[j]))
    if j==2: tmp+=abs(c-sum(G[j]))
  ans=min(ans,tmp)
print(ans)

Submission status: Accepted

Solution code:
import itertools

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

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

MIN=10**10
for i in range(len(d)):
    ans=0
    a_num=0
    b_num=0
    c_num=0
    a_kadomatu=A
    b_kadomatu=B
    c_kadomatu=C
    if {0,1,2} <= set(d[i]):
        for g in range(len(d[i])):
            if d[i][g]==0:
                a_num+=1
                a_kadomatu-=l[g]
            elif d[i][g]==1:
                b_num+=1
                b_kadomatu-=l[g]
            elif d[i][g]==2:
                c_num+=1
                c_kadomatu-=l[g]
        ans=abs(a_kadomatu)+abs(b_kadomatu)+abs(c_kadomatu)+10*(a_num-1)+10*(b_num-1)+10*(c_num-1)
        MIN=min(MIN,ans)
print(MIN)

Submission status: Accepted

Solution code:
N, A, B, C = list(map(int, input().split()))
l = [int(input()) for x 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))

Submission status: Accepted

Solution code:
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 10 ** 9
    no_add = dfs(cur + 1, a, b, c)
    add_a = dfs(cur + 1, a + l[cur], b, c) + 10
    add_b = dfs(cur + 1, a, b + l[cur], c) + 10
    add_c = dfs(cur + 1, a, b, c + l[cur]) + 10

    return min(no_add, add_a, add_b, add_c)


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

Submission status: Accepted

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


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


print(recur(n, 0, 0, 0, 0))

Submission status: Accepted

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

ans=float('inf')
for i in range(4**n):
  G=[[] for _ in range(4)]
  for j in range(n):
    G[(i//(4**j))%4].append(L[j])
  if len(G[0])==0 or len(G[1])==0 or len(G[2])==0: continue # 一つも割り当てられていない場合条件達成は無理
  tmp=0
  for i,g in enumerate(G):
    if i==3: continue
    wood=sum(g) # 竹の合体
    tmp+=(len(g)-1)*10
    if i==0: tmp+=abs(wood-a)
    if i==1: tmp+=abs(wood-b)
    if i==2: tmp+=abs(wood-c)
  ans=min(ans,tmp)
print(ans)

Submission status: Accepted

Solution code:
import itertools

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

def min_cost(target, l_list):
  return 10*(len(l_list) - 1) + abs(target - sum(l_list))


ans = 1000 * 3
combs = list(itertools.product([0, 1, 2, 3], repeat=N))
for comb in combs:
  labcn = [[], [], [], []]
  for i, idx in enumerate(comb):
    labcn[idx].append(L[i])
  
  la, lb, lc, ln = labcn
  if len(la)*len(lb)*len(lc):
    ans = min(ans, min_cost(A, la)+min_cost(B, lb)+min_cost(C, lc))

print(ans)

Submission status: Accepted

Solution code:
import itertools

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

def min_cost(target, l_list):
  if target in l_list:
    return 0
  else:
    res = 1000
    for l in l_list:
      res = min(res, abs(target - l))
    
    for i, l in enumerate(l_list):
      res = min(res, min_cost(target-l, l_list[:i]+l_list[i+1:]) + 10)
  
    return res

ans = 1000 * 3
combs = list(itertools.product([0, 1, 2], repeat=N))
for comb in combs:
  labc = [[], [], []]
  for i, idx in enumerate(comb):
    labc[idx].append(L[i])
  
  la, lb, lc = labc
  if len(la)*len(lb)*len(lc):
    ans = min(ans, min_cost(A, la)+min_cost(B, lb)+min_cost(C, lc))

print(ans)

Submission status: Accepted

Solution code:
import sys
import math
import fractions
import bisect
import queue
import heapq
from collections import deque
sys.setrecursionlimit(4100000)

MOD = int(1e9+7)
PI = 3.14159265358979323846264338327950288
INF = 1e18

'''
1行のint
N, K = map(int, input().split())

1行のstring
S, T = input().split()

1行の整数配列
P = list(map(int,input().split()))

改行あり整数
x = []
y = []
for i in range(N):
    x1,y1=[int(i) for i in input().split()]
    x.append(x1)
    y.append(y1)

N行M列の初期化
dp = [[INF] * M for i in range(N)]

'''

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



ans = INF
for use in range(4**N):  # 4進法　1:A 2:B 3:C 0:unused
    a = 0
    b = 0
    c = 0
    d = 0
    local = 0
    unuse = 0
    for i in range(N):
        where = use%(4**(i+1))//(4**i)
        if where == 1:
            a+=L[i]
        elif where == 2:
            b+= L[i]
        elif where == 3:
            c+=L[i]
        else :
            d+=L[i]
            unuse+=1

    if a*b*c == 0:
        continue

    local = abs(A-a) + abs(B-b) + abs(C-c) + (N-unuse-3)*10
    ans = min(ans, local)


print(ans)

Submission status: Accepted

Solution code:
import itertools 
def diff(a,b,c):
    return abs(A-a)+abs(B-b)+abs(C-c)
    
N,A,B,C,*l=map(int,open(0).read().split())
_min=10**6
for i in itertools.product([0,1,2,3],repeat=N):
    cnt=0
    abc=[0,0,0]
    for j in range(N):
        if i[j] !=3:
            abc[i[j]]+=l[j]
            cnt+=1
    if 0 in abc:
        continue
    _min=min(_min,10*cnt-30+diff(abc[0],abc[1],abc[2]))
print(_min)

Submission status: Accepted

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

ans=10**9

for i in range(4**8):
    mp=0
    a=b=c=0
    ac=bc=cc=0
    for j in range(N):
        f=i&3
        i>>=2
        if f==1:
            a+=L[j]
            ac+=1
        elif f==2:
            b+=L[j]
            bc+=1
        elif f==3:
            c+=L[j]
            cc+=1
    if ac==0 or bc==0 or cc==0: continue
    mp+=10*(ac-1+bc-1+cc-1)
    mp+=abs(A-a)+abs(B-b)+abs(C-c)
    ans=min(ans,mp)

print(ans)

Submission status: Accepted

Solution code:
from itertools import product

def dif(abc):
    return abs(A-abc[0]) + abs(B-abc[1]) + abs(C-abc[2])

N,A,B,C,*l=map(int,open(0).read().split())

_min=10**6
for i in product([0,1,2,3],repeat=N): #3=竹を使わない
    abc=[0,0,0] #各竹の現在値
    cnt=0 #合成回数
    for ii,ll in zip(i,l): #要素番号対応させる。
        if ii != 3:
            abc[ii]+=ll #指定の竹をll分伸ばす
            cnt+=1 
    if 0 in abc:
        continue #ABCは必ず最低1回は含まれないといけない
    _min=min(_min,cnt*10 - 30 + dif(abc))        
print(_min)

Submission status: Accepted

Solution code:
from itertools import product
def main():
    N, A, B, C = map(int, input().split())
    L = [int(input()) for _ in range(N)]
    m = 10**6
    def diff(a, b, c):
        return abs(A - a) + abs(B - b) + abs(C - c)

    for I in product((0, 1, 2, 3), repeat=N):
        abc = [0, 0, 0]
        t = 0
        for i, l in zip(I, L):
            if i != 3:
                abc[i] += l
                t += 1
        if 0 in abc:
            continue
        m = min(m, (t - 3) * 10 + diff(*abc))

    print(m)
main()

Submission status: Accepted

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


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

print(recur(0, 0, 0, 0))

Submission status: Accepted

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


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

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


print(recur(0, 0, 0, 0))

Submission status: Accepted

Solution code:

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

#rec part
def rec(a,b,c,gousei,now):
    global ans

    if(a != 0 and b != 0 and c != 0):
        ans = min(ans,(abs(a-A)+abs(b-B)+abs(c-C)+gousei*10))

        #print(a,b,c,ans) 

    if now == N:
        return 0


    #A
    rec(a,b,c, gousei,now+1)
    rec(a+l[now],b,c,gousei+1 if a!= 0 else gousei,now+1)
    rec(a,b+l[now],c,gousei+1 if b!= 0 else gousei,now+1)
    rec(a,b,c+l[now],gousei+1 if c!= 0 else gousei,now+1)


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

Submission status: Accepted

Solution code:
n,a,b,c = map(int,input().split())
abc = [a,b,c]
l = [int(input()) for i in range(n)]
ans = 10**10

def bfs(use):
    global l,abc,ans
    if len(use)==n:
        mp = 0
        x = [0]*4
        c = [0]*4
        for i in range(n):
            x[use[i]]+=l[i]
            c[use[i]]+=1
        for i in range(3):
            if x[i]==0: 
                mp=10**10
                break
            else :mp += (c[i]-1)*10+abs(abc[i]-x[i])
        if ans>mp: ans=mp
    else:
        bfs(use+[0])
        bfs(use+[1])
        bfs(use+[2])
        bfs(use+[3])

bfs([])
print(ans)

Submission status: Accepted

Solution code:
N,A,B,C = [int(x) for x in input().split()]
l = []
for _ in range(N):
    l.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+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))

Submission status: Accepted

Solution code:
N,A,B,C=map(int, input().split())
l=[int(input()) for i in range(N)]
from itertools import product
p=list(product([0,1,2,3],repeat=N))
t=10000
for i in p:
    a=b=c=ac=0
    for j in range(N):
        if i[j]==1:
            a+=l[j]
            ac+=1
        elif i[j]==2:
            b+=l[j]
            ac+=1
        elif i[j]==3:
            c+=l[j]
            ac+=1
    if a and b and c:
        tt=abs(a-A)+abs(b-B)+abs(c-C)+(ac-3)*10
        if t>tt:
            t=tt
print(t)

Submission status: Accepted

Solution code:
from bisect import *
from collections import *
from fractions import gcd
from math import factorial
from itertools import *
N,A,B,C=map(int,input().split())
L=[int(input()) for i in range(N)]
value=10**10
itelst=[list(permutations(L,i)) for i in range(3,N+1)]
for n in range(N-2):
    for lst in itelst[n]:
        lstsum=sum(lst)
        a=0
        for i1 in range(len(lst)-2):
            a+=lst[i1]
            b=0
            for i2 in range(i1+1,len(lst)-1):
                b+=lst[i2]
                c=lstsum-a-b
                c1,b1,a1=sorted([a,b,c])
                a1=A-a1 if A-a1>0 else a1-A
                b1=B-b1 if B-b1>0 else b1-B
                c1=C-c1 if C-c1>0 else c1-C
                value=min(value, (len(lst)-3)*10 + a1+b1+c1)





print(value)

Submission status: Accepted

Solution code:
N, A, B, C = map(int, input().split())
l = [int(input()) for _ in range(N)]
INF = float('inf')


def dfs(cue, a, b, c):
    if cue == N:
        if min(a, b, c) > 0:  # 全てに竹を使用している場合
            return abs(a - A) + abs(b - B) + abs(c - C) - 30
        else:  # 作られていない竹がある場合
            return INF
    ret0 = dfs(cue + 1, a, b, c)  # 使用しない
    ret1 = dfs(cue + 1, a + l[cue], b, c) + 10  # aに使う
    ret2 = dfs(cue + 1, a, b + l[cue], c) + 10  # bに使う
    ret3 = dfs(cue + 1, a, b, c + l[cue]) + 10  # cに使う
    return min(ret0, ret1, ret2, ret3)


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

Submission status: Accepted

Solution code:
n,a,b,c = map(int, input().split())
l=list(int(input()) for i in range(n))
v=[a,b,c]
v.sort()
ans=10**20
for i in range(4**n):
    x=[0,0,0]
    cnt=0
    for j in range(n):
        p=i%4
        i=i//4
        if p==0:
            if x[0]!=0:
                cnt+=10
            x[0]+=l[j]
        elif p==1:
            if x[1]!=0:
                cnt+=10
            x[1]+=l[j]
        elif p==2:
            if x[2]!=0:
                cnt+=10
            x[2]+=l[j]
        else:
            continue

    x.sort()
    if x[0]==0:
        continue
    else:
        cnt+=abs(x[0]-v[0])+abs(x[1]-v[1])+abs(x[2]-v[2])
        ans=min(cnt,ans)
print(ans)

Submission status: Accepted

Solution code:
import itertools
n,*abc=map(int,input().split())
l=[int(input()) for i in range(n)]
inf=100000000000000
mi=inf
for i in list(itertools.product([i for i in range(3)],repeat=n)):#(1)↓
    sub=[[] for j in range(3)]
    for j in range(n):
        sub[i[j]].append(j)
    mi_sub=0
    for j in range(3):
        l_sub=len(sub[j])
        if l_sub==0:#(2)
            mi_sub=inf
            break
        mi_subsub=inf
        for k in range(2**l_sub):#(3)↓
            mi_subsubsub=[0,0]
            for l_subsub in range(l_sub):#(5)↓
                if ((k>>l_subsub) &1):
                    mi_subsubsub[0]+=1
                    mi_subsubsub[1]+=l[sub[j][l_subsub]]
            if mi_subsubsub[0]!=0:#(4)
                mi_subsub=min(mi_subsub,abs(mi_subsubsub[1]-abc[j])+mi_subsubsub[0]*10-10)
        mi_sub+=mi_subsub
    mi=min(mi,mi_sub)
print(mi)

Submission status: Accepted

Solution code:
def main():
    N, A, B, C = map(int, input().split())
    l = []
    ll = [A, B, C]
    for _ in range(N):
        l.append(int(input()))
    r = 3000
    for i in range(4 ** N):
        t = [[],[],[]]
        for k in reversed(range(N)):
            if i // (4 ** k) == 0:
                t[0].append(l[k])
            elif i // (4 ** k) == 1:
                t[1].append(l[k])
            elif i // (4 ** k) == 2:
                t[2].append(l[k])
            i %= (4 ** k)
        if len(t[0]) == 0 or len(t[1]) == 0 or len(t[2]) == 0:
            continue
        r = min(r, sum(abs(sum(t[k]) - ll[k]) + 10 * (len(t[k]) - 1)for k in range(3)))
    print(r)
main()

Submission status: Accepted

Solution code:
from sys import stdout
printn = lambda x: stdout.write(str(x))
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True  and False
BIG = 999999999
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

n,a,b,c = inm()
l = []
for i in range(n):
    l.append(inn())
mn = BIG
for i in range(4**n):
    la = []
    lb = []
    lc = []
    x = i
    for j in range(n):
        y = x%4
        if y==1:
            la.append(l[j])
        if y==2:
            lb.append(l[j])
        if y==3:
            lc.append(l[j])
        x //= 4
    if len(la)==0 or len(lb)==0 or len(lc)==0:
        continue
    cost = len(la)*10-10 + abs(sum(la)-a) + \
           len(lb)*10-10 + abs(sum(lb)-b) + \
           len(lc)*10-10 + abs(sum(lc)-c)
    mn = min(mn, cost)
    if DBG and cost==110:
        print("la {} lb {} lc {}".format(la,lb,lc))
print(mn)

Submission status: Accepted

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

d=[a,b,c]
d.sort()

mpmin=sum(l)*4+len(l)*10+sum(d)
for i in range(4**n):
    k=[]
    kj=i%4
    k.append(kj)
    b=int((i-kj)/4)
    l0=[]
    l1=[]
    l2=[]
    l3=[]
    if kj==0:
        l0.append(l[0])
    elif kj==1:
        l1.append(l[0])
    elif kj==2:
        l2.append(l[0])
    elif kj==3:
        l3.append(l[0])
        
    for j in range(n-1):
        kj=b%4
        k.append(kj)
        b=int((b-kj)/4)
        if kj==0:
            l0.append(l[j+1])
        elif kj==1:
            l1.append(l[j+1])
        elif kj==2:
            l2.append(l[j+1])
        elif kj==3:
            l3.append(l[j+1])
    mp=0
    if len(l0)>=1:
        mp=mp+(len(l0)-1)*10
    if len(l1)>=1:
        mp=mp+(len(l1)-1)*10
    if len(l2)>=1:
        mp=mp+(len(l2)-1)*10
    ls=[]
    ls=[sum(l0),sum(l1),sum(l2)]
    ls.sort()
    mp=mp+abs(d[0]-ls[0])+abs(d[1]-ls[1])+abs(d[2]-ls[2])    
    if mpmin>mp and len(l0)>=1 and len(l1)>=1 and len(l2)>=1:
        mpmin=mp
print(mpmin)

Submission status: Accepted

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


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


print(recur(0, 0, 0, 0))

Submission status: Accepted

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


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


print(recur(0, 0, 0, 0))

Submission status: Accepted

Solution code:
N, A, B, C = map(int, input().split())
Ln = [int(input()) for _ 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+Ln[cur], b, c) + 10
    ret2 = dfs(cur+1, a, b+Ln[cur], c) + 10
    ret3 = dfs(cur+1, a, b, c+Ln[cur]) + 10

    return min(ret0, ret1, ret2, ret3)


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

Submission status: Accepted

Solution code:
n,a,b,c=map(int,input().split())
l=[int(input()) for i in range(n)]
ans=float('inf')
for i in range(4**n):
  la,lb,lc=[],[],[]
  for j in range(n):
    if i%4==3:
      lc.append(l[j])
    elif i%4==2:
      lb.append(l[j])
    elif i%4==1:
      la.append(l[j])
    i//=4
  if la and lb and lc:
    synthesize=(len(la)-1)*10+(len(lb)-1)*10+(len(lc)-1)*10
    extend_cutdown=abs(sum(la)-a)+abs(sum(lb)-b)+abs(sum(lc)-c)
    ans=min(ans,synthesize+extend_cutdown)
print(ans)

Submission status: Accepted

Solution code:
import itertools

N,A,B,C = map(int,input().split())
L = [int(input()) for i in range(N)]
ans = 10**4
for v in itertools.product([0,1,2,3], repeat=N):
    if 1 not in v:
        continue
    if 2 not in v:
        continue
    if 3 not in v:
        continue
    a,b,c,m = 0,0,0,0
    for i in range(N):
        if v[i] == 1:
            a += L[i]
            m += 10
        if v[i] == 2:
            b += L[i]
            m += 10
        if v[i] == 3:
            c += L[i]
            m += 10
    m += abs(A-a) + abs(B-b) + abs(C-c) -30
    ans = min(ans,m)
print(ans)

Submission status: Accepted

Solution code:
import itertools

N,A,B,C = map(int,input().split())
L = [int(input()) for i in range(N)]
ans = 10**4
for v in itertools.product([0,1,2,3], repeat=N):
    if 1 not in v or 2 not in v or 3 not in v:
        continue
    a = 0
    b = 0
    c = 0
    m = -30
    for i in range(N):
        if v[i] == 1:
            a += L[i]
            m += 10
        if v[i] == 2:
            b += L[i]
            m += 10
        if v[i] == 3:
            c += L[i]
            m += 10
    m += abs(A-a) + abs(B-b) + abs(C-c)
    ans = min(ans,m)
print(ans)

Submission status: Accepted

Solution code:
import sys
from itertools import product
def input(): return sys.stdin.readline().rstrip()

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

    ans = 10 ** 7
    for l_li in product(range(4), repeat = N):
        cost = -30
        a = 0
        b = 0
        c = 0
        for i, li in enumerate(l_li):
            if li > 0:
                cost += 10
                if li == 1:
                    a += l[i]
                elif li == 2:
                    b += l[i]
                elif li == 3:
                    c += l[i]
        cost += abs(A - a) + abs(B - b) + abs(C - c)
        if a * b * c > 0 and ans > cost:
            ans = cost
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N,A,B,C = map(int,input().split())
l = sorted([int(input()) for i in range(N)],reverse=True)
def Base_10_to_n(X, n):
    X_dumy = X
    out = ''
    while X_dumy>0:
        out = str(X_dumy%n)+out
        X_dumy = int(X_dumy/n)
    return out
ans = float('inf')
for i in range(4**N):
    q = Base_10_to_n(i,4)
    q = str('0'*(N-len(q))+q)
    a = []
    b = []
    c = []
    for j in range(N):
        if q[j] == '1':
            a.append(l[j])
        if q[j] == '2':
            b.append(l[j])
        if q[j] == '3':
            c.append(l[j])
    cost = 0
    if a and b and c:
        cost += 10*(len(a)+len(b)+len(c)-3)
        cost += abs(A-sum(a)) + abs(B-sum(b))+ abs(C-sum(c))
        ans = min(ans,cost)
print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

n,a,b,c = map(int,input().split())
la = [a,b,c]
lt = [int(input()) for i in range(n)]

import itertools as it
ans = 3000
for p in it.product(range(4),repeat=n):
  ll = [0]*4
  lc = [0]*4
  for i in range(n):
    ll[p[i]] += lt[i]
    lc[p[i]] += 1
  if lc[0]*lc[1]*lc[2] > 0:
    mp = 10*(sum(lc[:3])-3) + sum(abs(ll[i]-la[i]) for i in range(3))
    ans = min(ans, mp)
print(ans)

Submission status: Accepted

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

import itertools as it
ans = 3000
for p in it.product(range(4),repeat=n):
  ll = [0]*4
  lc = [0]*4
  for i in range(n):
    ll[p[i]] += lt[i]
    lc[p[i]] += 1
  if lc[0]*lc[1]*lc[2] > 0:
    mp = 10*(sum(lc[:3])-3) + sum(abs(ll[i]-la[i]) for i in range(3))
    ans = min(ans, mp)
print(ans)

Submission status: Accepted

Solution code:
import itertools
n,a,b,c=map(int,input().split())
l=[int(input()) for i in range(n)]
l.sort()
ll=[i for i in range(1,n+1)]
x=[]
for i in range(1,n+1):
    z=list(itertools.combinations(l,i))
    zz=list(itertools.combinations(ll,i))
    for j in range(len(z)):
        x.append([sum(z[j]),(i-1)*10,list(zz[j])])
x.sort()
A=[];B=[];C=[]
for i in range(len(x)):
    A.append([x[i][1]+abs(a-x[i][0]),x[i][2]])
    B.append([x[i][1]+abs(b-x[i][0]),x[i][2]])
    C.append([x[i][1]+abs(c-x[i][0]),x[i][2]])
A.sort();B.sort();C.sort()
m=len(A)
ans=10**10
for i in range(m):
    for j in range(m):
        for k in range(m):
            if len(A[i][1]+B[j][1]+C[k][1])>=n+1:
                break
            elif len(A[i][1]+B[j][1]+C[k][1])==len((set(A[i][1]+B[j][1]+C[k][1]))):
                ans=min(ans,A[i][0]+B[j][0]+C[k][0])
print(ans)

Submission status: Accepted

Solution code:
import itertools
n,a,b,c=map(int,input().split())
l=[int(input()) for i in range(n)]
l.sort()
ll=[i for i in range(1,n+1)]
x=[]
for i in range(1,n+1):
    z=list(itertools.combinations(l,i))
    zz=list(itertools.combinations(ll,i))
    for j in range(len(z)):
        x.append([sum(z[j]),(i-1)*10,list(zz[j])])
x.sort()
#print(x)
A=[];B=[];C=[]
for i in range(len(x)):
    A.append([x[i][1]+abs(a-x[i][0]),x[i][2]])
    B.append([x[i][1]+abs(b-x[i][0]),x[i][2]])
    C.append([x[i][1]+abs(c-x[i][0]),x[i][2]])
A.sort();B.sort();C.sort()
#print(A)
#print(B)
#print(C)
m=len(A)
ans=float('inf')
for i in range(m):
    for j in range(m):
        for k in range(m):
            if len(A[i][1]+B[j][1]+C[k][1])>=9:
                break
            elif len(A[i][1]+B[j][1]+C[k][1])==len((set(A[i][1]+B[j][1]+C[k][1]))):
                ans=min(ans,A[i][0]+B[j][0]+C[k][0])
print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.buffer.readline

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

A, B, C = sorted([A, B, C])

answer = 10 ** 15
for i in range(4 ** N):
  x =[0] * 3
  tmp = 0
  for j in range(N):
    index = (i // (4 ** j)) % 4
    if index != 3:
      if x[index]:
        tmp += 10
      x[index] += L[j]
  x.sort()
  if x[0] != 0:
    tmp += abs(x[0] - A) + abs(x[1] - B) + abs(x[2] - C)
    answer = min(answer, tmp)

print(answer)

Submission status: Accepted

Solution code:
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 a < 1 or b < 1 or c < 1:
            return 10 ** 18
        return abs(A-a) + abs(B-b) + abs(C-c)
    return min([
        dfs(i + 1, a, b, c),
        dfs(i + 1, a + l[i], b, c) + (10 if a > 0 else 0),
        dfs(i + 1, a, b + l[i], c) + (10 if b > 0 else 0),
        dfs(i + 1, a, b, c + l[i]) + (10 if c > 0 else 0)
    ])


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

Submission status: Accepted

Solution code:
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 < 1 or b < 1 or c < 1:
            return 10 ** 18
        return abs(A - a) + abs(B - b) + abs(C - c)
    return min([
        dfs(i + 1, a, b, c),
        dfs(i + 1, a + l[i], b, c) + (10 if a > 0 else 0),
        dfs(i + 1, a, b + l[i], c) + (10 if b > 0 else 0),
        dfs(i + 1, a, b, c + l[i]) + (10 if c > 0 else 0)])


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

Submission status: Accepted

Solution code:
N, A, B, C = map(int, input().split())
l = [int(input()) for _ 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))

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
# 各竹に対して使わない,A,B,Cに使う(0123)を割り当てて,合成してから延長短縮する
N,A,B,C=map(int,input().split())
L=[int(input()) for i in range(N)]

def bf(m):
    ret=[]
    while m!=0:
        ret.append(m%4)
        m=m//4
    return ret+[0]*(N-len(ret))

ans=10000
comp=[A,B,C]
for i in range(4**N): # 4進数で列挙
    ch=[0,0,0] # 2本以上あるかchecker
    tmp=[0,0,0] # 合成MPの保存場所
    tgt=[A,B,C] # 合成して作った竹との差を保存
    for j,l in zip(bf(i),L): # はじめに全て合成する
        if j==0: # 0なら使わない
            continue
        else:
            tgt[j-1]-=l # 目標値から引く
            if ch[j-1]: # 2本以上なら
                tmp[j-1]+=10 # 合成MP
            else:
                ch[j-1]+=1 # 初めて
    flag=True # 竹が作れない場合も含まれていて、そのための旗
    for l,com in zip(tgt,comp):
        if l==com: # 変わってない -> 何もひかれてないので竹は作れない
            flag=False
            break
    if flag:
        ans=min(ans,sum(abs(x) for x in tgt)+sum(tmp)) # xが目標値より大きいこともありうる
print(ans)

Submission status: Accepted

Solution code:
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 < 1 or b < 1 or c < 1:
            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))

Submission status: Accepted

Solution code:
# ABC119 Synthetic Kadomatsu
わからん=True
INF=1<<30

n,a,b,c=map(int,input().split())
# 8>=n>=3
# 1000>=a>b>c>=1
l=[int(input()) for _ in range(n)]
# 1000>=li>=1

def dfs(j,x,y,z):
    if n==j:
        return abs(a-x)+abs(b-y)+abs(c-z)-30 if min(x,y,z)>0 else INF
        
    t1 = dfs(j+1,x,y,z)
    t2 = dfs(j+1,x+l[j],y,z)+10
    t3 = dfs(j+1,x,y+l[j],z)+10
    t4 = dfs(j+1,x,y,z+l[j])+10
    return min(t1,t2,t3,t4)
    
ans=dfs(0,0,0,0)
print(ans)