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())
length=[]
for i in range(n):
  k=int(input())
  length.append(k)
length=sorted(length)
x=[0,1,2,3]
ans=[]
if len(length)==3:
    for p in x:
      for q in x:
        for r in x:
            value=0
            a_length=0
            b_length=0
            c_length=0
            line=[p,q,r]
            if line.count(1)==0:
                value+=10*18
            if line.count(2)==0:
                value+=10*18
            if line.count(3)==0:
                value+=10*18
            if line.count(1)>1:
              value+=10*(line.count(1)-1)
            if line.count(2)>1:
              value+=10*(line.count(2)-1)
            if line.count(3)>1:
              value+=10*(line.count(3)-1)
            A=[i for i, y in enumerate(line) if y ==1]
            B=[i for i, y in enumerate(line) if y ==2]
            C=[i for i, y in enumerate(line) if y ==3]
            for i in A:
              a_length+=length[i]
            for i in B:
              b_length+=length[i]
            for i in C:
              c_length+=length[i]
            value+=abs(a-a_length)
            value+=abs(b-b_length)
            value+=abs(c-c_length)
            ans.append(value)
if len(length)==4:
    for p in x:
      for q in x:
        for r in x:
          for s in x:
              value=0
              a_length=0
              b_length=0
              c_length=0
              line=[p,q,r,s]
              if line.count(1)==0:
                  value+=10*18
              if line.count(2)==0:
                  value+=10*18
              if line.count(3)==0:
                  value+=10*18
              if line.count(1)>1:
                value+=10*(line.count(1)-1)
              if line.count(2)>1:
                value+=10*(line.count(2)-1)
              if line.count(3)>1:
                value+=10*(line.count(3)-1)
              A=[i for i, y in enumerate(line) if y ==1]
              B=[i for i, y in enumerate(line) if y ==2]
              C=[i for i, y in enumerate(line) if y ==3]
              for i in A:
                a_length+=length[i]
              for i in B:
                b_length+=length[i]
              for i in C:
                c_length+=length[i]
              value+=abs(a-a_length)
              value+=abs(b-b_length)
              value+=abs(c-c_length)
              ans.append(value)
if len(length)==5:
    for p in x:
      for q in x:
        for r in x:
          for s in x:
            for t in x:
                value=0
                a_length=0
                b_length=0
                c_length=0
                line=[p,q,r,s,t]
                if line.count(1)==0:
                    value+=10*18
                if line.count(2)==0:
                    value+=10*18
                if line.count(3)==0:
                    value+=10*18
                if line.count(1)>1:
                  value+=10*(line.count(1)-1)
                if line.count(2)>1:
                  value+=10*(line.count(2)-1)
                if line.count(3)>1:
                  value+=10*(line.count(3)-1)
                A=[i for i, y in enumerate(line) if y ==1]
                B=[i for i, y in enumerate(line) if y ==2]
                C=[i for i, y in enumerate(line) if y ==3]
                for i in A:
                  a_length+=length[i]
                for i in B:
                  b_length+=length[i]
                for i in C:
                  c_length+=length[i]
                value+=abs(a-a_length)
                value+=abs(b-b_length)
                value+=abs(c-c_length)
                ans.append(value)
if len(length)==6:
    for p in x:
      for q in x:
        for r in x:
          for s in x:
            for t in x:
              for u in x:
                  value=0
                  a_length=0
                  b_length=0
                  c_length=0
                  line=[p,q,r,s,t,u]
                  if line.count(1)==0:
                      value+=10*18
                  if line.count(2)==0:
                      value+=10*18
                  if line.count(3)==0:
                      value+=10*18
                  if line.count(1)>1:
                    value+=10*(line.count(1)-1)
                  if line.count(2)>1:
                    value+=10*(line.count(2)-1)
                  if line.count(3)>1:
                    value+=10*(line.count(3)-1)
                  A=[i for i, y in enumerate(line) if y ==1]
                  B=[i for i, y in enumerate(line) if y ==2]
                  C=[i for i, y in enumerate(line) if y ==3]
                  for i in A:
                    a_length+=length[i]
                  for i in B:
                    b_length+=length[i]
                  for i in C:
                    c_length+=length[i]
                  value+=abs(a-a_length)
                  value+=abs(b-b_length)
                  value+=abs(c-c_length)
                  ans.append(value)
