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 i in range(N)]
INF = 10 ** 9

def dfs(cur, a, b, c):
    # curはN本の竹のうち何本を確認したか。
    if cur == N:
        # 全ての竹を確認した後の場合
        return abs(a - A) + abs(b - B) + abs(c - C) if min(a,b,c) > 0 else INF
    # cur本目の竹を使用しない場合
    ret0 = dfs(cur + 1, a, b, c)
    # cur本目の竹をAに使用する場合
    ret1 = dfs(cur + 1, a + l[cur], b, c) + (10 if a > 0 else 0)
    # cur本目の竹をBに使用する場合
    ret2 = dfs(cur + 1, a, b + l[cur], c) + (10 if b > 0 else 0)
    # cur本目の竹をCに使用する場合
    ret3 = dfs(cur + 1, a, b, c + l[cur]) + (10 if c > 0 else 0)
    return min(ret0, ret1, ret2, ret3)

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

Submission status: Accepted

Solution code:
#学習用
#
N, A, B, C = map(int, input().split())
l = [int(input()) for i in range(N)]
INF = 10 ** 9

def dfs(cur, a, b, c):
    # curはN本の竹のうち何本を確認したか。
    if cur == N:
        # 全ての竹を確認した場合
        #
        return abs(a - A) + abs(b - B) + abs(c - C) - 30 if min(a, b, c) > 0 else INF
    # cur本目の竹を使用しない場合
    ret0 = dfs(cur + 1, a, b, c)
    # cur本目の竹をAに使用する場合
    ret1 = dfs(cur + 1, a + l[cur], b, c) + 10
    # cur本目の竹をBに使用する場合
    ret2 = dfs(cur + 1, a, b + l[cur], c) + 10
    # cur本目の竹をCに使用する場合
    ret3 = dfs(cur + 1, a, b, c + l[cur]) + 10
    return min(ret0, ret1, ret2, ret3)

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from itertools import product

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

ans = 1e9
for idxs in product(range(4), repeat=n):
    la, lb, lc = [], [], []
    for idx, l in zip(idxs, ls):
        if idx == 0:
            la.append(l)
        elif idx == 1:
            lb.append(l)
        elif idx == 2:
            lc.append(l)
    if len(la) and len(lb) and len(lc):
        mpa = max(len(la) - 1, 0) * 10 + abs(sum(la) - a)
        mpb = max(len(lb) - 1, 0) * 10 + abs(sum(lb) - b)
        mpc = max(len(lc) - 1, 0) * 10 + abs(sum(lc) - c)
        ans = min(ans, mpa + mpb + mpc)
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())
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) > 0 else 10**9
    ret0 = dfs(cur + 1, a, b, c)
    ret1 = dfs(cur + 1, a + l[cur], b, c) + 10
    ret2 = dfs(cur + 1, a, b + l[cur], c) + 10
    ret3 = dfs(cur + 1, a, b, c + l[cur]) + 10
    return min(ret0, ret1, ret2, ret3)

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

Submission status: Accepted

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

import itertools
m = 4
n = N
bit_list = list(itertools.product([i for i in range(m)], repeat=n))

res = 10 ** 18
for b in bit_list:
    tmp1 = 0
    tmp2 = 0
    tmp3 = 0
    check1 = False
    check2 = False
    check3 = False
    cost = 0
    for i in range(n):
        if b[i] == 1:
            tmp1 += L[i]
            if check1:
                cost += 10
            check1 = True       
            
        if b[i] == 2:
            tmp2 += L[i]
            if check2:
                cost += 10
            check2 = True
            
        if b[i] == 3:
            tmp3 += L[i]
            if check3:
                cost += 10
            check3 = True
    if min(tmp1, tmp2, tmp3) != 0:
        res = min(res, abs(tmp1 - A)+abs(tmp2 - B)+abs(tmp3 - C)+cost)
print(res)

Submission status: Accepted

Solution code:
N,A,B,C=map(int,input().split())
L=[int(input()) for i in range(N)]
answ=10**9
for i in range(1<<N):
    for j in range(1<<N):
        M=[0]*4
        num=[0]*4
        for k in range(N):
            s=(1<<k)&i
            t=(1<<k)&j
            if s:
                if t:
                    continue
                else:
                    M[1]+=L[k]
                    num[1]+=1
            else:
                if t:
                    M[2]+=L[k]
                    num[2]+=1
                else:
                    M[3]+=L[k]
                    num[3]+=1
        ans=abs(A-M[1])+abs(B-M[2])+abs(C-M[3])
        for l in range(1,4):
            if num[l]==0:
                ans=10**9
            elif num[l]!=0:
                ans+=(num[l]-1)*10
        #print(M,num,ans)

        answ=min(ans,answ)
