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 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 dfs(i, a_len, b_len, c_len):
  if i == N:
    if a_len == 0 or b_len == 0 or c_len == 0:
      return float('inf')
    else:
      return abs(a_len - A) + abs(b_len - B) + abs(c_len - C) - 30
  
  br1 = dfs(i + 1, a_len + L[i], b_len, c_len) + 10
  br2 = dfs(i + 1, a_len, b_len + L[i], c_len) + 10
  br3 = dfs(i + 1, a_len, b_len, c_len + L[i]) + 10
  br4 = dfs(i + 1, a_len, b_len, c_len)
  
  return min(br1, br2, br3, br4)

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

Submission status: Accepted

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


def dfs(a, b, c, i):
    if i == N:
        if a == 0 or b == 0 or c == 0:
            return 1e9

        return abs(A - a) + abs(B - b) + abs(C - c)

    dfs_not_used = dfs(a, b, c, i+1)
    dfs_A = dfs(a+l[i], b, c, i+1) + + 10*(a != 0)
    dfs_B = dfs(a, b+l[i], c, i+1) + 10*(b != 0)
    dfs_C = dfs(a, b, c+l[i], i+1) + 10*(c != 0)

    return min(dfs_not_used, dfs_A, dfs_B, dfs_C)


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

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
    no_merge = dfs(cur+1, a, b, c)
    merge_a = dfs(cur+1, a+L[cur], b, c) + 10
    merge_b = dfs(cur+1, a, b+L[cur], c) + 10
    merge_c = dfs(cur+1, a, b, c+L[cur]) + 10
    return min(no_merge, merge_a, merge_b, merge_c)

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

Submission status: Accepted

Solution code:
N, A, B, C = map(int, input().split())
L = [int(input()) for _ in range(N)]
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
    non_marge = dfs(cur+1, a, b, c)
    marge_a = dfs(cur+1, a+L[cur], b, c) + 10
    marge_b = dfs(cur+1, a, b+L[cur], c) + 10
    marge_c = dfs(cur+1, a, b, c+L[cur]) + 10
    return min(non_marge, marge_a, marge_b, marge_c)

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

Submission status: Accepted

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

mina = 1001
minb = 1001
minc = 1001

ans = 999999
for i in range(4 ** N):
    tmpA = []
    tmpB = []
    tmpC = []
    mp = 0
    for j in reversed(range(0, N * 2, 2)):

            #bit 11の場合　使わない場合
            if (i & (1 << j)) and (i & (1 << (j + 1))):
                pass
            #bit 10の場合　Cの材料
            elif (i & (1 << j)) and not (i & (1 << (j + 1))):
                tmpC.append(bam[int(j / 2)])
            #bit 01の場合　Bの材料
            elif not (i & (1 << j)) and (i & (1 << (j + 1))):
                tmpB.append(bam[int(j / 2)])
            #bit 00の場合　Aの材料
            elif not (i & (1 << j)) and not (i & (1 << (j + 1))):
                tmpA.append(bam[int(j / 2)])
            #どれかが空なら次へ
            
    if not tmpA or not tmpB or not tmpC:
        continue
    #合体処理
    else:
        mp = (len(tmpA) - 1) * 10 + (len(tmpB) - 1) * 10 + (len(tmpC) - 1) * 10
        tmpA = sum(tmpA)
        tmpB = sum(tmpB)
        tmpC = sum(tmpC)
    mp += abs(A- tmpA) + abs(B - tmpB) + abs(C - tmpC)
    ans = min(ans, mp)

print(ans)

Submission status: Accepted

Solution code:
N, A, B, C = [int(x) for x in input().split()]
l = []
for i in range(N):
  l.append(int(input()))
 
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 10000000
  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:
#!/usr/bin/env python3
import sys
import itertools

def solve(N: int, A: int, B: int, C: int, L: "List[int]"):

    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 100000

        cur_l = L[cur]
        nex = cur + 1

        ret0 = dfs(nex,a,b,c)
        ret1 = dfs(nex,a+cur_l,b,c)+10
        ret2 = dfs(nex,a,b+cur_l,c)+10
        ret3 = dfs(nex,a,b,c+cur_l)+10

        return min(ret0,ret1,ret2,ret3)

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

    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = int(next(tokens))  # type: int
    B = int(next(tokens))  # type: int
    C = int(next(tokens))  # type: int
    l = [ int(next(tokens)) for _ in range(N) ]  # type: "List[int]"
    solve(N, A, B, C, l)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
