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: Wrong Answer

Solution code:
# encoding:utf-8
import copy
import numpy as np
import random

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

def to_4digit(i):
    digits = [0]*n
    digit = 0
    while(i>1):
        digits[digit] = i%4
        digit += 1
        i//=4
    return digits

ans = 10**10
for i in range(4**n):
    choice = to_4digit(i)
    abc = [0]*3
    mp = 0
    for j in range(len(choice)):
        if choice[j]==3:#trash
            pass
        else:
            if abc[choice[j]]!=0:
                mp += 10
            abc[choice[j]] += l[j]
    if 0 in abc:
        pass
    else:
        mp += abs(abc[0]-a)+abs(abc[1]-b)+abs(abc[2]-c)
        ans = min(ans,mp)
    # print(ans)
print(ans)

Submission status: Wrong Answer

Solution code:
# encoding:utf-8
import copy
import numpy as np
import random

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

def to_4digit(i):
    digits = [0]*n
    digit = 0
    while(i>1):
        digits[digit] = i%4
        digit += 1
        i//=4
    return digits

ans = 10**10
for i in range(4**n):
    choice = to_4digit(i)
    a_tem = 0
    b_tem = 0
    c_tem = 0
    mp = 0
    for j in range(len(choice)):
        if choice[j]==0:
            if a_tem != 0:
                mp += 10
            a_tem += l[j]
        elif choice[j]==1:
            if b_tem != 0:
                mp += 10
            b_tem+= l[j]
        elif choice[j]==2:
            if c_tem != 0:
                mp += 10
            c_tem+= l[j]
        else:pass
    if a_tem==0 or b_tem==0 or c_tem==0:
        pass
    else:
        mp += abs(a-a_tem)+abs(b-b_tem)+abs(c-c_tem)
        ans = min(ans,mp)
    # print(ans)
print(ans)


# print(to_4digit(10))

Submission status: Wrong Answer

Solution code:
ri = lambda: int(input())
rl = lambda: list(map(int,input().split()))

N,A,B,C=rl()
l=[ri() for _ in range(N)]

def dfs(cur,a,b,c):
    if cur==N:
        return abs(A-a)+abs(B-b)+abs(C-c)-30
    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: Wrong Answer

Solution code:
def ttt (x,y,z,i,m,p,q,r):
  if i==n and (p==0 or q==0 or r==0):
    print(p,q,r)
    return 10000000
  if i==n:
    m+=abs(x-a)+abs(y-b)+abs(z-c)
    return m
  else:
    return min(ttt(x,y,z,i+1,m,p,q,r), ttt(x+l[i],y,z,i+1,m+10,p+1,q,r), 
               ttt(x,y+l[i],z,i+1,m+10,p,q+1,r), ttt(x,y,z+l[i],i+1,m+10,p,q,r+1))
n,a,b,c=[5, 100, 90, 80]
l=[98,40,30,21,80]
print(ttt(0,0,0,0,0,0,0,0)-30)

Submission status: Wrong Answer

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 = (len(D['A']) - 1) * 10 + abs(sum(D['A']) - A)
    tmp += (len(D['B']) - 1) * 10 + abs(sum(D['B']) - A)
    tmp += (len(D['C']) - 1) * 10 + abs(sum(D['C']) - A)
    ans = min(ans, tmp)

print(ans)

Submission status: Wrong Answer

Solution code:
n, a, b, c=map(int, input().split())
l=[0]*n
ch=0
ans=0

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

ans+=abs(l[0]-a)
ans+=abs(l[1]-b)
ans+=abs(l[2]-c)

    
for i in range(4**n):
    ch=0
    mp=0
    s=[0]*4
    for j in range(n):
        for k in range(4):
            if (i>>2*j) % 2**2 ==k:
                if s[k]==0:
                    0
                else:
                    mp+=10
                s[k]+=l[j]
                    
    for k in range(3):
        if s[k]==0:
            #invalid
            ch=1
    if ch==1:
        0
        #invalid
    else:
        mp+=abs(int(s[0]-a))
        mp+=abs(int(s[1]-b))
        mp+=abs(int(s[2]-c))
        ans=min(ans,mp)
    #print('bin(j),ch,s,mp',bin(i),ch,s,mp,abs(int(s[0]-a)),abs(int(s[1]-b)),abs(int(s[2]-c)))
    
print(ans)

Submission status: Wrong Answer

