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=[int(i) for i in input().split()]
l=[int(input()) for _ in range(N)]
INF=10**18

def dfs(cur, a, b, c):
    if cur==N:
        return abs(a-A)+abs(b-B)+abs(c-C)-30
    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:
n,A,B,C = map(int,input().split())

list=[A, B, C]
list_s=[A, B, C]
l = []
mp = 0

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

for i in list:
	if l.count(i) > 0:
		l.remove(i)
		list_s.remove(i)
		continue
	elif l[0] > i:
		mp = mp + (l[0]-i)
		l.remove(l[0])
		list_s.remove(i)
		continue
	for j in l:
		if i - j <= 10:
			mp = mp + (i - j)
			l.remove(j)
			list_s.remove(i)
			break

for i in list_s:
	if len(l)-len(list_s)>=1 and l[0]+l[1]>i and l[0]+l[1]-i <= i-l[0]+10:
		mp = mp + (l[0]+l[1]-i) + 10
		l_0, l_1 = l[0], l[1]
		l.remove(l_0)
		l.remove(l_1)
	elif len(l)-len(list_s)>=2 and l[0]+l[1]+l[2]>i and l[0]+l[1]+l[2]-i < i-l[0]+20:
		mp = mp + (l[0]+l[1]+l[2]-i) + 20
		l_0, l_1, l_2 = l[0], l[1], l[2]
		l.remove(l_0)
		l.remove(l_1)
		l.remove(l_2)
	elif len(l)-len(list_s)>=3 and l[0]+l[1]+l[2]+l[3]>i and l[0]+l[1]+l[2]+l[3]-i < i-l[0]+30:
		mp = mp + (l[0]+l[1]+l[2]+l[3]-i) + 30
		l_0, l_1, l_2, l_3 = l[0], l[1], l[2], l[3]
		l.remove(l_0)
		l.remove(l_1)
		l.remove(l_2)
		l.remove(l_3)
	elif len(l)-len(list_s)>=4 and l[0]+l[1]+l[2]+l[3]+l[4]>i and l[0]+l[1]+l[2]+l[3]+l[4]-i < i-l[0]+40:
		mp = mp + (l[0]+l[1]+l[2]+l[3]+l[4]-i) + 40
		l_0, l_1, l_2, l_3, l_4 = l[0], l[1], l[2], l[3], l[4]
		l.remove(l_0)
		l.remove(l_1)
		l.remove(l_2)
		l.remove(l_3)
		l.remove(l_4)
	elif len(l)-len(list_s)>=5 and l[0]+l[1]+l[2]+l[3]+l[4]+l[5]>i and l[0]+l[1]+l[2]+l[3]+l[4]+l[5]-i < i-l[0]+50:
		mp = mp + (l[0]+l[1]+l[2]-l[3]+l[4]+l[5]-i) + 50
		l_0, l_1, l_2, l_3, l_4, l_5 = l[0], l[1], l[2], l[3], l[4], l[5]
		l.remove(l_0)
		l.remove(l_1)
		l.remove(l_2)
		l.remove(l_3)
		l.remove(l_4)
		l.remove(l_5)
	else:
		mp = mp + (i-l[0])
		l.remove(l[0])

print(mp)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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


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

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

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


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

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

