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)]
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
    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 = list(map(int, input().split()))
l = []

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

ans = 10000000

arr = [0,1,2,3]

import itertools

for a in itertools.product(arr, repeat=N):
    ind_1 = [i for i, x in enumerate(a) if x == 1]
    sum_1 = 0
    for i in ind_1:
        sum_1 += l[i]
    ans_1 = 10*(len(ind_1)-1) + abs(sum_1-A)

    ind_2 = [i for i, x in enumerate(a) if x == 2]
    sum_2 = 0
    for i in ind_2:
        sum_2 += l[i]
    ans_2 = 10*(len(ind_2)-1) + abs(sum_2-B)
    
    ind_3 = [i for i, x in enumerate(a) if x == 3]
    sum_3 = 0
    for i in ind_3:
        sum_3 += l[i]
    ans_3 = 10*(len(ind_3)-1) + abs(sum_3-C)
    
    ans = min(ans, ans_1 + ans_2 + ans_3)
    
print(ans)

Submission status: Wrong Answer

Solution code:
import copy

N,A,B,C = map(int,raw_input().split())
take = [A,B,C]

l = [map(int,(raw_input()+' 0 0 0').split()) for i in range(N)]

ans = 0
flag = [0,0,0]
for i in range(N):
    l[i][1] = abs(A-l[i][0])
    l[i][2] = abs(B-l[i][0])
    l[i][3] = abs(C-l[i][0])

l.sort(key = lambda x:x[1])
if l[0][1] < 10:
    ans += l[0][1]
    flag[0] = 1
    del l[0]

l.sort(key = lambda x:x[2])
if l[0][2] < 10:
    ans += l[0][2]
    flag[1] = 1
    del l[0]

l.sort(key = lambda x:x[3])
if l[0][3] < 10:
    ans += l[0][3]
    flag[2] = 1
    del l[0]

for i in [0,1,2]:
    minimam = 10000000000000
    sayonara = []
    if flag[i] == 1:
        continue
    for j in xrange(2**len(l)-1):
        ret = -10
        cnt = 0
        length = 0
        data = []
        while cnt < len(l):
            if j%2 == 0: #siyou
                ret += 10
                length += l[cnt][0]
                data.append(1)
            else:
                data.append(0)
            j /= 2
            cnt += 1
        if minimam > abs(take[i]-length)+ret:
            minimam = abs(take[i]-length)+ret
            sayonara = copy.copy(data)
    k = 0
    for j in range(len(sayonara)):
        if sayonara[j] == 1:
            del l[j-k]
            k += 1
    ans += minimam
    
print ans

Submission status: Wrong Answer

Solution code:
import copy

N,A,B,C = map(int,raw_input().split())
take = [A,B,C]

l = [map(int,(raw_input()+' 0 0 0').split()) for i in range(N)]

ans = 0
flag = [0,0,0]
for i in range(N):
    l[i][1] = abs(A-l[i][0])
    l[i][2] = abs(B-l[i][0])
    l[i][3] = abs(C-l[i][0])

l.sort(key = lambda x:x[1])
if l[0][1] < 10:
    ans += l[0][1]
    flag[0] = 1
    del l[0]

l.sort(key = lambda x:x[2])
if l[0][2] < 10:
    ans += l[0][2]
    flag[1] = 1
    del l[0]

l.sort(key = lambda x:x[3])
if l[0][3] < 10:
    ans += l[0][3]
    flag[2] = 1
    del l[0]

for i in [2,1,0]:
    minimam = 10000000000000
    sayonara = []
    if flag[i] == 1:
        continue
    for j in xrange(2**len(l)-1):
        ret = -10
        cnt = 0
        length = 0
        data = []
        while cnt < len(l):
            if j%2 == 0: #siyou
                ret += 10
                length += l[cnt][0]
                data.append(1)
            else:
                data.append(0)
            j /= 2
            cnt += 1
        if minimam > abs(take[i]-length)+ret:
            minimam = abs(take[i]-length)+ret
            sayonara = copy.copy(data)
    k = 0
    for j in range(len(sayonara)):
        if sayonara[j] == 1:
            del l[j-k]
            k += 1
    ans += minimam
    