n,goalA,goalB,goalC = list(map(int, input().split()))
l = [int(input()) for _ in range(n)]
cost = []
isum = 0

def dfs(depth: int, a: int, b: int, c:int, isum:int):
    if depth == n:
        if min(a,b,c) > 0:
            cost.append(abs(a - goalA) + abs(b - goalB) + abs(c - goalC) +isum - 30)
            return
        else:
            cost.append(10**8)
            return
    
    dfs(depth+1, a, b, c, isum)
    dfs(depth+1, a + l[depth], b, c, isum + 10) 
    dfs(depth+1, a , b + l[depth], c, isum+10) 
    dfs(depth+1, a , b, c + l[depth], isum + 10) 
    

dfs(0,0,0,0,isum)
print(min(cost))

Submission status: Accepted

Solution code:
# 解けなかったので解説を見た
# 竹の総数が8と少なく、それぞれの竹の利用パターンもA/B/Cの材料とするor使わないの4種類なので全列挙する
# 可能なパターンを全列挙するのであればdfsを用いて、消費MPの最小値を考えれば良い

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


def dfs(i, a_len, b_len, c_len):
    # 探索の葉に達したときは残りの延長・短縮の工数を調べて返す
    if i == N:
        if a_len == 0 or b_len == 0 or c_len == 0:  # 0を延長して竹を作ることはできない
            return float('inf')
        else:
            return abs(a_len - A) + abs(b_len - B) + abs(c_len - C) - 30

    br1 = dfs(i + 1, a_len + L[i], b_len, c_len) + 10  # L[i]をaに連結する場合
    br2 = dfs(i + 1, a_len, b_len + L[i], c_len) + 10  # L[i]をbに連結する場合
    br3 = dfs(i + 1, a_len, b_len, c_len + L[i]) + 10  # L[i]をcに連結する場合
    br4 = dfs(i + 1, a_len, b_len, c_len)  # L[i]を使わない場合

    return min(br1, br2, br3, br4)


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

Submission status: Accepted

Solution code:
import itertools
n,a,b,c=map(int,input().split())
ans=float("inf")
l=[]
for i in range(n):l.append(int(input()))
for i in itertools.product([1,2,3,0],repeat=n):
    if i.count(1) and i.count(2) and i.count(3):
        m=[0,0,0,0]
        for j in range(n):
            m[i[j]]+=l[j]
        ans=min(ans,abs(m[1]-a)+abs(m[2]-b)+abs(m[3]-c)+(n-i.count(0)-3)*10)
print(ans)

Submission status: Accepted

Solution code:
from sys import exit, stdin
N, A, B, C = [int(_) for _ in stdin.readline().rstrip().split()]
L = [int(stdin.readline().rstrip()) for _ in range(N)]
INF = 10**9

