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:
def min_mp(bls, A, B, C, a_used=False, b_used=False, c_used=False):
    if sum([a_used, b_used, c_used]) == 3:
        return 0
    else:
        min_val = 10**9  # large number
        non_used_num = 3 - sum([a_used, b_used, c_used])
        if len(bls) - 1 >= non_used_num:
            min_val = min([
                min_val,
                min_mp(bls[1:], A, B, C, a_used, b_used, c_used)
            ])
        if len(bls) > 0:
            if not a_used:
                min_val = min([
                    min_val,
                    abs(bls[0] - A) + min_mp(bls[1:], A, B, C, not a_used, b_used, c_used),
                    10 + min_mp(bls[1:], A - bls[0], B, C, a_used, b_used, c_used)
                ])
            if not b_used:
                min_val = min([
                    min_val,
                    abs(bls[0] - B) + min_mp(bls[1:], A, B, C, a_used, not b_used, c_used),
                    10 + min_mp(bls[1:], A, B - bls[0], C, a_used, b_used, c_used)
                ])
            if not c_used:
                min_val = min([
                    min_val,
                    abs(bls[0] - C) + min_mp(bls[1:], A, B, C, a_used, b_used, not c_used),
                    10 + min_mp(bls[1:], A, B, C - bls[0], a_used, b_used, c_used)
                ])
        return min_val


def main():
    N, A, B, C = list(map(int, input().split(' ')))
    bls = [int(input()) for _ in range(N)]  # bamboo length
    print(min_mp(bls, A, B, C))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

def dfs(cur, a,b,c):
  if cur == N:
    return(abs(a-A) + abs(b-B) + abs(c-C) - 30 if min(a,b,c) > 0 else float("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 _ 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)-30
    four = dfs(i+1, a, b, c)
    one = dfs(i+1, a+l[i], b, c) + 10
    two = dfs(i+1, a, b+l[i], c) + 10
    three = dfs(i+1, a, b, c+l[i]) + 10
    return min(one, two, three, four)
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)]
y=[0]*n
x=[]
def dfs(n,y,now):
    if now==n:
        z=y.copy()
        x.append(z)
        return
    for i in range(4):
        y[now]=i
        dfs(n,y,now+1)
dfs(n,y,0)
mi=1000000
for i in range(len(x)):
    al,bl,cl=0,0,0
    ac,bc,cc=0,0,0
    for j in range(n):
        if x[i][j]==1:
            al+=l[j]
            ac+=1
        if x[i][j]==2:
            bl+=l[j]
            bc+=1
        if x[i][j]==3:
            cl+=l[j]
            cc+=1
    if ac!=0 and bc!=0 and cc!=0:
        ans=abs(a-al)+abs(b-bl)+abs(c-cl)+10*(ac+bc+cc-3)
        mi=min(mi,ans)
print(mi)

Submission status: Accepted

Solution code:
n,a,b,c=map(int,input().split())
l=[]
for i in range(n):
  l.append(int(input()))
  
import itertools as it
pattern=list(it.product(range(4),repeat=n))

keep=10**100
for i in range(0,len(pattern)):
  kep1=0
  kep2=0
  kep3=0
  mp=0
  liis=pattern[i]
  for j in range(0,n):
    if liis[j]==1:
      if kep1==0:
        kep1=l[j]
      else:
        kep1+=l[j]
        mp+=10
    elif liis[j]==2:
      if kep2==0:
        kep2=l[j]
      else:
        kep2+=l[j]
        mp+=10
    elif liis[j]==3:
      if kep3==0:
        kep3=l[j]
      else:
        kep3+=l[j]
        mp+=10
  if kep1==0 or kep2==0 or kep3==0:
    continue
  mp+=abs(kep1-a)+abs(kep2-b)+abs(kep3-c)
  if mp<keep:
    keep=mp
print(keep)

Submission status: Accepted

Solution code:
N, A, B, C, *X = map(int, open(0).read().split())

