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:
N, A, B, C = map(int, input().split())

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


def convert(n):
    list_q = []
    while True:
        a = n % 4
        n //= 4
        list_q.insert(0, a)
        if n == 0:
            break
    return list_q


MP = -1
for i in range(21, 4**N):
    mp_a = 0
    mp_b = 0
    mp_c = 0
    use = convert(i)
    a_n = use.count(1)
    b_n = use.count(2)
    c_n = use.count(3)
    if a_n == 0 or b_n == 0 or c_n == 0:
        pass
    else:
        mp_a += 10 * (a_n - 1)
        mp_b += 10 * (b_n - 1)
        mp_c += 10 * (c_n - 1)
        b_a = 0
        b_b = 0
        b_c = 0
        idx = 0
        for i in use:
            if i == 1:
                b_a += l[idx]
            elif i == 2:
                b_b += l[idx]
            elif i == 3:
                b_c += l[idx]
            idx += 1
        mp_a += abs(A-b_a)
        mp_b += abs(B-b_b)
        mp_c += abs(C-b_c)
        mp = mp_a + mp_b + mp_c
        if MP == -1 or mp < MP:
            MP = mp
print(MP)

Submission status: Wrong Answer

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

def dfs(index, a, b, c):
    if index == N:
        return abs(a-A)+abs(b-B)+abs(c+C)-30 if min(a, b, c) > 0 else float('inf')
    
    val1 = dfs(index+1, a, b, c)
    val2 = dfs(index+1, a+l[index], b, c) + 10
    val3 = dfs(index+1, a, b+l[index], c) + 10
    val4 = dfs(index+1, a, b, c+l[index]) + 10
    return min(val1, val2, val3, val4)

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

Submission status: Wrong Answer

Solution code:
import copy

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

nums = [[0, 0, 0, 0],
        [0, 0, 0, 0],
        [0, 0, 0, 0]] 
    
for l in ls:
    tmpA = []
    for num in nums:
        for i in range(3):
            tmpB = copy.deepcopy(num)
            if tmpB[i]:
                tmpB[3] += 10
            tmpB[i] += l            
            tmpA.append(tmpB)
    nums.extend(tmpA)   

ans = 10**20

for num in nums:
    n = abs(A-num[0]) + abs(B-num[1]) + abs(C-num[2]) + num[3]
    ans = min(ans, n)
print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
import itertools
INF = 1000000007
n, a, b, c = map(int, input().split())
B = [a, b, c]
take = [int(input()) for i in range(n)]
sh = [i for i in itertools.product(range(4), repeat=n)]
res = INF
for C in sh:
    ex = [0] * 3
    ifres = 0
    for index, x in enumerate(C):
        if x != 3:
            if ex[x] != 0:
                ifres += 10
            ex[x] += take[index]
    for i in range(3):
        ifres += abs(B[i]-ex[i])
    if ifres < res:
        res = ifres
print(res)

Submission status: Wrong Answer

Solution code:
def main():
	n,a,b,c=map(int,input().split())
	l=[]
	for i in range(n):
		l.append(int(input()))
	
	key=[[0],[1],[2],[3]]
	key_4=key[:]
	for i in range(n-1):
		temp=[]
		for elem1 in key:
			for elem2 in key_4:
				temp.append(elem1+elem2)
		key=temp[:]

	res=10*9
	for elem in key:
		res=min(res,judge(elem,l,a,b,c))
	print(res)
	
def judge(key,l,a,b,c):
	num=[0,0,0,0]
	array=[0,0,0,0]
	for i in range(len(key)):
		array[key[i]]+=l[i]
		num[key[i]]+=1
	if(num[0]==0 or num[1]==0 or num[2]==0):
		return 10*9
	result=0
	result=abs(array[0]-a)+abs(array[1]-b)+abs(array[2]-c)
	result+=(num[0]+num[1]+num[2]-3)*10

	return result

def inputList():
	a=list(map(int,input().split()))
	return a

def multisort(li,index):
	return sorted(li,key=lambda x: x[index])