def rec(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 = 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:
from __future__ import print_function
import numpy as np
import sys
import math,string,itertools,fractions,heapq,collections,re,array,bisect,copy,functools
# from itertools import accumulate
# from collections import deque
import random

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


# a,b,cの長さの竹の組が欲しい．
# 目的竹a,b,c
# n本の竹l1,l2,l3...., l_{n}
#
# 竹の集合Lについて，Lの要素それぞれを{a,b,c}に分類していく重複順列を考える．
# 「 L->{a,b,c} 」の重複順列の選び方は3^n通り．
# それらを全て列挙したい．
# 変数bitは，竹の割り当て（重複順列の選び方）を表す．
# 例えば，bit= 00 10 01 11 00 10 
# のとき，
# l0 -> none
# l1 -> b
# l2 -> a
# l3 -> c
# l4 -> none
# l5 -> b
# などと表現できる．
# これは，
# bitを2*i桁右シフトして0b11の剰余をとったものを判定することで分岐できる．
# 
# それぞれの竹の割り当ての場合について，
# その時の{a,b,c}の値を計算する．
 
# l=np.zeros(n,dtype=int)
# for i in range(n):
#     l[i]=int(input())
# eprint(l)
# ll=np.sort(l)
# eprint(ll)


n,a,b,c=map(int,input().split())
l=np.zeros(n,dtype=int)

for i in range(n):
    l[i]=int(input())
# l=[int(x) for x in input()]

# eprint("l:",end="" )
# eprint(l)
    
minx = sys.maxsize

for bit in range(0,1<<2*n):
    da=0
    db=0
    dc=0
    cnt=0
    for i in range(0,n):
        if (bit>>2*i)&(0b11) ==0:
            continue
        elif (bit>>2*i)&(0b11) == 1:
            da+=l[i]
            cnt+=10
        elif (bit>>2*i)&(0b11) == 2:
            db+=l[i]
            cnt+=10
        elif (bit>>2*i)&(0b11) == 3:
            dc+=l[i]
            cnt+=10
    if da*db*dc==0:
        continue
    else:
        cnt+=abs(da-a)+abs(db-b)+abs(dc-c)-30
        # eprint("cnt: ", end="")
        # eprint(cnt)
        if  cnt < minx:
            minx = cnt

print(minx)

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 dfs(dep, a,b,c):
    if dep == n:
        return abs(a-A) + abs(b-B) + abs(c-C) if min(a,b,c) > 0 else 10**8
    re0 = dfs(dep+1, a,b,c)
    re1 = dfs(dep+1, a+l[dep], b,c) + (10 if a != 0 else 0)
    re2 = dfs(dep+1, a, b+l[dep],c) + (10 if b != 0 else 0)
    re3 = dfs(dep+1, a, b, c+l[dep]) + (10 if c != 0 else 0)
    return min(re0, re1, re2, re3)

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:
from collections import defaultdict
import sys,heapq,bisect,math,itertools,string,queue,datetime
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inpl(): return list(map(int, input().split()))
def inpl_str(): return list(input().split())

N,A,B,C = inpl()
ll = [int(input()) for i in range(N)]

def saiki(cnt,a,b,c,MP):
    if cnt == N:
        if a==0 or b==0 or c==0:
            return INF
        MP += abs(a-A) + abs(b-B) + abs(c-C)
        return MP
    else:
        l = ll[cnt]
        if a == 0:
            tmpa = saiki(cnt+1,a+l,b,c,MP)
        else:
            tmpa = saiki(cnt+1,a+l,b,c,MP+10)
        if b == 0:
            tmpb = saiki(cnt+1,a,b+l,c,MP)
        else:
            tmpb = saiki(cnt+1,a,b+l,c,MP+10)
        if c == 0:
            tmpc = saiki(cnt+1,a,b,c+l,MP)
        else:
            tmpc = saiki(cnt+1,a,b,c+l,MP+10)
        tmpd = saiki(cnt+1,a,b,c,MP)
        return min(tmpa,tmpb,tmpc,tmpd)

print(saiki(0,0,0,0,0))

Submission status: Accepted

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

    
def dfs(i, 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)
    p = 0 if a == 0 else 10
    q = 0 if b == 0 else 10
    r = 0 if c == 0 else 10
    res1 = dfs(i+1, a, b, c)
    res2 = dfs(i+1, a+L[i], b, c) + p
    res3 = dfs(i+1, a, b+L[i], c) + q
    res4 = dfs(i+1, a, b, c+L[i]) + r
    return min(res1, res2, res3, res4)


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

Submission status: Accepted

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

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

for i in range(4**n):
    A, B, C = 0, 0, 0
    tmp_ans, tmp = 0, i
    cnt = 0
    for j in range(n):
        digit = tmp//4**(n-j-1)
        if digit == 1:
            A += l[j]
            cnt += 1
        if digit == 2:
            B += l[j]
            cnt += 1
        if digit == 3:
            C += l[j]
            cnt += 1
        tmp %= 4**(n-j-1)
    if A != 0 and B != 0 and C != 0:
        tmp_ans += abs(A-a)+abs(B-b)+abs(C-c)
        tmp_ans += max(0, (cnt-3)*10)
        ans = min(ans, tmp_ans)

print(ans)

Submission status: Accepted

Solution code:
import itertools

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

index_list = list(itertools.product(range(4), repeat=N))

min_mp_used = 100000000
for i in range(len(index_list)):
    tmp_index = index_list[i]
    kadomatsu = [0] * 3
    count_stick = 0
    for m in range(N):
        for j in range(3):
            if tmp_index[m] == j:
                kadomatsu[j] += l[m]
                count_stick += 1
    if kadomatsu.count(0):
        continue
    mp_used = 0
    for k in range(3):
        mp_used += abs(ans_kadomatsu[k] - kadomatsu[k])
    mp_used += 10 * (count_stick - 3)
    if min_mp_used > mp_used:
        min_mp_used = mp_used
print(min_mp_used)

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:
#    print(cur, a, b, c)
    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
#  print(cur, 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 = []
INF = float('inf')
for i in range(N):
    L.append(int(input()))


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

    return tmp


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

Submission status: Accepted

Solution code:
from itertools import*
n,a,b,c,*l=map(int,open(0).read().split())
ans=float("inf")
for i in product(range(4),repeat=n): #3を捨てる
    take=[[],[],[]]
    for k,j in enumerate(i):
        if j==3:
            continue
        take[j].append(l[k])
    *tmp,=map(len,take)
    if all(tmp):
        x,y,z=map(sum,take)
        ans=min(ans, (sum(tmp)-3)*10 + abs(x-a) + abs(y-b) + abs(z-c) )
print(ans)

Submission status: Accepted

Solution code:
from itertools import*
n,a,b,c,*l=map(int,open(0).read().split())
a=a,b,c
m=10**18
for s in product(*[range(4)]*n):
  if all(i in s for i in(0,1,2)):
    t,c=[0]*4,[0]*4
    for i,v in zip(s,l):
      t[i]+=v
      c[i]+=1
    m=min(m,sum(abs(x-y)+~-z*10for x,y,z in zip(a,t,c)))
print(m)

Submission status: Accepted

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

import itertools

w = [0,1,2,3]

ss=1000000000000000
for ci in itertools.product(*([w]*N)):
  cc=[0,0,0,0]
  vc=[0,0,0]
  for i in range(N):
    cc[ci[i]]+=1
    if (ci[i]>0):
      vc[ci[i]-1]+=li[i]
  if ( cc[1]>0 and cc[2]>0 and cc[3]>0 ):
    _ss = abs(vc[0]-A)+abs(vc[1]-B)+abs(vc[2]-C)+(cc[1]+cc[2]+cc[3]-3)*10
    if (_ss<ss):
      ss = _ss
print ( ss )

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

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


def dfs(idx, a, b, c):

    if idx == N:
        if a == 0 or b == 0 or c == 0:
            return 1e9

        return abs(A - a) + abs(B - b) + abs(C - c)

    x = dfs(idx + 1, a, b, c)
    y = dfs(idx + 1, a + L[idx], b, c) + 10*(a != 0)
    z = dfs(idx + 1, a, b + L[idx], c) + 10*(b != 0)
    w = dfs(idx + 1, a, b, c + L[idx]) + 10*(c != 0)
    return min(x, y, z, w)


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

Submission status: Accepted

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


def f(a, b, c, n):
    if n == N:
        return abs(A - a) + abs(B - b) + abs(C - c) - 30 if min(a, b, c) > 0 else 10 ** 9
    ans = f(a, b, c, n + 1)
    ans = min(ans, f(a + l[n], b, c, n + 1) + 10)
    ans = min(ans, f(a, b + l[n], c, n + 1) + 10)
    ans = min(ans, f(a, b, c + l[n], n + 1) + 10)
    return ans


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


def dfs(lances, d, len_a, len_b, len_c):
    if d == N:
        if min(len_a, len_b, len_c) == 0:
            return INF
        else:
            return abs(A-len_a) + abs(B-len_b) + abs(C-len_c) - 30
    use_no = dfs(lances, d+1, len_a, len_b, len_c)
    use_for_a = dfs(lances, d+1, len_a + lances[d], len_b, len_c) + 10
    use_for_b = dfs(lances, d+1, len_a, len_b + lances[d], len_c) + 10
    use_for_c = dfs(lances, d+1, len_a, len_b, len_c + lances[d]) + 10
    return min(use_no, use_for_a, use_for_b, use_for_c)


print(dfs(L, 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(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 INF
    else:
        p = dfs(n+1, a, b, c)
        q = dfs(n+1, a+l[n], b, c) + 10
        r = dfs(n+1, a, b+l[n], c) + 10
        s = dfs(n+1, a, b, c+l[n]) + 10
        return min(p, q, r, s)

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

Submission status: Accepted

Solution code:
def grouping(data, n):
  return _grouping(data, n, 0)
  
    
def _grouping(data, n, c):
  if c==len(data):
    return [tuple([] for _ in range(n))]
  else:
    tg = data[c]
    b  = _grouping(data, n, c+1)
    a  = [] 
    for i in range(n):
      for t in b:
        a.append(tuple(e + [tg] if idx==i else e for idx, e in enumerate(t)))
    return a

n, a, b, c = map(int, input().split(" "))
reqs = (a, b, c)
ls = [int(input()) for _ in range(n)]
gr = filter(lambda x: len(x[0])!=0 and len(x[1])!=0 and len(x[2])!=0, grouping(ls, 4))
print(min(sum(10*(len(es)-1) + abs(sum(es)-r)for es, r in zip(t, reqs))for t in gr))

Submission status: Accepted

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

def dfs(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
  res0 = dfs(cur + 1, a, b, c)
  res1 = dfs(cur + 1, a + l[cur], b, c) + 10
  res2 = dfs(cur + 1, a, b + l[cur], c) + 10
  res3 = dfs(cur + 1, a, b, c + l[cur]) + 10
  return min(res0, res1, res2, res3)

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

Submission status: Accepted

Solution code:
# 破壊的なのでなおしたい
def grouping(lst):
  if len(lst)==0:
    return [([],[],[],[])]
  else:
    elm = lst.pop()
    ans = []
    bf = grouping(lst)
    for i in range(4):
      for t in bf:
        ans.append(tuple(g + [elm] if idx==i else g for idx, g in enumerate(t)))
    return ans

n, a, b, c = map(int, input().split(" "))
reqs = (a, b, c)
ls = [int(input()) for _ in range(n)]
gr = filter(lambda x: len(x[0])!=0 and len(x[1])!=0 and len(x[2])!=0, grouping(ls))
print(min(sum(10*(len(es)-1) + abs(sum(es)-r)for es, r in zip(t, reqs))for t in gr))

Submission status: Accepted

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


def dfs(cur, a, b, c):
    if cur == N:
        return abs(a-A) + abs(b-B) + abs(c-C) - 30 if min(a, b, c) else 1000000000

    path_null = dfs(cur+1, a, b, c)
    path_a = dfs(cur+1, a+L[cur], b, c) + 10
    path_b = dfs(cur+1, a, b+L[cur], c) + 10
    path_c = dfs(cur+1, a, b, c+L[cur]) + 10

    return min(path_null, path_a, path_b, path_c)

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) 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())
length_array = [int(input()) for i in range(N)]
# print(length_array)

ans = 100000

for i in range(4**N):
    use_array = [[], [], []]
    for j in range(N):
        digit_j = (i & (3 << (j * 2))) >> (j * 2)
        if digit_j == 0:
            continue
        use_array[digit_j - 1].append(length_array[j])
    if len(use_array[0]) == 0 or len(use_array[1]) == 0 or len(
            use_array[2]) == 0:
        continue
    ans = min(ans,
              abs(sum(use_array[0]) - A) +
              (len(use_array[0]) - 1) * 10 + abs(sum(use_array[1]) - B) +
              (len(use_array[1]) - 1) * 10 + abs(sum(use_array[2]) - C) +
              (len(use_array[2]) - 1) * 10)
print(ans)

Submission status: Accepted

Solution code:
n, a, b, c = map(int, input().split())
l = sorted([int(input()) for i in range(n)])
ans = 10**10
for order in [[a, b, c],[b, c, a],[c, a, b],[a, c, b],[b, a, c],[c, b, a]]:
    mp = 0
    l_copy = l.copy()
    for goal in order:
        tem_mp = 10**10
        used = []
        for bit in range(1, 2 ** len(l_copy)):
            length = 0
            cost = 0
            count = 0
            tem_used = []
            for j in range(len(l_copy)):
                if (bit >> j) & 1:
                    length += l_copy[j]
                    count += 1
                    if count > 1:
                        cost += 10
                    tem_used.append(j)
            if tem_mp  > abs(goal - length) + cost:
                used = tem_used
                tem_mp = abs(goal - length) + cost
        mp += tem_mp
        for u in used[::-1]:
            l_copy.pop(u)
    ans = min(ans, mp)
print(ans)

Submission status: Accepted

Solution code:
a = [0]*3
n,a[0],a[1],a[2] = map(int,input().split())

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

def f(x):
  if len(x) == n:
    if not (0 in x and 1 in x and 2 in x):
      return 5000
    b = [0]*3
    c = -30
    for i in range(n):
      t = x[i]
      if t >= 0:
        b[t] += l[i]
        c += 10
    for i in range(3):
      c += abs(a[i]-b[i])
    return c
  else:
    return min(f(x+[-1]),f(x+[0]),f(x+[1]),f(x+[2]))
  
print(f([]))

Submission status: Accepted

Solution code:
a = [0]*4
n,a[1],a[2],a[3] = map(int,input().split())

l = []
for i in range(n):
  l += [int(input())]
  
Inf = 5000
  
def f(x):
  if len(x) == n:
    if not (1 in x and 2 in x and 3 in x):
      return Inf
    b = [0]*4
    c = -30
    for i in range(n):
      t = x[i]
      if t > 0:
        b[t] += l[i]
        c += 10
    for i in range(1,4):
      c += abs(a[i]-b[i])
    return c
  else:
    return min(f(x+[0]),f(x+[1]),f(x+[2]),f(x+[3]))
  
print(f([]))

Submission status: Accepted

Solution code:
INF=10**9

def dfs(cur, a, b, c, l,n):
    if cur==n:
        if not (a==0 or b==0 or c==0):
            return abs(A-a)+abs(B-b)+abs(C-c)-30
        else:
            return INF
    ret0=dfs(cur+1, a+l[cur], b, c, l, n)+10
    ret1=dfs(cur+1, a, b+l[cur], c, l, n)+10
    ret2=dfs(cur+1, a, b, c+l[cur], l, n)+10
    ret3=dfs(cur+1, a, b, c, l, n)
    #print(ret0,ret1,ret2,ret3)
    return min(ret0,ret1,ret2,ret3)

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

print(dfs(0,0,0,0,l,n))

Submission status: Accepted

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

def f(a,b,c,k):
    if k == N:
        if not a or not b or not c:
            return 10**9
        return abs(a-A)+abs(b-B)+abs(c-C)-30
    ans0 = f(a,b,c,k+1)
    ans1 = f(a+L[k],b,c,k+1)+10
    ans2 = f(a,b+L[k],c,k+1)+10
    ans3 = f(a,b,c+L[k],k+1)+10
    return min(ans0,ans1,ans2,ans3)

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

Submission status: Accepted

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


def solve(index, ma, mb, mc):
    if index == N:
        if len(ma) == 0 or len(mb) == 0 or len(mc) == 0:
            return INF
        cost = (len(ma) + len(mb) + len(mc) - 3) * 10
        cost += abs(sum(ma) - A)
        cost += abs(sum(mb) - B)
        cost += abs(sum(mc) - C)
        return cost
    t0 = solve(index + 1, ma, mb, mc)
    ta = solve(index + 1, ma + [L[index]], mb, mc)
    tb = solve(index + 1, ma, mb + [L[index]], mc)
    tc = solve(index + 1, ma, mb, mc + [L[index]])
    return min(t0, ta, tb, tc)


res = solve(0, [], [], [])
print(res)

Submission status: Accepted

Solution code:
N, A, B, C = [int(a) for a in input().split(" ")]
ls = [int(input()) for i in range(N)]
tab = [([float("inf")]*N, ref) for ref in (A, B, C)]
INF = float("inf")
from heapq import heappush, heappop, heapreplace
maxtarg = max(A, B, C)

# O(n)
def solve(ls):
    n = len(ls)
    qs = [[(-INF, -1)]*3 for i in range(3)]
    # 3*n times
    for ref, q in zip((A, B, C), qs):
        for i in range(n):
            val = -abs(ref-ls[i])
            if q[0][0] < val:
                heapreplace(q, (val, i))
    ans = INF
    # 9 times
    for a, i in qs[0]:
        for b, j in qs[1]:
            for c, k in qs[2]:
                if i!=j and j!=k and k!=i:
                    ans = min(ans, -(a+b+c))
    return ans
dic = {}
def dfs(ls):
    key = tuple(sorted(ls))
    if key in dic:
        return dic[key]
    n = len(ls)
    if n < 3: return INF
    ans = solve(ls)
    for i in range(n-1):
        for j in range(i+1,n):
            tmp = ls[i]+ls[j]
            if min(maxtarg-ls[i], maxtarg-ls[j]) < 10:
                continue
            ls2 = ls[:i]+ls[i+1:]
            ls2[j-1] = tmp
            ans = min(ans, 10+dfs(ls2))
    dic[key] = ans
    return ans

print(dfs(ls))

Submission status: Accepted

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

def dfs(i, a, b, c):
    if i == N:
        if min(a, b, c) == 0:
            return 1000000000
        else:
            return sum([abs(A - a), abs(B - b), abs(C - c)]) - 30

    n = i + 1
    x = lst[i]
    ret1 = dfs(n, a, b, c)
    ret2 = dfs(n, a + x, b, c) + 10
    ret3 = dfs(n, a, b + x, c ) + 10
    ret4 = dfs(n, a, b, c + x) + 10
    return min(ret1, ret2, ret3, ret4)

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