Solution code:
import itertools

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

l_list = []
for i in range(N):
    l_list.append(int(input()))
l_list.sort()

todo_list = [0,1,2,3]

todo_comb = itertools.product(todo_list, repeat=N)

ABCNo = [[],[],[],[]]

MP = float('inf')

for todo in todo_comb:
    for i in range(N):
        ABCNo[todo[i]].append(l_list[i])

    if ABCNo[0] == [] or ABCNo[1] == [] or ABCNo[2] == []:
        continue

    AA = sum(ABCNo[0])
    BB = sum(ABCNo[1])
    CC = sum(ABCNo[2])

    tmp = abs(AA-A) + abs(BB-B) + abs(CC-C)
    tmp += (len(ABCNo[0]) + len(ABCNo[1]) + len(ABCNo[2]))*10

    if tmp < MP:
        MP = tmp

print(tmp)

Submission status: Wrong Answer

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

def dfs(depth, a, b, c):
	if depth == N:
		if a == 0 or b == 0 or c == 0:
			return 1e9
		return abs(A - a) + abs(B - b) + abs(C - c)
	
	ans = dfs(depth+1, a, b, c)
	ans = min(ans, dfs(depth + 1, a + inputs[depth], b, c)) + 10 * (a != 0)
	ans = min(ans, dfs(depth + 1, a, b + inputs[depth], c)) + 10 * (b != 0)
	ans = min(ans, dfs(depth + 1, a, b, c + inputs[depth])) + 10 * (c != 0)
	
	return ans

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

Submission status: Wrong Answer

Solution code:
from itertools import product

import numpy as np

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

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

l = np.array(l)

###########

'''
Aの竹の材料にする、Bの竹の材料にする、Cの竹の材料にする、使わない
'''

min_cost = 1e10

for i in product(np.arange(4), repeat=N):
    i = np.array(i)

    if(np.sum(np.unique(i)) == 6):
        continue

    for_a = l[i == 0]
    for_b = l[i == 1]
    for_c = l[i == 2]

    A_diff = np.abs(np.sum(for_a) - A) + (len(for_a) - 1) * 10
    B_diff = np.abs(np.sum(for_b) - B) + (len(for_b) - 1) * 10
    C_diff = np.abs(np.sum(for_c) - C) + (len(for_c) - 1) * 10

    cost = A_diff + B_diff + C_diff

    if(cost < min_cost):
        min_cost = cost

print(min_cost)

Submission status: Wrong Answer

Solution code:
from itertools import product

import numpy as np

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

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

l = np.array(l)

###########

'''
Aの竹の材料にする、Bの竹の材料にする、Cの竹の材料にする、使わない
'''

min_cost = 1e10

for i in product(np.arange(4), repeat=N):
    i = np.array(i)

    if(len(np.unique(i)) != 4):
        continue

    for_a = l[i == 0]
    for_b = l[i == 1]
    for_c = l[i == 2]

    A_diff = np.abs(np.sum(for_a) - A) + (len(for_a) - 1) * 10
    B_diff = np.abs(np.sum(for_b) - B) + (len(for_b) - 1) * 10
    C_diff = np.abs(np.sum(for_c) - C) + (len(for_c) - 1) * 10

    cost = A_diff + B_diff + C_diff

    if(cost < min_cost):
        min_cost = cost

print(min_cost)

Submission status: Wrong Answer

Solution code:
import numpy as np

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

# goals = [1000,800,100]
# ls = [300,333,400,444,500,555,600,666]
# goals = [100, 90, 80]
# ls = [98, 40,30,21,80]

def count(x,ls):
    try:
        x.index(0)
        x.index(1)
        x.index(2)        
    except:
        return False
    A = np.zeros(len(goals))
    for i,l in enumerate(ls):
        add_ind = x[i]
        if add_ind==3:
            pass
        else:
            A[add_ind] += l
    combine_MP = 0
    for i in range(3):
        combine_MP += 10*(x.count(i)-1)
    return A, combine_MP
    
    
score_min = 100000
for N in range(4**8):
    x = []
    for j in range(len(ls)):
        x.append(N % 4)
        N = N // 4 
    try:
        A, combine_MP = count(x,ls)
        score = np.abs(A-np.array(goals)).sum() + combine_MP
        if score_min >= score:
            score_min = score
    except:
        pass
print(score_min)

Submission status: Wrong Answer