if(__name__=='__main__'):
	main()

Submission status: Wrong Answer

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

def dfs(x, a, b, c):
  if x == N:
    if min(a, b, c) >= 30:
      return abs(a-A)+abs(b-B)+abs(c-C)-30
    else:
      return float("inf")
  
  ret0 = dfs(x+1, a, b, c)
  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:
N, A, B, C = map(int,input().split())
l = [int(input()) for _ in range(N)]

def dfs(x, a, b, c):
  if x == N:
    if min(a, b, c) >= 30:
      print(a, b, c)
      return abs(a-A)+abs(b-B)+abs(c-C)-30
    else:
      return float("inf")
  
  ret0 = dfs(x+1, a, b, c)
  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:
N, A, B, C = map(int,input().split())
l = [int(input()) for _ in range(N)]

inf = float("inf")
def dfs(x, a, b, c):
  if x == N:
    if min(a, b, c) > 30:
      return abs(a-A)+abs(b-B)+abs(c-C)-30
    else:
      return inf
  
  ret0 = dfs(x+1, a, b, c)
  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:
N, A, B, C = map(int, input().split())
l = [int(input()) for i in range(N)]
INF = 10 ** 9

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

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

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

Submission status: Wrong Answer

Solution code:
import numpy as np
import itertools
def choose_least_diff(rest_nums, used, target, cur_step):
    #targetを実現するのに最小のパターンを探す
    if cur_step == len(rest_nums) or np.abs(target) <= 10:
        #探索終了
        if np.sum(used.astype(np.int)) == 0:
            #全部offのパターンを返してしまうのを避ける
            return np.sum(rest_nums),used
        else:
            final_cost = (np.sum(used.astype(int)) - 1) * 10 + np.abs(target)
            return final_cost,used
    
    #Offパターンを試す
    cur_used = used.copy()
    ret_off,used_off = choose_least_diff(rest_nums, cur_used, target,cur_step + 1)
        
    #Onパターンを試す
    cur_used[cur_step] = True
    ret_on,used_on = choose_least_diff(rest_nums, cur_used, target - rest_nums[cur_step],cur_step + 1)
    
    #cur_stepがOff,Onのそれぞれの場合で最も良い結果が得られるパターンを比較して良い方を返す
    if ret_on > ret_off:
        return ret_off, used_off
    else:
        return ret_on, used_on
    
def q3():
    N,A,B,C = list(map(int,input().split()))
    l = []
    for n in range(N):
        l.append(int(input()))
    l = np.array(l)
    
    min_ret = np.sum(l)
    used = np.zeros_like(l,dtype=np.bool)
    orders = list(itertools.permutations([A,B,C]))
    for (first,second,third) in orders:
        ret1,used1 = choose_least_diff(l,used,first,0)
        l2 = l[~used1]
        used1 = np.zeros_like(l2,dtype=np.bool)
        
        ret2, used2 =  choose_least_diff(l2,used1,second,0)
        l3 = l2[~used2]
        used2 = np.zeros_like(l3,dtype=np.bool)
        
        ret3, _ =  choose_least_diff(l3,used2,third,0)
        min_ret = min(min_ret,ret1 + ret2 + ret3)

    print(min_ret)
    
q3()

Submission status: Wrong Answer

Solution code:
import numpy as np
def choose_least_diff(rest_nums, used, target, cur_step):
    #targetを実現するのに最小のパターンを探す
    if cur_step == len(rest_nums) or np.abs(target) < 10:
        #探索終了
        if np.sum(used.astype(np.int)) == 0:
            #全部offのパターンを返してしまうのを避ける
            return np.sum(rest_nums),used
        else:
            final_cost = (np.sum(used.astype(int)) - 1) * 10 + np.abs(target)
            #print(target,final_cost)
            return final_cost,used
    
    #Offパターンを試す
    ret_off,used_off = choose_least_diff(rest_nums, used, target,cur_step + 1)
        
    #Onパターンを試す
    cur_used = used.copy()
    cur_used[cur_step] = True
    ret_on,used_on = choose_least_diff(rest_nums, cur_used, target - rest_nums[cur_step],cur_step + 1)
    
    #cur_stepがOff,Onのそれぞれの場合で最も良い結果が得られるパターンを比較して良い方を返す
    if ret_on > ret_off:
        return ret_off, used_off
    else:
        return ret_on, used_on
    
