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:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(10000)


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


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


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, A, B, C, L):
    ans = sys.maxsize
    
    S = (A, B, C)
    for l in product([0, 1, 2, None], repeat=len(L)):
        ta = 0
        s = [A, B, C]
        for i, f in enumerate(l):
            if f is None:
                continue
            if s[f] != S[f]:
                ta += 10
            s[f] -= L[i]

        if any(map(lambda x: x[0] == x[1], zip(s, S))):
            continue
        
        ta += sum(map(abs, s))
        ans = min(ans, ta)

    return ans


def main():
    # N = read_int()
    N, A, B, C = read_int_n()
    L = [read_int() for _ in range(N)]
    print(slv(N, A, B, C, L))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
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,a,b,c=map(int,input().split())
L=[]
for _ in range(n):
    L.append(int(input()))

ans=99999
for i in range(4**len(L)):
    i_4_list=list(Base_10_to_n(i, 4).zfill(len(L)))
    at,bt,ct=[],[],[]
    for num,i_4 in enumerate(i_4_list):
        if i_4=='0':
            at.append(L[num])
        elif i_4=='1':
            bt.append(L[num])
        elif i_4=='2':
            ct.append(L[num])
        else:
            continue
    if min(len(at),len(bt),len(ct))>0:
        al=0
        al+=(len(at)+len(bt)+len(ct)-3)*10
        al+=(abs(sum(at)-a)+abs(sum(bt)-b)+abs(sum(ct)-c))
        if al < ans:
            ans=al
print(ans)

Submission status: Accepted

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

def dfs(depth=0, a=0, b=0, c=0):
    if depth == N:
        if a*b*c != 0:
            return abs(a-A) + abs(b-B) + abs(c-C) - 30
        return inf
    res0 = dfs(depth+1,a,b,c)
    res1 = dfs(depth+1,a+l[depth],b,c) + 10
    res2 = dfs(depth+1,a,b+l[depth],c) + 10
    res3 = dfs(depth+1,a,b,c+l[depth]) + 10
    return min([res0,res1,res2,res3])

print(dfs())

Submission status: Accepted