if len(length)==7:
    for p in x:
      for q in x:
        for r in x:
          for s in x:
            for t in x:
              for u in x:
                for v in x:
                    value=0
                    a_length=0
                    b_length=0
                    c_length=0
                    line=[p,q,r,s,t,u,v]
                    if line.count(1)==0:
                        value+=10*18
                    if line.count(2)==0:
                        value+=10*18
                    if line.count(3)==0:
                        value+=10*18
                    if line.count(1)>1:
                      value+=10*(line.count(1)-1)
                    if line.count(2)>1:
                      value+=10*(line.count(2)-1)
                    if line.count(3)>1:
                      value+=10*(line.count(3)-1)
                    A=[i for i, y in enumerate(line) if y ==1]
                    B=[i for i, y in enumerate(line) if y ==2]
                    C=[i for i, y in enumerate(line) if y ==3]
                    for i in A:
                      a_length+=length[i]
                    for i in B:
                      b_length+=length[i]
                    for i in C:
                      c_length+=length[i]
                    value+=abs(a-a_length)
                    value+=abs(b-b_length)
                    value+=abs(c-c_length)
                    ans.append(value)
if len(length)==8:
    for p in x:
      for q in x:
        for r in x:
          for s in x:
            for t in x:
              for u in x:
                for v in x:
                  for w in x:
                    value=0
                    a_length=0
                    b_length=0
                    c_length=0
                    line=[p,q,r,s,t,u,v,w]
                    if line.count(1)==0:
                        value+=10*18
                    if line.count(2)==0:
                        value+=10*18
                    if line.count(3)==0:
                        value+=10*18
                    if line.count(1)>1:
                      value+=10*(line.count(1)-1)
                    if line.count(2)>1:
                      value+=10*(line.count(2)-1)
                    if line.count(3)>1:
                      value+=10*(line.count(3)-1)
                    A=[i for i, y in enumerate(line) if y ==1]
                    B=[i for i, y in enumerate(line) if y ==2]
                    C=[i for i, y in enumerate(line) if y ==3]
                    for i in A:
                      a_length+=length[i]
                    for i in B:
                      b_length+=length[i]
                    for i in C:
                      c_length+=length[i]
                    value+=abs(a-a_length)
                    value+=abs(b-b_length)
                    value+=abs(c-c_length)
                    ans.append(value)
print(min(ans))

Submission status: Accepted

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

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 as itr
n, a, b, c = list(map(int, input().split()))
abc = [a, b ,c]
l = [int(input()) for _ in range(n)]
ans = float('inf')
for i in list(itr.product([0, 1, 2, 3], repeat=n)):
    tans = 0
    pd = [True]*3
    tabc = abc.copy()
    if 0 not in i or 1 not in i or 2 not in i:
        continue
    for j in range(len(i)):
        if i[j] != 3:
            tabc[i[j]] -= l[j]
            if pd[i[j]]:
                pd[i[j]] = False
            else:
                tans += 10
    for k in tabc:
        tans += abs(k)
    ans = min(ans, tans)
print(ans)

Submission status: Accepted

Solution code:
from itertools import product

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

abc = [a, b, c]