print(answ)

Submission status: Accepted

Solution code:
class ans:
  def __init__(self):
    self.N, self.A, self.B, self.C = map(int,input().split())
    self.L = [int(input()) for _ in range(self.N)]
    print(self.dfs(0, 0, 0, 0))
    exit()

  def dfs(self, x, a, b, c):
    if(x == self.N):
      if(min(a, b, c) > 0):
        return abs(a-self.A)+abs(b-self.B)+abs(c-self.C) - 30
      else:
        return 1e9+7
    ret0 = self.dfs(x+1, a, b, c)
    ret1 = self.dfs(x+1, a+self.L[x], b, c) + 10
    ret2 = self.dfs(x+1, a, b+self.L[x], c) + 10
    ret3 = self.dfs(x+1, a, b, c+self.L[x]) + 10
    return min(ret0, ret1, ret2, ret3)

ans()

Submission status: Accepted

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

def dfs(now,a,b,c):
  if now==N:
    return abs(a-A)+abs(b-B)+abs(c-C)-30 if min(a,b,c)>0 else float ("inf")
  res0 =dfs(now+1,a,b,c)
  res1=dfs(now+1,a+l_list[now],b,c)+10
  res2=dfs(now+1,a,b+l_list[now],c)+10
  res3=dfs(now+1,a,b,c+l_list[now])+10
  return min(res0,res1,res2,res3)

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

Submission status: Accepted

Solution code:
data = input().split()
N = int(data[0])
A = int(data[1])
B = int(data[2])
C = int(data[3])

i = 0
kd = []
while i < N:
  kd.append(int(input()))
  i += 1


cur = 0 
sc = 0
def ans(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 1000000
  ret0 = ans(cur+1,a,b,c)
  ret1 =  ans(cur+1,a+kd[cur],b,c) + 10
  ret2 = ans(cur + 1,a,b+kd[cur],c) +10 
  ret3 = ans(cur+1,a,b,c+kd[cur])+ 10
  return min(ret0,ret1,ret2,ret3)

print(ans(0,0,0,0))

Submission status: Accepted

Solution code:
n, A, B, C = (int(i) for i in input().split())  #a=1, b=3, c=5, d=7
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:
# -*- coding:utf-8 -*-

INF = 10**9

def solve():
    N, A, B, C = [int(x) for x in input().split(" ")]
    ls = []
    for _ in range(N):
        ls.append(int(input()))

    def _dfs(depth, a, b, c):
        if depth == N:
            # 全ての竹を選び終わったら、延長魔法と短縮魔法を使ってA,B,Cの長さにする
            return abs(a-A) + abs(b-B) + abs(c-C) - 30 if min(a, b, c) > 0 else INF
        ret0 = _dfs(depth+1, a, b, c)  # 使わない
        ret1 = _dfs(depth+1, a+ls[depth], b, c) + 10  # Aに使う
        ret2 = _dfs(depth+1, a, b+ls[depth], c) + 10  # Bに使う
        ret3 = _dfs(depth+1, a, b, c+ls[depth]) + 10  # Cに使う
        
        return min(ret0, ret1, ret2, ret3)

    print(_dfs(0,0,0,0))


if __name__ == "__main__":
    solve()

Submission status: Accepted

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

INF = 10**9

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


def solve():
    N, A, B, C = [int(x) for x in input().split(" ")]
    ls = []
    for _ in range(N):
        ls.append(int(input()))
    
    print(dfs(0,0,0,0, A, B, C, N, ls))
    
    


if __name__ == "__main__":
    solve()

Submission status: Accepted

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

# N<=8より4**8=65536通りしかないので全探索できる
INF = 10**9


def dfs(n_bamboo, a, b, c):
    if n_bamboo == N:
        if a != 0 and b != 0 and c != 0:
            # n個合成するとき、n-1回しか魔法は使わないのに"+10"をn回したから
            return abs(A - a) + abs(B - b) + abs(C - c) - 30
        else:
            return INF
    else:
        return1 = dfs(n_bamboo+1, a, b, c)
        return2 = dfs(n_bamboo+1, a + L[n_bamboo], b, c)+10
        return3 = dfs(n_bamboo+1, a, b+L[n_bamboo], c)+10
        return4 = dfs(n_bamboo+1, a, b, c+L[n_bamboo])+10
        return min([return1, return2, return3, return4])


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)]
m = []
def m_dep(s):
    if len(s) == N:
        if '1' in s and '2' in s and '3' in s:
            m.append(calc([int(i) for i in s]))
    else:
        for i in range(4):
            m_dep(s + str(i))