Solution code:
import itertools
A=[0,0,0]
N,A[0],A[1],A[2]=map(int,input().split())
l=[int(input()) for i in range(N)]
ans=10**15
for seq in itertools.product(range(4),repeat=N):
    X=[0,0,0]
    Y=[0,0,0]
    for i in range(N):
        if seq[i]!=3:
            X[seq[i]]+=l[i]
            Y[seq[i]]+=1
    if Y[0]*Y[1]*Y[2]==0:
        continue
    MP=(sum(Y)-3)*10
    for i in range(3):
        MP+=abs(X[i]-A[i])
    if MP<ans:
        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(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
  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 = [0] * N
for i in range(N):
    L[i] = int(input())


def dfs(depth, a, b, c):    # 深さは何本目の竹まで場合分けしたかを示す  a,b,cにはA,B,Cに割り当てられている竹の長さの和が入っている　戻り値はMP
    if depth == N:
        # ABCのどれかが1本も割り当てられていない場合はだめなのでINFを返す
        return abs(a-A)+abs(b-B)+abs(c-C) - 30 if min(a, b, c) > 0 else 1000000000

    ret0 = dfs(depth + 1, a, b, c)  # depth本目の竹を使わない
    # depth本目の竹をAを作るのに使用する    コストを足して、長さには使用する竹の長さを足してやる
    ret1 = dfs(depth + 1, a + L[depth], b, c) + 10
    ret2 = dfs(depth + 1, a, b + L[depth], c) + 10  # depth本目の竹をBを作るのに使用する
    ret3 = dfs(depth + 1, a, b, c + L[depth]) + 10  # depth本目の竹をCを作るのに使用する

    return min(ret0, ret1, ret2, ret3)  # 一番小さいMPを返す


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

def bamboo_search(l,a,b,c):
    if l==N:
        return abs(A-a)+abs(B-b)+abs(C-c)-30 if min(a,b,c)>0 else INF
    else:
        bcan = bamboo_search(l+1,a,b,c)
        ba = bamboo_search(l+1,a+L[l],b,c)+10
        bb = bamboo_search(l+1,a,b+L[l],c)+10
        bc = bamboo_search(l+1,a,b,c+L[l])+10
        return min(bcan,ba,bb,bc)
    
print(bamboo_search(0,0,0,0))

Submission status: Accepted

Solution code:
INF = int(1e9)
n, A, B, C = map(int, input().split())
L = tuple(map(int, (int(input()) for _ in range(n))))
def f(a, b, c, num):
    if num == n:
        return abs(A - a) + abs(B - b) + abs(C - c) if min(a, b, c) > 0 else INF
    return min(
        f(a + L[num], b, c, num + 1) + (10 if a else 0),
        f(a, b + L[num], c, num + 1) + (10 if b else 0),
        f(a, b, c + L[num], num + 1) + (10 if c else 0),
        f(a, b, c, num + 1)
    )
print(f(0, 0, 0, 0))

Submission status: Accepted

Solution code:
INF = float("inf")
n, A, B, C = map(int, input().split())
L = tuple(map(int, (int(input()) for _ in range(n))))
def f(a, b, c, num):
    if num == n:
        return abs(A - a) + abs(B - b) + abs(C - c) if min(a, b, c) > 0 else INF
    return min(
        f(a + L[num], b, c, num + 1) + (10 if a else 0),
        f(a, b + L[num], c, num + 1) + (10 if b else 0),
        f(a, b, c + L[num], num + 1) + (10 if c else 0),
        f(a, b, c, num + 1)
    )
print(f(0, 0, 0, 0))

Submission status: Accepted

Solution code:
INF = float("inf")
n, A, B, C = map(int, input().split())
L = tuple(map(int, (int(input()) for _ in range(n))))
def f(a, b, c, num):
    if num == n:
        if min(a, b, c) > 0:
            return abs(A - a) + abs(B - b) + abs(C - c)
        else:
            return INF
    return min(
        f(a + L[num], b, c, num + 1) + (10 if a else 0),
        f(a, b + L[num], c, num + 1) + (10 if b else 0),
        f(a, b, c + L[num], num + 1) + (10 if c else 0),
        f(a, b, c, num + 1)
    )
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)]
inf = float('inf')
ans = inf
for pair in itertools.product(range(4),repeat=N):
    mA = 0 ; mB = 0; mC = 0; cnt = 0
    for i in range(N):
        if pair[i] == 1:
            mA += l[i]
            cnt += 1
        elif pair[i] == 2:
            mB += l[i]
            cnt += 1
        elif pair[i] == 3:
            mC += l[i]
            cnt += 1
    if mA*mB*mC == 0:continue
    cost = abs(mA-A) + abs(mB-B) + abs(mC-C) + (cnt-3)*10
    if cost < ans:
        ans = cost
print(ans)

Submission status: Accepted

Solution code:
def solve(n, a, b, c, l):
    v = gen(a,b,c,l,n,0,[],[],[])
    print(v)

    
def gen(a,b,c,l,n,idx,al,bl,cl):
    if idx == n:
        if (len(al) > 0) and (len(bl) > 0) and (len(cl) > 0):
            v = calc(a,b,c,al,bl,cl)
            # print(f"{a}:{al}, {b}:{bl}, {c}:{cl} => {v}")
            return v
        else:
            return 3000

    l1 = l[idx]
    nal = al.copy()
    nbl = bl.copy()
    ncl = cl.copy()

    nal.append(l1)
    nbl.append(l1)
    ncl.append(l1)

    va = gen(a, b, c, l, n, (idx+1), nal,bl,cl)
    vb = gen(a, b, c, l, n, (idx+1), al,nbl,cl)
    vc = gen(a, b, c, l, n, (idx+1), al,bl,ncl)
    vn = gen(a, b, c, l, n, (idx+1), al,bl,cl)
    return min(va, vb, vc, vn)

def calc(a,b,c,al,bl,cl):
    mp = 0
    
    t = al
    mp += (len(t) - 1) * 10
    length = sum(t)
    mp += abs(a - length)

    t = bl
    mp += (len(t) - 1) * 10
    length = sum(t)
    mp += abs(b - length)

    t = cl
    mp += (len(t) - 1) * 10
    length = sum(t)
    mp += abs(c - length)

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

solve(n, a, b, c, sorted(l))

Submission status: Accepted