res = float('inf')
for p in product(range(4), repeat=n):
    num = [0, 0, 0, 0]
    le = [0, 0, 0, 0]
    for i, category in enumerate(p):
        num[category] += l[i]
        le[category] += 1
    tmp = 0
    if le[0] * le[1] * le[2] == 0:
        continue
    for i in range(3):
        tmp += abs(num[i] - abc[i]) + 10 * (le[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 i in range(N)]
INF = 10**9


# cur 番目の竹をA,B,Cどの竹の合成に使うか(使わないか)で場合分けして全探索
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  # そもそも1本はあるのでその分引く
        else: return INF  # A,B,Cに一本も竹を使わない場合は除外
    ret0 = dfs( cur + 1, a, b, c )                # cur 番目の竹を合成に使わない
    ret1 = dfs( cur + 1, a + l[cur], b, c ) + 10  # cur 番目の竹をAの合成に使う
    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 = map(int, input().split())
l = [int(input()) for _ in range(N)]
INF = float('inf')


def calc(mask):
    abc = [0 for _ in range(4)]
    cnt = 0
    for j in range(N):
        idx = (mask >> (2 * j)) & 3
        abc[idx] += l[j]
        if 0 <= idx < 3:
            cnt += 1
    if 0 in abc[:3]:
        return INF
    return sum(abs(x - t) for x, t in zip(abc, [A, B, C])) + (cnt - 3) * 10


def dfs(cur, mask):
    if cur == N:
        return calc(mask)

    res = INF
    for i in range(4):
        res = min(res, dfs(cur + 1, mask | i << (2 * cur)))
    return res


print(dfs(0, 0))

Submission status: Accepted

Solution code:
N,A,B,C=map(int,input().split())
T=[A,B,C]
L=[int(input()) for i in range(N)]
ans=1999999999
for i in range(4**N):
  L_=[[] for i in range(4)]
  for k in range(N):
    r=i%4
    i=(i-r)//4
    L_[r].append(L[k])
  if len(L_[0])*len(L_[1])*len(L_[2])==0:
    continue
  MP=10*(len(L_[0])+len(L_[1])+len(L_[2])-3)
  for r in range(3):
    s=0
    for l in L_[r]:
      s+=l
    MP+=abs(s-T[r])
  ans=min(ans,MP)
print(ans)

Submission status: Accepted

Solution code:
from itertools import product

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

def get_mp(q):
    assert len(q) == N
    res = 0
    assign = [[] for _ in range(4)]
    
    for i, j in enumerate(q):
        assign[j].append(bl[i])

    if any([l == [] for l in assign[:3]]):
        return 10**18
    
    for l, p in zip(assign, [A, B, C]):
        ext_sht = abs(sum(l) - p)
        syn = (len(l) - 1) * 10
        res += ext_sht + syn
    
    return res

res = []
for q in product(*([[0,1,2,3]]*N)):
    res.append(get_mp(q))
print(min(res))

Submission status: Accepted

Solution code:
# 4進数

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

ans = INF
for ptn in range(1 << (2 * N)):
    abc = [0 for _ in range(4)]

    v = [None for _ in range(N)]
    # lの各idxをどの竹用に割り当てるか, ABC無 = 0123

    cnt = 0
    for j in range(N):
        v[j] = (ptn >> (2 * j)) & 3
        abc[v[j]] += l[j]
        if 0 <= v[j] < 3:
            cnt += 1

    if 0 in abc[:3]: continue
    ans = min(ans, sum(abs(x - t) for x, t in zip(abc[:3], [A, B, C])) + (cnt - 3) * 10)
print(ans)

Submission status: Accepted

Solution code:
from itertools import product
from collections import Counter
N, A, B, C = map(int, input().split())
h = {1: A, 2: B, 3: C}
l = [int(input()) for i in range(N)]
ans = float('inf')
for p in product([0, 1, 2, 3], repeat=N):
    c = Counter(p)
    if c[1] == 0 or c[2] == 0 or c[3] == 0:
        continue
    t = {1: 0, 2: 0, 3: 0}
    for i, v in enumerate(p):
        if v == 0:
            continue
        t[v] += l[i]
    mp = 0
    for i in range(1, 4):
        if c[i] >= 2:
            mp += (c[i] - 1) * 10
        mp += abs(h[i] - t[i])
    ans = min(ans, mp)
print(ans)

Submission status: Accepted

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

def dfs(depth, a, b, c):
    if depth == N:
        return abs(A-a)+abs(B-b)+abs(C-c)-30 if min(a, b, c) > 0 else 10**9

    ret0 = dfs(depth+1, a, b, c)# depth本目の棒を使わない
    ret1 = dfs(depth+1, a+l[depth], b, c)+10
    ret2 = dfs(depth+1, a, b+l[depth], c)+10
    ret3 = dfs(depth+1, a, b, c+l[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())
take=[int(input()) for _ in range(N)]

class T:
    def __init__(self):
        self.ans=A+B+C

    def rec(self,i,a,b,c,m):
        if i==N:
            if a*b*c:
                self.ans=min(self.ans,abs(A-a)+abs(B-b)+abs(C-c)+m*10)
            return
        self.rec(i+1,a+take[i],b,c,m+1)
        self.rec(i+1,a,b+take[i],c,m+1)
        self.rec(i+1,a,b,c+take[i],m+1)
        self.rec(i+1,a,b,c,m)
        
    def __call__(self):
        self.rec(0,0,0,0,-3)
        return self.ans
        
print(T()())

Submission status: Accepted

Solution code:
import itertools

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


ans = 10 ** 8
for t in list(itertools.product((0, 1, 2, 3), repeat=len(l))):
    a = [i for i, j in zip(l, t) if j == 0]
    b = [i for i, j in zip(l, t) if j == 1]
    c = [i for i, j in zip(l, t) if j == 2]
    if len(a) * len(b) * len(c) == 0:
        continue
    tmp1 = abs(sum(a) - A) + max(0, len(a) - 1) * 10
    tmp2 = abs(sum(b) - B) + max(0, len(b) - 1) * 10
    tmp3 = abs(sum(c) - C) + max(0, len(c) - 1) * 10
    ans = min(ans, tmp1 + tmp2 + tmp3)


print(ans)

Submission status: Accepted

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

l_list = [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_list[cur], b, c) + 10
  ret2 = dfs(cur + 1, a, b + l_list[cur], c) + 10
  ret3 = dfs(cur + 1, a, b, c + l_list[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:
        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 = list(map(int, input().split()))
lll = [int(input()) for _ in range(n)]

ans = 10 ** 9
for i in range(4 ** n):
    abc = [0] * 3
    tmp = 0
    for j in range(n):
        k = (i >> (2 * j)) & 3
        if k < 3:
            if abc[k] > 0:
                tmp += 10
            abc[k] += lll[j]
    if any(t == 0 for t in abc):
        continue
    tmp += sum(abs(d - e) for d, e in zip((a, b, c), abc))
    ans = min(ans, tmp)
print(ans)

Submission status: Accepted

Solution code:
def main():
    #入力をとる
    n,a,b,c = map(int,input().split())
    moto = [int(input()) for i in range(n)]

    take = []
    take.append(a)
    take.append(b)
    take.append(c)

    mp = 10000000000
    pt= []
    #全探索
    for i in range(2**(2*n)):
        pt = []
        for j in range(n):
            if (i >> j*2)&1:
                if (i >> j*2+1)&1:
                    pt.append(3)
                else:
                    pt.append(1)
            else:
                if (i >> j*2+1)&1:
                    pt.append(2)
                else:
                    pt.append(0)       

        #keisan
        mp_tmp = 0
        all_used = True
        for k in range(3):
            if pt.count(k) == 0:
                all_used = False
                break

            sum_tmp = 0
            mp_tmp += (pt.count(k) - 1)*10
            for l in range(n):
                if pt[l] == k:
                    sum_tmp += moto[l]
            
            mp_tmp += abs(sum_tmp-take[k])

        if all_used == True:
            mp = min(mp_tmp, mp)
    print(mp)

main()

Submission status: Accepted

Solution code:
from statistics import mean, median,variance,stdev
import numpy as np
import sys
import math
import fractions
import itertools

def j(q):
    if q==1: print("Heisei")
    else:print("TBD")
    exit(0)


def ct(x,y):
    if (x>y):print("")
    elif (x<y): print("")
    else: print("")

def ip():
    return int(input())

def swap(s,pos):
    s[pos]-=1
    s[pos+1]+=1
    print(s)
    if pos == n-2: return s
    if s[pos+2] == 0: swap(s,pos+1)
    return s


#n = ip()                              #入力整数1つ
#h,w= (int(i) for i in input().split())      #入力整数横2つ
#n,x,y = (int(i) for i in input().split())    #入力整数横3つ
n,a,b,c= (int(i) for i in input().split())  #入力整数横4つ
#a = [int(i) for i in input().split()]        #入力整数配列
#a = input()                                  #入力文字列
#a = input().split()                          #入力文字配列
#n = ip()                      #入力セット(整数改行あり)(1/2)
#a=[ip() for i in range(n)]    #入力セット(整数改行あり)(2/2)

#jの変数はしようできないので注意
#全足しにsum変数使用はsum関数使用できないので注意
l = []
for i in range(n):
    l.append(ip())
if a in l and b in l and c in l:
    print(0)
    exit(0)
l.sort()
req = [a, b,c]
m  =set()
set = [0 for i in range(n)]
set[0] = -1
while sum(set) < n*3:
    while sum(set) < n*3:
        d = 0
        set[d]+=1
        while set[d] == 4:
            set[d]=0
            d+=1
            set[d]+=1
        if 0 in set and 1 in set and 2 in set: break
    if sum(set) != n*3:
        s = [0,0,0,0]
        mi = 0
        for i in range(n):
            s[set[i]]+= l[i]
        for i in range(3):
            mi += abs(s[i] - req[i]) + (set.count(i)-1) * 10
        m.add(mi)
print(min(m))

Submission status: Accepted

Solution code:
import itertools
N, A, B, C = map(int, raw_input().split())
banboos = [int(raw_input()) for _ in range(N)]

INF = 10**9
#saw banboos from 0 to k-1 and the length is a, b, c
#see the k'th banboo and return the smallest cost
def dfs(k, a, b, c):
  if k == N:
    return abs(a-A)+abs(b-B)+abs(c-C) - 30 if min(a, b, c) > 0 else INF
  ret0 = dfs(k+1, a, b, c)
  ret1 = dfs(k+1, a+banboos[k], b, c)+10
  ret2 = dfs(k+1, a, b+banboos[k],  c)+10
  ret3 = dfs(k+1, a, b, c+banboos[k])+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 dps(a,b,c,index):
  if index == N:
    return abs(a-A) + abs(b-B) + abs(c-C) if min(a,b,c) != 0 else float('infinity')
  
  take = L[index]
  mp1 = dps(a,b,c,index+1)
  mp2 = dps(a+take,b,c,index+1) + 10
  mp3 = dps(a,b+take,c,index+1) + 10
  mp4 = dps(a,b,c+take,index+1) + 10

  return min(mp1,mp2,mp3,mp4)


print(dps(0,0,0,0)-30)

Submission status: Accepted

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

ans = 100000000000000
for li in itertools.product(range(4), repeat=n):
  tmp = [[] for i in range(4)] #tmp[0]がaの竹、tmp[1]がbの竹、tmp[2]がcの竹、tmp[3]は使わない竹
  for i in range(len(li)):
    tmp[li[i]].append(l[i])
  if len(tmp[0])*len(tmp[1])*len(tmp[2]):
    ans = min(ans,(len(tmp[0])-1)*(10)+(len(tmp[1])-1)*(10)+(len(tmp[2])-1)*(10)+abs(sum(tmp[0])-a)+abs(sum(tmp[1])-b)+abs(sum(tmp[2])-c))
print(ans)

Submission status: Accepted

Solution code:
import itertools
n, a, b, c = map(int, input().split())
NOT_USED = 0
USED_A = 1
USED_B = 2
USED_C = 3

ans = 10000
l = [0] * n

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

for use in itertools.product([0, 1, 2, 3], repeat=n):
    cnt_a = 0
    cnt_b = 0
    cnt_c = 0
    len_a = 0
    len_b = 0
    len_c = 0

    for i in range(n):
        if use[i] == USED_A:
            cnt_a += 1
            len_a += l[i]

        if use[i] == USED_B:
            cnt_b += 1
            len_b += l[i]

        if use[i] == USED_C:
            cnt_c += 1
            len_c += l[i]

    if cnt_a == 0 or cnt_b == 0 or cnt_c == 0:
        continue
        
    ans = min(ans,
              10 * (cnt_a - 1) + abs(len_a - a) +
              10 * (cnt_b - 1) + abs(len_b - b) +
              10 * (cnt_c - 1) + abs(len_c - c))
print(ans)

Submission status: Accepted

Solution code:
import sys
from itertools import combinations, product


fin = sys.stdin.readline
N, A, B, C = [int(elem) for elem in fin().split()]
l_list = [int(fin()) for _ in range(N)]

min_mp = float('inf')
for flags in product(range(4), repeat=N):
    powers = [0] * 3
    num_items = [0] * 3
    for l, flag in zip(l_list, flags):
        if flag == 3:
            continue
        powers[flag] += l
        num_items[flag] += 1
    if not all(powers):
        continue
    mp = sum(
        abs(power - necessary_power) 
        for power, necessary_power in zip(powers, (A, B, C))
        ) + 10 * (sum(num_items) - 3)
    min_mp = min(mp, min_mp)
print(min_mp)

Submission status: Accepted

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

from itertools import permutations, combinations


def dellist(items, contents):
    items = items.copy()
    for rm in contents:
        # print(rm)
        items.remove(rm)
    return items


ans = []
L_temp = L.copy()

for n_g1 in range(1, N):
    for n_g2 in range(1, N - n_g1+1):
        for n_g3 in range(1, N - n_g1 - n_g2 + 1):
            # print(n_g1, n_g2, n_g3)
            L_temp = L.copy()
            for G1 in combinations(L_temp, n_g1):
                L_temp = L.copy()
                L_temp = dellist(L_temp, G1)
                # print('G1', L_temp)
                for G2 in combinations(L_temp, n_g2):
                    L_temp = L.copy()
                    L_temp = dellist(L_temp, G1)
                    L_temp = dellist(L_temp, G2)
                    # print('G2', L_temp)
                    for G3 in combinations(L_temp, n_g3):
                        # print('G3', L_temp)
                        s1, s2, s3 = sum(G1), sum(G2), sum(G3)
                        syn = len(G1) + len(G2) + len(G3) - 3
                        ans.append(abs(s1 - A) + abs(s2 - B) +
                                   abs(s3 - C) + syn * 10)

# print(ans)
print(min(ans))

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
        # A,B,Cに使用する材料を合成し、目的の長さの差を加算（必要なMP）
        # ret1-3で一つ目の材料にも合成魔法のMP10を要求しているので、A,B,Cの初回加算合わせて3回分のMP30を減算
        # a,b,cのいずれかが0のとき、材料が1つも宛がわれていない竹が存在するため、不適（必要MPを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)
    # l[cur]について、使用しないか、(A,B,C)のいずれかに使用の4分岐


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

Submission status: Accepted

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

import itertools
INF = 1e10

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

ans = INF
for prod in itertools.product('NABC', repeat=N):
    if not ('A' in prod and 'B' in prod and 'C' in prod):
        continue

    D = {'A': [], 'B': [], 'C': [], 'N': []}
    for i in range(N):
        D[prod[i]].append(L[i])

    tmp = max(len(D['A']) - 1, 0) * 10 + abs(sum(D['A']) - A)
    tmp += max(len(D['B']) - 1, 0) * 10 + abs(sum(D['B']) - B)
    tmp += max(len(D['C']) - 1, 0) * 10 + abs(sum(D['C']) - C)
    ans = min(ans, tmp)

print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**7)

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

mp = [float('inf')]
def f(i, anum, alen, bnum, blen, cnum, clen):
    if i == n:
        if alen * blen * clen == 0:
            return
        mp[0] = min(mp[0], abs(anum-a)+10*(alen-1) + abs(bnum-b)+10*(blen-1) + abs(cnum-c)+10*(clen-1))
    else:
        f(i+1, anum+l[i], alen+1, bnum, blen, cnum, clen)
        f(i+1, anum, alen, bnum+l[i], blen+1, cnum, clen)
        f(i+1, anum, alen, bnum, blen, cnum+l[i], clen+1)
        f(i+1, anum, alen, bnum, blen, cnum, clen)

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


print(mp[0])

Submission status: Accepted

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

def f(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 float("inf")
    ret0 = f(cur+1,a,b,c)
    ret1 = f(cur+1,a+L[cur],b,c) + 10
    ret2 = f(cur+1,a,b+L[cur],c) + 10
    ret3 = f(cur+1,a,b,c+L[cur]) + 10
    return min(ret0,ret1,ret2,ret3)
    
print(f(0,0,0,0))

Submission status: Accepted

Solution code:
from statistics import mean, median,variance,stdev
import numpy as np
import sys
import math
import fractions
import itertools

def j(q):
    if q==1: print("Heisei")
    else:print("TBD")
    exit(0)


def ct(x,y):
    if (x>y):print("")
    elif (x<y): print("")
    else: print("")

def ip():
    return int(input())

def swap(s,pos):
    s[pos]-=1
    s[pos+1]+=1
    print(s)
    if pos == n-2: return s
    if s[pos+2] == 0: swap(s,pos+1)
    return s


#n = ip()                              #入力整数1つ
#h,w= (int(i) for i in input().split())      #入力整数横2つ
#n,x,y = (int(i) for i in input().split())    #入力整数横3つ
n,a,b,c= (int(i) for i in input().split())  #入力整数横4つ
#a = [int(i) for i in input().split()]        #入力整数配列
#a = input()                                  #入力文字列
#a = input().split()                          #入力文字配列
#n = ip()                      #入力セット(整数改行あり)(1/2)
#a=[ip() for i in range(n)]    #入力セット(整数改行あり)(2/2)

#jの変数はしようできないので注意
#全足しにsum変数使用はsum関数使用できないので注意
l = []
for i in range(n):
    l.append(ip())
if a in l and b in l and c in l:
    print(0)
    exit(0)
l.sort()
req = [a, b,c]


m  = pow(10,30)
set = [0 for i in range(n)]
set[0] = -1
while sum(set) < n*3:
    while sum(set) < n*3:
        d = 0
        set[d]+=1
        while set[d] == 4:
            set[d]=0
            d+=1
            set[d]+=1
        if 0 in set and 1 in set and 2 in set: break
    if 0 not in set or 1 not in set or 2 not in set:
        continue
    s = [0,0,0,0]
    mi = 0
    for i in range(n):
        s[set[i]]+= l[i]
    for i in range(3):
        mi += abs(s[i] - req[i]) + (set.count(i)-1) * 10
    m  = min(m,mi)
print(m)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# import math   # math.infが使えない？(3.5から。AtCoderは3.4)

# 解説見ながら記述
# 延長魔法は、合成前・合成後で実施しても等価。
# 合成魔法が使えないパターンにのみ延長魔法を使うようにして全探索するみたい

N, A, B, C = map(int, input().split())
ls = [int(input()) for i in range(N)]
INF = 10 ** 10  # int32.max = 2*10^9より大きくしている

def rec(cur, a, b, c):
    """
    cur: 現在のlsのindex
    a: 合成中の1本目の長さ
    b: 合成中の1本目の長さ
    c: 合成中の1本目の長さ
    """
    if cur == N:
        if min(a, b, c) > 0:
            # 合成1回目：1本目は選ぶだけで合成する必要ないので10*3を引いておく
            return abs(A-a) + abs(B-b) + abs(C-c) - 30
        else:
            # 1回も合成していない場合は無効
            # return math.inf
            return INF
    else:
        r0 = rec(cur+1, a, b, c)    # ls[cur]に対して何もしない
        r1 = rec(cur+1, a+ls[cur], b, c) + 10    # ls[cur]をaに合成
        r2 = rec(cur+1, a, b+ls[cur], c) + 10    # ls[cur]をbに合成
        r3 = rec(cur+1, a, b, c+ls[cur]) + 10    # ls[cur]をcに合成
        return min(r0, r1, r2, r3)

r = rec(0, 0, 0, 0)

print(r)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**7)

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

mp = float('inf')
def f(i, anum, alen, bnum, blen, cnum, clen):
    global mp
    if i == n:
        if alen * blen * clen == 0:
            return
        mp = min(mp, abs(anum-a)+10*(alen-1) + abs(bnum-b)+10*(blen-1) + abs(cnum-c)+10*(clen-1))
    else:
        f(i+1, anum+l[i], alen+1, bnum, blen, cnum, clen)
        f(i+1, anum, alen, bnum+l[i], blen+1, cnum, clen)
        f(i+1, anum, alen, bnum, blen, cnum+l[i], clen+1)
        f(i+1, anum, alen, bnum, blen, cnum, clen)

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


print(mp)