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:
l=[]
n,a,b,c=map(int,input().split())
for i in range(n):
    li=int(input())
    l.append(li)

d=[a,b,c]
d.sort()

mpmin=sum(l)*4+len(l)*10
for i in range(4**n):
    k=[]
    kj=i%4
    k.append(kj)
    b=int((i-kj)/4)
    l0=[]
    l1=[]
    l2=[]
    l3=[]
    if kj==0:
        l0.append(l[0])
    elif kj==1:
        l1.append(l[0])
    elif kj==2:
        l2.append(l[0])
    elif kj==3:
        l3.append(l[0])
        
    for j in range(n-1):
        kj=b%4
        k.append(kj)
        b=int((b-kj)/4)
        if kj==0:
            l0.append(l[j+1])
        elif kj==1:
            l1.append(l[j+1])
        elif kj==2:
            l2.append(l[j+1])
        elif kj==3:
            l3.append(l[j+1])
    mp=0
    if len(l0)>=1:
        mp=mp+(len(l0)-1)*10
    if len(l1)>=1:
        mp=mp+(len(l1)-1)*10
    if len(l2)>=1:
        mp=mp+(len(l2)-1)*10
    ls=[]
    ls=[sum(l0),sum(l1),sum(l2)]
    ls.sort()
    mp=mp+abs(d[0]-ls[0])+abs(d[1]-ls[1])+abs(d[2]-ls[2])    
    if mpmin>mp and len(l0)>=1 and len(l1)>=1 and len(l2)>=1:
        mpmin=mp
print(mpmin)

Submission status: Wrong Answer

Solution code:
import itertools
n,a,b,c=map(int,input().split())
l=[int(input()) for i in range(n)]
l.sort()
ll=[i for i in range(1,n+1)]
x=[]
for i in range(1,n+1):
    z=list(itertools.combinations(l,i))
    zz=list(itertools.combinations(ll,i))
    for j in range(len(z)):
        x.append([sum(z[j]),(i-1)*10,list(zz[j])])
x.sort()
A=[];B=[];C=[]
for i in range(len(x)):
    A.append([x[i][1]+abs(a-x[i][0]),x[i][2]])
    B.append([x[i][1]+abs(b-x[i][0]),x[i][2]])
    C.append([x[i][1]+abs(c-x[i][0]),x[i][2]])
A.sort();B.sort();C.sort()
m=len(A)
ans=10**10
for i in range(m):
    for j in range(m):
        for k in range(m):
            if len(A[i][1]+B[j][1]+C[k][1])>=n+1:
                break
            else:
                ans=min(ans,A[i][0]+B[j][0]+C[k][0])
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)]
l.sort()
ll=[i for i in range(1,n+1)]
x=[]
for i in range(1,n+1):
    z=list(itertools.combinations(l,i))
    zz=list(itertools.combinations(ll,i))
    for j in range(len(z)):
        x.append([sum(z[j]),(i-1)*10,list(zz[j])])
x.sort()
#print(x)
A=[];B=[];C=[]
for i in range(len(x)):
    A.append([x[i][1]+abs(a-x[i][0]),x[i][2]])
    B.append([x[i][1]+abs(b-x[i][0]),x[i][2]])
    C.append([x[i][1]+abs(c-x[i][0]),x[i][2]])
A.sort();B.sort();C.sort()
m=len(A)
ans=float('inf')
for i in range(m):
    for j in range(m):
        for k in range(m):
            if len(A[i][1]+B[j][1]+C[k][1])>=8:
                break
            elif len(A[i][1]+B[j][1]+C[k][1])==len((set(A[i][1]+B[j][1]+C[k][1]))):
                ans=min(ans,A[i][0]+B[j][0]+C[k][0])
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)]
l.sort()
ll=[i for i in range(1,n+1)]
x=[]
for i in range(1,n+1):
    z=list(itertools.combinations(l,i))
    zz=list(itertools.combinations(ll,i))
    for j in range(len(z)):
        x.append([sum(z[j]),(i-1)*10,list(zz[j])])
x.sort()
#print(x)
A=[];B=[];C=[]
for i in range(len(x)):
    A.append([x[i][1]+abs(a-x[i][0]),x[i][2]])
    B.append([x[i][1]+abs(b-x[i][0]),x[i][2]])
    C.append([x[i][1]+abs(c-x[i][0]),x[i][2]])