def dfs(i, a, b, c):
    if i == N:
        if a * b * c == 0:
            return 10 ** 9 + 7
        return abs(A - a) + abs(B - b) + abs(C - c)

    v0 = dfs(i + 1, a, b, c)
    v1 = dfs(i + 1, a + X[i], b, c) + int(a > 0) * 10
    v2 = dfs(i + 1, a, b + X[i], c) + int(b > 0) * 10
    v3 = dfs(i + 1, a, b, c + X[i]) + int(c > 0) * 10

    return min(v0, v1, v2, v3)


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

res2=1000000000

for i in range(2**n-1):
	for j in range(2**n-1):
		x=[]
		y=[]
		z=[]
		for k in range(n):
			
			if i>>k&1:
				if j>>k&1:
					x.append(L[k])
				else:
					y.append(L[k])
			else:
				if j>>k&1:
					z.append(L[k])
		res1=abs(sum(x)-a)+abs(sum(y)-b)+abs(sum(z)-c)
		
		if len(x)==0 or len(y)==0 or len(z)==0:
			continue
	
		if len(x)>1:
			res1+=10*(len(x)-1)
		if len(y)>1:
			res1+=10*(len(y)-1)
		if len(z)>1:
			res1+=10*(len(z)-1)				
		res2=min(res1,res2)

print(res2)

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

from itertools import product
from collections import Counter

n, a, b, c, *l = map(int, read().split())
ans = float('inf')
for bit in product([0, 1, 2, 3], repeat=n):
    if 0 not in bit or 1 not in bit or 2 not in bit:
        continue
    counter = Counter()
    memo = 0
    for ll, num in zip(l, bit):
        if num == 3:
            continue
        if counter[num] != 0:
            memo += 10
        counter[num] += ll
    ans = min(ans, abs(counter[0] - a) + abs(counter[1] - b) + abs(counter[2] - c) + memo)
print(ans)

Submission status: Accepted

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

def dfs(cnt, x, y, z):
    if cnt == n:
        if x == 0 or y == 0 or z == 0:
            return float('INF')
        return abs(a - x) + abs(b - y) + abs(c - z) - 30
    r0 = dfs(cnt + 1, x, y, z)
    r1 = dfs(cnt + 1, x + ll[cnt], y, z) + 10
    r2 = dfs(cnt + 1, x, y + ll[cnt], z) + 10
    r3 = dfs(cnt + 1, x, y, z + ll[cnt]) + 10
    return min(r0, r1, r2, r3)

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

Submission status: Accepted

Solution code:
from itertools import product 
n,a,b,c = map(int,input().split())
l = [int(input()) for _ in range(n)]
tar = [a,b,c]
ans = float("inf")
for p in product(range(4),repeat=n):
    mp = 0
    length = [0]*3
    for i,j in enumerate(p):
        if j == 3: continue
        if length[j] > 0: mp += 10
        length[j] += l[i]
    for j in range(3):
        if length[j] == 0: break
        mp += abs(length[j]-tar[j])
    else:
        ans = min(ans, mp)
print(ans)

Submission status: Accepted

Solution code:
# dfs版
def dfs(d, A, B, C, f):
    global ans
    if d == n:
        if not 0 in [A, B, C]:
            ans = min(ans, abs(a - A) + abs(b - B) + abs(c - C) + f - 30)
        return
    dfs(d + 1, A, B, C, f)
    dfs(d + 1, A + l[d], B, C, f + 10)
    dfs(d + 1, A, B + l[d], C, f + 10)
    dfs(d + 1, A, B, C + l[d], f + 10)


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

Submission status: Accepted

Solution code:
from itertools import product

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

ans = 10 ** 10
for bit in product([0, 1, 2, 3], repeat=n):
    tmp = [0] * 4
    mp = 0
    for i, j in enumerate(bit):
        if j != 3 and tmp[j] != 0:
            mp += 10
        tmp[j] += l[i]
    if any([i == 0 for i in tmp[:3]]):
        continue
    mp += abs(a - tmp[0]) + abs(b - tmp[1]) + abs(c - tmp[2])
    ans = min(ans, mp)