Solution code:
n, *li = map(int, input().split())
L = list(map(int, (int(input()) for _ in range(n))))
ans = int(1e9)
def f(A, num, cnt):
    if num == n:
        if 0 in A:
            return
        global ans
        ans = min(ans, abs(A[0] - li[0]) + abs(A[1] - li[1]) + abs(A[2] - li[2]) + cnt)
        return
    for i in range(3):
        p = 0 if A[i] == 0 else 10
        B = A[::]
        B[i] += L[num]
        f(B, num + 1, cnt + p)
    f(A, num + 1, cnt)
f([0, 0, 0], 0, 0)
print(ans)

Submission status: Accepted

Solution code:
import sys
import numpy as np

def parse(inp):
    N,A,B,C=map(int,next(inp).split())
    L=[int(next(inp).split()[0]) for _ in range(N)]
        
    return N,[A,B,C],sorted(L,reverse=True)
  
def main(inp):
    N,target,L=inp
    
    pattern=[0 for _ in range(N)]
    
    ans = np.iinfo(np.int32).max
    
    for i in range(27,4**N-6):
        tmp = i
        for j in range(N-1,-1,-1):
#             print(tmp,4**j)
            pattern[j]=int(tmp/(4**j))
            if(tmp >= 4**j): tmp -= int(tmp/(4**j))*4**j
        
        if(pattern.count(1)==0 or pattern.count(2)==0 or \
               pattern.count(3)==0): continue
            
        taketest = [0 for _ in range(3)]
        
        for j in range(N):
            if(pattern[j]==0): continue
            else: taketest[pattern[j]-1] += L[j]
        
        param=0
        for j in range(3):
            param += abs(taketest[j]-target[j]) + 10*(pattern.count(j+1)-1)
        ans = min(ans,param)
        
    return ans
  
print(main(parse(sys.stdin)))

Submission status: Accepted

Solution code:
N, A, B, C = map(int,input().split())
l = [int(input()) for x in range(N)]
V = [0 for x in range(N)]
ans = 0
minv = float("INF")

def calc(v, trees):
    global minv
    for j, val in enumerate(v):
        trees[val] += l[j]

    if 0 in trees[:3]:
        return float("INF")

    work = abs(trees[0] - A) +  ((v.count(0) - 1) * 10 if v.count(0) > 1 else 0)
    work += abs(trees[1] - B) + ((v.count(1) - 1) * 10 if v.count(1) > 1 else 0)
    work += abs(trees[2] - C) + ((v.count(2) - 1) * 10 if v.count(2) > 1 else 0)
    return work

def dfs(depth, v):
    global minv
    if depth == N:
        w = calc(v, [0, 0, 0, 0])
        if w < minv:
            minv = min(w, minv)
        return

    for i in range(4):
        v[depth] = i
        dfs(depth + 1, v)

dfs(0,V)
print(minv)

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:
# DFS
# N,A,B,C = 5,100,90,80
# L = [98,40,30,21,80]
N,A,B,C = map(int, input().split())
L = [int(input()) for _ in range(N)]
ans = float('inf')

def dfs(cur,a,b,c)->int:
    if cur==N:
        if min(a,b,c)>0: return abs(a-A)+abs(b-B)+abs(c-C)-30
        else:            return 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 = sorted([int(input()) for _ in range(N)])[-1::-1]

INF = 1e+10

def DFS(depth, current_a, current_b, current_c):
    if depth == N:
        if min(current_a, current_b, current_c) > 0:
            return abs(current_a - A) + abs(current_b - B) + abs(current_c - C) -30
        else:
            return INF
    
    ret0 = DFS(depth+1, current_a, current_b, current_c)
    ret1 = DFS(depth+1, current_a + l[depth], current_b, current_c) + 10
    ret2 = DFS(depth+1, current_a , current_b+ l[depth], current_c) + 10
    ret3 = DFS(depth+1, current_a, current_b, current_c + l[depth]) + 10
    
    return min(ret0, ret1, ret2, ret3)

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

Submission status: Accepted