print ans

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]) + 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 += 10*(len(arg_min)-1) if len(arg_min)>0 else 0
            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:
N, A, B, C = map(int, input().split(" "))
L = [int(input()) for i in range(N)]

def calc_cost(num, group):
    cost = 10 * (len(group) - 1) + abs(sum(group) - num)
    return cost

cand = []
for n in range(4 ** N):
    groups = [[], [], []]
    for i in range(N):
        temp = n % 4
        if n > 0:
            groups[temp-1].append(i)
        n = n // 4
    if len(groups[0]) * len(groups[1]) * len(groups[2]) > 0: 
        cand.append(calc_cost(A, groups[0]) + calc_cost(B, groups[1]) + calc_cost(C, groups[2]))
print(min(cand))

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/abc119/tasks/abc119_c
import copy

def solve(t, l, c):
    # 長さ t の竹を L から少ないコストで作る方法を探す
    min_cost = 100000
    cand = []

    ptn = 1 << len(l)
    # print(l)
    # print("ptn = {}".format(ptn))
    for i in range(1, ptn):
        answers = []
        for j in range(0, len(l)):
            if (i >> j) & 1:
                answers.append(l[j])
        temp_cost = abs(t - sum(answers)) + (10 * (len(answers) - 1) if len(answers) > 1 else 0)
        if temp_cost < min_cost:
            min_cost = temp_cost
            cand = answers

    # print("cand = {}".format(cand))
    for d in cand:
        del l[l.index(d)]
    return min_cost, l

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

    answer = 1000000
    for targets in [[A, B, C], [A, C, B],[B, A, C],[B, C, A], [C,B,A],[C,A,B]]:
        is_done = [False] * 3
        L2 = copy.copy(L)
        total_cost = 0
        for i in range(len(targets)):
            if targets[i] in L:
                del L[L.index(targets[i])]
                is_done[i] = True

        for i in range(3):
            if is_done[i]:
                continue

            # i の竹を L から少ないコストで作る方法を探す
            # print("try target = {}".format(targets[i]))
            cost, L2 = solve(targets[i], L2, 0)
            # print("cost = {}".format(cost))
            total_cost += cost

        answer = min(answer, total_cost)

    print(answer)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def sum_count(vec):
    return sum(vec), len(vec)-1
def main():
    N, A, B, C = list(map(int, input().split()))
    ls = []
    for _ in range(N):
        ls.append(int(input()))
    gs = [[], [], [], []]
    a = 0
    aa = [0] * N
    min_mp = 10000000000
    while a != 4^8:
        a += 1
        for i in range(N):
            aa[i] = (a//(4**i))%4
        for i, k in enumerate(aa):
            gs[k].append(ls[i])
        if any(x == [] for x in gs[1:]):
            gs = [[], [], [], []]
            continue
        else:
            abcs = []
            gousei = 0
            for abc in gs[1:]:
                s, l = sum_count(abc)
                abcs.append(s)
                abcs.sort()
                gousei += l
            mp = gousei*10 + abs(abcs[0]-A) + abs(abcs[1]-B) + abs(abcs[2]-C)
            if mp < min_mp:
                min_mp = mp
    print(min_mp)

            
            

main()

Submission status: Wrong Answer

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


l = []
sum = 0

for _ in range(n):
  l.append(input())
  
l = [int(i) for i in l]
l.sort(reverse = True)

for x in bum:
  for i in l:
    for j in l:
      if x>i and x==(i+j) and i!=j and (x-i)>10:
        sum += 10
        l.remove(j)
        
      else:
        sum += abs(x-i)
    l.remove(i)
        
      
print(sum)

Submission status: Wrong Answer

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

tar = []
for x in C, B, A:
    if x in l:
        l.remove(x)
    else:
        tar.append(x)

if not tar:
    print(0)
    exit()

ans = float('inf')
for j in range((len(tar) + 1) ** len(l)):
    vvv = [[] for _ in range(len(tar) + 1)]
    for i in sorted(range(len(l)), reverse=True):
        idx, j = divmod(j, (len(tar) + 1) ** i)
        vvv[idx].append(l[i])
    bbb = 0
    # print(tar, vvv)
    for k in range(len(tar)):
        bbb += 10 * (len(vvv[k]) - 1)
        bbb += abs(sum(vvv[k]) - tar[k])
    ans = min(ans, bbb)

print(ans)

Submission status: Wrong Answer

Solution code:
import itertools
import numpy as np

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.combinations(target, len(target)))
mana_cost = []
for target in target_comb:
    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(cost[arg_min])
        #print(combs[arg_min])
    mana_cost.append(mana)