print(ans)

Submission status: Accepted

Solution code:
from itertools import product
from collections import Counter as ct

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

ans = 10 ** 10
for bit in product([0, 1, 2, 3], repeat=n):
    if len(set(bit) - {0}) != 3:
        continue
    tmpa = 0
    tmpb = 0
    tmpc = 0
    for i, j in enumerate(bit):
        if j == 1:
            tmpa += l[i]
        elif j == 2:
            tmpb += l[i]
        elif j == 3:
            tmpc += l[i]
    mp = abs(a - tmpa) + abs(b - tmpb) + abs(c - tmpc)
    tmp = ct(bit)
    for i in range(1, 4):
        mp += max(0, tmp[i] - 1) * 10
    ans = min(ans, mp)
print(ans)

Submission status: Accepted

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

def DFS(cnt, a, b, c):
    if cnt == N:
        # print(cnt, a, b, c)
        if a == 0 or b == 0 or c == 0:
            return 10 ** 10
        return abs(A-a) + abs(B-b) + abs(C-c) - 30

    r1 = DFS(cnt+1, a, b, c)
    r2 = DFS(cnt+1, a+l[cnt], b, c) + 10
    r3 = DFS(cnt+1, a, b+l[cnt], c) + 10
    r4 = DFS(cnt+1, a, b, c+l[cnt]) + 10
    return min(r1, r2, r3, r4)

print(DFS(0, 0, 0, 0))

Submission status: Accepted

Solution code:
# Python3 (3.4.3)
import sys
input = sys.stdin.readline

# -------------------------------------------------------------
# function
# -------------------------------------------------------------


# -------------------------------------------------------------
# main
# -------------------------------------------------------------
N,A,B,C = map(int,input().split())
L = [int(input()) for _ in range(N)]
 
from itertools import product
ans = float("inf")

# 全パターン試す
# 0 : 使わない
# 1,2,3 : A,B,Cとして使う
for p in product(range(4),repeat=N):
    a = [L[i] for i in range(N) if p[i]==1]
    b = [L[i] for i in range(N) if p[i]==2]
    c = [L[i] for i in range(N) if p[i]==3]
 
    if a==[] or b==[] or c==[]:
        continue
    
    t = 0
    for x in [a,b,c]:
        # 複数要素あるなら合成魔法を使って1つに
        if len(x)>=2:
            t += (len(x)-1)*10
    sumA = sum(a)
    sumB = sum(b)
    sumC = sum(c)
    # 目標との差分を 延長魔法or短縮魔法 で埋める
    t += abs(A-sumA) + abs(B-sumB) + abs(C-sumC)
 
    ans = min(ans,t)
 
print(ans)

Submission status: Accepted

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

def calc(a, b, c):
    la, lb, lc = len(a), len(b), len(c)
    if la + lb + lc < 3 or 0 in (la, lb, lc): return INF
    sa, sb, sc = sum(a), sum(b), sum(c)
    t = 0
    return abs(sa-A) + abs(sb-B) + abs(sc-C) + (la+lb+lc-3) * 10

def dfs(n, a, b, c):
    global ans
    if n == N:
        ans = min(ans, calc(a, b, c))
        return
    dfs(n+1, a, b, c)
    dfs(n+1, a+[l[n]], b, c)
    dfs(n+1, a, b+[l[n]], c)
    dfs(n+1, a, b, c+[l[n]])

dfs(0, [], [], [])
print(ans)

Submission status: Accepted

Solution code:
from itertools import product
N, A, B, C = map(int, input().split())
l = [int(input()) for i in range(N)]
INF = 10 ** 9
t1 = set([0,1,2,3])
t2= set([1,2,3])
def calc(t):
    t3 = set(t)
    if not (t3 == t1 or t3 == t2): return INF
    a, b, c = 0, 0, 0
    cnt = 0
    for i, v in enumerate(t):
        if v != 0: cnt += 1
        if v == 1: a += l[i]
        elif v == 2: b += l[i]
        elif v == 3: c += l[i]
    return abs(a-A) + abs(b-B) + abs(c-C) + (cnt-3) * 10