Solution code:
def dfs(i, A, B, C):
    global ans
    if i == n:
        if A and B and C:
            s = 0
            ABC = [A, B, C]
            for i in range(3):
                s += (len(ABC[i]) - 1) * 10 + abs(sum(ABC[i]) - abc[i])
            if s == 110:
                print(A, B, C)
            ans = min(ans, s)
    else:
        dfs(i + 1, A + [l[i]], B, C)
        dfs(i + 1, A, B + [l[i]], C)
        dfs(i + 1, A, B, C + [l[i]])
        dfs(i + 1, A, B, C)

n, a, b, c = map(int, input().split())
l = [int(input()) for i in range(n)]
abc = [a, b, c]
ans = float("inf")
dfs(0, [], [], [])
print(ans)

Submission status: Wrong Answer

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 
  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))
#example answer

Submission status: Wrong Answer

Solution code:
import numpy as np
n,A,B,C=list(map(int,input().split(" ")))
l_list = [0]*n
for i in range(n):
    l_list[i] = int(input())
    
flags = [0]*n

anss = []

def f(d):
    if d < n:
        for i in [0,1,2,3]:
            flags[d] = i
            f(d+1)
    else:
        tA = A
        tB = B
        tC = C
        for i in range(n):
            if flags[i] == 1:
                tA -= l_list[i]
            elif flags[i] == 2:
                tB -= l_list[i]
            elif flags[i] == 3:
                tC -= l_list[i]
        ans = 0
        ans += max(0,(flags.count(1)-1)*10)
        ans += max(0,(flags.count(2)-1)*10)
        ans += max(0,(flags.count(3)-1)*10)
        ans += abs(tA)+abs(tB)+abs(tC)
        anss.append(ans)
f(0)
print(min(anss))

Submission status: Wrong Answer

Solution code:
N, A, B, C = map(int, input().split())
l = [int(input()) for i in range(N)]
def calc(take, a, b, c):
    if take == N:
        return abs(a-A)+abs(b-B)+abs(c-C) - 30
    ret0 = calc(take+1, a, b, c)
    ret1 = calc(take+1, a+l[take], b, c) + 10
    ret2 = calc(take+1, a, b+l[take], c) + 10
    ret3 = calc(take+1, a, b, c+l[take]) + 10
    return min(ret0, ret1, ret2, ret3)
    
print(calc(0,0,0,0))

Submission status: Wrong Answer

Solution code:
N, A, B, C = map(int, input().split())
l = [int(input()) for i in range(N)]
def calc(take, a, b, c):
    if take == N:
        return abs(a-A)+abs(b-B)+abs(c-C) 
    ret0 = calc(take+1, a, b, c)
    ret1 = calc(take+1, a+l[take], b, c) + 10
    ret2 = calc(take+1, a, b+l[take], c) + 10
    ret3 = calc(take+1, a, b, c+l[take]) + 10
    return min(ret0, ret1, ret2, ret3)
    
print(calc(0,0,0,0))

Submission status: Wrong Answer

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

INF = 10 ** 9


def dfs(cursor, a, b, c):  # cursor:カーソル a,b,c:現在の竹の長さ
    if cursor == N:  # cursorが最後まで行ったら終了する。
        return abs(a - A) + abs(b - B) + abs(c - C) - 30 if min(a, b, c) > 0 else INF
    # abs(a - A) + abs(b - B) + abs(c - C) - 30 でなぜ30を減じているのかというと、
    # dfs(0,0,0)
    # dfs(0,0,0,0)で始まる以上、最初に選ばれるa,b,cを決定する際にもコストが10増加してしまうからである。
    # また、全探索を行う中で、a,b,cの初期値が0,0,0である以上、a,b,cのどれかが0のまま終了する場合が存在する。
    # この場合はa,b,cに竹が対応してないと言えるため、解にはならない、そこで三項演算子を利用して
    # その場合についてコストをINFとしている

    # 以下は4**Nで展開される再起処理となる。
    # カーソルの当たっている竹に対して、(A or B or Cに合成する) or (合成しない)の場合に分ける
    no_compound_pattern = dfs(cursor, a, b, c)
    compound_a_pattern = dfs(cursor, a+L[cursor], b, c)
    compound_b_pattern = dfs(cursor, a, b+L[cursor], c)
    compound_c_pattern = dfs(cursor, a, b, c+L[cursor])

    # 結果的に以下の値が返るのはそれぞれのパターンのコストが決定されてからなので
    # 以下のコードは最終的なコストの最小値
    return min(no_compound_pattern,compound_a_pattern,compound_b_pattern,compound_c_pattern)