def calc(l):
    A=0
    B=0
    C=0
    M=0
    for i,n in enumerate(l):
        if n==1:
            if A!=0:
                M+=10
            A+=L[i]
        if n==2:
            if B!=0:
                M+=10
            B+=L[i]
        if n==3:
            if C!=0:
                M+=10
            C+=L[i]
    M += abs(a-A)+abs(b-B)+abs(c-C)
    return(M)


m_dep('')
print(min(m))

Submission status: Accepted

Solution code:
from itertools import product


def solve(string):
    n, a, b, c, *l = map(int, string.split())
    ans = 3000
    for p in product(range(4), repeat=n):
        if not ({0, 1, 2} <= set(p)):
            continue
        sums = [0, 0, 0, 0]
        for _p, _l in zip(p, l):
            sums[_p] += _l
        mp12 = sum(abs(_s - _abc) for _s, _abc in zip(sums, [a, b, c]))
        mp3 = sum(1 for _p in p if _p < 3) - 3
        ans = min(ans, mp12 + mp3 * 10)
    return str(ans)


if __name__ == '__main__':
    n, a, b, c = map(int, input().split())
    print(solve('{} {} {} {}\n'.format(n, a, b, c) + '\n'.join([input() for _ in range(n)])))

Submission status: Accepted

Solution code:
from itertools import product


def solve(string):
    n, a, b, c, *l = map(int, string.split())
    ans = 3000
    for p in product(range(4), repeat=n):
        if not ({0, 1, 2} <= set(p)):
            continue
        sums = [0, 0, 0, 0]
        for _p, _l in zip(p, l):
            sums[_p] += _l
        mp12 = sum(abs(_s - _abc) for _s, _abc in zip(sums[:3], [a, b, c]))
        mp3 = sum(1 for _p in p if _p < 3) - 3
        ans = min(ans, mp12 + mp3 * 10)
    return str(ans)


if __name__ == '__main__':
    n, a, b, c = map(int, input().split())
    print(solve('{} {} {} {}\n'.format(n, a, b, c) + '\n'.join([input() for _ in range(n)])))

Submission status: Accepted

Solution code:
from itertools import permutations


def solve(string):
    n, a, b, c, *l = map(int, string.split())
    ans = 3000

    for _l in permutations(l):
        for i in range(1, len(l) - 1):
            tmp_a = 10 * (i - 1) + abs(a - sum(_l[:i]))
            if tmp_a >= ans:
                continue
            for j in range(i + 1, len(l)):
                tmp_b = 10 * (j - i - 1) + abs(b - sum(_l[i:j]))
                if tmp_a + tmp_b >= ans:
                    continue
                for k in range(j + 1, len(l) + 1):
                    tmp = tmp_a + tmp_b + 10 * (k - j - 1) + abs(c - sum(_l[j:k]))
                    if tmp < ans:
                        ans = tmp
    return str(ans)


if __name__ == '__main__':
    n, a, b, c = map(int, input().split())
    print(solve('{} {} {} {}\n'.format(n, a, b, c) + '\n'.join([input() for _ in range(n)])))

Submission status: Accepted

Solution code:
from itertools import permutations


def solve(string):
    n, a, b, c, *l = map(int, string.split())
    ans = 3000

    for _l in permutations(l):
        for i in range(1, len(l) - 1):
            for j in range(i + 1, len(l)):
                for k in range(j + 1, len(l) + 1):
                    tmp = 10 * (i - 1) + abs(a - sum(_l[:i])) + 10 * (j - i - 1) + abs(
                        b - sum(_l[i:j])) + 10 * (k - j - 1) + abs(c - sum(_l[j:k]))
                    if tmp < ans:
                        ans = tmp
    return str(ans)


if __name__ == '__main__':
    n, a, b, c = map(int, input().split())
    print(solve('{} {} {} {}\n'.format(n, a, b, c) + '\n'.join([input() for _ in range(n)])))