ans = 10 ** 9
# 4進数の配列を生成する
# 0:未使用,1:aに使用,2:bに使用,3:cに使用
for p in product(range(4), repeat=N):
    ans = min(ans, calc(p))

print(ans)

Submission status: Accepted

Solution code:
from itertools import product
N, A, B, C = map(int, input().split())
l = [int(input()) for i in range(N)]
INF = 10 ** 9
t1 = set([0,1,2,3])
t2= set([1,2,3])
def calc(t):
    t3 = set(t)
    if not (t3 == t1 or t3 == t2): return INF
    a, b, c = 0, 0, 0
    cnt = 0
    for i, v in enumerate(t):
        if v != 0: cnt += 1
        if v == 1: a += l[i]
        elif v == 2: b += l[i]
        elif v == 3: c += l[i]
    return abs(a-A) + abs(b-B) + abs(c-C) + max((cnt-3) * 10, 0)


ans = 10 ** 9
for p in product(range(4), repeat=N):
    ans = min(ans, calc(p))

print(ans)

Submission status: Accepted

Solution code:
from itertools import product
N, A, B, C = map(int, input().split())
l = [int(input()) for i in range(N)]
INF = 10 ** 9
t1 = set([0,1,2,3])
t2= set([1,2,3])
def calc(t):
    t3 = set(t)
    if not (t3 == t1 or t3 == t2): return INF
    a, b, c = 0, 0, 0
    cnt = 0
    for i, v in enumerate(t):
        if v != 0: cnt += 1
        if v == 1: a += l[i]
        elif v == 2: b += l[i]
        elif v == 3: c += l[i]
    return abs(a-A) + abs(b-B) + abs(c-C) + max((cnt-3) * 10, 0)


ans = 10 ** 9
for p in product(range(2), repeat=N*2):
    t = [0] * N
    for i in range(N):
        for j in range(2):
            t[i] += p[i*2+j] * (2 ** j)
    #print(t)
    ans = min(ans, calc(t))

print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n,a,b,c = list(map(int,input().split()))
l = [int(input()) for i in range(n)]

#a,b,cに使うベースの竹を選ぶ
from itertools import permutations,product
inf=float("inf")
min_mp = inf
for k in product(range(4),repeat=n):
    if 2 not in k or 1 not in k or 0 not in k:
        continue
    len_abc = [0,0,0]
    mp = - 30
    for i,v in enumerate(k):
        if v == 3:
            pass
        else:
            len_abc[v]+=l[i]
            mp += 10
    mp += (abs(a-len_abc[0])+abs(b-len_abc[1])+abs(c-len_abc[2]))
    min_mp = min(min_mp,mp)
print(min_mp)

Submission status: Accepted

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

s_list = [a, b, c]

ans = 10000
for i in range(4 ** n):
    mp = 0
    temp_l_list = [0, 0, 0]
    
    for j in range(n):
        temp = (i >> (2 * j)) & 3
        if temp == 3:
            continue

        if temp_l_list[temp] != 0:
            mp += 10
        temp_l_list[temp] += l_list[j]

    for s, temp_l in zip(s_list, temp_l_list):
        if temp_l == 0:
            break
        mp += abs(s - temp_l)
    else:
        if ans > mp:
            ans = mp
print(ans)

Submission status: Accepted

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

from itertools import product
ptns = list(product([0,1,2,-1], repeat = n))

ans = 10**9
for i in range(len(ptns)):
    if 0 not in ptns[i] or 1 not in ptns[i] or 2 not in ptns[i]:
        continue
    abc = [0, 0, 0, -30]
    for j in range(n):
        tmp = ptns[i][j]
        if tmp >= 0:
            abc[tmp] += l[j]
            abc[3] += 10
    mp = abc[3]
    mp += abs(abc[0] - a)
    mp += abs(abc[1] - b)
    mp += abs(abc[2] - c)
    ans = min(ans, mp)