Submission status: Wrong Answer

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

def dfs(i, a, b, c):
    if i == N:
        res = abs(A - a) + abs(B - b) + abs(C - c) - 30
        return res
    res0 = dfs(i + 1, a, b, c)
    res1 = dfs(i + 1, a + L[i], b, c) + 10
    res2 = dfs(i + 1, a, b + L[i], c) + 10
    res3 = dfs(i + 1, a, b, c + L[i]) + 10
    return min(res0, res1, res2, res3)

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

Submission status: Wrong Answer

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, ret2, ret3)

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

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
# ----------------------------------------------------------------
# Use Solve Function

def solve(lines):
    def dfs(depth, l):
        if depth == 0:
            calc(l)
            return
        for i in range(4):
            dfs( depth - 1, l + str(i) )

    def calc(l):
        res = 0
        if not '1' in l or not '2' in l or not '3' in l:
            return
        aa = [[] for i in range(3)]
        for i,x in enumerate(list(l)):
            x = int(x)
            if x == 3:
                continue
            take = NLIST[i]
            aa[x].append(take)
        for i, a in enumerate(aa):
            if len(a) > 1:
                res += 10 * (len(a) - 1)
            res += abs(ABC[i] - sum(a))
        RESULT.append(res)




    RESULT = []
    N,A,B,C = map(int, lines.pop(0).split(' '))
    ABC = [A,B,C]
    NLIST = []
    for i in range(N):
        NLIST.append(int(lines.pop(0)))
    dfs(N, "")
    print(min(RESULT))

lines = [x.strip() for x in sys.stdin.readlines()]

#
# solve !!
#
solve(lines)

Submission status: Wrong Answer

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


def f(a, b, c, bl, ans):
    if len(bl) == 0:
        if len(a) == 0 or len(b) == 0 or len(c) == 0:
            return ans
        return abs(A - sum(a)) + (len(a) - 1) * 10 + \
            abs(B - sum(b)) + (len(b) - 1) * 10 + \
            abs(C - sum(c)) + (len(c) - 1) * 10
    for i in range(len(bl)):
        l = bl[i]
        a.append(l)
        ans = min(ans, f(a, b, c, bl[i + 1:], ans))
        a.pop(-1)
        b.append(l)
        ans = min(ans, f(a, b, c, bl[i + 1:], ans))
        b.pop(-1)
        c.append(l)
        ans = min(ans, f(a, b, c, bl[i + 1:], ans))
        c.pop(-1)
        ans = min(ans, f(a, b, c, bl[i + 1:], ans))
    return ans


def main():
    bl = []
    for _ in range(N):
        bl.append(int(input()))
    print(f([], [], [], bl, 1000))


main()

Submission status: Wrong Answer

Solution code:
from itertools import product
N,*ABC=map(int,input().split())
L=[int(input()) for i in range(N)]
MP=10**9
for P in product([0,1,2,3],repeat=N):
    if {0,1,2}<=set(P):
        m=[0,0,0]
        for p,l in zip(P,L):
            if p!=3:m[p]+=l
            cost1=sum(abs(m-l) for m,l in zip(m,ABC))
            cost2=sum(1 for i in P if i!=3)-3
            MP=min(MP,cost1+cost2*10)
print(MP)

Submission status: Wrong Answer

Solution code:
n, A, B, C = map(int, input().split())
l = [int(input()) for _ in range(n)]
inf = 10 ** 10
def dfs(k, a, b, c):
    if k == n:
        if set() in (a,b,c):
            return inf
        return abs(A - sum(a)) + abs(B - sum(b)) + abs(C - sum(c)) + (len(a) + len(b) + len(c) - 3) * 10
    ret = [0] * 4
    ad = a.copy()
    bd = b.copy()
    cd = c.copy()
    ad.add(l[k])
    bd.add(l[k])
    cd.add(l[k])
    ret[0] = dfs(k+1, ad, b, c)
    ret[1] = dfs(k+1, a, bd, c)
    ret[2] = dfs(k+1, a, b, cd)
    ret[3] = dfs(k+1, a, b, c)
    return min(ret)
print(dfs(0, set(), set(), set()))

Submission status: Wrong Answer