def q3():
    N,A,B,C = list(map(int,input().split()))
    l = []
    for n in range(N):
        l.append(int(input()))
    l = np.array(l)
    
    min_ret = np.sum(l)
    used = np.zeros_like(l,dtype=np.bool)
    rest = [A,B,C]
    for i,first in enumerate(rest):
        ret1,used1 = choose_least_diff(l,used,first,0)
        l2 = l[~used1]
        used1 = np.zeros_like(l2,dtype=np.bool)
        for j,second in enumerate(rest):
            if i == j:
                continue
            ret2, used2 =  choose_least_diff(l2,used1,second,0)
            l3 = l2[~used2]
            used2 = np.zeros_like(l3,dtype=np.bool)
            for k, third in enumerate(rest):
                if k == i or k == j:
                    continue
                ret3, _ =  choose_least_diff(l3,used2,third,0)
                ret = ret1 + ret2 + ret3
                if ret < min_ret:
                    min_ret = ret
    print(min_ret)
    
q3()

Submission status: Wrong Answer

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

# calc_sub_score
def search_min( target, LL ):
    # print( "search_min :", target, "LL", LL )
    if len( LL ) < 1 :
        return( -1 )
    t_min = LL[0]
    sub_score = abs( target - t_min )
    for tp in LL :
        sub_len = abs( target - tp )
        if sub_score > sub_len :
            sub_score = sub_len
            t_min = tp
    return( t_min )

# calc_sub_score
def sub_score( mp, target, sLL ):
    min_t = search_min( target, sLL )
    if min_t < 0 :
        return( target )
    sLL.remove( min_t )
    # print( "sub_score = ", target, ":", sLL , "mp = ", mp, "min_t=", min_t)
    nokori = abs( target - min_t )
    if nokori >= 10 :
        if len(sLL) <1 :
            return( mp + 10 + nokori )
        mp = sub_score( 0, nokori, sLL )+10
    elif nokori > 0 and nokori  < 10 :
        mp = mp + nokori 
    return( mp )

# calc_score
def score( mp, ANS, LL ):
    sub = copy.deepcopy( ANS )
    for target in ANS :
        sub.remove( target )
        n_mp = sub_score( 0, target, LL )
        # print( "score = ", target, ":",  sub, "mp=", mp, "n_mp=", n_mp )
        mp += n_mp
    return( mp )

# データ数の取込み
NN, AA, BB, CC =list(map(int, input().split()))
mp = 0
ANS = [[AA, BB, CC],[AA, CC, BB],[BB, AA, CC],[BB, CC, AA],[CC, AA, BB],[CC, BB, AA]]
LL = []
for ii in range( NN ) :
    LL.append( int( input() ) )
    # print( ii, LL )
# kumi = ANS[0]
# mp = score( mp, kumi, LL )

res = -1
for kumi in ANS :
    pipe = copy.deepcopy( LL )
    mp = score( 0, kumi, pipe )
    # print( "##### kumi=", kumi,"mp=", mp, "res=", res)
    if res == -1 or res > mp :
        res = mp

print( res )

Submission status: Wrong Answer

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

# calc_sub_score
def search_min( target, LL ):
    # print( "search_min :", target, "LL", LL )
    if len( LL ) < 1 :
        return( -1 )
    t_min = LL[0]
    sub_score = abs( target - t_min )
    for tp in LL :
        sub_len = abs( target - tp )
        if sub_score > sub_len :
            sub_score = sub_len
            t_min = tp
    return( t_min )