print(ans)

Submission status: Accepted

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

from itertools import product
ptns = list(product([0,1,2,-1], repeat = n))

anss = []
for i in range(len(ptns)):
    if 0 not in ptns[i] or 1 not in ptns[i] or 2 not in ptns[i]:
        continue
    abc = [0, 0, 0, -30]
    ans = 10**9
    for j in range(n):
        tmp = ptns[i][j]
        if tmp >= 0:
            abc[tmp] += l[j]
            abc[3] += 10

    anss.append(abc[3] + abs(abc[0] - a) + abs(abc[1] - b) + abs(abc[2] - c))

print(min(anss))

Submission status: Accepted

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

from itertools import product
ptns = list(product([0,1,2,-1], repeat = n))

ans = 10**9
for i in range(len(ptns)):
    if 0 not in ptns[i] or 1 not in ptns[i] or 2 not in ptns[i]:
        continue
    abc = [0, 0, 0, -30]
    for j in range(n):
        tmp = ptns[i][j]
        if tmp >= 0:
            abc[tmp] += l[j]
            abc[3] += 10

    ans = min(ans, abc[3] + abs(abc[0] - a) + abs(abc[1] - b) + abs(abc[2] - c))

print(ans)

Submission status: Accepted

Solution code:

def minMP(length,LIST):
    k=len(LIST)
    LONG=[]
    if k==0:
        return -1
    for i in range(1,2**k):
        long=0
        cnt=-1
        for j in range(k):
            mod=i%2
            if mod==1:
                long+=LIST[j]
                cnt+=1
            i//=2
        LONG.append([long,10*cnt])
    
    ans=float("inf")
    for ll,mp in LONG:
        ans=min(ans,mp+abs(ll-length))
    return ans 

N,a,b,c=map(int,input().split())
L=[]
Ans=float("inf")
for i in range(N):
    l=int(input())
    L.append(l)

for n in range(3**N):
    L_A=[];L_B=[];L_C=[]
    for i in range(N):
        mod=n%3
        n//=3
        if mod==0:
            L_A.append(L[i])
        elif mod==1:
            L_B.append(L[i])
        else:
            L_C.append(L[i])
    MP_A=minMP(a,L_A)
    MP_B=minMP(b,L_B)
    MP_C=minMP(c,L_C)
    
    if MP_A==-1 or MP_B==-1 or MP_C==-1:
        continue
    else:
        Ans=min(Ans,MP_A+MP_B+MP_C)

print(Ans)

Submission status: Accepted

Solution code:
def abc119_c():
  n, a, b, c = map(int, input().split())
  L = [int(input()) for _ in range(n)]
  ans = 10**9

  def dfs(arr):
    if len(arr) == n:
      # ABCともに材料が1つ以上あるなら得点計算する
      if arr.count(1) > 0 and arr.count(2) > 0 and arr.count(3) > 0:
        calc_score(arr)
      return
    for i in range(4):
      dfs(arr + [i])

  def calc_score(arr):
    nonlocal ans
    # パターンにあわせて棒を用意する
    tools_a = [L[i] for i in range(n) if arr[i] == 1]
    tools_b = [L[i] for i in range(n) if arr[i] == 2]
    tools_c = [L[i] for i in range(n) if arr[i] == 3]
    # 得点計算して答えを更新
    score = magic(tools_a, a)
    score += magic(tools_b, b)
    score += magic(tools_c, c)
    ans = min(score, ans)

  def magic(tools, target):
    # 延長短縮で長さ合わせにいく
    pt = abs(target - sum(tools))
    # 棒が複数あると合成コスト追加、棒の数マイナス1
    if len(tools) >= 2:
      pt += 10 * (len(tools) - 1)
    return pt

  dfs([])
  print(ans)

abc119_c()

Submission status: Accepted

Solution code:
from itertools import permutations
from copy import deepcopy
from collections import Counter