Submission status: 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(1,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:
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(1,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:
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:
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:
from statistics import mean, median,variance,stdev
import numpy as np
import sys
import math
import fractions
import itertools

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


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

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

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


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

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


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

Submission status: Wrong Answer

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

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

for i in range(4**n):
    tmp=ns(i,4)
    tmp="0"*(n-len(tmp))+tmp
    h=[0]*3
    p=0
    for j in range(n):
        if tmp[j]=="1":
            if h[0]!=0: p+=10
            h[0]+=l[j]

        elif tmp[j]=="2":
            if h[1]!=0: p+=10
            h[1]+=l[j]

        elif tmp[j]=="3":
            if h[2]!=0: p+=10
            h[2]+=l[j]

    if 0 in h:continue
    print(tmp,)
    for k in range(3):
        p+=abs(h[k]-t[k])
    if ans==-1: ans=p
    else: ans=min(ans,p)
print(ans)

Submission status: Wrong Answer

Solution code:
N, A, B, C = map(int, input().split())
l = [int(input()) for _ in range(N)]
def bintoset(b):
    ret = set()
    for i in range(N):
        if b & 1 == 1:
            ret.add(l[i])
        b = b >> 1
    return ret
def cost(b, x):
    s = bintoset(b)
    return abs(x - sum(s)) + (len(s) - 1) * 10
mincost = 10 ** 10
count = 0
for a in range(1, 1 << N):
    for b in range(1, (1 << N) - a):
        if b & a == 0:
            for c in range(1, (1 << N) - a - b):
                if c & (a+b) == 0 and a * b * c != 0:
                    mincost = min(mincost, cost(a, A) + cost(b, B) + cost(c, C))
                    count += 1
print(mincost)

Submission status: Wrong Answer

Solution code:
N, A, B, C = map(int, input().split())
l = [int(input()) for _ in range(N)]
def bintoset(b):
    ret = set()
    for i in range(N):
        if b & 1 == 1:
            ret.add(l[i])
        b = b >> 1
    return ret
def cost(b, x):
    s = bintoset(b)
    return abs(x - sum(s)) + (len(s) - 1) * 10
mincost = 10 ** 10
count = 0
for a in range(1, 1 << N):
    for b in range(1, (1 << N) - a):
        if b & a == 0:
            for c in range(1, (1 << N) - a - b):
                if c & (a+b) == 0 and a * b * c != 0:
                    mincost = min(mincost, cost(a, A) + cost(b, B) + cost(c, C))
                    count += 1
print(mincost)
print(count)

Submission status: Wrong Answer

Solution code:
import itertools
import numpy as np
import copy

N, A, B, C = map(int, input().split(' '))
target = [A, B, C]
length = []
for i in range(N):
    length.append(int(input()))
    
mana = 0
for i in target:
    if(i in length):
        target.remove(i)
        length.remove(i)
        
for i in target:
    if(i < min(length)):
        mana += min(length) - i
        target.remove(i)
        length.remove(min(length))

target_comb = list(itertools.permutations(target, len(target)))
mana_cost = []
length_copy = copy.deepcopy(length)
for target in target_comb:
    length = copy.deepcopy(length_copy)
    for target_num in target:
        """
        length = sorted(length)        
        for num in range(1, len(length)+1):
            if(sum(length[:num]) > target_num):
                break
        """
        
        combs = []
        for i in range(1, len(length)+1):
            combs = combs + list(itertools.combinations(length, i))
        
        #combs = [flatten for inner in combs for flatten in inner]
        cost = np.zeros(len(combs))
        for col, comb_num in enumerate(combs):
            cost[col] = abs(target_num - sum(comb_num)) + (len(comb_num)-1)*10
        
        arg_min = cost.argmin()
        mana += cost[arg_min]
        for i in combs[arg_min]:
            length.remove(i)
        #print(length)
        #print(combs[arg_min])
    mana_cost.append(int(mana))
print(min(mana_cost))

Submission status: Wrong Answer

Solution code:
import itertools
import numpy as np
import copy

N, A, B, C = map(int, input().split(' '))
target = [A, B, C]
length = []
for i in range(N):
    length.append(int(input()))
    
mana = 0
for i in target:
    if(i in length):
        target.remove(i)
        length.remove(i)
        
for i in target:
    if(i < min(length)):
        mana += min(length) - i
        target.remove(i)
        length.remove(min(length))

target_comb = list(itertools.permutations(target, len(target)))
mana_cost = []
length_copy = copy.deepcopy(length)
for target in target_comb:
    length = copy.deepcopy(length_copy)
    print('-'*20, flush=True)
    for target_num in target:
        """
        length = sorted(length)        
        for num in range(1, len(length)+1):
            if(sum(length[:num]) > target_num):
                break
        """
        
        combs = []
        for i in range(1, len(length)+1):
            combs = combs + list(itertools.combinations(length, i))
        
        #combs = [flatten for inner in combs for flatten in inner]
        cost = np.zeros(len(combs))
        for col, comb_num in enumerate(combs):
            cost[col] = abs(target_num - sum(comb_num)) + (len(comb_num)-1)*10
        
        arg_min = cost.argmin()
        mana += cost[arg_min]
        for i in combs[arg_min]:
            length.remove(i)
        #print(length)
        #print(combs[arg_min])
    mana_cost.append(int(mana))
print(min(mana_cost))

Submission status: Wrong Answer

Solution code:
import itertools
import numpy as np
import copy

N, A, B, C = map(int, input().split(' '))
target = [A, B, C]
length = []
for i in range(N):
    length.append(int(input()))
    
mana = 0
for i in target:
    if(i in length):
        target.remove(i)
        length.remove(i)
        
for i in target:
    if(i < min(length)):
        mana += min(length) - i
        target.remove(i)
        length.remove(min(length))

target_comb = list(itertools.permutations(target, len(target)))
mana_cost = []
length_copy = copy.deepcopy(length)
for target in target_comb:
    length = copy.deepcopy(length_copy)
    print('-'*20, flush=True)
    for target_num in target:
        """
        length = sorted(length)        
        for num in range(1, len(length)+1):
            if(sum(length[:num]) > target_num):
                break
        """
        
        combs = []
        for i in range(1, len(length)+1):
            combs = combs + list(itertools.combinations(length, i))
        
        #combs = [flatten for inner in combs for flatten in inner]
        cost = np.zeros(len(combs))
        for col, comb_num in enumerate(combs):
            cost[col] = abs(target_num - sum(comb_num)) + (len(comb_num)-1)*10
        
        arg_min = cost.argmin()
        mana += cost[arg_min]
        for i in combs[arg_min]:
            length.remove(i)
        #print(length)
        #print(combs[arg_min])
    mana_cost.append(mana)
print(min(mana_cost))

Submission status: Wrong Answer

Solution code:
n, a, b, c = map(int, input().split())
L = [int(input()) for _ in range(n)]
mp = 0
def use(n, d, r):
  bit = [[0]*n for i in range(2**n)]
  for i in range(2**n):
    for j in range(n):
      if i&(2**j) > 0:
        bit[i][j] = 1
  cl = []
  for i in range(2**n):
    s = 0
    for j in range(n):
      s += L[j] * bit[i][j]
    cl.append(s)
  cmin = 999999
  for i in range(1, 2**n):
    if abs(cl[i] - d) < cmin and sum(bit[i]) + r <= n:
      cmin = abs(cl[i] - d)
      cindex = i
  ng = []
  use = []
  for j in range(n):
    if bit[cindex][j]:
      use.append(L[j])
      if L[j] < 10:
        ng.append(L[j])
  for x in use:
    L.remove(x)
  if sum(bit[cindex]) == 1:
     return cmin
  elif use == ng:
    return abs(d - max(ng))
  else:
    cmin = abs(cl[cindex] - d - sum(ng))
    return sum(bit[cindex]) * 10 - 10 * (len(ng) + 1) + cmin
mp += use(n, c, 2)
n = len(L)
mp += use(n, b, 1)
n = len(L)
mp += use(n, a, 0)
print(mp)

Submission status: Wrong Answer

Solution code:
import itertools
 
def rotate(l, n):
    return l[-n:] + l[:-n]
 
def main():
    N, A, B, C = list(map(int, input().split()))
    bars = [A,B,C]
    l = []
    for i in range(N):
        l.append(int(input()))
 
    all_min = 10000000000
 
    for z in range(len(bars)):
        ret = 0
        bars = rotate(bars, 1)
        for k in range(len(bars)):
            full = []
            for i in range(1, N):
                full += list(itertools.combinations(l, i))
            # print(full)
 
            mini = 1000000000
            arg_min = []
            for i in full:
                if abs(sum(i)-bars[k]) + max(len(i)-1, 0)*10 < mini:
                    mini = abs(sum(i)-bars[k]) + max(len(i)-1, 0)*10
                    arg_min = i
            
            # print(mini, arg_min)
            ret += mini
            for i in arg_min:
                del l[l.index(i)]
 
        all_min = min(all_min, ret)
    print(all_min) 
 
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import copy 

n,a,b,c=map(int,input().split())
l=[int(input()) for i in range(n)]
table=[]
for i in range(4**n):
    lis=[]
    x=copy.deepcopy(i)
    for j in range(n):
        lis.append(x%4)
        x=x//4
    table.append(lis)
ans=float('inf')
for i in table:
    x,y,z=0,0,0
    ans_sub=0
    cnt=-3
    for j in range(n):
        if i[j]==1:
            x+=l[j]
            cnt+=1
        elif i[j]==2:
            y+=l[j]
            cnt+=1
        elif i[j]==3:
            z+=l[j]
            cnt+=1
    ans_sub+=abs(a-x)+abs(b-y)+abs(c-z)+max(cnt,0)*10
    ans=min(ans,ans_sub)
print(ans)

Submission status: Wrong Answer

Solution code:

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

infty = 1000000

def DFS(i,uselist0,uselista,uselistb,uselistc):
    if i == N:
        return calc_mp(uselista,uselistb,uselistc)
    mplist = []
    u0 = uselist0 + [l[i]]
    ua = uselista + [l[i]]
    ub = uselistb + [l[i]]
    uc = uselistc + [l[i]]
    mplist.append(DFS(i+1, u0,uselista,uselistb,uselistc))
    mplist.append(DFS(i+1, uselist0,ua,uselistb,uselistc))
    mplist.append(DFS(i+1, uselist0,uselista,ub,uselistc))
    mplist.append(DFS(i+1, uselist0,uselista,uselistb,uc))
    return min(mplist)


def calc_mp(uselista,uselistb,uselistc):
    mp = 0
    #合成魔法
    mp += (max(len(uselista) - 1, 0)) * 10
    mp += (max(len(uselistb) - 1, 0)) * 10
    mp += (max(len(uselistc) - 1, 0)) * 10

    #その他魔法
    mp += abs(A - sum(uselista))
    mp += abs(B - sum(uselistb))
    mp += abs(C - sum(uselistc))

    if len(uselista)==0:
        mp += infty
    if len(uselistb)==0:
        mp += infty
    if len(uselistc)==0:
        mp += infty
    return mp

print(DFS(0,[],[],[],[]))

Submission status: Wrong Answer

Solution code:
import itertools

def rotate(l, n):
    return l[-n:] + l[:-n]

def main():
    N, A, B, C = list(map(int, input().split()))
    bars = [A,B,C]
    l = []
    for i in range(N):
        l.append(int(input()))

    all_min = 10000000000

    for z in range(len(bars)):
        bars = rotate(bars, 1)
        ret = 0
        for k in range(len(bars)):
            full = []
            for i in range(1, N):
                full += list(itertools.combinations(l, i))
            # print(full)

            mini = 1000000000
            arg_min = []
            for i in full:
                if abs(sum(i)-bars[k]) + 10*max(len(i)-1, 0) < mini:
                    mini = abs(sum(i)-bars[k]) + 10*max(len(i)-1, 0)
                    arg_min = i
            
            # print(mini, arg_min)
            ret += mini
            for i in arg_min:
                del l[l.index(i)]

        all_min = min(all_min, ret)
    print(all_min) 

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from itertools import combinations
N, A, B, C = [int(i) for i in input().split()]
l_ls = list()
for i in range(N):
    l_ls.append(int(input()))
dis_ls = [(i,[i]) for i in l_ls]
for i in range(2,N-1):
    dis_ls += [(sum(i),i) for i in combinations(l_ls, i)]
best = 1e9
for hoge in [[A, B, C], [A, C, B],[B, A, C],[B, C, A], [C,B,A],[C,A,B]]:
    dis_ls_copy = dis_ls.copy()
    _sum = 0
    for target in hoge:
        min = 1e9
        for dis in dis_ls_copy:
            sa = abs(dis[0] - target) + 10 * (len(dis[1]) - 1)
            if sa < min:
                min = sa
                min_combi = dis[1]
        _sum += min
        dis_ls_copy = [i for i in dis_ls_copy if len(set(min_combi).intersection(set(i[1]))) == 0]
    if _sum < best:
        best = _sum
print(best)

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(1,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
    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:
from itertools import combinations
N, A, B, C = [int(i) for i in input().split()]
l_ls = list()
for i in range(N):
    l_ls.append(int(input()))
dis_ls = [(i,[i]) for i in l_ls]
for i in range(2,7):
    dis_ls += [(sum(i),i) for i in combinations(l_ls, i)]
best = 1e9
for hoge in [[A, B, C], [A, C, B],[B, A, C],[B, C, A], [C,B,A],[C,A,B]]:
    dis_ls_copy = dis_ls.copy()
    _sum = 0
    for target in hoge:
        min = 1e9
        for dis in dis_ls_copy:
            sa = abs(dis[0] - target) + 10 * (len(dis[1]) - 1)
            if sa < min:
                min = sa
                min_combi = dis[1]
        _sum += min
        dis_ls_copy = [i for i in dis_ls_copy if len(set(min_combi).intersection(set(i[1]))) == 0]
    if _sum < best:
        best = _sum
print(best)

Submission status: Wrong Answer

Solution code:
n, a, b, c = map(int, input().split())
l = [int(input()) for _ in range(n)]
cnt = 0
for k in [a,b,c]:
    ans = 100000
    for i in range(1, 2**n+1):
        s = 0
        for j in range(n):
            if i & 2**j:
                if s > 0:
                    s += 10
                s += l[j]
        v = abs(k - s)
        if v < ans:
            ans = v
            mast = i
    cnt += ans
    for j in range(n):
        if mast & 2**j:
            l[j] = 0
print(cnt)