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:
import itertools

def calc_MP(take, num):
    return abs(sum(take) - num) + (len(take)-1) * 10

def main():
    n, a, b, c = [int(i) for i in input().split()]

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

    ans = float("inf")
    for i0, i1, i2, i3, i4, i5, i6, i7 in itertools.product(range(4), range(4), range(4),range(4), range(4), range(4),range(4), range(4)):
        i_list = [i0, i1, i2, i3, i4, i5, i6, i7]
        MP = 0
        take_A = []
        take_B = []
        take_C = []

        for i in range(n):
            if i_list[i] == 1:
                take_A.append(l[i])
            elif i_list[i] == 2:
                take_B.append(l[i])
            elif i_list[i] == 3:
                take_C.append(l[i])

        if len(take_A) != 0 and len(take_B) != 0 and len(take_C) != 0:
            MP += calc_MP(take_A, a)
            MP += calc_MP(take_B, b)
            MP += calc_MP(take_C, c)
        else:
            MP = float("inf")
        ans = min(ans, MP)

    print(ans)

main()

Submission status: Accepted

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

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


def convert(n):
    list_q = []
    while True:
        a = n % 4
        n //= 4
        list_q.insert(0, a)
        if n == 0:
            break
    list_q = [str(i) for i in list_q]
    return list_q


MP = -1
for i in range(21, 4**N):
    mp_a = 0
    mp_b = 0
    mp_c = 0
    use = [int(j) for j in ''.join(convert(i)).zfill(N)]
    a_n = use.count(1)
    b_n = use.count(2)
    c_n = use.count(3)
    if a_n == 0 or b_n == 0 or c_n == 0:
        pass
    else:
        mp_a += 10 * (a_n - 1)
        mp_b += 10 * (b_n - 1)
        mp_c += 10 * (c_n - 1)
        b_a = 0
        b_b = 0
        b_c = 0
        idx = 0
        for i in use:
            if i == 1:
                b_a += l[idx]
            elif i == 2:
                b_b += l[idx]
            elif i == 3:
                b_c += l[idx]
            idx += 1
        mp_a += abs(A-b_a)
        mp_b += abs(B-b_b)
        mp_c += abs(C-b_c)
        mp = mp_a + mp_b + mp_c
        if MP == -1 or mp < MP:
            MP = mp
print(MP)

Submission status: Accepted

Solution code:
n, a, b, c = [int(_) for _ in input().split()]
l = [int(input()) for _ in range(n)] + [0 for _ in range(8-n)]
ans = 1e9
for i in range(4**8):
    quas = format(i, '016b')
    d = [0, 0, 0, 0]
    m = 0
    for j in range(8):
        t = int(quas[2*j:2*(j+1)], 2)
        if d[t] != 0 and l[j] != 0 and t != 3 : m += 10
        d[t] += l[j]
    if not 0 in d[:3] : ans = min(ans, m + abs(a-d[0]) + abs(b-d[1]) + abs(c-d[2]))
print(ans)

Submission status: Accepted

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

def dfs(index, a, b, c):
    if index == N:
        return abs(a-A)+abs(b-B)+abs(c-C)-30 if min(a, b, c) > 0 else float('inf')
    
    val1 = dfs(index+1, a, b, c)
    val2 = dfs(index+1, a+l[index], b, c) + 10
    val3 = dfs(index+1, a, b+l[index], c) + 10
    val4 = dfs(index+1, a, b, c+l[index]) + 10
    return min(val1, val2, val3, val4)

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

Submission status: Accepted

Solution code:
def culc_mp(l,num):
  mp = (len(l)-1)*10
  mp += abs(sum(l)-num)
  return(mp)

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

min_mp = float('inf')
for i in range(4**n):
  a_l = []
  b_l = []
  c_l = []
  mp = 0
  for j in range(n):
    tmp  = (i >> j*2) & 3
    num = l[j]
    if tmp == 0:
      a_l.append(num)
    elif tmp == 1:
      b_l.append(num)
    elif tmp == 2:
      c_l.append(num)
    
  if len(a_l) * len(b_l) * len(c_l) == 0:
      continue
    
  mp += culc_mp(a_l,a)
  mp += culc_mp(b_l,b)
  mp += culc_mp(c_l,c)
  if mp < min_mp:
    min_mp = mp
    