print(min(mana_cost))

Submission status: Wrong Answer

Solution code:
import itertools

n,a,b,c=map(int,input().split())
l0=[a,b,c]
l0.sort()
l02=l0[:]
l02.reverse()
l=[]
for i in range(n):
  x=int(input())
  if l0.count(x)>0:
    l0.remove(x)
  else:
    l.append(x)
if len(l0)==0:
  print(0)
  exit()
ll=l[:]

cst1=0
l2=[]
for i in l0:
  if i<min(l):
    cst1=min(l)-i
    l.remove(min(l))
  else:
    p=100000000000000
    r=[]
    for j in range(1,len(l)+1):
      for k in list(itertools.combinations(l,j)):
        q=abs(i-sum(k))+(j-1)*10
        if q<p:
          p=q
          r=k
    l2.append(tuple([r,p]))
    for j in r:
      l.remove(j)
for i in l2:
  cst1+=i[1]
print(cst1)

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)]
_sum = 0
for target in [C, B, A]:
    min = 1e9
    for dis in dis_ls:
        sa = abs(dis[0] - target) + 10 * (len(dis[1]) - 1)
        if sa < min:
            min = sa
            min_combi = dis[1]
    _sum += min
    dis_ls = [i for i in dis_ls if len(set(min_combi).intersection(set(i[1]))) == 0]
print(_sum)

Submission status: Wrong Answer

Solution code:
n,a,b,c=[int(i) for i in input().split()]
goal=[a,b,c]
l=[]
for i in range(n):
    l.append(int(input()))
point=0
for j in range(len(l)):
    try:
        for i in range(9):
            if (l[j]+i) in goal:
                goal.remove(l[j]+i)
                l.remove(l[j])
                point+=i
            elif (l[j]-i) in goal:
                goal.remove(l[j]-i)
                l.remove(l[j])
                point+=i
    except:
        pass
print(point)

Submission status: Wrong Answer

Solution code:

now_ans = 100000


def kotae(A, a_tree, a_tree_sum):
    ans = (a_tree - 1) * 10 + abs(a_tree_sum - A)
    if a_tree == 0:
        ans = 100000
    return ans

def calc_final(A, B, C, a_tree, b_tree, c_tree, a_tree_sum, b_tree_sum, c_tree_sum):
    global now_ans
    ans = 0
    ans += kotae(A, a_tree, a_tree_sum)
    ans += kotae(B, b_tree, b_tree_sum)
    ans += kotae(C, c_tree, c_tree_sum)
    if ans < now_ans:
        now_ans = ans