# calc_sub_score
def sub_score( mp, target, sLL ):
    min_t = search_min( target, sLL )
    if min_t < 0 :
        return( target )
    sLL.remove( min_t )
    # print( "sub_score = ", target, ":", sLL , "mp = ", mp, "min_t=", min_t)
    nokori = abs( target - min_t )
    if nokori >= 10 :
        if target < min_t :
            return( target )
        if len(sLL) <1 :
            return( mp + min_t )
        mp = sub_score( 10, nokori, sLL )
    elif nokori > 0 and nokori  < 10 :
        mp = mp + nokori
    return( mp )

# calc_score
def score( mp, ANS, LL ):
    sub = copy.deepcopy( ANS )
    for target in ANS :
        sub.remove( target )
        n_mp = sub_score( 0, target, LL )
        # print( "score = ", target, ":",  sub, "mp=", mp, "n_mp=", n_mp )
        mp += n_mp
    return( mp )

# データ数の取込み
NN, AA, BB, CC =list(map(int, input().split()))
mp = 0
ANS = [[AA, BB, CC],[AA, CC, BB],[BB, AA, CC],[BB, CC, AA],[CC, AA, BB],[CC, BB, AA]]
LL = []
for ii in range( NN ) :
    LL.append( int( input() ) )
    # print( ii, LL )
# kumi = ANS[0]
# mp = score( mp, kumi, LL )

res = -1
for kumi in ANS :
    pipe = copy.deepcopy( LL )
    mp = score( 0, kumi, pipe )
    # print( "##### kumi=", kumi,"mp=", mp, "res=", res)
    if res == -1 or res > mp :
        res = mp

print( res )

Submission status: Wrong Answer

Solution code:
#coding:utf-8
import sys

BAMB_NUM = 0
BAMB_A = 0
BAMB_B = 0
BAMB_C = 0
BAMB_LIST = []

def calcMP(idx, lenA, lenB, lenC):
    """
    calculate MP.
    """
    # when classified all bamboos
    if (idx == BAMB_NUM):
        # exists a zero length bamboo
        if (lenA <= 0 or lenB <= 0 or lenC <= 0):
            return sys.maxsize
        else:
            return abs(lenA - BAMB_A) + abs(lenB - BAMB_B) + abs(lenC - BAMB_C)
    
    # don't use idx-th bamboo
    mp = calcMP(idx + 1, lenA, lenB, lenC)

    # use idx-th bamboo
    mp = min(mp, calcMP(idx + 1, lenA + BAMB_LIST[idx], lenB, lenC) + (10 if BAMB_A > 0 else 0))
    mp = min(mp, calcMP(idx + 1, lenA, lenB + BAMB_LIST[idx], lenC) + (10 if BAMB_B > 0 else 0))
    mp = min(mp, calcMP(idx + 1, lenA, lenB, lenC + BAMB_LIST[idx]) + (10 if BAMB_C > 0 else 0))

    return mp

if __name__ == "__main__":
    """
    http://drken1215.hatenablog.com/entry/2019/02/24/224100
    """
    # requested bamboo lengths
    BAMB_NUM, BAMB_A, BAMB_B, BAMB_C = map(lambda x: int(x), input().split())
    # available bamboo lengths
    for bamb in range(BAMB_NUM):
        BAMB_LIST.append(int(input()))

    mp = calcMP(0, 0, 0, 0)
    print(mp)

Submission status: Wrong Answer

Solution code:
from itertools import combinations

def f(in_A,in_B,in_C,A,B,C,ex_l,counter):
    if (counter == len(ex_l)):
        return abs(in_A-A)+abs(in_B-B)+abs(in_C-C)
    else:
        tmp = ex_l[counter]
        counter +=1
        a1 = f(in_A+tmp,in_B,in_C,A,B,C,ex_l,counter)+10
        a2 = f(in_A,in_B+tmp,in_C,A,B,C,ex_l,counter)+10
        a3 = f(in_A,in_B,in_C+tmp,A,B,C,ex_l,counter)+10
        a4 = f(in_A,in_B,in_C,A,B,C,ex_l,counter)
    return min(a1,a2,a3,a4)

N,A,B,C=map(int,input().split())
l = []
for i in range(N):
    l.append(int(input()))