print(min_mp)

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(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:
import itertools

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

def mp(assigns):
    counts = [-1, -1, -1]
    totals = [0, 0, 0]
    for i in range(N):
        a = assigns[i]
        if a < 0:
            continue
        totals[a] += ls[i]
        counts[a] += 1
    if any([count < 0 for count in counts]):
        return A + B + C
    return sum([abs(totals[i] - target[i]) + 10 * counts[i] for i in range(3)])

min_mp = A + B + C
for p in itertools.product([-1, 0, 1, 2], repeat=N):
    min_mp = min(min_mp, mp(p))
print(min_mp)

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 a * b * c != 0 else 10 ** 9
    else:
        ret0 = dfs(cur + 1, a, b, c)
        reta = dfs(cur + 1, a + l[cur], b, c) + 10
        retb = dfs(cur + 1, a, b + l[cur], c) + 10
        retc = dfs(cur + 1, a, b, c + l[cur]) + 10
        return min(ret0, reta, retb, retc)
print(dfs(0,0,0,0))

Submission status: Accepted

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

def dfs(n, a = 0, b = 0, c = 0, mp = 0):
    if n == N:
        if a > 0 and b > 0 and c > 0:
            return abs(A-a) + abs(B-b) + abs(C-c) + mp
        else:
            return 9223372036854775807
    else:
        return min(
            dfs(n+1, a + ls[n], b, c, mp + (10 if a > 0 else 0)),
            dfs(n+1, a, b + ls[n], c, mp + (10 if b > 0 else 0)),
            dfs(n+1, a, b, c + ls[n], mp + (10 if c > 0 else 0)),
            dfs(n+1, a, b, c, mp),
        )

print(dfs(0))

Submission status: Accepted

Solution code:
def search(idx, a, b, c):
    if idx == N:
        if min(a, b, c) > 0:
            return abs(A-a) + abs(B-b) + abs(C-c) - 30
        else:
            return 10**20
            
    n0 = search(idx+1, a, b, c)
    n1 = search(idx+1, a+l[idx], b, c) + 10
    n2 = search(idx+1, a, b+l[idx], c) + 10
    n3 = search(idx+1, a, b, c+l[idx]) + 10
    
    return min(n0, n1, n2, n3)
    
N, A, B, C = list(map(int, input().split()))
l = [int(input()) for i in range(N)]

print(search(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=3000

def mp(g,a,b,c,):
    if g==N:
        return abs(A-a)+abs(B-b)+abs(C-c)-30 if min(a,b,c)>0 else inf

    l0=mp(g+1,a+l[g],b,c)+10
    l1=mp(g+1,a,b+l[g],c)+10
    l2=mp(g+1,a,b,c+l[g])+10
    l3=mp(g+1,a,b,c)
    
    return min(l0,l1,l2,l3)

print(mp(0,0,0,0))

Submission status: Accepted

Solution code:
read = input
# read = Read(False, """


# 8 1000 800 100
# 300
# 333
# 400
# 444
# 500
# 555
# 600
# 666

# """).read
tmp = read()
tmp = list(map(int, tmp.split(' ')))
N,A,B,C = tmp
opt = list(map(int, map(lambda x: read(), range(N))))
def dfs(n,a,b,c):
    ret = 10**7
    if(n == N):
        if(min(a,b,c) == 0):
            return ret
        return abs(A - a) + abs(B - b) + abs(C - c) - 30
    cur = opt[n]
    ret = min(ret, dfs(n+1, a + cur, b, c) + 10)
    ret = min(ret, dfs(n+1, a, b + cur, c) + 10)
    ret = min(ret, dfs(n+1, a, b ,c + cur) + 10)
    ret = min(ret, dfs(n+1, a, b ,c ) + 0)
    return ret
print(dfs(0, 0, 0, 0))

Submission status: Accepted

Solution code:
many, A, B, C = map(int, input().split())
bamboos = [int(input()) for i in range(many)]

# point: 訳が分からなくなるものは自分でルールづけをする
# 合成魔法を全て最初に行う、というルールづけ

# 解法: DFS or 4進数(4patternsで2の累乗だからできる)
# PS: DFS(深さ優先探索), BFS(幅優先探索)

infinity = 1e10

def dfs(depth, a, b, c):
    if depth == many:
        # 謎の場合分け
        if min(a, b, c) > 0:
            # 竹が何もない状態でも各竹に+10がかかっていることから-30する必要がある。
            # また、+10が一個はないとベースとなる竹がないことになるので
            # a,b,c = 0はコストを無限にすることで避ける。
            return abs(a-A) + abs(b-B) + abs(c-C) - 30
        else:
            return infinity
    else:
        ret0 = dfs(depth+1, a, b, c)
        ret1 = dfs(depth+1, a+bamboos[depth], b, c) + 10
        ret2 = dfs(depth+1, a, b+bamboos[depth], c) + 10
        ret3 = dfs(depth+1, a, b, c+bamboos[depth]) + 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())
ls = []
for _ in range(N):
    ls.append(int(input()))
INF = 10 ** 10


def dfs(i, a, b, c):
    if i == N:
        if min(a, b, c) > 0:
            return abs(a - A) + abs(b - B) + abs(c - C) - 30
        else:
            return INF
    ret0 = dfs(i + 1, a, b, c)
    ret1 = dfs(i + 1, a + ls[i], b, c) + 10
    ret2 = dfs(i + 1, a, b + ls[i], c) + 10
    ret3 = dfs(i + 1, a, b, c + ls[i]) + 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(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(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))

Submission status: Accepted

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

mp = []
for i in range(4**N):
    Test = [0,0,0]
    MP = 0
    for j in range(N):
        n = i%4
        if n < 3:
            MP += 10 if Test[n] else 0
            Test[n] += l[j]
        i >>= 2
    for i in range(3):
        MP += abs(L[i]-Test[i]) if Test[i] else 3000
    mp.append(MP)

print(min(mp))

Submission status: Accepted

Solution code:
def main():
	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, x, y, z):
		if cur == n:
			return abs(a-x) + abs(b-y) + abs(c-z) - 30 if min(x, y, z) > 0 else INF
		ret0 = dfs(cur+1, x, y, z)
		ret1 = dfs(cur+1, x+l[cur], y, z) + 10
		ret2 = dfs(cur+1, x, y+l[cur], z) + 10
		ret3 = dfs(cur+1, x, y, z+l[cur]) + 10
		return min(ret0, ret1, ret2, ret3)
	print(dfs(0, 0, 0, 0))

if __name__ == '__main__':
    main()

Submission status: Accepted

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

def solve(w,x,y,z):
    if w == N:
        if min(x,y,z)>0:
            return abs(x-A) + abs(y-B) + abs(z-C) -30
        else: 
            return 10e4
    can0 = solve(w + 1,x,y,z)
    can1 = solve(w + 1,x + l[w],y,z) + 10
    can2 = solve(w + 1,x,y + l[w],z) + 10
    can3 = solve(w + 1,x,y,z + l[w]) + 10
    return min(can0,can1,can2,can3)

print(solve(0,0,0,0))

Submission status: Accepted

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

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 min(a, b, c) > 0:
            return abs(A-a) + abs(B-b) + abs(C-c)
        else:
            return 10 ** 9
    
    res0 = dfs(i+1, a, b, c)
    if a == 0:
        res1 = dfs(i+1, a+L[i], b, c)
    else:
        res1 = dfs(i+1, a+L[i], b, c) + 10
    if b == 0:
        res2 = dfs(i+1, a, b+L[i], c)
    else:
        res2 = dfs(i+1, a, b+L[i], c) + 10
    if c == 0:
        res3 = dfs(i+1, a, b, c+L[i])
    else:
        res3 = dfs(i+1, a, b, c+L[i]) + 10

    return min(res0, res1, res2, res3)

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(cnt,la,lb,lc,cost):
    if cnt == N:
        if la == 0 or lb == 0 or lc == 0:
            return 10**10
        else:
            return cost+abs(A-la)+abs(B-lb)+abs(C-lc)-30
    else:
        a = dfs(cnt+1,la+L[cnt],lb,lc,cost+10)
        b = dfs(cnt+1,la,lb+L[cnt],lc,cost+10)
        c = dfs(cnt+1,la,lb,lc+L[cnt],cost+10)
        d = dfs(cnt+1,la,lb,lc,cost) 
        return min(a,b,c,d)

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

Submission status: Accepted

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

def dfs(i, a, b, c):
    if i == len(L):
        if min(a, b, c) > 0:
            return abs(x[0] - a) + abs(x[1] - b) + abs(x[2] - c) - 30
        else:
            return 10 ** 9

    ret0 = dfs(i + 1, a, b, c)
    ret1 = dfs(i + 1, a + L[i], b, c) + 10
    ret2 = dfs(i + 1, a, b + L[i], c) + 10
    ret3 = dfs(i + 1, a, b, c + L[i]) + 10
    return min(ret0, ret1, ret2, ret3)

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
import collections

def main():
	N, A, B, C = map(int, input().split(" "))
	ls = []
	for _ in range(N):
		tmp = int(input())
		ls.append(tmp)
	
	ptn = []
	for i in range(4**N):
		tmp = []
		for j in range(N):
			tmp.append(i >> (j*2) & 3)
		ptn.append(tmp)
	
	gattai = 0
	mp = -1
	for i, val in enumerate(ptn):
		a_count = 0
		b_count = 0
		c_count = 0
		a_ni = 0
		b_ni = 0
		c_ni = 0
		for j, vol in enumerate(val):
			if vol == 1:
				a_ni += ls[j]
				a_count += 1
			if vol == 2:
				b_ni += ls[j]
				b_count += 1
			if vol == 3:
				c_ni += ls[j]
				c_count += 1
		
		if a_count == 0 or b_count == 0 or c_count == 0:
			continue
		mp_tmp = abs(a_ni - A) + abs(b_ni - B) + abs(c_ni - C) + ((a_count + b_count + c_count -3) * 10)
		if mp == -1 or mp > mp_tmp:
			mp = mp_tmp
			#print("{} {} {}".format(a_count , b_count , c_count))
			
	print(mp)
	
	
if __name__ == "__main__":
	main()

Submission status: Accepted

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

ans = 10**100
for x in itertools.product([0,1,2,3], repeat=n):
    if not 0 in x or not 1 in x or not 2 in x: continue
    bum = [0,0,0]
    res = 0
    for i,j in enumerate(x):
        if j<3:
            if bum[j]!=0:
                res+=10
            bum[j]+=l[i]
    res += abs(a-bum[0])+abs(b-bum[1])+abs(c-bum[2])
    ans = min(ans, res)
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()))
def Base_10_to_n(X,n):
  if (int(X/n)):
    return Base_10_to_n(int(X/n),n)+str(X%n)
  return str(X%n)