A.sort();B.sort();C.sort()
#print(A)
#print(B)
#print(C)
m=len(A)
ans=float('inf')
for i in range(m):
    for j in range(m):
        for k in range(m):
            if len(A[i][1]+B[j][1]+C[k][1])>=6:
                break
            elif len(A[i][1]+B[j][1]+C[k][1])==len((set(A[i][1]+B[j][1]+C[k][1]))):
                ans=min(ans,A[i][0]+B[j][0]+C[k][0])
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)]
ll=[i for i in range(1,n+1)]
x=[]
for i in range(1,n+1):
    z=list(itertools.combinations(l,i))
    zz=list(itertools.combinations(ll,i))
    for j in range(len(z)):
        x.append([sum(z[j]),(i-1)*10,list(zz[j])])
x.sort()
#print(x)
A=[];B=[];C=[]
for i in range(len(x)):
    A.append([x[i][1]+abs(a-x[i][0]),x[i][2]])
    B.append([x[i][1]+abs(b-x[i][0]),x[i][2]])
    C.append([x[i][1]+abs(c-x[i][0]),x[i][2]])
A.sort();B.sort();C.sort()
m=len(A)
ans=float('inf')
for i in range(m):
    for j in range(m):
        for k in range(m):
            if len(A[i][1]+B[j][1]+C[k][1])!=len((set(A[i][1]+B[j][1]+C[k][1]))):
                break
            else:
                ans=min(ans,A[i][0]+B[j][0]+C[k][0])
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
import os
import itertools
import _collections
import string

class cin():
    def int():
        return int(sys.stdin.readline().rstrip())
    def string():
        return sys.stdin.readline().rstrip()
    def mapInt():
        return [int(x) for x in cin.string().split()]
    def stringList(n):
        return [input() for i in range(n)]
    def intListList(n):
        return [cin.mapInt() for i in range(n)]
    def intColsList(n):
        return [int(input()) for i in range(n)]