def calc_data(tree_data, A, B, C, a_tree, b_tree, c_tree, index, a_tree_sum, b_tree_sum, c_tree_sum ):
    if a_tree_sum >= A and b_tree_sum >= B and c_tree_sum >= C:
        calc_final(A, B, C, a_tree, b_tree, c_tree, a_tree_sum, b_tree_sum, c_tree_sum)
        return
    if index == len(tree_data) - 1 or tree_data[index + 1] <= 10:
        calc_final(A, B, C, a_tree, b_tree, c_tree, a_tree_sum, b_tree_sum, c_tree_sum)
        return
    if a_tree_sum < A:
        calc_data(tree_data, A, B, C, a_tree + 1, b_tree, c_tree, index + 1, a_tree_sum + tree_data[index + 1], b_tree_sum, c_tree_sum)
    if b_tree_sum < B:
        calc_data(tree_data, A, B, C, a_tree, b_tree + 1, c_tree, index + 1, a_tree_sum, b_tree_sum + tree_data[index + 1], c_tree_sum)

    if c_tree_sum < C:
        calc_data(tree_data, A, B, C, a_tree, b_tree, c_tree + 1, index + 1, a_tree_sum, b_tree_sum, c_tree_sum + tree_data[index + 1])

    calc_data(tree_data, A, B, C, a_tree, b_tree, c_tree, index + 1, a_tree_sum, b_tree_sum, c_tree_sum)