n = 4
ans = float('inf')
for X in range(n**N):
  STR = Base_10_to_n(X,n).zfill(N)
  cur = 0
  TAKEA = 0
  TAKEB = 0
  TAKEC = 0
  for i in range(len(STR)):
    if STR[i] == '1':
      TAKEA += L[i]
      cur += 10
    elif STR[i] == '2':
      TAKEB += L[i]
      cur += 10
    elif STR[i] == '3':
      TAKEC += L[i]
      cur += 10
  if TAKEA != 0 and TAKEB != 0 and TAKEC != 0:
    cur += abs(TAKEA-A)+abs(TAKEB-B)+abs(TAKEC-C)
    cur -= 30
    if cur <= ans:
      ans = cur
print(ans)

Submission status: Accepted

Solution code:
from itertools import product

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

answer = 1e9
for X in product('ABCD', repeat=N):
  total_length={'A':0,'B':0,'C':0,'D':0}
  total_nums={'A':0,'B':0,'C':0,'D':0}
  for i, x in enumerate(X):
    total_length[x]+=L[i]
    total_nums[x]+=1
  na = total_nums['A']
  nb = total_nums['B']
  nc = total_nums['C']
  la = total_length['A']
  lb = total_length['B']
  lc = total_length['C']
  
  if na and nb and nc:
    a = (na+nb+nc -3)*10 + abs(la-A) + abs(lb-B) + abs(lc-C)
    answer = min(a,answer)
    