Submission status: Accepted

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

remain = []
res = []
for i in range(n):
    remain.append(int(input()))
x =tuple([ [0,1,2,3]]*n)
for t in list(itertools.product(*x)):
    l = [0,0,0,0]
    s = [0,0,0,0]
    for i in range(n):
        l[t[i]] += remain[i]
        s[t[i]] += 1
    if l[1]*l[2]*l[3] == 0:
        continue
    else:
        magic = (s[1]+s[2]+s[3]-3)*10
        magic += abs(l[1]-a)
        magic += abs(l[2]-b)
        magic += abs(l[3]-c)
        res.append(magic)
print(min(res))

Submission status: Accepted

Solution code:
inf = 10**9
n, a, b, c = map(int, input().split())
l = [int(input()) for _ in range(n)]

def dfs(i, a_cur, b_cur, c_cur):
    if i == n:
        return abs(a_cur-a) + abs(b_cur-b) + abs(c_cur-c) - 30 if min(a_cur, b_cur, c_cur) > 0 else inf
    ans0 = dfs(i+1, a_cur+l[i], b_cur, c_cur) + 10
    ans1 = dfs(i+1, a_cur, b_cur+l[i], c_cur) + 10
    ans2 = dfs(i+1, a_cur, b_cur, c_cur+l[i]) + 10
    ans3 = dfs(i+1, a_cur, b_cur, c_cur)
    return min(ans0, ans1, ans2, ans3)

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

Submission status: Accepted

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

    INF = 10 ** 9

    def dfs(current_bamboo_index, a, b, c):
        if current_bamboo_index == N:
            return abs(a - A) + abs(b - B) + abs(c - C) - 30 if a * b * c > 0 else INF
        stuff_a = dfs(current_bamboo_index + 1, a + l_list[current_bamboo_index], b, c) + 10
        stuff_b = dfs(current_bamboo_index + 1, a, b + l_list[current_bamboo_index], c) + 10
        stuff_c = dfs(current_bamboo_index + 1, a, b, c + l_list[current_bamboo_index]) + 10
        unused = dfs(current_bamboo_index + 1, a, b, c)
        return min(stuff_a, stuff_b, stuff_c, unused)

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


if __name__ == "__main__":
    main()

Submission status: Accepted

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

ans = 10**9
for m in range(4**n):
    length = [0, 0, 0, 0]
    counts = [0, 0, 0, 0]
    for d in range(n-1, -1, -1):
        i = m//(4**d)
        length[i] += l[d]
        counts[i] += 1
        m %= 4**d
        
    if 0 in length[:3]:
        continue
    ans_tmp = 0
    for i in range(3):
        ans_tmp += abs([a,b,c][i] - length[i]) + 10*(counts[i]-1)
    ans = min(ans, ans_tmp)
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

def find(idx, a1, b1, c1):
    if idx == n:
        if min(a1, b1, c1) == 0:
            return 100000000
        return abs(a - a1) + abs(b - b1) + abs(c - c1) - 30
    r0 = find(idx + 1, a1, b1, c1)
    r1 = find(idx + 1, a1 + l[idx], b1, c1) + 10
    r2 = find(idx + 1, a1, b1 + l[idx], c1) + 10
    r3 = find(idx + 1, a1, b1, c1 + l[idx]) + 10
    return min(r0, r1, r2, r3)