def main():
    global now_ans
    num, A, B, C = list(map(int, input().split()))
    tree_data = [int(input()) for i in range(num)]
    tree_data.sort(reverse=True)

    calc_data(tree_data, A, B, C, 1, 0, 0, 0, tree_data[0], 0, 0)
    calc_data(tree_data, A, B, C, 0, 1, 0, 0, 0, tree_data[0], 0)
    calc_data(tree_data, A, B, C, 0, 0, 1, 0, 0, 0, tree_data[0])
    calc_data(tree_data, A, B, C, 0, 0, 0, 0, 0, 0, 0)

    print(now_ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from sys import stdin
import itertools
N,A,B,C=[int(x) for x in stdin.readline().rstrip().split()]
data=[]

for i in range(N):
    data.append(int(input()))
    
target=[A,B,C]

ans=0

if A in data:
    data.remove(A)
    target.remove(A)

if B in data:
    data.remove(B)
    target.remove(B)
    
if C in data:
    data.remove(C)
    target.remove(C)

if target==[]:
    print(ans)

else:
    for k in target:
        sa=k
        for l in range(1,len(data)):
            for j in itertools.combinations(data,l):
                if abs(k-sum(j)) < sa:
                    sa= abs(k-sum(j))
                    ansj=j
        if len(ansj)==1:
            ans+=sa
        else:
            ans=ans+sa+10*(len(ansj)-1)
print(ans)

Submission status: Wrong Answer

Solution code:
NABC = input().split()

N = int(NABC[0])
ABC = []
ABC.append( int(NABC[1]) )
ABC.append( int(NABC[2]) )
ABC.append( int(NABC[3]) )
MP = 0

Tlist = []
for lop in range(N):
    l = int(input())
    Tlist.append(l)

Tlist = sorted(Tlist)

if ABC[2] in Tlist:
    Tlist.pop(Tlist.index(ABC[2]))
    ABC.pop(2)
if ABC[1] in Tlist:
    Tlist.pop(Tlist.index(ABC[1]))
    ABC.pop(1)
if ABC[0] in Tlist:
    Tlist.pop(Tlist.index(ABC[0]))
    ABC.pop(0)

if len(ABC) == 3:
    if ABC[2] < Tlist[0]:
        MP = MP + (Tlist[0] - ABC[2])
        ABC.pop(2)
        Tlist.pop(0)
if len(ABC) >= 2:
    if ABC[1] < Tlist[0]:
        MP = MP + (Tlist[0] - ABC[1])
        ABC.pop(1)
        Tlist.pop(0)
if len(ABC) >= 1:
    if ABC[0] < Tlist[0]:
        MP = MP + (Tlist[0] - ABC[0])
        ABC.pop(0)
        Tlist.pop(0)
while(True):
    if ABC == []:
        break
    take = ABC[0]
    if take < Tlist[0]:
        MP = MP + (Tlist[0] - take)
        ABC.pop(0)
        Tlist.pop(0)
    else:
        sa = 10000
        Tindex = -1
        for lop in range(len(Tlist)):
            if abs(take - Tlist[lop]) < sa:
                Tindex = lop
                sa = abs(take - Tlist[lop])
        if sa < 10:
            Tlist.pop(Tindex)
            ABC.pop(0)
            MP = MP + sa
        else:
            sa2 = 10000
            maeI = -1
            atoI = -1
            for lop in range(len(Tlist)):
                mae = Tlist[lop]
                for lop2 in range(len(Tlist)):
                    if not(lop == lop2):
                        ato = Tlist[lop2]
                        if abs(take - (mae + ato)) < sa2:
                            maeI = lop
                            atoI = lop2
                            sa2 = abs(take - (mae + ato))
            if sa > sa2 +10 :
                MP = MP + 10
                if maeI > atoI:
                    Tlist.append(Tlist[maeI] + Tlist[atoI])
                    Tlist.pop(maeI)
                    Tlist.pop(atoI)
                    Tlist = sorted(Tlist)  
                else:
                    Tlist.append(Tlist[maeI] + Tlist[atoI])
                    Tlist.pop(atoI)
                    Tlist.pop(maeI)
                    Tlist = sorted(Tlist)
            else:
                Tlist.pop(Tindex)
                ABC.pop(0)
                MP = MP + sa
    
print(MP)

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/abc119/tasks/abc119_c
import copy

def solve(t, l, c):
    # 長さ t の竹を L から少ないコストで作る方法を探す
    min_cost = 100000
    cand = []

    ptn = 1 << len(l)
    # print(l)
    # print("ptn = {}".format(ptn))
    for i in range(1, ptn):
        answers = []
        for j in range(0, len(l)):
            if (i >> j) & 1:
                answers.append(l[j])
        temp_cost = abs(t - sum(answers)) + (10 * (len(answers) - 1) if len(answers) > 1 else 0)
        if temp_cost < min_cost:
            min_cost = temp_cost
            cand = answers

    # print("cand = {}".format(cand))
    for d in cand:
        del l[l.index(d)]
    return min_cost, l

def main():
    N, A, B, C = map(int, input().split())
    target = [A, B, C]

    is_done = [False] * 3

    L = [int(input()) for _ in range(N)]
    L.sort()

    for i in range(len(target)):
        if target[i] in L:
            del L[L.index(target[i])]
            is_done[i] = True

    answer = 1000000
    for targets in [[A, B, C], [A, C, B],[B, A, C],[B, C, A], [C,B,A],[C,A,B]]:
        L2 = copy.copy(L)
        total_cost = 0
        for i in range(3):
            if is_done[i]:
                continue

            # i の竹を L から少ないコストで作る方法を探す
            # print("try target = {}".format(targets[i]))
            cost, L2 = solve(targets[i], L2, 0)
            # print("cost = {}".format(cost))
            total_cost += cost

        answer = min(answer, total_cost)

    print(answer)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

def get_best(li, goal):
    bam_mp_lis = {0: (-10, tuple())}
    li.sort()
    for i, v in enumerate(li):
        tmp = bam_mp_lis.copy()
        for bam, (mp, lis) in tmp.items():
            b0 = bam + v
            m0 = mp + 10
            l0 = lis + (i,)
            if b0 not in bam_mp_lis or m0 < bam_mp_lis[b0][0]:
                bam_mp_lis[b0] = (m0, l0)
    del bam_mp_lis[0]
    best = 10 ** 9
    for bam, (mp, lis) in bam_mp_lis.items():
        cost = mp + abs(goal - bam)
        if cost < best:
            best = cost
            beslis = lis
    return best, beslis

ans = 10 ** 9
from itertools import permutations
for junjo in permutations(goals):
    tmp = 0
    bambooo = bamboos.copy()
    for ju in junjo:
        be, beli = get_best(bambooo, ju)
        beli = sorted(beli, reverse=True)
        for beli0 in beli:
            del bambooo[beli0]
        tmp += be
        if len(bambooo) == 0:
            tmp = 10 ** 9
            break
    if tmp < ans:
        ans = tmp

print(ans)

Submission status: Wrong Answer

Solution code:
def sum_count(vec):
    return sum(vec), len(vec)-1
def main():
    N, A, B, C = list(map(int, input().split()))
    ls = []
    for _ in range(N):
        ls.append(int(input()))
    gs = [[], [], [], []]
    a = 0
    aa = [0] * N
    min_mp = 10000000000
    while a != 4^8:
        a += 1
        for i in range(N):
            aa[i] = (a//(4**i))%4
        for i, k in enumerate(aa):
            gs[k].append(ls[i])
        if any(x == [] for x in gs[1:]):
            gs = [[], [], [], []]
            continue
        else:
            abcs = []
            gousei = 0
            for abc in gs[1:]:
                s, l = sum_count(abc)
                abcs.append(s)
                abcs.sort()
                gousei += l
            mp = gousei*10 + abs(abcs[0]-A) + abs(abcs[1]-B) + abs(abcs[2]-C)
            if mp < min_mp:
                min_mp = mp
    return min_mp

            
            

main()

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)+"0"*8
    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
    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:
import math, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools


sys.setrecursionlimit(10**7)
inf = 10 ** 20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]