print(answer)

Submission status: Accepted

Solution code:
from functools import lru_cache

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

inf = 10**9


# DP
@lru_cache(maxsize=1000)
def f(i, _a, _b, _c):
    # A,B,Cにしようとしている枝の長さが現在_a,_b,_cのとき,i番目以降の材料を使って達成される点数の最小値
    if i == n:
        return abs(a - _a) + abs(b - _b) + abs(c - _c) if min(_a, _b, _c) > 0 else inf

    # i番目を使わないとき
    val1 = f(i + 1, _a, _b, _c)
    # i番目をAに使うとき
    val2 = f(i + 1, _a + _l[i], _b, _c) + (0 if _a == 0 else 10)
    val3 = f(i + 1, _a, _b + _l[i], _c) + (0 if _b == 0 else 10)
    val4 = f(i + 1, _a, _b, _c + _l[i]) + (0 if _c == 0 else 10)
    return min(val1, val2, val3, val4)


print(f(0, 0, 0, 0))

Submission status: Accepted

Solution code:
from functools import lru_cache

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

inf = 10**9


# DP
@lru_cache(maxsize=10000)
def f(i, _a, _b, _c):
    # A,B,Cにしようとしている枝の長さが現在_a,_b,_cのとき,i番目以降の材料を使って達成される点数の最小値
    if i == n:
        return abs(a - _a) + abs(b - _b) + abs(c - _c) if min(_a, _b, _c) > 0 else inf

    # i番目を使わないとき
    val1 = f(i + 1, _a, _b, _c)
    # i番目をAに使うとき
    val2 = f(i + 1, _a + _l[i], _b, _c) + (0 if _a == 0 else 10)
    val3 = f(i + 1, _a, _b + _l[i], _c) + (0 if _b == 0 else 10)
    val4 = f(i + 1, _a, _b, _c + _l[i]) + (0 if _c == 0 else 10)
    return min(val1, val2, val3, val4)