in_l = list(map(list,combinations(l,3)))
ex_l = [l[:] for _ in range(len(in_l))]
for i in range(len(in_l)):
    ex_l[i].remove(in_l[i][0])
    ex_l[i].remove(in_l[i][1])
    ex_l[i].remove(in_l[i][2])

mp = 1000000
for i in range(len(in_l)):
    tmp = f(in_l[i][0],in_l[i][1],in_l[i][2],A,B,C,ex_l[i],0)
    if(mp>tmp):
        mp = tmp
print(mp)

Submission status: Wrong Answer

Solution code:
from itertools import combinations
N,A,B,C=map(int,input().split())
l = []
for i in range(N):
    l.append(int(input()))
input_l = list(map(list,combinations(l,3)))
except_l = [l[:] for _ in range(len(input_l))]
for i in range(len(input_l)):
    except_l[i].remove(input_l[i][0])
    except_l[i].remove(input_l[i][1])
    except_l[i].remove(input_l[i][2])
print(input_l)
print(except_l)

Submission status: Wrong Answer

Solution code:
N, A, B, C = [int(item) for item in input().split()]

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

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

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

Submission status: Wrong Answer

Solution code:

import itertools
import math
import copy

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

pattern = [list(itertools.combinations(range(x), 3)) for x in range(3, N+1)]

def stretch_shortening(sticks, A, B, C):
    min_cost = 1e10
    
    for i1, i2, i3 in pattern[len(sticks) - 3]:
        cost = abs(sticks[i1] - A) + abs(sticks[i2] - B) + abs(sticks[i3] - C)
        if min_cost > cost:
            min_cost = cost
    return min_cost
'''
def connect(sticks, A, B, C):
    
    min_cost = stretch_shortening(sticks, A, B, C) + (N - len(sticks)) * 10
    
    if len(sticks) <= 3:
        return min_cost
    
    for i1, i2 in itertools.combinations(range(len(sticks)), 2):
        if sticks[i1] + sticks[i2] > sticks[-1] and sticks[-1] > C:
            continue
        copied_sticks = copy.copy(sticks)
        copied_sticks[i1] += copied_sticks[i2]
        copied_sticks.pop(i2)
        copied_sticks = sorted(copied_sticks)
        min_cost_connect = connect(copied_sticks, A, B, C)
        if min_cost_connect < min_cost:
            min_cost = min_cost_connect
    
    return min_cost
 '''   
def connect(sticks, A, B, C, min_cost):
    
    new_min_cost = stretch_shortening(sticks, A, B, C) + (N - len(sticks)) * 10
    
    if len(sticks) <= 3:
        if min_cost > new_min_cost:
            return new_min_cost
        else:
            return min_cost
    
    if min_cost < new_min_cost:
        return min_cost
    else:
        min_cost = new_min_cost
    
    min_copied_sticks = None
    for i1, i2 in itertools.combinations(range(len(sticks)), 2):
        if sticks[i1] + sticks[i2] > sticks[-1] and sticks[-1] > C:
            continue
        copied_sticks = copy.copy(sticks)
        copied_sticks[i1] += copied_sticks[i2]
        copied_sticks.pop(i2)
        copied_sticks = sorted(copied_sticks)
        #min_cost_connect = connect(copied_sticks, A, B, C, min_cost)
        min_cost_connect = stretch_shortening(copied_sticks, A, B, C) + (N - len(copied_sticks)) * 10
        if min_cost_connect < min_cost:
            min_cost = min_cost_connect
            min_copied_sticks = copied_sticks
    
    if min_copied_sticks is not None:
        min_cost_connect = connect(min_copied_sticks, A, B, C, min_cost)
        if min_cost_connect < min_cost:
            min_cost = min_cost_connect
    
    return min_cost

sticks = []
for _ in range(N):
    sticks.append(int(input()))
sticks = sorted(sticks)

min_cost = connect(sticks, C, B, A, 1e10)
#min_cost = stretch_shortening(sticks, A, B, C)
#print(pattern)

print(min_cost)