class Util():
    def roundUp(a,b):
        return -(-a // b)
    def toUpperMultiple(a,x):
        return Util.round_up(a,x) * x
    def toLowerMultiple(a,x):
        return (a // x) * x
class Math():
    def gcd(a,b):
        if b == 0:
            return a
        return Math.gcd(b,a % b)

def main():
    print(5 & 3)
    N,A,B,C = cin.mapInt()
    L = cin.intColsList(N)

    ans = int(1e09)
    for i in range(1 << N * 2):
        D = [0] * 3
        mp = 0
        for j in range(N):
            n = (i >> (j * 2)) & 3
            if n == 0:
                continue
            if D[n - 1] > 0:
                mp += 10
            D[n - 1] += L[j]
        if 0 in D:
            continue
        mp += abs(A - D[0])
        mp += abs(B - D[1])
        mp += abs(C - D[2])
        ans = min(ans,mp)
    print(ans)
    return

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

orgPetternList =[]
def petternMake(cnt,s):
  if cnt == n+1:
    orgPetternList.append(s)
    return
  for i in range(4):
    petternMake(cnt+1,s+str(i))
  

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

#全部一度全パターンで足しきる場合の回す元リスト作成
petternMake(1,'')

min = 999999999
#全パターンループして、最低MP算出
for ptn in orgPetternList:
  ptnList = list(ptn)
  sumMp = 0
  nowa =0
  nowb =0
  nowc =0
  for cnt in range(len(ptnList)):	
    if ptnList[cnt] == '1':
      nowa += banboo[cnt]
      sumMp +=10
    elif ptnList[cnt]=='2':
      nowb += banboo[cnt]
      sumMp +=10
    elif ptnList[cnt]=='3':
      nowc += banboo[cnt]
      sumMp +=10
  sumMp = sumMp + abs(a-nowa) + abs(b-nowb) +abs(c-nowc)

  if sumMp < min:    
    min = sumMp

print(min)

Submission status: Wrong Answer

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

banboo = [input() for i in range(n)]
print(banboo)
ranegeList = [0,0,0,0]
print(ranegeList)

Submission status: Wrong Answer

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


def dfs(i, a, b, c):
    if i == n:
        if a < 1 or b < 1 or c < 1:
            return 10 ** 18
        return abs(A - a) + abs(B - b) + abs(C - c)
    res = dfs(i + 1, a, b, c)
    res = min([res, dfs(i + 1, a + l[i], b, c)])
    res = min([res, dfs(i + 1, a, b + l[i], c)])
    res = min([res, dfs(i + 1, a, b, c + l[i])])
    return res


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(i, a, b, c):
    if i == n:
        if a < 1 or b < 1 or c < 1:
            return 10 ** 18
        return abs(A - a) + abs(B - b) + abs(C - c)
    res = dfs(i + 1, a, b, c)
    res = max([res, dfs(i + 1, a + l[i], b, c)])
    res = max([res, dfs(i + 1, a, b + l[i], c)])
    res = max([res, dfs(i + 1, a, b, c + l[i])])
    return res


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

Submission status: Wrong Answer

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

def dfs(cur, a, b, c):
    if cur == n: #curは０スタートだから、cur=nのときにはｎ本の竹の使い道が決まっている。
        print(str(cur)+" "+str(a)+" "+str(b)+" "+str(c))
        return abs(a - A) + abs(b - B) + abs(c - C) - 30 if min(a, b, c) > 0 else INF 
        #-30する理由は、２本の竹を使うときに20mpで合体してる。本当は、10mpでいいのに。
        #min(a, b, c) > 0 この条件は、A,B,Cどれにも最低一本は木を使わないといけないから
    ret0 = dfs(cur + 1, a, b, c)
    ret1 = dfs(cur + 1, a + bam[cur], b, c) + 10 #Aにcur番目の竹を使う　使うときに10mp使っておく。
    ret2 = dfs(cur + 1, a, b + bam[cur], c) + 10
    ret3 = dfs(cur + 1, a, b, c + bam[cur]) + 10
    print(min(ret0, ret1, ret2, ret3))
    return min(ret0, ret1, ret2, ret3)

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

Submission status: Wrong Answer

Solution code:
#coding: utf-8
import math
import heapq
import bisect
import numpy as np
#from scipy.misc import comb

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

L = []
for i in range(N):
    l = int(input())
    L.append(l)

ans = 10**9

for i in range(1<<(2*N)):
    a = b = c = 0
    tmp = 0
    for j in range(N):
        flg = 0
        if i&(1<<(2*j)):
            flg += 1
        if i&(1<<(2*j+1)):
            flg += 2

        if flg == 1:
            if a:
                tmp += 10
            a += L[j]
        elif flg == 2:
            if b:
                tmp += 10
            b += L[j]
        elif flg == 3:
            if c:
                tmp += 10
            c += L[j]
        print(tmp)

    if a*b*c==0:
        continue

    tmp += abs(A-a)+abs(B-b)+abs(C-c)
    ans = min(ans,tmp)

print(ans)

Submission status: Wrong Answer

Solution code:
# coding: utf-8

list = [int(i) for i in input().rstrip().split(" ")]

matsu_count = list[0]
require = sorted(list[1:])

matsu_list = sorted([int(input().rstrip()) for i in range(matsu_count)])

def get_mp(matsu:list):
    ans = 0
    for i in range(3):
        ans += abs(matsu[i] - require[i])
    return ans
mp = {}
def get_ans(matsu:list):
    if len(matsu) == 3:
        return get_mp(matsu)
    key = "#".join(map(str, matsu))
    if key in mp:
        return mp[key]
    ans = 80000
    for i in range(len(matsu) - 3 + 1):
        ans = min(ans, get_mp(matsu[i:]))
    for i in range(len(matsu)):
        for j in range(i + 1, len(matsu)):
            sub_matsu = []
            for k in range(len(matsu)):
                if k == i or k == j:
                    continue
                else:
                    sub_matsu.append(matsu[k])
            sub_matsu.append(matsu[i] + matsu[j])
            ans = min(ans, get_ans(sorted(sub_matsu)) + 10)

    mp[key] = ans
    return ans

ans = get_ans(matsu_list)
print(ans)

Submission status: Wrong Answer

Solution code:
N,*T = map(int,input().split())
L = [int(input()) for i in range(N)]
ans = float('inf')

for x in range(4**N):
    l = [0]*4
    t = [0]*4
    n = 4
    i = 0

    while x>0:
        l[x%n] += L[i]
        t[x%n] += 1
        x//=n
        i+=1

    if 0 in l[:3]:
        continue
    cost = 0
    for i in range(3):
        cost += (t[i]-1)*10
        cost += abs(l[i]-T[i])

    ans = min(ans, cost)

print(ans)

Submission status: Wrong Answer

Solution code:
N,*T = map(int,input().split())
L = [int(input()) for i in range(N)]
ans = 3000

for x in range(4**N):
    l = [0]*4
    t = [0]*4
    n = 4
    i = 0

    while x>0:
        l[x%n] += L[i]
        t[x%n] += 1
        x//=n
        i+=1

    if 0 in l[:3]:
        continue
    cost = 0
    for i in range(3):
        cost += (t[i]-1)*10
        cost += abs(l[i]-T[i])

    ans = min(ans, cost)

print(ans)

Submission status: Wrong Answer

Solution code:
n,A,B,C = map(int,input().split())
l = list(int(input())for _ in range(n))
def dfs(depth,a,b,c):
    if depth == n:
        return abs(a-A)+abs(b-B)*abs(c-C)-30  if min(a,b,c) >0 else 10**9
    x = dfs(depth+1,a,b,c)
    y = dfs(depth+1,a+l[depth],b,c) +10
    z = dfs(depth+1,a,b+l[depth],c) +10
    w = dfs(depth+1,a,b,c+l[depth]) +10
    return min(x,y,z,w)
print(dfs(0,0,0,0))

Submission status: Wrong Answer

Solution code:
n,A,B,C = map(int,input().split())
l = list(int(input())for _ in range(n))
def dfs(depth,a,b,c):
    if depth == n:
        return abs(a-A)+abs(b-B)*abs(c-C)-30  if min(a,b,c) >0 else 10**9
    x = dfs(depth+1,a,b,c)
    y = dfs(depth+1,a+l[depth],b,c) +10
    z = dfs(depth+1,a,b+l[depth],c) +10
    w = dfs(depth+1,a,b,c+l[depth]) +10
    return min(x,y,z,w)
dfs(0,0,0,0)

Submission status: Wrong Answer

Solution code:
def f(x):
  q, r = divmod(x, 4)
  if q:
    return f(q) + str(r)
  return str(r)
n, *abc = map(int, input().split())
L = [ int(input()) for _ in range(n) ]
ans = float("inf")
for i in range(4**n):
  T = [set(), set(), set()]
  s = f(i)
  s = "0"*(n-len(s)) + s
  for j in range(n):
    if s[j] != "3":
      T[int(s[j])].add(L[j])
  temp = 0
  for j in range(3):
    if not T[j]:
      temp = float("inf")
      break
    l = len(T[j])
    temp += 10*(l-1) + abs(sum(T[j])-abc[j])
  ans = min(ans, temp)
print(ans)

Submission status: Wrong Answer

Solution code:
def f(x):
  q, r = divmod(x, 4)
  if q:
    return f(q) + str(r)
  return str(r)
n, *abc = map(int, input().split())
L = [ int(input()) for _ in range(n) ]
ans = float("inf")
for i in range(4**n):
  T = [set(), set(), set()]
  s = f(i)
  s = "0"*(n-len(s)) + s
  if {"0", "1", "2"} <= set(s):
    for j in range(n):
      if s[j] != "3":
        T[int(s[j])].add(L[j])
    temp = 0
    for j in range(3):
      l = len(T[j])
      temp += 10*(l-1) + abs(sum(T[j])-abc[j])
    ans = min(ans, temp)
print(ans)

Submission status: Wrong Answer

Solution code:
def f(x):
  q, r = divmod(x, 4)
  if q:
    return f(q) + str(r)
  return str(r)
n, *abc = map(int, input().split())
L = [ int(input()) for _ in range(n) ]
ans = float("inf")
for i in range(4**n):
  T = [set(), set(), set()]
  s = f(i)
  s = "0"*(n-len(s)) + s
  if not ({"0", "1", "2"} <= set(s)):
    continue
  for j in range(n):
    if s[j] != "3":
      T[int(s[j])].add(L[j])
  temp = 0
  for j in range(3):
    l = len(T[j])
    temp += 10*(l-1) + abs(sum(T[j])-abc[j])
  ans = min(ans, temp)
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
N, A, B, C = (int(i) for i in input().split())  
l = [int(input()) for i in range(N)] 

def dfs(cur, a, b, c):
  if cur == N:
    return abs(a-A) + abs(b-B) + abs(c-C) - 30 if min(a, b, c) > 0 else np.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
  if cur == 1:
    print(a, b, c, ret0, ret1, ret2, ret3)
  return min(ret0, ret1, ret2, ret3)

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

Submission status: Wrong Answer

Solution code:
n, a, b, c = list(map(int, input().split(' ')))
l = []
d = {'a': a, 'b': b, 'c': c}
for i in range(n):
    l.append(int(input()))
l = sorted(l)[::-1]
seed = {
	3: [[1,1,1]],
	4: [[1,1,2], [1,2,1], [2,1,1]], # mp=10
	5: [[1,1,3], [1,3,1], [3,1,1],  # mp=20
        [1,2,2], [2,1,2], [2,2,1]],
	6: [[1,1,4], [1,4,1], [4,1,1],  # mp=30
        [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1],
        [2,2,2]],
	7: [[1,1,5], [1,5,1], [5,1,1],  # mp=40
        [1,2,4], [1,4,2], [2,1,4], [2,4,1], [4,1,2], [4,2,1],
        [1,3,3], [3,1,3], [3,3,1],
        [2,2,3], [2,3,2], [3,2,2]],
	8: [[1,1,6], [1,6,1], [6,1,1],  # mp=50
        [1,2,5], [1,5,2], [2,1,5], [2,5,1], [5,1,2], [5,2,1],
        [1,3,4], [1,4,3], [3,1,4], [3,4,1], [4,1,3], [4,3,1],
        [2,2,4], [2,4,2], [4,2,2],
        [2,3,3], [3,2,3], [3,3,2]],
}
ans = 99999999999999

def next(ch):
    return 'b' if ch == 'a' else 'c' if ch == 'b' else 'd'
abc = ['a', 'b', 'c']
min_a = 99999999999999
memo = {}
def f(index, current_ch, sss, used, costs):
    global min_a
    #key = str((index, current_ch, sss, used, costs))
    key = str((index, current_ch, sss, used, (len(costs['a']), sum(costs['a'])), (len(costs['b']), sum(costs['b'])), (len(costs['c']), sum(costs['c']))))
    if key in memo:
        return memo[key]
    
    if costs['a'] and current_ch == 'b':
        if min_a >= abs(sum(costs['a']) - a):
            min_a = abs(sum(costs['a']) - a)
        elif min_a < abs(sum(costs['a']) - a) * 0.1:
            return 99999999999999

    if current_ch == 'd':
        memo[key] = abs(sum(costs['a']) - a) + abs(sum(costs['b']) - b) + abs(sum(costs['c']) - c)
        return memo[key]
    # if index >= n or n-index < sss[abc.index(current_ch)] - len(costs[current_ch]) + (2-abc.index(current_ch)):
    if index >= n:
        # print ('hoge', sss, costs, n, index, current_ch)
        memo[key] = 99999999999999
        return memo[key]
    # skip
    temp = min(99999999999999, f(index+1, current_ch, sss, used, costs))
    # 1つ追加
    tugihe = len(costs[current_ch]) == sss[abc.index(current_ch)] - 1
    next_ch = next(current_ch) if tugihe else current_ch
    next_index = 0 if tugihe else index + 1
    for i in range(index, n):
        if used[i]:
            continue
        used[i] = True
        costs[current_ch].append(l[i])
        temp = min(temp, f(next_index, next_ch, sss, used, costs))
        used[i] = False
        costs[current_ch].pop(len(costs[current_ch])-1)
    memo[key] = temp
    return memo[key]

for k in range(3, n+1):
  mp = (k - 3) * 10
  used = [False] * n
  for seed_k_j in seed[k]:
      hoge = f(0, 'a', seed_k_j, used, {'a':[], 'b':[], 'c':[]}) + mp
    #   if hoge < 99999999999999:
    #       print (hoge, seed_k_j)
      ans = min(ans, hoge)
print(ans)

Submission status: Wrong Answer

Solution code:
import os, sys, re, math

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

bamboo = []
for i in range(N):
    bamboo.append(int(input()))
#bamboo = sorted(bamboo)
memo = []

def doit2(targets):
    ret = 0
    c_bamboo = bamboo[:]
    for t in targets:
        tmp = doit(t, c_bamboo)
        ret += tmp
    return ret

def doit(target, bamboo):
    memo = [[[] for _ in range(1001)] for __ in range(N+1)]
    memo[0][0] = [0,[]]
    for i in range(len(bamboo)):
        b = bamboo[i]
        for j in range(1001):
            memo[i+1][j] = memo[i][j]
        for j in range(1001):
            if memo[i][j] and memo[i][j][0] >= 0 and b + j < 1001:
                if memo[i+1][b+j]:
                    if memo[i+1][b+j][0] > memo[i][j][0] + 1:
                        memo[i+1][b+j] = [memo[i][j][0]+1, memo[i][j][1] + [b]]
                else:
                    memo[i+1][b+j] = [memo[i][j][0]+1, memo[i][j][1] + [b]]

    ret = 1001
    remove_items = []
    for i in range(1,1001):
        if memo[len(bamboo)][i]:
            if ret > abs(target - i) + (memo[len(bamboo)][i][0] - 1) * 10:
                ret = abs(target - i) + (memo[len(bamboo)][i][0] - 1) * 10
                remove_items = memo[len(bamboo)][i][1]

    for r in remove_items:
        bamboo.remove(r)

    return ret

ret = float('inf')
for ts in [[A,B,C],[A,C,B],[B,A,C],[B,C,A],[C,A,B],[C,B,A]]:
    ret = min(ret,doit2(ts))

print(ret)

Submission status: Wrong Answer

Solution code:
import os, sys, re, math

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

bamboo = []
for i in range(N):
    bamboo.append(int(input()))
bamboo = sorted(bamboo)
memo = []

def doit2(targets):
    ret = 0
    c_bamboo = bamboo[:]
    for t in targets:
        tmp = doit(t, c_bamboo)
        ret += tmp
    return ret

def doit(target, bamboo):
    memo = [[[] for _ in range(1001)] for __ in range(N+1)]
    memo[0][0] = [0,[]]
    for i in range(len(bamboo)):
        b = bamboo[i]
        for j in range(1001):
            memo[i+1][j] = memo[i][j]
        for j in range(1001):
            if memo[i][j] and memo[i][j][0] >= 0 and b + j < 1001:
                if memo[i+1][b+j]:
                    if memo[i+1][b+j][0] > memo[i][j][0] + 1:
                        memo[i+1][b+j] = [memo[i][j][0]+1, memo[i][j][1] + [b]]
                else:
                    memo[i+1][b+j] = [memo[i][j][0]+1, memo[i][j][1] + [b]]

    ret = 1001
    remove_items = []
    for i in range(1,1001):
        if memo[len(bamboo)][i]:
            if ret > abs(target - i) + (memo[len(bamboo)][i][0] - 1) * 10:
                ret = abs(target - i) + (memo[len(bamboo)][i][0] - 1) * 10
                remove_items = memo[len(bamboo)][i][1]

    for r in remove_items:
        bamboo.remove(r)

    return ret

ret = float('inf')
for ts in [[A,B,C],[A,C,B],[B,A,C],[B,C,A],[C,A,B],[C,B,A]]:
    ret = min(ret,doit2(ts))

print(ret)

Submission status: Wrong Answer

Solution code:
import os, sys, re, math

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

bamboo = []
for i in range(N):
    bamboo.append(int(input()))
bamboo = sorted(bamboo)
memo = []

def doit2(targets):
    ret = 0
    c_bamboo = bamboo[:]
    for t in targets:
        tmp = doit(t, c_bamboo)
        ret += tmp
    return ret

def doit(target, bamboo):
    memo = [[[] for _ in range(1001)] for __ in range(N+1)]
    memo[0][0] = [0,[]]
    for i in range(len(bamboo)):
        b = bamboo[i]
        for j in range(1001):
            memo[i+1][j] = memo[i][j]
        for j in range(1001):
            if memo[i][j] and memo[i][j][0] >= 0 and b + j < 1001:
                if memo[i+1][b+j]:
                    if memo[i+1][b+j][0] > memo[i][j][0] + 1:
                        memo[i+1][b+j] = [memo[i][j][0]+1, memo[i][j][1] + [b]]
                else:
                    memo[i+1][b+j] = [memo[i][j][0]+1, memo[i][j][1] + [b]]

    ret = 1001
    remove_items = []
    for i in range(1,1001):
        if memo[len(bamboo)][i]:
            if ret > abs(target - i) + (memo[len(bamboo)][i][0] - 1) * 10:
                ret = abs(target - i) + (memo[len(bamboo)][i][0] - 1) * 10
                remove_items = memo[len(bamboo)][i][1]

    for r in remove_items:
        bamboo.remove(r)

    return ret

ret = 100000000000000000000000000
for ts in [[A,B,C],[A,C,B],[B,A,C],[B,C,A],[C,A,B],[C,B,A]]:
    ret = min(ret,doit2(ts))

print(ret)

Submission status: Wrong Answer

Solution code:
import os, sys, re, math

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

bamboo = []
for i in range(N):
    bamboo.append(int(input()))
bamboo = sorted(bamboo)
memo = []

def doit2(targets):
    ret = 0
    c_bamboo = bamboo[:]
    for t in targets:
        tmp = doit(t, c_bamboo)
        ret += tmp
    return ret

def doit(target, bamboo):
    memo = [[[] for _ in range(1001)] for __ in range(N+1)]
    memo[0][0] = [0,[]]
    for i in range(len(bamboo)):
        b = bamboo[i]
        for j in range(1001):
            memo[i+1][j] = memo[i][j]
        for j in range(1001):
            if memo[i][j] and memo[i][j][0] >= 0 and b + j < 1001:
                if memo[i+1][b+j]:
                    if memo[i+1][b+j][0] > memo[i][j][0] + 1:
                        memo[i+1][b+j] = [memo[i][j][0]+1, memo[i][j][1] + [b]]
                else:
                    memo[i+1][b+j] = [memo[i][j][0]+1, memo[i][j][1] + [b]]

    ret = 1001
    remove_items = []
    for i in range(1,1001):
        if memo[len(bamboo)][i]:
            if ret > abs(target - i) + (memo[len(bamboo)][i][0] - 1) * 10:
                ret = abs(target - i) + (memo[len(bamboo)][i][0] - 1) * 10
                remove_items = memo[len(bamboo)][i][1]

    for r in remove_items:
        bamboo.remove(r)

    return ret

ret = 10000000000
for ts in [[A,B,C],[A,C,B],[B,A,C],[B,C,A],[C,A,B],[C,B,A]]:
    ret = min(ret,doit2(ts))

print(ret)

Submission status: Wrong Answer

Solution code:
n, a, b, c = list(map(int, input().split(' ')))
l = []
d = {'a': a, 'b': b, 'c': c}
for i in range(n):
    l.append(int(input()))
l = sorted(l)[::-1]
seed = {
	3: [[1,1,1]],
	4: [[1,1,2], [1,2,1], [2,1,1]], # mp=10
	5: [[1,1,3], [1,3,1], [3,1,1],  # mp=20
        [1,2,2], [2,1,2], [2,2,1]],
	6: [[1,1,4], [1,4,1], [4,1,1],  # mp=30
        [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1],
        [2,2,2]],
	7: [[1,1,5], [1,5,1], [5,1,1],  # mp=40
        [1,2,4], [1,4,2], [2,1,4], [2,4,1], [4,1,2], [4,2,1],
        [1,3,3], [3,1,3], [3,3,1],
        [2,2,3], [2,3,2], [3,2,2]],
	8: [[1,1,6], [1,6,1], [6,1,1],  # mp=50
        [1,2,5], [1,5,2], [2,1,5], [2,5,1], [5,1,2], [5,2,1],
        [1,3,4], [1,4,3], [3,1,4], [3,4,1], [4,1,3], [4,3,1],
        [2,2,4], [2,4,2], [4,2,2],
        [2,3,3], [3,2,3], [3,3,2]],
}
ans = 9999999
 
def next(ch):
    return 'b' if ch == 'a' else 'c' if ch == 'b' else 'd'
abc = ['a', 'b', 'c']
min_a = 9999999
memo = {}
def f(index, current_ch, sss, used, costs):
    global min_a
    #key = str((index, current_ch, sss, used, costs))
    key = str((index, current_ch, sss, used, (sum(costs['a'])), (sum(costs['b'])), (sum(costs['c']))))
    if key in memo:
        return memo[key]
    
    if costs['a'] and current_ch == 'b':
        if min_a >= abs(sum(costs['a']) - a):
            min_a = abs(sum(costs['a']) - a)
        elif min_a < abs(sum(costs['a']) - a) * 0.2:
            return 9999999
 
    if current_ch == 'd':
        memo[key] = abs(sum(costs['a']) - a) + abs(sum(costs['b']) - b) + abs(sum(costs['c']) - c)
        return memo[key]
    # if index >= n or n-index < sss[abc.index(current_ch)] - len(costs[current_ch]) + (2-abc.index(current_ch)):
    if index >= n:
        # print ('hoge', sss, costs, n, index, current_ch)
        memo[key] = 9999999
        return memo[key]
    # skip
    temp = min(9999999, f(index+1, current_ch, sss, used, costs))
    # 1つ追加
    tugihe = len(costs[current_ch]) == sss[abc.index(current_ch)] - 1
    next_ch = next(current_ch) if tugihe else current_ch
    next_index = 0 if tugihe else index + 1
    for i in range(index, n):
        if used[i]:
            continue
        used[i] = True
        costs[current_ch].append(l[i])
        temp = min(temp, f(next_index, next_ch, sss, used, costs))
        used[i] = False
        costs[current_ch].pop(len(costs[current_ch])-1)
    memo[key] = temp
    return memo[key]
 
for k in range(3, n+1):
  mp = (k - 3) * 10
  used = [False] * n
  for seed_k_j in seed[k]:
      hoge = f(0, 'a', seed_k_j, used, {'a':[], 'b':[], 'c':[]}) + mp
    #   if hoge < 9999999:
    #       print (hoge, seed_k_j)
      ans = min(ans, hoge)
print(ans)

Submission status: Wrong Answer

Solution code:
n, a, b, c = list(map(int, input().split(' ')))
l = []
d = {'a': a, 'b': b, 'c': c}
for i in range(n):
    l.append(int(input()))
l = sorted(l)[::-1]
seed = {
	3: [[1,1,1]],
	4: [[1,1,2], [1,2,1], [2,1,1]], # mp=10
	5: [[1,1,3], [1,3,1], [3,1,1],  # mp=20
        [1,2,2], [2,1,2], [2,2,1]],
	6: [[1,1,4], [1,4,1], [4,1,1],  # mp=30
        [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1],
        [2,2,2]],
	7: [[1,1,5], [1,5,1], [5,1,1],  # mp=40
        [1,2,4], [1,4,2], [2,1,4], [2,4,1], [4,1,2], [4,2,1],
        [1,3,3], [3,1,3], [3,3,1],
        [2,2,3], [2,3,2], [3,2,2]],
	8: [[1,1,6], [1,6,1], [6,1,1],  # mp=50
        [1,2,5], [1,5,2], [2,1,5], [2,5,1], [5,1,2], [5,2,1],
        [1,3,4], [1,4,3], [3,1,4], [3,4,1], [4,1,3], [4,3,1],
        [2,2,4], [2,4,2], [4,2,2],
        [2,3,3], [3,2,3], [3,3,2]],
}
ans = 9999999

def next(ch):
    return 'b' if ch == 'a' else 'c' if ch == 'b' else 'd'
abc = ['a', 'b', 'c']
min_a = 9999999
memo = {}
def f(index, current_ch, sss, used, costs):
    global min_a
    #key = str((index, current_ch, sss, used, costs))
    key = str((index, current_ch, (len(costs['a']), sum(costs['a'])), (len(costs['b']), sum(costs['b'])), (len(costs['c']), sum(costs['c']))))
    if key in memo:
        return memo[key]
    
    if costs['a'] and current_ch == 'b':
        if min_a >= abs(sum(costs['a']) - a):
            min_a = abs(sum(costs['a']) - a)
        elif min_a < abs(sum(costs['a']) - a) * 0.01:
            return 9999999

    if current_ch == 'd':
        memo[key] = abs(sum(costs['a']) - a) + abs(sum(costs['b']) - b) + abs(sum(costs['c']) - c)
        return memo[key]
    # if index >= n or n-index < sss[abc.index(current_ch)] - len(costs[current_ch]) + (2-abc.index(current_ch)):
    if index >= n:
        # print ('hoge', sss, costs, n, index, current_ch)
        memo[key] = 9999999
        return memo[key]
    # skip
    temp = min(9999999, f(index+1, current_ch, sss, used, costs))
    # 1つ追加
    tugihe = len(costs[current_ch]) == sss[abc.index(current_ch)] - 1
    next_ch = next(current_ch) if tugihe else current_ch
    next_index = 0 if tugihe else index + 1
    for i in range(index, n):
        if used[i]:
            continue
        used[i] = True
        costs[current_ch].append(l[i])
        temp = min(temp, f(next_index, next_ch, sss, used, costs))
        used[i] = False
        costs[current_ch].pop(len(costs[current_ch])-1)
    memo[key] = temp
    return memo[key]

for k in range(3, n+1):
  mp = (k - 3) * 10
  used = [False] * n
  for seed_k_j in seed[k]:
      hoge = f(0, 'a', seed_k_j, used, {'a':[], 'b':[], 'c':[]}) + mp
    #   if hoge < 9999999:
    #       print (hoge, seed_k_j)
      ans = min(ans, hoge)
print(ans)