print(f(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


# DP
def f(i, _a, _b, _c):
    # A,B,Cにしようとしている枝の長さが現在_a,_b,_cのとき,i番目以降の材料を使って達成される点数の最小値
    if i == n:
        return abs(a - _a) + abs(b - _b) + abs(c - _c) if min(_a, _b, _c) > 0 else inf

    # i番目を使わないとき
    val1 = f(i + 1, _a, _b, _c)
    # i番目をAに使うとき
    val2 = f(i + 1, _a + _l[i], _b, _c) + (0 if _a == 0 else 10)
    val3 = f(i + 1, _a, _b + _l[i], _c) + (0 if _b == 0 else 10)
    val4 = f(i + 1, _a, _b, _c + _l[i]) + (0 if _c == 0 else 10)
    return min(val1, val2, val3, val4)


print(f(0, 0, 0, 0))

Submission status: Accepted

Solution code:
import itertools

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

cl = ('A', 'B', 'C', 'N')

result = 1e10
for pattern in itertools.product(*[cl]*n):
    wands = {'A': 0, 'B': 0, 'C': 0}
    point = 0
    for i, char in enumerate(pattern):
        if char != 'N':
            if wands[char] != 0:
                point += 10
            wands[char] += _l[i]
    if min(wands.values()) > 0:
        point += abs(wands['A'] - a) + abs(wands['B'] - b) + abs(wands['C'] - c)
        if point < result:
            result = point

print(result)

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(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 _ in range(N)]

def make_length(cur,A_,B_,C_,cost):
  if cur == N:
    if min(A_,B_,C_) > 0:
      return abs(A-A_)+abs(B-B_)+abs(C-C_) + cost
    else:
      return 1000000
  a_use = make_length(cur+1,A_+l_[cur],B_,C_,cost+10)
  b_use = make_length(cur+1,A_,B_+l_[cur],C_,cost+10)
  c_use = make_length(cur+1,A_,B_,C_+l_[cur],cost+10)
  not_use = make_length(cur+1,A_,B_,C_,cost)
  return min(a_use,b_use,c_use,not_use)

print(make_length(0,0,0,0,-30))

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(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 _ 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:
# -*- coding: utf-8 -*-
"""
C - Synthetic Kadomatsu
https://atcoder.jp/contests/abc119/tasks/abc119_c

"""
import sys
from itertools import product, groupby


def solve(N, A, B, C, L):
    ans = float('inf')
    for p in product('abc_', repeat=N):
        if not all([ch in p for ch in 'abc']):
            continue
        cost = 0
        t = sorted([(usage, l) for usage, l in zip(p, L)], key=lambda x:x[0])
        for k, g in groupby(t, key=lambda x: x[0]):
            if k not in 'abc':
                continue
            g = list(g)
            cost += 10 * (len(g) - 1)
            l = sum(i[1] for i in g)
            if k == 'a':
                cost += abs(A - l)
            elif k == 'b':
                cost += abs(B - l)
            elif k == 'c':
                cost += abs(C - l)
        ans = min(ans, cost)
    return ans


def main(args):
    N, A, B, C = map(int, input().split())
    L = [int(input()) for _ in range(N)]
    ans = solve(N, A, B, C, L)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Accepted

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

def dif(stick,abc):
    return abs(abc-sum(stick)) + 10*(len(stick)- 1)

mp = 300000

for i in range(0,1 << 2*n):
    bamb = [[] for i in range(0,4)]
    for j in range(0,2*n,2):
        if ((i >> j) & 3) == 3:
            bamb[0].append(l[j//2])
        elif ((i >> j)&2) == 2:
            bamb[1].append(l[j//2])
        elif ((i >> j)&1) == 1:
            bamb[2].append(l[j//2])
        elif((i >> j)&0) == 0:
            bamb[3].append(l[j//2])
    if bamb[0] == [] or bamb[1] == [] or bamb[2] == []:
        continue
    mp = min(mp,dif(bamb[0],a)+dif(bamb[1],b)+dif(bamb[2],c))
print(mp)

Submission status: Accepted

Solution code:
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict, deque, OrderedDict
from copy import deepcopy
from functools import lru_cache, reduce
from math import ceil, floor
from sys import setrecursionlimit

import heapq
import itertools
import operator


inf = float('inf')


# globals
N, A, B, C = 0, 0, 0, 0
L = []


def set_inputs():
    global N, A, B, C, L
    N, A, B, C = get_li()
    L = get_data(N, [int])
    return


def main():
    setrecursionlimit(100000)
    set_inputs()
    ans = inf
    for x in itertools.product(range(4), repeat=N):
        take = [[], [], [], []]
        for i in range(N):
            take[x[i]].append(L[i])
        if len(take[0]) == 0 or len(take[1]) == 0 or len(take[2]) == 0:
            continue
        cost = 0
        cost += abs(sum(take[0]) - A)
        cost += abs(sum(take[1]) - B)
        cost += abs(sum(take[2]) - C)
        cost += (len(take[0]) - 1) * 10
        cost += (len(take[1]) - 1) * 10
        cost += (len(take[2]) - 1) * 10
        ans = min(ans, cost)
    print(ans)
    return


def get_int():
    return int(input())


def get_float():
    return float(input())


def get_str():
    return input().strip()


def get_li():
    return [int(i) for i in input().split()]


def get_lf():
    return [float(f) for f in input().split()]


def get_lc():
    return list(input().strip())


def get_data(n, types):
    if len(types) == 1:
        return [types[0](input()) for _ in range(n)]
    return zip(*(
        [t(x) for t, x in zip(types, input().split())]
        for _ in range(n)
    ))


if __name__ == '__main__':
    main()

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(cur,a,b,c):
  if cur==N:
    #1本目にはコストはかからないので30引く
    return abs(A-a)+abs(B-b)+abs(C-c)-30 if min(a,b,c)>0 else INF
  #cur本目の竹を使わない
  ret0=dfs(cur+1,a,b,c)
  #cur本目の竹をAにつなぐ(MP10)
  ret1=dfs(cur+1,a+l[cur],b,c)+10
  #cur本目の竹をBにつなぐ(MP10)
  ret2=dfs(cur+1,a,b+l[cur],c)+10
  #cur本目の竹をCにつなぐ(MP10)
  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


# 最小MPを返す
def rec(index, a, b, c):

    # 終了条件
    if index >= N:
        if a != 0 and b != 0 and c != 0:
            # 不足分は延長魔法、短縮魔法を使う
            return abs(A - a) + abs(B - b) + abs(C - c)
        else:
            return INF

    # L[index] をAの材料に使う
    cost_mp_a = 10 if a != 0 else 0
    result_a = rec(index + 1, a + L[index], b, c) + cost_mp_a

    # L[index] をBの材料に使う
    cost_mp_b = 10 if b != 0 else 0
    result_b = rec(index + 1, a, b + L[index], c) + cost_mp_b

    # L[index] をCの材料に使う
    cost_mp_c = 10 if c != 0 else 0
    result_c = rec(index + 1, a, b, c + L[index]) + cost_mp_c

    # L[index] を使わない
    result_none = rec(index + 1, a, b, c)

    return min(result_a, result_b, result_c, result_none)


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

Submission status: Accepted

Solution code:
# coding: utf-8

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

def dfs(cur=0, a=0, b=0, c=0):
  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())

Submission status: Accepted

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

for i in range(4**n):
    ta, tb, tc = 0, 0, 0
    tmp = 0
    for j in range(n):
        k = (i>>(2*j)) & 3
        if k == 1:
            if ta > 0:
                tmp += 10
            ta += L[j]
        elif k == 2:
            if tb > 0:
                tmp += 10
            tb += L[j]
        elif k == 3:
            if tc > 0:
                tmp += 10
            tc += L[j]
    if ta * tb * tc == 0:
        continue
    tmp += abs(a - ta) + abs(b - tb) + abs(c - tc)
    ans = min(ans,tmp)

print(ans)

Submission status: Accepted

Solution code:
n,A,B,C,=map(int,input().split())
l=[int(input()) for i in range(n)]
INF=1e9
def dfs(i,a,b,c):
  if i==n:
    if min(a,b,c)>0:
      return abs(A-a)+abs(B-b)+abs(C-c)-30
    else:
      return INF
  ret0=dfs(i+1,a,b,c)
  ret1=dfs(i+1,a+l[i],b,c)+10
  ret2=dfs(i+1,a,b+l[i],c)+10
  ret3=dfs(i+1,a,b,c+l[i])+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(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 _ in range(n)]
INF = 10 ** 9

# 足し算の可換性を用いる（延長/短縮 と合成は順序によらない）
# 合成を先にし 延長/短縮 は後でやる
# n本の竹に対し「a/b/cに使う + 使わない」の4パターンがある
# 4^8 = 65536


def dfs(l_index, aa, bb, cc):
    # 終端処理
    if l_index == n:
        return abs(a - aa) + abs(b - bb) + abs(c - cc) \
               - 30 if min(aa, bb, cc) > 0 else INF
    # -30するのは最初の1本目の合成のペナルティを余分に足してたから
    rtrn0 = dfs(l_index + 1, aa, bb, cc)
    rtrn1 = dfs(l_index + 1, aa + l[l_index], bb, cc) + 10
    rtrn2 = dfs(l_index + 1, aa, bb + l[l_index], cc) + 10
    rtrn3 = dfs(l_index + 1, aa, bb, cc + l[l_index]) + 10
    return min(rtrn0, rtrn1, rtrn2, rtrn3)


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

Submission status: Accepted

Solution code:
from sys import stdin
import itertools

def get_index(l,x):
    return [i for i, _x in enumerate(l) if _x == x]

N,A,B,C=[int(x) for x in stdin.readline().rstrip().split()]
data=[]
for i in range(N):
    data.append(int(input()))
target=[A,B,C]

ans=float("inf")
for j in (list(itertools.product(["none","A","B","C"],repeat=N))):
    if set(j) >= {"A","B","C"}:
        score=(j.count("A")-1)*10 + (j.count("B")-1)*10 + (j.count("C")-1)*10
        score_a=sum([data[k] for k in (get_index(j,"A"))])
        score_b=sum([data[l] for l in (get_index(j,"B"))])
        score_c=sum([data[m] for m in (get_index(j,"C"))])
        score=abs(A-score_a)+abs(B-score_b)+abs(C-score_c)+score
        if score < ans:
            ans=score
print(ans)