Solution code:
from itertools import product
N, A, B, C = [int(i) for i in input().split()]
l_ls = list()
for i in range(N):
    l_ls.append(int(input()))
best = 1e9
for combi in product(range(4),repeat=N):
    a = [l for l, label in zip(l_ls, combi) if label == 1]
    b = [l for l, label in zip(l_ls, combi) if label == 2]
    c = [l for l, label in zip(l_ls, combi) if label == 3]
    dis = abs(A - sum(a)) + abs(B - sum(b)) + abs(C - sum(c)) \
          + (len(a) + len(b) + len(c) - 3) * 10
    if best > dis:
        best = dis
print(best)

Submission status: Wrong Answer

Solution code:
n,a,b,c=map(int,input().split())
l = [int(input()) for _ in range(n)]
ans = 100000000
for i in range(4**n):
  x,y,z=0,0,0
  s,t,u=0,0,0
  for j in range(n):
    t = i % 4
    if t == 0:
      x += l[j]
      s += 1
    elif t == 1:
      y += l[j]
      t += 1
    elif t == 2:
      z += l[j]
      u += 1
  if s == 0 or t == 0 or u == 0:
    continue
  k = (s + t + u - 3) * 10
  k += abs(a - x)
  k += abs(b - y)
  k += abs(c - z)
  ans = min(ans, k)
print(ans)

Submission status: Wrong Answer

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

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


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

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

    return min(part0, part1, part2, part3)


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

Submission status: Wrong Answer

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

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

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

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

Submission status: Wrong Answer

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

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

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

  return min(res0, res1, res2, res3)

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

Submission status: Wrong Answer

Solution code:
def Base_10_to_n(X, n):
    if (int(X/n)):
        return Base_10_to_n(int(X/n), n)+str(X%n)
    return str(X%n)

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

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

Submission status: Wrong Answer

Solution code:
def Base_10_to_n(X, n):
    if (int(X/n)):
        return Base_10_to_n(int(X/n), n)+str(X%n)
    return str(X%n)

n,a,b,c=map(int,input().split())
L=[]
for _ in range(n):
    L.append(int(input()))
at,bt,ct=[],[],[]
ans_list=[]
ans=999999999
for i in range(4**len(L)-1):
    i_4_list=list(Base_10_to_n(i, 4).zfill(len(L)))
    at,bt,ct=[],[],[]
    for num,i_4 in enumerate(i_4_list):
        if i_4=='0':
            at.append(L[num])
        elif i_4=='1':
            bt.append(L[num])
        elif i_4=='2':
            ct.append(L[num])
        else:
            continue
    if at!=[] and ct!=[] and ct!=[]:
        al=0
        al+=(len(at)+len(bt)+len(ct)-3)*10
        al+=(abs(sum(at)-a)+abs(sum(bt)-b)+abs(sum(ct)-c))
        if al < ans:
            ans=al
print(ans)

Submission status: Wrong Answer

Solution code:
def Base_10_to_n(X, n):
    if (int(X/n)):
        return Base_10_to_n(int(X/n), n)+str(X%n)
    return str(X%n)

n,a,b,c=map(int,input().split())
L=[]
for _ in range(n):
    L.append(int(input()))
print(L)
at,bt,ct=[],[],[]
ans_list=[]
ans=999999999
for i in range(4**len(L)-1):
    i_4_list=list(Base_10_to_n(i, 4).zfill(len(L)))
    at,bt,ct=[],[],[]
    for num,i_4 in enumerate(i_4_list):
        if i_4=='0':
            at.append(L[num])
        elif i_4=='1':
            bt.append(L[num])
        elif i_4=='2':
            ct.append(L[num])
        else:
            continue
    if at!=[] and ct!=[] and ct!=[]:
        al=0
        al+=(len(at)+len(bt)+len(ct)-3)*10
        al+=(abs(sum(at)-a)+abs(sum(bt)-b)+abs(sum(ct)-c))
        if al < ans:
            ans=al
print(ans)

Submission status: Wrong Answer

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

L = [int(input()) for i in range(N)]

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

Submission status: Wrong Answer

Solution code:
# DFS
# N,A,B,C = 5,100,90,80
# L = [98,40,30,21,80]
N,A,B,C = map(int, input().split())
L = [int(input()) for _ in range(N)]
ans = float('inf')