Submission status: Wrong Answer

Solution code:

import itertools
import math
import copy

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

pattern = [list(itertools.combinations(range(x), 3)) for x in range(3, N+1)]

def stretch_shortening(sticks, A, B, C):
    min_cost = 1e10
    
    for i1, i2, i3 in pattern[len(sticks) - 3]:
        cost = abs(sticks[i1] - A) + abs(sticks[i2] - B) + abs(sticks[i3] - C)
        if min_cost > cost:
            min_cost = cost
    return min_cost
'''
def connect(sticks, A, B, C):
    
    min_cost = stretch_shortening(sticks, A, B, C) + (N - len(sticks)) * 10
    
    if len(sticks) <= 3:
        return min_cost
    
    for i1, i2 in itertools.combinations(range(len(sticks)), 2):
        if sticks[i1] + sticks[i2] > sticks[-1] and sticks[-1] > C:
            continue
        copied_sticks = copy.copy(sticks)
        copied_sticks[i1] += copied_sticks[i2]
        copied_sticks.pop(i2)
        copied_sticks = sorted(copied_sticks)
        min_cost_connect = connect(copied_sticks, A, B, C)
        if min_cost_connect < min_cost:
            min_cost = min_cost_connect
    
    return min_cost
 '''   
def connect(sticks, A, B, C, min_cost):
    
    new_min_cost = stretch_shortening(sticks, A, B, C) + (N - len(sticks)) * 10
    
    if len(sticks) <= 3:
        if min_cost > new_min_cost:
            return new_min_cost
        else:
            return min_cost
    
    if min_cost < new_min_cost:
        return min_cost
    else:
        min_cost = new_min_cost
    
    for i1, i2 in itertools.combinations(range(len(sticks)), 2):
        if sticks[i1] + sticks[i2] > sticks[-1] and sticks[-1] > C:
            continue
        copied_sticks = copy.copy(sticks)
        copied_sticks[i1] += copied_sticks[i2]
        copied_sticks.pop(i2)
        copied_sticks = sorted(copied_sticks)
        min_cost_connect = connect(copied_sticks, A, B, C, min_cost)
        if min_cost_connect < min_cost:
            min_cost = min_cost_connect
    
    return min_cost

sticks = []
for _ in range(N):
    sticks.append(int(input()))
sticks = sorted(sticks)

min_cost = connect(sticks, C, B, A, 1e10)
#min_cost = stretch_shortening(sticks, A, B, C)
#print(pattern)

print(min_cost)

Submission status: Wrong Answer

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

def dfs(n, a, b, c): # n本目を使用するかしないか
  if n == N: # 全ての竹を選び終わったら、延長魔法と短縮魔法を使ってA,B,Cの長さにする
    return abs(A-a)+abs(B-b)+abs(C-c)-30 # 最低でも1本のみ使用するが、その場合は合成しないため-30
  ret0 = dfs(n + 1, a, b, c) # 使わない
  ret1 = dfs(n + 1, a + L[n], b, c) + 10 # Aに使う
  ret2 = dfs(n + 1, a, b + L[n], c) + 10 # Bに使う
  ret3 = dfs(n + 1, a, b, c + L[n]) + 10 # Cに使う
  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 = [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)
    # 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: Wrong Answer

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:
                        print("{} {} {} {}".format(l, i, j, k))
                        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: Wrong Answer

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+l[i], b, c) + 10
    ans1 = dfs(i+1, a, b+l[i], c) + 10
    ans2 = dfs(i+1, a, b, c+l[i]) + 10
    ans3 = dfs(i+1, a, b, c)
    return min(ans0, ans1, ans2, ans3)

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)]
ans=10**9
a=b=c=[]
for i in range(1<<n):
    for j in range(1<<n):
        res=0
        a=b=c=[]
        for k in range(n):
            if i&(1<<k):
                if j&(1<<k):
                    b.append(l[k])
                else:
                    a.append(l[k])
            elif j&(1<<k):
                c.append(l[k])
        if a and b and c:
            res = 10*(len(a)-1)+abs(A-sum(a))+10*(len(b)-1)+abs(B-sum(b))+10*(len(c)-1)+abs(C-sum(c))
            ans=min(ans,res)
