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 = 12345678901234

for i in range(4**n):
    aa=0
    bb=0
    cc=0
    cnt = 0
    for j in range(n):
        flag = ((i//(4**j))%4)
        if flag == 0:
            aa += l[j]
            cnt += 1
        if flag == 1:
            bb += l[j]
            cnt += 1          
        if flag == 2:
            cc += l[j]
            cnt += 1
    if aa==0 or bb==0 or cc == 0:
        continue
    aans = abs(aa-a)+abs(bb-b)+abs(cc-c)+10*(cnt-3)
    ans = min(ans,aans)
print(ans)

Submission status: Accepted

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

ans = float('inf')
for i in range(4**N):
    a,b,c = [],[],[]
    cost = 0
    for keta in range(N):
        v = (i//(4**keta))%4
        if v==0:   a.append(L[keta])
        elif v==1: b.append(L[keta])
        elif v==2: c.append(L[keta])
        else: pass
    
    if len(a)*len(b)*len(c)==0: continue
    cost += 10*(len(a)+len(b)+len(c)-3)
    cost += abs(sum(a)-A) + abs(sum(b)-B) + abs(sum(c)-C)
    ans = min(ans, cost)

print(ans)

Submission status: Accepted

Solution code:
import itertools

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

cost = 10**10
for s in itertools.product(' ABC', repeat=n):
  al, bl, cl = [], [], []
  for i, _s in enumerate(s):
    if _s == 'A':
      al.append(ll[i])
    elif _s == 'B':
      bl.append(ll[i])
    elif _s == 'C':
      cl.append(ll[i])
  if not al or not bl or not cl:
    continue
  cost = min(cost, 
             10 * (len(al) - 1) + abs(a - sum(al)) + 
             10 * (len(bl) - 1) + abs(b - sum(bl)) + 
             10 * (len(cl) - 1) + abs(c - sum(cl)))
print(cost)

Submission status: Accepted

Solution code:
tl = [0] * 3
n, tl[0], tl[1], tl[2] = map(int, input().split())
l = []
for i in range(n):
	l.append(int(input()))
ans = -1
arr = None
for mask in range(1 << (2 * n)):
	grp = [[], [], []]
	for i in range(n):
		ty = (mask >> (i * 2)) & 3
		if ty > 0:
			grp[ty - 1].append(l[i])
	res = 0
	for i in range(3):
		res += abs(sum(grp[i]) - tl[i]) + 10 * (len(grp[i]) - 1)
		if len(grp[i]) == 0:
			res = ans
			break
	if ans == -1 or res < ans:
		ans = res
		arr = grp
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()))
INF=100000
 
def mp(j,a,b,c):
    if j==N:
      return abs(a-A)+abs(b-B)+abs(c-C)-30 if min(a,b,c)>0 else INF
    ret0=mp(j+1,a,b,c)
    ret1=mp(j+1,a+l[j],b,c)+10
    ret2=mp(j+1,a,b+l[j],c)+10
    ret3=mp(j+1,a,b,c+l[j])+10
    return min(ret0,ret1,ret2,ret3)
print(mp(0,0,0,0))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on 2019/2/24
Solved on 2019/2/24
@author: shinjisu
"""


# ABC 119
def getInt(): return int(input())


def getIntList(): return [int(x) for x in input().split()]


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


def getIntLines(n): return [int(input()) for i in range(n)]


def getIntMat(n):
    mat = []
    for i in range(n):
        mat.append(getIntList())
    return mat


def zeros2(n, m): return [zeros(m)]*n


ALPHABET = [chr(i+ord('a')) for i in range(26)]
DIGIT = [chr(i+ord('0')) for i in range(10)]
N1097 = 10**9 + 7
INF = 10**10


def dmp(x, cmt=''):
    global debug
    if debug:
        if cmt != '':
            print(cmt, ':  ', end='')
        print(x)
    return x


def gen2Bits(n):  # n個の0/1のリストを生成　[0]から変化
    bits = zeros(n)
    for i in range(4**n):
        for k in range(n):
            bits[k] = i % 4
            i //= 4
        yield bits


# 多重ループを再帰で実現
def loop(level, a, b, c, mp):
    global N, L, A, B, C, minMP
    if level == N:
        if min(a, b, c) == 0:
            return INF
        mp += abs(A-a) + abs(B-b) + abs(C-c) - 30  #  一本目にはMP不要
        dmp((minMP, mp), 'min, MP')
        minMP = min(minMP,mp)
        return minMP
    else:
        mp0 = loop(level+1, a, b, c, mp)
        mp1 = loop(level+1, a+L[level], b, c, mp+10)
        mp2 = loop(level+1, a, b+L[level], c, mp+10)
        mp3 = loop(level+1, a, b, c+L[level], mp+10)
        dmp((mp0, mp1, mp2, mp3))
        return min(mp0, mp1, mp2, mp3)


def probC():
    global N, L, A, B, C, minMP
    
    N, A, B, C = getIntList()
    L = []
    for i in range(N):
        L.append(getInt())
    dmp((N, A, B, C ))
    dmp(L)
    d = zeros(3)
    d = A, B, C
    dmp(d)
    minMP = INF
    return loop(0, 0, 0, 0, 0)


def probC_v2():
    N, A, B, C = getIntList()
    L = []
    for i in range(N):
        L.append(getInt())
    dmp((N, A, B, C ))
    dmp(L)
    d = zeros(3)
    d = A, B, C
    dmp(d)
    minMP = INF
    for f in gen2Bits(N):
        dmp(f)
        mp = 0
        lg = zeros(4)
        for i in range(N):
            if lg[f[i]] > 0 and f[i] < 3:
                mp += 10
            lg[f[i]] += L[i]
            #dmp((i,lg,mp))
        #dmp(lg)
        if min(lg[:3]) == 0:  # 4番目は使用しない竹
            continue
        for i in range(3):
            mp += abs(lg[i]-d[i])
        dmp((minMP, mp), 'min, MP')
        minMP = min(minMP, mp)
    return minMP


def probC_v1():
    N, A, B, C = getIntList()
    L = []
    for i in range(N):
        L.append(getInt())
    dmp((N, A, B, C ))
    dmp(L)
    d = zeros(3)
    d[0] = A
    d[1] = B
    d[2] = C
    dmp(d)
    minMP = INF
    for f in gen2Bits(N):
        dmp(f)
        mp = 0
        lg = zeros(4)
        for i in range(N):
            if lg[f[i]] > 0 and f[i] < 3:
                mp += 10
            lg[f[i]] += L[i]
            #dmp((i,lg,mp))
        #dmp(lg)
        allUsed = True
        for i in range(3):  # 4番目は使用しない竹
            if lg[i] == 0:
                allUsed = False
                break
            mp += abs(lg[i]-d[i])
        if not allUsed:
            continue
        dmp((minMP, mp))
        minMP = min(minMP, mp)
    return minMP


debug = False  # True False
print(probC())


def gcd(x, y):  # 最大公約数
    m = max(x, y)
    n = min(x, y)
    while m % n != 0:
        w = m % n
        m = n
        n = w
    return n


def probD():
    N, M = getIntList()
    A = getIntList()
    dmp((N, M))
    dmp(A)
    return num


def probA():
    S = input()
    dmp(S)
    y = int(S[0:4])
    m = int(S[5:7])
    d = int(S[8:10])
    dmp((y,m,d))
    HEI = 'Heisei'
    if y <= 2018:
        return HEI
    if m <= 4:
        return HEI
    else:
        return 'TBD'


def probB():
    N = getInt()
    dmp(N)
    sm = 0.0
    for i in range(N):
        x, u = [a for a in input().split()]
        dmp((x,u))
        if u == 'JPY':
            x = int(x)
        else:
            x = float(x) * 380000.0
        sm += x
    return sm

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc119/tasks/abc119_c
import itertools
import collections
from bisect import bisect



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

    def solve(i, a, b, c):
        if i == N:
            # 最初の追加は合成じゃないので +10 * 3(A,B,Cの分)が余分なので除外する
            # 最低でも１本使わないとダメ
            return abs(A-a) + abs(B-b) + abs(C-c) - 30 if min(a, b, c) > 0 else int(1e15)
        ret0 = solve(i + 1, a, b, c)
        ret1 = solve(i + 1, a + L[i], b, c) + 10
        ret2 = solve(i + 1, a, b + L[i], c) + 10
        ret3 = solve(i + 1, a, b, c + L[i]) + 10
        return min(ret0, ret1, ret2, ret3)

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N,*A=map(int,input().split());a=[[0]*4];r=[0,1,2]
for i in range(N):l=int(input());a+=[[p[j]+l*(i==j)for j in r]+[p[3]+10]for i in r for p in a]
print(min(sum(abs(p[i]-A[i])for i in r)-30+p[3]for p in a if min(p[:3])>0))

Submission status: Accepted

Solution code:
N,*A=map(int,input().split());a=[[0]*4];r=[0,1,2]
for i in range(N):l=int(input());a=[[p[j]+l*(i==j)for j in r]+[p[3]+(i<3)*10]for i in r+[3]for p in a]
print(min(sum(abs(p[i]-A[i])for i in r)-30+p[3]for p in a if min(p[:3])>0))

Submission status: Accepted

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


def dfs(cur, a, b, c):
    if cur == N:
        if min(a, b, c) == 0:  # もし使われない竹があったら,最大のコストを返す
            return float('INF')
        # 最終的に出来上がった三本の竹のコストはどうか
        return sum([abs(_ - __) for _, __ in zip([a, b, c], [A, B, C])])
    sub_cost_A = dfs(cur+1, a+L[cur], b, c) + 10
    sub_cost_B = dfs(cur+1, a, b+L[cur], c)+10
    sub_cost_C = dfs(cur+1, a, b, c+L[cur])+10
    sub_cost_X = dfs(cur + 1, a, b, c)  # どのグループにも使わない
    return min(sub_cost_A, sub_cost_B, sub_cost_C, sub_cost_X)


print(dfs(0, 0, 0, 0)-30)  # a,b,cについて0に連結して＋10のコストをそれぞれ1回受けているので−30して帳尻合わせ

Submission status: Accepted

Solution code:
N,A,B,C=map(int,input().split());a=[[0,0,0,-30]]
for i in range(N):l=int(input());a=[[p[j]+l*(i==j)for j in[0,1,2]]+[p[3]+(i<3)*10]for i in range(4)for p in a]
print(min(abs(p[0]-A)+abs(p[1]-B)+abs(p[2]-C)+p[3]for p in a if min(p[:3])>0))

Submission status: Accepted

Solution code:
N, A, B, C = map(int,input().split())
ans = [[0, 0, 0, -30]]
for i in range(N):
    l = int(input())
    ans = [[p[j] + l * (i == j) for j in [0, 1, 2]] + [p[3] + (i < 3) * 10] for p in ans for i in range(4)]
print(min(abs(p[0] - A) + abs(p[1] - B) + abs(p[2] - C) + p[3] for p in ans if min(p[:3]) > 0))

Submission status: Accepted

Solution code:
N, A, B, C = map(int,input().split())
ans = [[0, 0, 0, 0]]
for i in range(N):
    l = int(input())
    ans = [[p[j] + l * (i == j) for j in [0, 1, 2]] + [p[3]+(i < 3)] for p in ans for i in range(4)]
print(min(abs(p[0] - A) + abs(p[1] - B) + abs(p[2] - C) + p[3] * 10 - 30 for p in ans if min(p[:3]) > 0))

Submission status: Accepted

Solution code:
n,a,b,c=map(int,input().split())
l=[int(input()) for _ in range(n)]
def cal(l,a):
    return abs(sum(l)-a)+10*(len(l)-1)
def ten_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
#0:A
#1:B
#2:C
#3:unused
res=[]
for i in range(4**n):
    s=str(ten_to_n(i+4**n,4))[1:]
    if s.count("0")==0 or s.count("1")==0 or s.count("2")==0:
        continue
    
    ta,tb,tc=[],[],[]
    for j in range(n):
        if s[j]=="0":
            ta.append(l[j])
        if s[j]=="1":
            tb.append(l[j])
        if s[j]=="2":
            tc.append(l[j])
    res.append(cal(ta,a)+cal(tb,b)+cal(tc,c))
print(min(res))

Submission status: Accepted

Solution code:
N, A, B, C = map(int,input().split())
ls = [int(input()) for i in range(N)]
def func(pos, a, b, c):return abs(a - A) + abs(b - B) + abs(c - C) - 30 + 9999 * max(0, 1 - a * b * c) if pos == N else min(func(pos + 1, a + ls[pos] * (i == 0), b + ls[pos] * (i == 1), c + ls[pos] * (i == 2)) + 10 * (i < 3) for i in range(4))
print(func(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 func(pos, a, b, c):return abs(a - A) + abs(b - B) + abs(c - C) - 30 + 10000 * max(0, 1 - a * b * c) if pos == N else min(func(pos + 1, a + ls[pos] * (i == 0), b + ls[pos] * (i == 1), c + ls[pos] * (i == 2)) + 10 * (i < 3) for i in range(4))
print(func(0, 0, 0, 0))

Submission status: Accepted

Solution code:
n, a, b, c = map(int, input().split())
length = []
inf = 10**9 + 7

for i in range(n):
    length.append(int(input()))


def DFS(cur, x, y, z):
    if cur == n:
        if min(x, y, z) > 0:
            return abs(x - a) + abs(y - b) + abs(z - c) - 30
        else:
            return inf

    part0 = DFS(cur + 1, x, y, z)
    part1 = DFS(cur + 1, x + length[cur], y, z) + 10
    part2 = DFS(cur + 1, x, y + length[cur], z) + 10
    part3 = DFS(cur + 1, x, y, z + length[cur]) + 10

    return min(part0, part1, part2, part3)


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

Submission status: Accepted

Solution code:
INF = 10 ** 18

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

def func(pos, a, b, c):return abs(a - A) + abs(b - B) + abs(c - C) - 30 + INF * max(0, 1 - a * b * c) if pos == N else min(func(pos + 1, a + ls[pos] * (i == 0), b + ls[pos] * (i == 1), c + ls[pos] * (i == 2)) + 10 * (i < 3) for i in range(4))
print(func(0, 0, 0, 0))

Submission status: Accepted

Solution code:
n, a, b, c = map(int, input().split())
length = []
inf = 10**9 + 7

for i in range(n):
    length.append(int(input()))


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) else inf

    part0 = DFS(cur + 1, x, y, z)
    part1 = DFS(cur + 1, x + length[cur], y, z) + 10
    part2 = DFS(cur + 1, x, y + length[cur], z) + 10
    part3 = DFS(cur + 1, x, y, z + length[cur]) + 10

    return min(part0, part1, part2, part3)


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

Submission status: Accepted

Solution code:
PENALTY = 3000


def func(n, a, b, c):
    if n == N:
        return (abs(a - A) if a > 0 else PENALTY) +\
               (abs(b - B) if b > 0 else PENALTY) +\
               (abs(c - C) if c > 0 else PENALTY)
    else:
        r0 = func(n + 1, a, b, c)
        r1 = func(n + 1, a + l[n], b, c) + 10
        r2 = func(n + 1, a, b + l[n], c) + 10
        r3 = func(n + 1, a, b, c + l[n]) + 10
        return min(r0, r1, r2, r3)


N, A, B, C = map(int, input().split())
l = [int(input()) for _ in range(N)]
ans = func(0, 0, 0, 0)
print(ans - 30)

Submission status: Accepted

Solution code:
#!/usr/bin/env python

from collections import deque
import itertools as it
import sys
import math

sys.setrecursionlimit(1000000)

INF = 10 ** 18

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

ans = 10 ** 12

for i in range(4 ** N):
  S = [0, 0, 0, 0]
  hoge = i
  cost = -30
  for j in range(N):
    S[hoge % 4] += l[j]
    if hoge % 4 < 3:
      cost += 10
    hoge /= 4
  if S[0] * S[1] * S[2] == 0:
    continue
  ans = min(ans, cost + abs(S[0] - A) + abs(S[1] - B) + abs(S[2] - C))

print ans

Submission status: Accepted

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

inf=10**9

# cur + 1 本目までの竹を合成して長さx, y, zの竹を作る -> この3本を短縮, 延長して長さa, b, cの竹を得る
# x, y, z > 0が必要

def dfs(cur, x, y, z):
    if cur == n:
        if min(x, y, z) > 0:
            return abs(x - a) + abs(y - b) + abs(z - c) - 30 # x, y, z = 0, 0, 0 からスタートしているので, 余分な合成が3回ある 
        else:
            return inf
    ret0 = dfs(cur + 1, x, y, z) # cur + 1 本目の竹 = L[cur] を使わない
    ret1 = dfs(cur + 1, x + L[cur], y, z) + 10 # cur + 1 本目の竹を x と合成
    ret2 = dfs(cur + 1, x, y + L[cur], z) + 10 # cur + 1 本目の竹を y と合成
    ret3 = dfs(cur + 1, x, y, z + L[cur]) + 10 # cur + 1 本目の竹を z と合成
    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()))
l=[int(input()) for i in range(n)]
ans=10**9
for k in itertools.product(range(4),repeat=n):
    A=[[] for i in range(4)]
    for i in range(n):
        A[k[i]]+=[l[i]]
    if A[1] and A[2] and A[3]:
        tmp=10*(n-len(A[0])-3)
        tmp+=abs(a-sum(A[1]))
        tmp+=abs(b-sum(A[2]))
        tmp+=abs(c-sum(A[3]))
        ans=min(tmp,ans)
print(ans)

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())
takes = []
for i in range(N):
    takes.append(int(input()))
#  g = [100,90,80]
#  takes = [98, 40, 30, 21, 80, 0,0,0]
takes = takes+[0,0,0,0,0,0,0]
takes = takes[:8]
g = [A, B, C]
combis = []
for i in range(4):
    for j in range(4):
        for k in range(4):
            for l in range(4):
                for m in range(4):
                    for n in range(4):
                        for o in range(4):
                            for p in range(4):
                                #  print(i,j,k,l,m,n,o,p)
                                combis.append([i,j,k,l,m,n,o,p])
#  print(combis)
min_diff = 10**10
for combi in combis:
    A,B,C = 0,0,0
    for idx, el in enumerate(combi):
        if el == 0:
            A += takes[idx]
        elif el == 1:
            B += takes[idx]
        elif el == 2:
            C += takes[idx]

        if A == 0 or B == 0 or C == 0:
            continue

        A_diff = abs(g[0] - A) + (len([_ for _ in combi if _ == 0]) - 1)*10
        B_diff = abs(g[1] - B) + (len([_ for _ in combi if _ == 1]) - 1)*10
        C_diff = abs(g[2] - C) + (len([_ for _ in combi if _ == 2]) - 1)*10
        diff = A_diff + B_diff + C_diff
        min_diff = min(diff, min_diff)

print(min_diff)

Submission status: Accepted

Solution code:
from itertools import permutations
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

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


def dfs(dep, a, b, c, not_use)->int:
    if dep == N:
        return abs(A-a)+abs(B-b)+abs(C-c) - 30 if min(a, b, c) > 0 else float('inf')
    else:
        d1 = dfs(dep+1, a+l[dep], b, c, not_use)+10
        d2 = dfs(dep+1, a, b+l[dep], c, not_use)+10
        d3 = dfs(dep+1, a, b, c+l[dep], not_use)+10
        d4 = dfs(dep+1, a, b, c, not_use+l[dep])

        return min(d1, d2, d3, d4)


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

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc119/tasks/abc119_c
import copy
import itertools


# 長さ t の竹を L から少ないコストで作る方法を探す
def solve(t, l, c):
    min_cost = 100000
    cand = []

    ptn = 1 << len(l)
    # print(l)
    # print("ptn = {}".format(ptn))
    for i in range(1, ptn):
        answers = []
        for j in range(0, len(l)):
            if (i >> j) & 1:
                answers.append(l[j])
        temp_cost = abs(t - sum(answers)) + (10 * (len(answers) - 1) if len(answers) > 1 else 0)
        if temp_cost < min_cost:
            min_cost = temp_cost
            cand = answers

    # print("cand = {}".format(cand))
    for d in cand:
        del l[l.index(d)]
    return min_cost, l


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

    answer = 1000000
    for targets in itertools.permutations([A, B, C]):
        is_done = [False] * 3
        L2 = copy.copy(L)
        total_cost = 0
        for i in range(len(targets)):
            if targets[i] in L2:
                del L2[L2.index(targets[i])]
                is_done[i] = True

        for i in range(3):
            if is_done[i]:
                continue

            cost, L2 = solve(targets[i], L2, 0)
            total_cost += cost

        answer = min(answer, total_cost)

    print(answer)


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, x, y, z):
    if cur == n:
        if min(x, y, z) > 0:
            return abs(x - a) + abs(y - b) + abs(z - c) - 30
        else:
            return 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))

Submission status: Accepted

Solution code:
def func(n):
    if n == N:
        p = 0
        for c in count:
            if c == 0:
                return
            p += 10 * (c - 1)
        p += abs(length[0] - A)
        p += abs(length[1] - B)
        p += abs(length[2] - C)
        global ans
        if p < ans:
            ans = p
    else:
        count[0] += 1
        length[0] += l[n]
        func(n + 1)
        count[0] -= 1
        length[0] -= l[n]
        count[1] += 1
        length[1] += l[n]
        func(n + 1)
        count[1] -= 1
        length[1] -= l[n]
        count[2] += 1
        length[2] += l[n]
        func(n + 1)
        count[2] -= 1
        length[2] -= l[n]
        func(n + 1)


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

ans = 3000
count = [0, 0, 0]
length = [0, 0, 0]
func(0)
print(ans)

Submission status: Accepted

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

def dfs(cnt, a, b, c):
    if cnt == N:
        return abs(a - A) + abs(b - B) + abs(c - C) - 30 if min(a, b, c) > 0 else INF
    
    ans1 = dfs(cnt+1, a, b, c)
    ans2 = dfs(cnt+1, a + L[cnt], b, c) + 10
    ans3 = dfs(cnt+1, a, b + L[cnt], c) + 10
    ans4 = dfs(cnt+1, a, b, c + L[cnt]) + 10
    return min(ans1, ans2, ans3, ans4)

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

Submission status: Accepted

Solution code:
import itertools

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

# kind=0: 使わない
# kind=1: aの材料とする
# kind=2: bの材料とする
# kind=3: cの材料とする
ans = float('inf')
for bit in range(4 ** n):
    kinds = []
    while bit > 0:
        kinds.append(bit % 4)
        bit = bit // 4
    while len(kinds) != n:
        kinds.append(0)
    
    sizes = [0, 0, 0, 0]
    counts = [0, 0, 0, 0]
    for i in range(n):
        sizes[kinds[i]] += l[i]
        counts[kinds[i]] += 1

    _, p, q, r = sizes
    _, s, t, u = counts

    if s == 0 or t == 0 or u == 0: continue
    cost = abs(p - a) + abs(q - b) + abs(r - c) + 10 * max(0, s - 1) + 10 * max(0, t - 1) + 10 * max(0, u - 1)
    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)]
def calc(r,a,b,c):
    if r == n:
        return abs(a-A) + abs(b-B) + abs(c-C) - 30 if min(a,b,c) > 0 else 10**9
    else:
        root1 = calc(r+1,a,b,c)
        root2 = calc(r+1,a+l[r],b,c) +10
        root3 = calc(r+1,a,b+l[r],c) +10
        root4 = calc(r+1,a,b,c+l[r]) +10
        return min(root1,root2,root3,root4)
print(calc(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

ans=1e10
for p in product([0,1,2,3],repeat=N):
    m=[-1,-1,-1]
    mp=0
    for i in range(N):
        if p[i]!=3:
            if m[p[i]]==-1:
                m[p[i]]=l[i]
            else:
                mp+=10
                m[p[i]]+=l[i]
    mp+=abs(m[0]-A)+abs(m[1]-B)+abs(m[2]-C)
    if min(m)>=0:
        ans=min(mp,ans)
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())
fouradic=[0]*n
ret=3000
while fouradic!=([3]*n):
  a_=0
  b_=0
  c_=0
  for i in range(n):
    if fouradic[i]<3:
      fouradic[i]+=1
      break
    fouradic[i]=0
  if not (fouradic.count(1) and fouradic.count(2) and fouradic.count(3)):
    continue
  ret_in=0
  ret_in+=10*(n-fouradic.count(0)-3)
  for i in range(n):
    if fouradic[i]==1:
      a_+=l[i]
    if fouradic[i]==2:
      b_+=l[i]
    if fouradic[i]==3:
      c_+=l[i]
  ret_in+=abs(a-a_)+abs(b-b_)+abs(c-c_)
  ret=min(ret,ret_in)
print(ret)

Submission status: Accepted

Solution code:
N,A,B,C=map(int,input().split(" "))
INF=10**9
tmp=[int(input()) for _ in range(N)]
def calc(num,a,b,c):
    if(num==N):
        if(min(a,b,c)>0):
            return abs(A-a)+abs(B-b)+abs(C-c)-30
        else:
            return INF
    res=calc(num+1,a,b,c)
    res1=calc(num+1,a+tmp[num],b,c)+10
    res2=calc(num+1,a,b+tmp[num],c)+10
    res3=calc(num+1,a,b,c+tmp[num])+10
    return min(res,res1,res2,res3)
print(calc(0,0,0,0))

Submission status: Accepted

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


def recur(i, a, b, c, cost):
    if i == N:
        if a*b*c == 0:
            return INF
        else:
            return cost + abs(A-a) + abs(B-b) + abs(C-c) - 30

    cand1 = recur(i+1, a+l[i], b, c, cost + 10)
    cand2 = recur(i+1, a, b+l[i], c, cost + 10)
    cand3 = recur(i+1, a, b, c+l[i], cost + 10)
    cand4 = recur(i+1, a, b, c, cost)
    return min([cand1, cand2, cand3, cand4])


print(recur(0, 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()))

def dfs(n,a,b,c):
    if n == N:
        if min(a,b,c)>0:
            return abs(A-a)+abs(B-b)+abs(C-c)-30
        else:
            return 10000
    root1=dfs(n+1,a+L[n],b,c)+10
    root2=dfs(n+1,a,b+L[n],c)+10
    root3=dfs(n+1,a,b,c+L[n])+10
    root4=dfs(n+1,a,b,c)
    return min(root1,root2,root3,root4)
    
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(k,a,b,c):
    global N,A,B,C,l
    if k == N:
        return abs(a-A)+abs(b-B)+abs(c-C)-30 if min(a,b,c)>0 else 1000000
    det0 = dfs(k+1, a+l[k], b, c)+10
    det1 = dfs(k+1, a, b+l[k], c)+10
    det2 = dfs(k+1, a, b, c+l[k])+10
    det3 = dfs(k+1, a, b, c)
    return min(det0, det1, det2, det3)
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 return_4(x, k):
    four_hairetu = [0 for i in range(k)]
    for i in range(k):
        four_hairetu[i] = x%4
        x = x//4
    return four_hairetu
min_mp = 1000000
for i in range(4**N):
    ABC = [0,0,0,0] #[not,A,B,C] = [0,1,2,3]
    zABC = return_4(i,N)
    if zABC.count(1)==0 or zABC.count(2)==0 or zABC.count(3)==0:
        continue
    else:
        kouho_mp = (zABC.count(1)-1)*10 + \
                   (zABC.count(2)-1)*10 + \
                   (zABC.count(3)-1)*10
        for j in range(N):
            ABC[zABC[j]] += l[j]
        kouho_mp += abs(ABC[1]-A) + abs(ABC[2]-B) + abs(ABC[3]-C)
        min_mp = min(min_mp, kouho_mp)
print(min_mp)

Submission status: Accepted

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

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

inf = float('inf')

def dfs(x, a, b, c, m):
  if(x == N):
    if(min(a, b, c) > 0):
      return abs(a-A) + abs(b-B) + abs(c-C) + (m-3) * 10
    else:
      return inf
  ret0 = dfs(x+1, a, b, c, m)
  ret1 = dfs(x+1, a+L[x], b, c, m+1)
  ret2 = dfs(x+1, a, b+L[x], c, m+1)
  ret3 = dfs(x+1, a, b, c+L[x], m+1)
  return min(ret0, ret1, ret2, ret3)

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

Submission status: Accepted

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

INF = 10 ** 9
def dfs(cur, a, b, c, m):
  if cur == N:
    if min(a, b, c) == 0:
      return INF
    return abs(A-a)+abs(B-b)+abs(C-c)+(m-3)*10

  res0 = dfs(cur+1, a, b, c, m)
  res1 = dfs(cur+1, a+L[cur], b, c, m+1)
  res2 = dfs(cur+1, a, b+L[cur], c, m+1)
  res3 = dfs(cur+1, a, b, c+L[cur], m+1)

  return min(res0, res1, res2, res3)

print(dfs(0, 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()))

def dfs(cnt,for_a,for_b,for_c):
    if cnt==n:
        if for_a != 0 and for_b != 0 and for_c != 0:
            return abs(a-for_a) + abs(b-for_b) + abs(c-for_c) - 30
        else:
            return 10**10
    else:
        return min(dfs(cnt+1,for_a,for_b,for_c),\
                   dfs(cnt+1,for_a+l[cnt],for_b,for_c) + 10,\
                   dfs(cnt+1,for_a,for_b+l[cnt],for_c) + 10,\
                   dfs(cnt+1,for_a,for_b,for_c+l[cnt]) + 10)

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

Submission status: Accepted

Solution code:
from collections import Counter
import itertools
N,A,B,C=map(int,input().split())
l=[]
for i in range(N):
  l.append(int(input()))
ans=float('inf')
for item in itertools.product(range(4),repeat=N):
  c=Counter(item)
  if c[0]==0 or c[1]==0 or c[2]==0:
    continue
  d=[0,0,0,0]
  for i in range(N):
    d[item[i]]+=l[i]
  kans=(abs(A-d[0])+abs(B-d[1])+abs(C-d[2])+10*(c[0]+c[1]+c[2]-3))
  ans=min(ans,kans)
print(ans)

Submission status: Accepted

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

from collections import deque
q = deque()
q.append([0,0,0,0,0])
mp = []
while q:
    depth, a, b, c, mcnt= q.pop()
    if depth == N:
        mp.append(abs(A-a)+abs(B-b)+abs(C-c)+mcnt*10-30 if min(a,b,c) > 0 else 10**9)
    else:
        q.append([depth+1, a, b, c,mcnt])
        q.append([depth+1, a+l[depth], b, c,mcnt+1])
        q.append([depth+1, a, b+l[depth], c,mcnt+1])
        q.append([depth+1, a, b, c+l[depth],mcnt+1])

print(min(mp))