def dfs(cur,a,b,c)->int:
    if cur==N:
        if min(a,b,c)>0: return abs(a-A)+abs(b-B)+abs(c-C)-30
        else:            return float('inf')
        
    ret0 = dfs(cur+1, a,       b,       c) + 10
    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: Wrong Answer

Solution code:
# DFS
N,A,B,C = 5,100,90,80
L = [98,40,30,21,80]
# N,A,B,C = map(int, input().split())
# L = [int(input()) for _ in range(N)]
ans = float('inf')

def dfs(cur,a,b,c)->int:
    if cur==N:
        if min(a,b,c)>0: return abs(a-A)+abs(b-B)+abs(c-C)-30
        else:            return float('inf')
        
    ret0 = dfs(cur+1, a,       b,       c) + 10
    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: Wrong Answer

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

INF = 1e+10

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

def dfs(cur, aa, bb, cc):
    if cur == n:
        return abs(aa-a) + abs(bb-b) + abs(cc-c) -30 if min(a, b, c) > 0 else inf
    ret0 = dfs(cur+1, aa, bb, cc)
    ret1 = dfs(cur+1, aa + l[cur], bb, cc) + 10
    ret2 = dfs(cur+1, aa, bb + l[cur], cc) + 10
    ret3 = dfs(cur+1, aa, bb, cc + l[cur]) + 10
    return min(ret0, ret1, ret2, ret3)

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

Submission status: Wrong Answer

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

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

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

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

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
n, a, b, c = map(int, input().split(" "))
take = [a, b, c]
l = []
for i in range(n):
    l.append(int(input()))
take.sort()
l.sort()
mp = 0
get = []
for i in take:
    if i in l:
        get.append(i)
for i in get:
    l.remove(i)
    take.remove(i)

get2 = []
get3 = [[],[],[]]
for i in take:
    mini = [0, 0, 10]
    mini2 = [0, 0, 10]
    mini3 = [0, 0, 10]
    for j in l:
        d = abs(i - j)
        if d <= mini[2]:
            if mini2[0] == 0 and mini[0] != 0:
                mini2 = mini
            elif mini2[0] != 0 and mini[0] != 0:
                mini3 = mini2
                mini2 = mini
            mini = [j, i, d]
    get2.append([mini, mini2, mini3])
for i in get2:
    for ii in i:
        if ii[0] == 0:
            break
        else:
            get3[0].append(ii[0])
            get3[1].append(ii[1])
            get3[2].append(ii[2])
ind = []
mark = []
for i in get3[2]:
    ii = get3[2].index(min(get3[2]))
    if get3[1][ii] not in mark:
        s = get3[0].pop(ii)
        t = get3[1].pop(ii)
        u = get3[2].pop(ii)
        ind.append([s,t,u])
        mark.append(t)
    if len(ind) == 3:
        break
for i in ind:
    l.remove(i[0])
    take.remove(i[1])
    mp += i[2]

def add_take():
    global take, l, mp
    get2 = []
    for i in take:
        added = 0
        mp2 = -10
        mini = [[], 0, 10, 0]
        for j in l:
            mp2 += 10
            added += j
            mini[0].append(j)
            d = abs(i - added)
            if d <= mini[2]:
                mini[1] = i
                mini[2] = d
                mini[3] = mp2
                if added > i:
                    for k in l:
                        added -= k
                        d = abs(i - added)
                        if d <= mini[2]:
                            mp2 -= 10
                            mini[0].remove(k)
                            mini[1] = i
                            mini[2] = d
                            mini[3] = mp2
                        else:
                            break
            elif added > i + 10:
                break
        get2.append(mini)
    get3 = [[],[],[],[]]
    for ii in get2:
        if ii[1] == 0:
            break
        else:
            get3[0].append(ii[0])
            get3[1].append(ii[1])
            get3[2].append(ii[2])
            get3[3].append(ii[3])
    try:
        index = get3[2].index(min(get3[2]))
        for i in get3[0][index]:
            l.remove(i)
        take.remove(get3[1][index])
        mp += get3[2][index] + get3[3][index]
    except ValueError:
        err = True
        return err

if len(take) != 0:
    a = add_take()
    if a:
        while True:
            list = [0,0,1000]
            for i in take:
                for j in l:
                    if abs(i-j) < list[2]:
                        list = [j, i, abs(i-j)]
            l.remove(list[0])
            take.remove(list[1])
            mp += list[2]
            if len(take)==0:
                break

print("{}".format(mp))

Submission status: Wrong Answer

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


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