print(ans)

Submission status: Wrong Answer

Solution code:
import itertools
n,a,b,c=(map(int,input().split()))
l=[int(input()) for i in range(n)]
ans=10**9
for k in itertools.product(range(4),repeat=n):
    A=[[] for i in range(4)]
    for i in range(n):
        A[k[i]]+=[l[i]]
    print(A)
    if A[1] and A[2] and A[3]:
        tmp=10*(n-len(A[0])-3) #A[0]が少なければ少ないほどコストが増える(=なんども合成してる、最初の3回は合成していない)
        tmp+=abs(a-sum(A[1]))
        tmp+=abs(b-sum(A[2]))
        tmp+=abs(c-sum(A[3]))
        ans=min(tmp,ans)
print(ans)

Submission status: Wrong Answer

Solution code:
n,a,b,c = map(int,input().split())
l=[int(input()) for i in range(n)]
l.sort()
answer = 10*100
for i in range(2**(2*n)+2):
    a_u = []
    b_u = []
    c_u = []
    ans = 0
    j = i
    counte = 0
    while counte < n:
        if j%4 == 1:
            a_u.append(l[counte])
        if j%4 == 2:
            b_u.append(l[counte])
        if j%4 == 3:
            c_u.append(l[counte])
        j = j//4
        counte += 1
    if len(a_u)*len(b_u)*len(c_u) > 0:
        ans += abs(sum(a_u)-a) + 10*(len(a_u)-1)
        ans += abs(sum(b_u)-b) + 10*(len(b_u)-1)
        ans += abs(sum(c_u)-c) + 10*(len(c_u)-1)
        answer = min(ans,answer)

print(answer)

Submission status: Wrong Answer

Solution code:
n,a,b,c = map(int,input().split())
l=[int(input()) for i in range(n)]
l.sort()
answer = 10*100
for i in range(2**(2*n)+1):
    a_u = []
    b_u = []
    c_u = []
    ans = 0
    j = i
    counte = 0
    while counte < n:
        if j%4 == 1:
            a_u.append(l[counte])
        if j%4 == 2:
            b_u.append(l[counte])
        if j%4 == 3:
            c_u.append(l[counte])
        j = j//4
        counte += 1
    if len(a_u)*len(b_u)*len(c_u) > 0:
        ans += abs(sum(a_u)-a) + 10*(len(a_u)-1)
        ans += abs(sum(b_u)-b) + 10*(len(b_u)-1)
        ans += abs(sum(c_u)-c) + 10*(len(c_u)-1)
        answer = min(ans,answer)

print(answer)

Submission status: Wrong Answer

Solution code:
n,a,b,c = map(int,input().split())
l=[int(input()) for i in range(n)]
l.sort()
answer = 10*100
for i in range(2**(2*n)):
    a_u = []
    b_u = []
    c_u = []
    ans = 0
    j = i
    counte = 0
    while counte <= n:
        if j%4 == 1:
            a_u.append(l[counte])
        if j%4 == 2:
            b_u.append(l[counte])
        if j%4 == 3:
            c_u.append(l[counte])
        j = j//4
        counte += 1
    if len(a_u)*len(b_u)*len(c_u) > 0:
        ans += abs(sum(a_u)-a) + 10*(len(a_u)-1)
        ans += abs(sum(b_u)-b) + 10*(len(b_u)-1)
        ans += abs(sum(c_u)-c) + 10*(len(c_u)-1)
        answer = min(ans,answer)

print(answer)

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 for i in range(n)]
    digit = 0
    while(i>1):
        digits[digit] = i%4
        digit += 1
        i//=4
    return digits

ans = 10**20
for i in range(4**n):
    choice = to_4digit(i)
    abc = [0 for i in range(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**20
for i in range(4**n):
    choice = to_4digit(i)
    abc = [0 for i in range(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)
    abc = [0 for i in range(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)