n,a,b,c = map(int,input().split())
take = []
for _ in range(n):
	take.append(int(input()))
tmp = deepcopy(take)
ans_list = []

sum_abc = 10 ** 5
for abc in permutations([a,b,c],3):
	take = deepcopy(tmp)
	ans = 0
	l = []
	l2 = []
	s = Counter(take)
	#print(s)
	for x in abc:
		res = []
		l2 = []
		take = []
		for i,num in s.items():
			for j in range(num):
				take.append(i)
		#take = list(s)
		for i in range(2**n):
			l = []
			for j in range(len(take)):
					if i & (1<<j):
						l.append(take[j])
			if len(l) == 0:
				continue
			res.append(10*(len(l)-1) + abs(sum(l) - x))
			l2.append(l)
		minimum = sum_abc
		for i in range(len(res)):
			if minimum > res[i]:
				minimum = res[i]
				s2 = l2[i]
		for i in range(len(s2)):
			s[s2[i]] -= 1
			if s[s2[i]] == 0:
				s.pop(s2[i])
		#s = s - s2
		ans += minimum
		#print(res,s,minimum)
	ans_list.append(ans)

print(min(ans_list))

Submission status: Accepted

Solution code:
n,a,b,c = map(int,input().split())
L = []
for i in range(n):
    L.append(int(input()))
import itertools
ans = 10**9
tmp = 0
for use in itertools.product([0,1,2,-1] ,repeat=n):
    tmp = 0
    ls = [0,0,0]
    for idx,u in enumerate(use):
        if u>-1:
            if ls[u] >0:
                tmp+=10
            ls[u]+=L[idx]

    if ls[0] ==0 or ls[1] ==0 or ls[2] ==0:continue
    tmp = tmp + abs(ls[0] -a) +abs(ls[1] -b)+abs(ls[2] -c)
    ans = min(tmp,ans)
print(ans)

Submission status: Accepted

Solution code:
n,a,b,c = map(int,input().split())
L = []
for i in range(n):
    L.append(int(input()))
import itertools
ans = 10**9
tmp = 0
for perm in itertools.permutations(range(n),3):
    ua,ub,uc = perm
    la,lb,lc = L[ua],L[ub],L[uc]
    for use in itertools.product([0,1,2,-1] ,repeat=n-3):
        idx = 0
        tmp = 0
        ls = [la,lb,lc]
        for i,l in enumerate(L):
            if i== ua or  i== ub or i== uc:
                continue
            else:
                if use[idx]>-1:
                    ls[use[idx]] +=l
                    tmp+=10
            idx+=1
        tmp = tmp + abs(ls[0] -a) +abs(ls[1] -b)+abs(ls[2] -c)
        ans=min(ans,tmp)
print(ans)

Submission status: Accepted

Solution code:
n,A,B,C = map(int, input().split())
l=[]
for i in range(n):
  l.append(int(input()))
# 1,1,10
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**12
  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 = []
for i in range(n):
    l.append(int(input()))
from itertools import product
bamboo = [0, a, b, c]
min_num = float('inf')
for i in product([0, 1, 2, 3], repeat=n):
    if set(i) >= set([1, 2, 3]):
        sums = [0, 0, 0, 0]
        tmp = -30
        for j in range(n):
            if i[j] != 0:
                sums[i[j]] += l[j]
                tmp += 10
        min_num = min(min_num,tmp+sum(abs(sums[k] - bamboo[k]) for k in range(1, 4)))