print(find(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(depth, a, b, c):
	if depth == N:
		if min(a, b, c) > 0:
			MP = abs(A - a) + abs(B - b) + abs(C - c) - 30
		else:
			MP = 10**9
		return MP

	ret0 = dfs(depth + 1, a, b, c)
	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())
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, ao, bo, co = map(int, input().split())
l = [int(input()) for _ in range(n)]

def solve(ln, a, b, c):
    if len(l) <= ln:
        if a * b * c > 0:
            return abs(ao - a) + abs(bo - b) + abs(co - c)
        else:
            return float("inf")
    
    x = l[ln]
    return min([
        solve(ln + 1, a, b, c),
        solve(ln + 1, a + x, b, c) + (10 if a > 0 else 0),
        solve(ln + 1, a, b + x, c) + (10 if b > 0 else 0),
        solve(ln + 1, a, b, c + x) + (10 if c > 0 else 0)
    ])
print(solve(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 func(k, x, y, z):
  if k == n:
    if not x or not y or not z:
      return 10**9
    else:
      return abs(x-a) + abs(y-b) + abs(z-c) - 30
  else:
    p = func(k+1, x, y, z)
    q = func(k+1, x + l[k], y, z) + 10
    r = func(k+1, x, y + l[k], z) + 10
    s = func(k+1, x, y, z + l[k]) + 10
    return min(p, q, r, s)

print(func(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 ** 5

# L[i]までのコスト
# L[i+1]をAにしたコスト
# L[i+1]をBにしたコスト
# L[i+1]をCにしたコスト

def cost(lres, a, b, c):
    if len(lres) == 0:
        if a * b * c > 0:
            return abs(a - A) + abs(b - B) + abs(c - C)
        else:
            return INF

    x = lres[0]
    return min(
        cost(lres[1:], a, b, c),
        cost(lres[1:], a + x, b, c) + (10 if a > 0 else 0),
        cost(lres[1:], a, b + x, c) + (10 if b > 0 else 0),
        cost(lres[1:], a, b, c + x) + (10 if c > 0 else 0),
    )

print(cost(l, 0, 0, 0))

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 9)
N, A, B, C = map(int, input().split())
l_list = [int(input()) for _ in range(N)]


def dfs(now, a, b, c):
    if now == N:
        return abs(a - A) + abs(b - B) + abs(c - C) - 30 if min(a, b, c) > 0 else float("inf")
    res0 = dfs(now + 1, a, b, c)
    res1 = dfs(now + 1, a + l_list[now], b, c) + 10
    res2 = dfs(now + 1, a, b + l_list[now], c) + 10
    res3 = dfs(now + 1, a, b, c + l_list[now]) + 10
    return min(res0, res1, res2, res3)


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

Submission status: Accepted

Solution code:
N,A,B,C=map(int,input().split())
l=[int(input()) for _ 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
    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:
N, A, B, C = list(map(int, input().split()))
vectors = [int(input()) for i in range(N)]

def recursiv(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 9999
    r0 = recursiv(cur + 1, a, b, c)
    r1 = recursiv(cur + 1, a + vectors[cur], b, c) + 10
    r2 = recursiv(cur + 1, a, b + vectors[cur], c) + 10
    r3 = recursiv(cur + 1, a, b, c + vectors[cur]) + 10
    return min(r0, r1, r2, r3)

print(recursiv(0,0,0,0))

Submission status: Accepted

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


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

    # See:
    # https://img.atcoder.jp/abc119/editorial.pdf
    # http://drken1215.hatenablog.com/entry/2019/02/24/224100?_ga=2.107425799.427838713.1551485650-1201012407.1527836447#fnref:1
    # https://atcoder.jp/contests/abc119/submissions/4372229
    def dfs(i, cur_a, cur_b, cur_c):
        if i == n:
            if cur_a == 0 or cur_b == 0 or cur_c == 0:
                return float('inf')
            return abs(a - cur_a) + abs(b - cur_b) + abs(c - cur_c)

        w = dfs(i + 1, cur_a, cur_b, cur_c)
        x = dfs(i + 1, cur_a + l[i], cur_b, cur_c) + 10 * (cur_a != 0)
        y = dfs(i + 1, cur_a, cur_b + l[i], cur_c) + 10 * (cur_b != 0)
        z = dfs(i + 1, cur_a, cur_b, cur_c + l[i]) + 10 * (cur_c != 0)

        return min(w, x, y, z)

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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


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

    # See:
    # https://img.atcoder.jp/abc119/editorial.pdf
    # http://drken1215.hatenablog.com/entry/2019/02/24/224100?_ga=2.107425799.427838713.1551485650-1201012407.1527836447#fnref:1
    def dfs(i, cur_a, cur_b, cur_c):
        if i == n:
            if cur_a == 0 or cur_b == 0 or cur_c == 0:
                return float('inf')
            return abs(a - cur_a) + abs(b - cur_b) + abs(c - cur_c) - 30

        result0 = dfs(i + 1, cur_a, cur_b, cur_c)
        result1 = dfs(i + 1, cur_a + l[i], cur_b, cur_c) + 10
        result2 = dfs(i + 1, cur_a, cur_b + l[i], cur_c) + 10
        result3 = dfs(i + 1, cur_a, cur_b, cur_c + l[i]) + 10

        return min(result0, result1, result2, result3)

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N,A,B,C = map(int, input().split())
l = [int(input()) for i in range(N)]
INF = 10**9
def dfs(cur,a,b,c):
    if cur == N:
        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 = list(map(int, raw_input().split()))
length = list()
for n in xrange(N): length.append(int(raw_input()))

INF = 10 ** 16

def cost(idx, a, b, c):
    if idx == N: return abs(a-A)+abs(b-B)+abs(c-C)-30 if min(a,b,c) > 0 else INF
    val1 = cost(idx+1, a, b, c)
    val2 = cost(idx+1, a+length[idx], b, c) + 10
    val3 = cost(idx+1, a, b+length[idx], c) + 10
    val4 = cost(idx+1, a, b, c+length[idx]) + 10
    return min(val1, val2, val3, val4)

print cost(0, 0, 0, 0)

Submission status: Accepted

Solution code:
from bisect import bisect, bisect_left
from sys import stdin

def dfs(n):
    l = [0] * n
    ret = []
    def dfs_(n):
        if not n:
            ret.append(l[:])
            return
        for i in ('a', 'b', 'c', '_'):
            l[n-1] = i
            dfs_(n-1)
    dfs_(n)
    return ret

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

    ans = float('inf')
    for s in dfs(N):
        banboo = {'a': 0, 'b': 0, 'c':0, '_': 0}
        for i, t in enumerate(s):
            banboo[t] += L[i]
        if all((banboo['a'] > 0,
                banboo['b'] > 0,
                banboo['c'] > 0)):
            mp = (s.count('a') + s.count('b') + s.count('c') - 3) * 10
            mp += (abs(banboo['a'] - A) +
                   abs(banboo['b'] - B) +
                   abs(banboo['c'] - C))
            ans = min(ans, mp)
    print(ans)

input = lambda: stdin.readline()
main()

Submission status: Accepted

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

for n in range(N):
	l.append(int(input()))

m = 4
K = 4**N-1

a = 0
b = 0
c = 0
jointmagic = 0
magic = 2**20

def dec(m , k):
	k_m = []
	for count in range(N-1):
		k_m.append(k%m)
		k = k // m
	k_m.append(k)
	return(k_m)

for k in range(K):
	Takesakusen.append(dec(m , k))
#print(Takesakusen)

for T in Takesakusen:
	if 1 in T and 2 in T and 3 in T:
		for s in range(N):
			if T[s] == 1:
				a += l[s]
				jointmagic += 10
			if T[s] == 2:
				b += l[s]
				jointmagic += 10
			if T[s] == 3:
				c += l[s]
				jointmagic += 10
		if magic > abs(a - A) + abs(b - B) + abs(c - C) + jointmagic - 30 :
			magic = abs(a - A) + abs(b - B) + abs(c - C) + jointmagic - 30
#		print("T =" + str(T) )
#		print("a = " + str(a))
#		print("b = " + str(b))
#		print("c = " + str(c)	)
#		print(abs(a - A) + abs(b - B) + abs(c - C) + jointmagic - 30)
#		print("magic =" + str(magic))


	a = 0
	b = 0
	c = 0
	jointmagic = 0

print(magic)

Submission status: Accepted

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

def dfs(h, a, b, c):
    if not h:
        return INF if a * b * c == 0 else abs(A - a)+  abs(B - b) + abs(C - c)

    k = h[0]
    return min(dfs(h[1:], a, b, c),
               dfs(h[1:], a + k, b, c) + 10,
               dfs(h[1:], a, b + k, c) + 10,
               dfs(h[1:], a, b, c + k) + 10)

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

Submission status: Accepted

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

import itertools
import copy

MP = []
l_temp=[]

for abc_perm in list(itertools.permutations([A,B,C])):
    l_temp = copy.deepcopy(l)
    MP_consumption = 0
    for i,j in enumerate(abc_perm):
        c_list = []
        MP_consumption_list = []
        min_index = 0
        for k in range(1,len(l_temp)-(2-i)+1):
            for c in list(itertools.combinations(l_temp,k)):
                c_list.append(c)
                MP_consumption_list.append( abs(j - sum(c)) + 10*(len(c)-1 ) )
        MP_consumption += min(MP_consumption_list)
        min_index = MP_consumption_list.index(min(MP_consumption_list))
        [l_temp.remove(a) for a in c_list[min_index]]
    MP.append(MP_consumption)
print(min(MP))