Solution code:
# 8重for
# N,A,B,C = 5,100,90,80
# L = [98,40,30,21,80]
N,A,B,C = map(int, input().split())
L = [int(input()) for _ in range(N)]
ans = float('inf')
for i1 in range(4):
    for i2 in range(4):
        for i3 in range(4):
            for i4 in range(4):
                for i5 in range(4):
                    for i6 in range(4):
                        for i7 in range(4):
                            for i8 in range(4):
                                p = [i1,i2,i3,i4,i5,i6,i7,i8]
                                take = [[] for _ in range(4)] # [A,B,C,使わない]
                                for i in range(N):
                                    take[p[i]].append(L[i])
                                a,b,c = take[0],take[1],take[2]
                                if len(a)*len(b)*len(c)==0:
                                    continue 
                                cost = (len(a)+len(b)+len(c)-3)*10
                                cost += abs(sum(a)-A)+abs(sum(b)-B)+abs(sum(c)-C)
                                ans = min(ans, cost)
print(ans)

Submission status: Accepted

Solution code:
N = 0
N, A, B, C = [int(s) for s in input().split()]

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


# def divide(l):
#     if len(l) == 0:
#         return []
#     if len(l) == 1:
#         return [
#             [[], [], []],  # 使わない
#             [[l[0]], [], []],  # A で使う
#             [[], [l[0]], []],  # B で使う
#             [[], [], [l[0]]],  # C で使う
#         ]

#     res = []
#     for a, b, c in divide(l[1:]):
#         # 使わない
#         res.append([a, b, c])
#         # A で使う
#         res.append([l[0:1] + a, b, c])
#         # B で使う
#         res.append([a, l[0:1] + b, c])
#         # C で使う
#         res.append([a, b, l[0:1] + c])

#     return res


# min_mp = -1
# for a, b, c in divide(bamboos):
#     al, ac = sum(a), len(a)
#     bl, bc = sum(b), len(b)
#     cl, cc = sum(c), len(c)
#     if al == 0 or bl == 0 or cl == 0:
#         continue

#     # 合成に使った魔力
#     composite = 10 * ((ac - 1) + (bc - 1) + (cc - 1))
#     # 総合魔力
#     mp = abs(al - A) + abs(bl - B) + abs(cl - C) + composite

#     if mp < min_mp or min_mp == -1:
#         min_mp = mp

# print(min_mp)

# ------------------------
# dfs を使うともっと短くかける
# ------------------------
def dfs(l, a, b, c):
    global A, B, C
    INF = 10**18
    if len(l) == 0:
        if min(a, b, c) > 0:
            return abs(a - A) + abs(b - B) + abs(c - C) - 30
        else:
            return INF

    ret0 = dfs(l[1:], a, b, c)  # l[0] を使わない
    retA = dfs(l[1:], a + l[0], b, c) + 10  # l[0] を A で使う
    retB = dfs(l[1:], a, b + l[0], c) + 10  # l[0] を B で使う
    retC = dfs(l[1:], a, b, c + l[0]) + 10  # l[0] を C で使う

    return min(ret0, retA, retB, retC)


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

Submission status: Accepted

Solution code:
n,a,b,c = map(int,input().split())

ko = [int(input()) for i in range(n)]
INF = 10**9

def dfs(q,w,e,r):
 	if q == n:
 		 return abs(a-w) + abs(b-e) + abs(c-r) - 30 if min(w,e,r) > 0 else INF
 	re0 = dfs(q+1,w,e,r)
 	re1 = dfs(q+1,w+ko[q],e,r) + 10
 	re2 = dfs(q+1,w,e+ko[q],r) + 10
 	re3 = dfs(q+1,w,e,r+ko[q]) + 10
 	return min(re0,re1,re2,re3)

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

Submission status: Accepted

Solution code:
def c_synthetic_kadomatsu(N, A, B, C, L):
    def dfs(cur, a, b, c):
        # cur本竹を使ったとき、長さA, B, Cにしたい竹のそれぞれの長さがa, b, cの状態
        if cur == N:
            # 竹をすべて使った。長さが違う分だけ伸縮する
            # -30しているのは、 長さ0の竹に "合成" はしないため
            # (合成魔法のMP10 * 最終的に作りたい竹の数3 = 30 が余分に計上されている)
            # なお、最終的にできる3本の竹について、1本も竹を使わないことがあってはいけない
            return abs(a - A) + abs(b - B) + abs(c - C) - 30 if min(a, b, c) > 0 else float('inf')
        # cur番目の竹をどれかの竹と合成するか、何もしない。そのときのMP
        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)
    ans = dfs(0, 0, 0, 0)  # 初期状態では0本目の竹に注目し、どの竹も長さ0とする
    return ans

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N = 0
N, A, B, C = [int(s) for s in input().split()]

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