def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    global N, A, B, C, l
    N, A, B, C = LI()
    l = []
    [l.append(I()) for i in range(N)]
    l = sorted(l, reverse=True)
   #  print(N, A, B, C, l)
    flags = [False]*3
    if A in l:
        l.remove(A)
        flags[0] = True

    if B in l:
        l.remove(B)
        flags[1] = True

    if C in l:
        l.remove(C)
        flags[2] = True

    g = [A, B, C]
    for idx, f in enumerate(flags):
        if f:
            g[idx] = None
 
    g = [i for i in g if i != None]
    if len(g) == 0:
        print(0)
        return
    mp = 0
    for idx, i in enumerate(g[::-1]):
        if i < min(l):
            mp += abs(l[-1]-i)
            l = l[:-1]
            g[len(g)-1 - idx] = None
    g = [i for i in g if i != None]
    

    if len(g) == 0:
        print(mp)
        return

    for idx, i in enumerate(g):
        # iの数値を目指す
        # 一番近い値を目指す
        #  print('i', i)
        #  print('rest items l', l)
        min_diff = 10**8
        min_combis = []
        for n in range(0, len(l) - len(g) + idx + 2):
            #  print('n', n)
            combis = list(itertools.combinations(l, n))
            #  print('combis', combis)
            for c in combis:
                diff = (len(c)-1) * 10 + abs(i - sum(c))
                if diff < min_diff:
                    min_diff = diff
                    min_combis = c
        #  print('min_combis', min_combis, min_diff)
        mp += min_diff
        for mc in min_combis:
            l.remove(mc)

        #  nearest = min(l, key=lambda x:abs(x-i))
        #  #  for i in range(0, len(l) - (len(g) - 1) - idx):
        #  print('nearest', nearest)
        #  if abs(i - nearest) <= 10:
        #      mp += 10
        #      l.remove(nearest)
        #      continue




    print(mp)

#  def dfs(0, s, m, target, limit):
     


def find_combi(target):
    print('target', target)
    min_diff = 10**9
    min_combi = []
    for idx, i in enumerate(l):
        for jidx, j in enumerate(l):
            if idx == jidx: continue
            if abs(target - (i + j)) < min_diff:
                min_diff = abs(target - (i+j))
                min_combi = [idx, jidx]
            print(i, j)

    print('min_combi, min_diff', min_combi, min_diff)
    i_item = l[min_combi[0]]
    j_item = l[min_combi[1]]
    l.remove(i_item)
    l.remove(j_item)
    return min_combi, min_diff