print(min_num)

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:
def main():
    N, A, B, C = (int(_) for _ in input().split())
    L = [int(input()) for _ in range(N)]
    INF = 10 ** 12

    def dfs(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
        c0 = dfs(i+1, a, b, c)
        c1 = dfs(i+1, a+L[i], b, c) + 10
        c2 = dfs(i+1, a, b+L[i], c) + 10
        c3 = dfs(i+1, a, b, c+L[i]) + 10
        return min(c0, c1, c2, c3)

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

def dfs(i, cnt, 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) + 10 * cnt
    MP = INF
    if a == 0:
        MP = min(MP, dfs(i+1, cnt, a+L[i], b, c))
    else:
        MP = min(MP, dfs(i+1, cnt+1, a+L[i], b, c))
    if b == 0:
        MP = min(MP, dfs(i+1, cnt, a, b+L[i], c))
    else:
        MP = min(MP, dfs(i+1, cnt+1, a, b+L[i], c))
    if c == 0:
        MP = min(MP, dfs(i+1, cnt, a, b, c+L[i]))
    else:
        MP = min(MP, dfs(i+1, cnt+1, a, b, c+L[i]))
    MP = min(MP, dfs(i+1, cnt, a, b, c))
    
    return MP

print(dfs(0, 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 = 100000000
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)
    ret0 = rec(i+1,a,b,c)
    ret1 = rec(i+1,a+l[i],b,c)+10
    ret2 = rec(i+1,a,b+l[i],c)+10
    ret3 = rec(i+1,a,b,c+l[i])+10
    return min(ret0,ret1,ret2,ret3)
print(rec(0,0,0,0))

Submission status: Accepted

Solution code:
MOD = 10 ** 9 + 7
INF = 10 ** 11
import sys
sys.setrecursionlimit(100000000)

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

ans = INF
def dfs(i,before,score):
    global ans
    if i == N:
        if all(length > 0  for length in before):
            x,y,z = before
            for _ in range(3):
                ans = min(ans,abs(x - A) + abs(y - B) + abs(z - C) + score - 30)
                x,y,z = y,z,x
    else:
        dfs(i + 1,before,score)
        for j in range(3):
            before[j] += L[i]
            dfs(i + 1,before,score + 10)
            before[j] -= L[i]
dfs(0,[0,0,0],0)
print(ans)

Submission status: Accepted

Solution code:

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

inf=10**9

def dfs(cur,aa,bb,cc):
    if cur==n:
        if min(aa,bb,cc)>0:
            return abs(a-aa)+abs(b-bb)+abs(c-cc)-30
        else:
            return inf
    ret0=dfs(cur+1,aa,bb,cc)
    ret1=dfs(cur+1,aa+l[cur],bb,cc)+10
    ret2=dfs(cur+1,aa,bb+l[cur],cc)+10
    ret3=dfs(cur+1,aa,bb,cc+l[cur])+10
#    print(cur,l[cur],aa,bb,cc,ret0,ret1,ret2,ret3)
    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=[0]*n
for i in range(n):
    l[i]=int(input())
    
import itertools
 
mpmin=a+b+c+1
for i in itertools.product([0,1,2,3], repeat=n):
    aa=0
    bb=0
    cc=0
    mpa=0
    mpb=0
    mpc=0
    ia=0
    ib=0
    ic=0
    for ii in range(n):
        if i[ii]==1:
            ia=1
            if aa>0:
                mpa+=10
            aa+=l[ii]
        elif i[ii]==2:
            ib=1
            if bb>0:
                mpb+=10
            bb+=l[ii]
        elif i[ii]==3:
            ic=1
            if cc>0:
                mpc+=10
            cc+=l[ii]
    if ia==1 and ib==1 and ic==1:
        mp=mpa+abs(a-aa)+mpb+abs(b-bb)+mpc+abs(c-cc)
#        if mp<mpmin:
#            print(mp,mpa,mpb,mpc,i)
        mpmin=min(mp,mpmin)

print(mpmin)

Submission status: Accepted

Solution code:
# C - Synthetic Kadomatsu
'''
100, 90, 80
98, 80, 40, 30, 21
L[0]〜L[N-1]がそれぞれ、A,B,C,使わないのいずれかかを考える。
4**8=65536
'''
import itertools
INF = 10**18
N, A, B, C = map(int, input().split())
L = [0] * N
for i in range(N):
    L[i] = int(input())
    
c = (0, 1, 2, 3) # 3は使わない
ans = INF

for comb in itertools.product(c,repeat=N):
    tmp = [0]*4 
    ansArr = [0]*3
    #print(comb)
    for i, j in enumerate(comb):
        if tmp[j]==0:
            tmp[j] += L[i]
        elif j!=3:
            tmp[j] += L[i]
            ansArr[j] += 10
    if tmp[0]==0 or tmp[1]==0 or tmp[2]==0:
        continue
    ansArr[0] += abs(A-tmp[0])
    ansArr[1] += abs(B-tmp[1])        
    ansArr[2] += abs(C-tmp[2])
    #print(tmp)
    #print(ansArr)
    ans = min(ans, sum(ansArr))
    #print(ans)

print(ans)

Submission status: Accepted

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

def base_10_to_n(i, n):
    if i//n:
        return base_10_to_n(i//n, n) + str(i % n)
    return str(i % n)

def main():
    N, A, B, C = map(int, readline().rstrip().split())
    l = [int(readline()) for _ in range(N)]
    ans = 10**6
    for i in range(4**N):
        b = base_10_to_n(i, 4).zfill(N)
        if '1' not in b or '2' not in b or '3' not in b:
            continue
        ai = [l[j] for j, e in enumerate(b) if e == '1']
        bi = [l[j] for j, e in enumerate(b) if e == '2']
        ci = [l[j] for j, e in enumerate(b) if e == '3']
        cost = (len(ai)+len(bi)+len(ci)-3) * 10 + abs(A-sum(ai)) + abs(B-sum(bi)) + abs(C-sum(ci))
        ans = min(ans, cost)

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import math
# N, A, B, C = 7, 100, 90, 80
# ARR = [
#     98,
#     40,
#     30,
#     21,
#     80,
#     25,
#     100
# ]

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


def dfs(deep, crr):
    if deep == N:
        ok, res = calculate(crr)
        if ok:
            result.append(int(res))
    else:
        for i in range(4):
            crr[deep] = i
            dfs(deep + 1, crr)


def calculate(crr):
    sA = []
    sB = []
    sC = []
    sN = []
    for index, cr in enumerate(crr):
        if cr == 0:
            sN.append(ARR[index])
        if cr == 1:
            sA.append(ARR[index])
        if cr == 2:
            sB.append(ARR[index])
        if cr == 3:
            sC.append(ARR[index])

    if len(sA) == 0:
        return False, -1
    if len(sB) == 0:
        return False, -1
    if len(sC) == 0:
        return False, -1


    s1 = (len(sA) - 1) * 10 + math.fabs(sum(sA) - A)
    s2 = (len(sB) - 1) * 10 + math.fabs(sum(sB) - B)
    s3 = (len(sC) - 1) * 10 + math.fabs(sum(sC) - C)

    return True, s1 + s2 + s3


dfs(0, [0 for i in range(N)])

print(min(result))

Submission status: Accepted

Solution code:
#template
from collections import Counter
def inputlist(): return [int(k) for k in input().split()]
def base10to(n, b):
    if (int(n/b)):
        return base10to(int(n/b), b) + str(n%b)
    return str(n%b)
#template
ans = 10**18
N,A,B,C = inputlist()
l = [0]*N
for i in range(N):
    l[i] = int(input())

n = 4**N

def count(li,n):
    co = 0
    co += (len(li)-1)*10
    co += abs(sum(li) - n)
    return co

for i in range(n+1):
    k = base10to(i,4)
    if len(k) < N:
        d = N-len(k)
        for j in range(d):
            k = '0'+k
    c = Counter(list(k))
    if c['1'] < 1 or c['2'] < 1 or c['3'] < 1:
        continue
    ans_k = 0
    la = []
    lb = []
    lc = []
    for j in range(N):
        if k[j] == '1':
            la.append(l[j])
        if k[j] == '2':
            lb.append(l[j])
        if k[j] == '3':
            lc.append(l[j])
    ans_k += count(la,A) + count(lb,B) + count(lc,C)
    ans = min(ans,ans_k)
print(ans)