def divide(l):
    if len(l) == 0:
        return []
    if len(l) == 1:
        return [
            [[], [], []],  # 使わない
            [[l[0]], [], []],  # A で使う
            [[], [l[0]], []],  # B で使う
            [[], [], [l[0]]],  # C で使う
        ]

    res = []
    for a, b, c in divide(l[1:]):
        # 使わない
        res.append([a, b, c])
        # A で使う
        res.append([l[0:1] + a, b, c])
        # B で使う
        res.append([a, l[0:1] + b, c])
        # C で使う
        res.append([a, b, l[0:1] + c])

    return res


min_mp = -1
for a, b, c in divide(bamboos):
    al, ac = sum(a), len(a)
    bl, bc = sum(b), len(b)
    cl, cc = sum(c), len(c)
    if al == 0 or bl == 0 or cl == 0:
        continue

    # 合成に使った魔力
    composite = 10 * ((ac - 1) + (bc - 1) + (cc - 1))
    # 総合魔力
    mp = abs(al - A) + abs(bl - B) + abs(cl - C) + composite

    if mp < min_mp or min_mp == -1:
        min_mp = mp

print(min_mp)

Submission status: Accepted

Solution code:
# coding:utf-8

import sys

INF = float('inf')
MOD = 10 ** 9 + 7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()


n, a, b, c = LI()
A = [a, b, c]
K = [II() for _ in range(n)]


def DFS(cur, x, y, z):
    if cur == n:
        return abs(x - a) + abs(y - b) + abs(z - c) - 30 if min(x, y, z) > 0 else INF
    ret0 = DFS(cur + 1, x, y, z)
    ret1 = DFS(cur + 1, x + K[cur], y, z) + 10
    ret2 = DFS(cur + 1, x, y + K[cur], z) + 10
    ret3 = DFS(cur + 1, x, y, z + K[cur]) + 10
    return min(ret0, ret1, ret2, ret3)


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)]
ans = float('inf')
for p in product(range(4), repeat=N): # 4^Nループ
    take = [[] for _ in range(4)] # [A,B,C,使わない]
    for i in range(N):
        take[p[i]].append(L[i])
    a,b,c = take[0],take[1],take[2]
    if len(a)*len(b)*len(c)==0:
        continue 
    cost = (len(a)+len(b)+len(c)-3)*10
    cost += abs(sum(a)-A)+abs(sum(b)-B)+abs(sum(c)-C)
    ans = min(ans, cost)
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
N, A, B, C = map(int, input().split())
l = [int(input()) for _ in range(N)]
INF = 10**9


def dfs(now):
    global color
    if now == N:
        gosei = 0
        cnt = [0] * 4
        for i in range(N):
            if color[i] > 0 and cnt[color[i]] > 0:
                gosei += 1
            cnt[color[i]] += l[i]
        if min(cnt[1:]) == 0:
            return INF
        diff = abs(A - cnt[1]) + abs(B - cnt[2]) + abs(C - cnt[3])
        return gosei * 10 + diff

    res = INF
    for c in range(4):
        color[now] = c
        res = min(res, dfs(now + 1))
    return res


color = [None] * N
print(dfs(0))

Submission status: Accepted

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

def d(count1,count2,a1,b1,c1):
    if count1==n:
        if min(a1,b1,c1)==0:
            return float('inf')
        else:
            return abs(a-a1)+abs(b-b1)+abs(c-c1)+count2*10
    else:
        e1=d(count1+1,count2,a1,b1,c1)
        e2=d(count1+1,count2,a1+l[count1],b1,c1) if a1==0 else d(count1+1,count2+1,a1+l[count1],b1,c1)
        e3=d(count1+1,count2,a1,b1+l[count1],c1) if b1==0 else d(count1+1,count2+1,a1,b1+l[count1],c1)
        e4=d(count1+1,count2,a1,b1,c1+l[count1]) if c1==0 else d(count1+1,count2+1,a1,b1,c1+l[count1])
    return min(e1,e2,e3,e4)

print(d(0,0,0,0,0))