if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import itertools as itr

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

cost = 10**9
for cr in range(1, n-2):
    for ccomb in itr.combinations(l, cr):
        ccosti = abs(c-sum(ccomb)) + (cr-1)*10
        cl = l[:]
        for icomb in ccomb:
            cl.remove(icomb)
        for br in range(1, n-2-cr):
            for bcomb in itr.combinations(cl, br):
                bcosti = abs(b-sum(bcomb)) + (br-1)*10
                bl = cl[:]
                for icomb in bcomb:
                    bl.remove(icomb)
                for ar in range(1, n-2-cr-br):
                    for acomb in itr.combinations(bl, ar):
                        acosti = abs(a-sum(acomb)) + (ar-1)*10
                        if cost > ccosti + bcosti + acosti:
                            cost = ccosti + bcosti + acosti

print(cost)

Submission status: Wrong Answer

Solution code:
import itertools

n,a,b,c=map(int,input().split())
l0=[a,b,c]
l0.sort()
l02=l0[:]
l02.reverse()
l=[]
for i in range(n):
  x=int(input())
  if l0.count(x)>0:
    l0.remove(x)
  else:
    l.append(x)
if len(l0)==0:
  print(0)
  exit()
l.sort()
ll=l[:]

cst1=0
l2=[]
for i in l0:
  if i<l[0]:
    cst1=l[0]-i
    l.remove(l[0])
  else:
    p=100000000000000
    r=[]
    for j in range(1,len(l)+1):
      for k in list(itertools.combinations(l,j)):
        q=abs(i-sum(k))+(j-1)*10
        if q<p:
          p=q
          r=k
    l2.append(tuple([r,p]))
    for j in r:
      l.remove(j)
for i in l2:
  cst1+=i[1]
print(cst1)

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/abc119/tasks/abc119_c
import copy

def solve(t, l, c):
    # 長さ t の竹を L から少ないコストで作る方法を探す
    min_cost = 100000
    cand = []

    ptn = 1 << len(l)
    # print(l)
    # print("ptn = {}".format(ptn))
    for i in range(1, ptn):
        answers = []
        for j in range(0, len(l)):
            if (i >> j) & 1:
                answers.append(l[j])
        temp_cost = abs(t - sum(answers)) + (10 * (len(answers) - 1) if len(answers) > 1 else 0)
        if temp_cost < min_cost:
            min_cost = temp_cost
            cand = answers

    # print("cand = {}".format(cand))
    for d in cand:
        del l[l.index(d)]
    return min_cost, l

def main():
    N, A, B, C = map(int, input().split())
    targets = [A, B, C]

    is_done = [False] * 3

    L = [int(input()) for _ in range(N)]
    L.sort()

    for i in range(len(targets)):
        if targets[i] in L:
            del L[L.index(targets[i])]
            is_done[i] = True

    answer = 1000000
    for targets in [[A, B, C], [A, C, B],[B, A, C],[B, C, A], [C,B,A],[ C,A,B]]:
        L2 = copy.copy(L)
        total_cost = 0
        for i in range(3):
            if is_done[i]:
                continue

            # i の竹を L から少ないコストで作る方法を探す
            # print("try target = {}".format(targets[i]))
            cost, L = solve(targets[i], L, 0)
            # print("cost = {}".format(cost))
            total_cost += cost

        answer = min(answer, total_cost)

    print(answer)


if __name__ == '__main__':
    main()

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]) < mini:
                    mini = abs(sum(i)-bars[k])
                    arg_min = i
            
            # print(mini, arg_min)
            ret += 10*(len(arg_min)-1) if len(arg_min)>0 else 0
            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()