Submission status: Accepted

Solution code:
#https://atcoder.jp/contests/abc119/tasks/abc119_c
#2019-02-24
N, A, B, C = map(int, input().split())
l = [int(input()) for _ in range(N)]


ans = []
# 00 なら使わない、 01 ならAをつくる、10 ならBをつくる、11ならCをつくる　
for i in range(1 << 2*N):
    A_hon = 0
    B_hon = 0
    C_hon = 0
    A_zai = 0
    B_zai = 0
    C_zai = 0
    for k in range(N):
        if i & (1<<(2*k)) and i & (1<< (2*k)+1):
            C_hon += 1
            C_zai += l[k]
        elif i & (1<<(2*k)+1):
            B_hon += 1
            B_zai += l[k]
        elif i & (1<<(2*k)):
            A_hon += 1
            A_zai += l[k]
    if A_hon * B_hon * C_hon >0:
       ans += [10*(A_hon - 1)+ abs(A_zai - A)+10*(B_hon - 1)+ abs(B_zai - B)+ 10*(C_hon - 1)+ abs(C_zai - C)]
print(min(ans))

Submission status: Accepted

Solution code:
#https://atcoder.jp/contests/abc119/tasks/abc119_c
#2019-02-24
N, A, B, C = map(int, input().split())
l = [int(input()) for _ in range(N)]


ans = float("inf")
# 00 なら使わない、 01 ならAをつくる、10 ならBをつくる、11ならCをつくる　
for i in range(1 << 2*N):
    cost = float("inf")
    A_hon = 0
    B_hon = 0
    C_hon = 0
    A_zai = 0
    B_zai = 0
    C_zai = 0
    for k in range(N):
        if i & (1<<(2*k)) and i & (1<< (2*k)+1):
            C_hon += 1
            C_zai += l[k]
        elif i & (1<<(2*k)+1):
            B_hon += 1
            B_zai += l[k]
        elif i & (1<<(2*k)):
            A_hon += 1
            A_zai += l[k]
    if A_hon * B_hon * C_hon >0:
       cost = 10*(A_hon - 1)+ abs(A_zai - A)+10*(B_hon - 1)+ abs(B_zai - B)+ 10*(C_hon - 1)+ abs(C_zai - C)
    ans = min(cost, ans)
print(ans)

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(1<<N):
    for j in range(1<<N):
        a,b,c = [],[],[]
        cost = 0
        for keta in range(N):
            bit_i = i&(1<<keta)
            bit_j = j&(1<<keta)
            if bit_i==0 and bit_j==0:
                a.append(keta)
            elif bit_i==0 and bit_j>0:
                b.append(keta)
            elif bit_i>0 and bit_j==0:
                c.append(keta)
            else:
                pass
        if a==[] or b==[] or c==[]:
            continue
        cost += 10*(len(a)+len(b)+len(c)-3)
        for l,val in zip([a,b,c],
                         [A,B,C]):
            s = sum([L[li] for li in l])
            cost += abs(s-val)
        ans = min(ans, cost)

print(ans)

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:
N, A, B, C = map(int, input().split())
l = [int(input()) for i in range(N)]
INF = 10**8
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:
from itertools import product

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

abc = [a, b, c]

res = float('inf')
for p in product(range(4), repeat=n):
    le = [0, 0, 0, 0]
    count = [0, 0, 0, 0]
    for i, category in enumerate(p):
        le[category] += l[i]
        count[category] += 1
    tmp = 0
    if count[0] * count[1] * count[2] == 0:
        continue
    for i in range(3):
        tmp += abs(le[i] - abc[i]) + 10 * (count[i] - 1)
    res = min(res, tmp)

print(res)

Submission status: Accepted

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


def dfs(n, a, b, c):
    global N, A, B, C, l
    if n == N:
        return abs(a - A) + abs(b - B) + abs(c - C) if min(a, b, c) > 0 else 10 ** 100
    alpha = dfs(n + 1, a, b, c)
    beta = dfs(n + 1, a + l[n], b, c) + 10
    gamma = dfs(n + 1, a, b + l[n], c) + 10
    delta = dfs(n + 1, a, b, c + l[n]) + 10
    return min(alpha, beta, gamma, delta)


print(dfs(0, 0, 0, 0) - 30)