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:
import itertools

def list_difference(list1, list2):
  result = list1.copy()
  for value in list2:
    if value in result:
      result.remove(value)
  return result

N,A,B,C = map(int, input().split())
lst = [int(input()) for i in range(N)]
ans_lst = []
for i in range(1, N-1):
  sub_lst = list(itertools.combinations(lst,i))
  for j in sub_lst:
    diff_lst = list_difference(lst, list(j))
    diff_lst.append(sum(list(j)))
    sub_diff_lst = list(itertools.permutations(diff_lst, r=3))
    for k in sub_diff_lst:
      ans_lst.append(abs(A - list(k)[0]) + abs(B - list(k)[1]) + abs(C - list(k)[2]) + 10 * (i - 1))

print(min(ans_lst))

Submission status: Wrong Answer

Solution code:
import itertools

def list_difference(list1, list2):
  result = list1.copy()
  for value in list2:
    if value in result:
      result.remove(value)
  return result

N,A,B,C = map(int, input().split())
lst = [int(input()) for i in range(N)]
ans_lst = []
for i in range(1, N-1):
  sub_lst = list(itertools.combinations(lst,i))
  for j in sub_lst:
    diff_lst = list_difference(lst, list(j))
    diff_lst.append(sum(list(j)))
    diff_lst.sort(reverse=True)
    ans_lst.append(abs(A - diff_lst[0]) + abs(B - diff_lst[1]) + abs(C - diff_lst[2]) + 10 * (i - 1))

print(min(ans_lst))

Submission status: Wrong Answer

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

length = [0] * N

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

inf = 10 ** 9

def dfs(pos, a, b, c):
    if pos == N:
        if min(a, b, c) > 0:
            #a = 0, b = 0, c = 0だったら題意が成立しない
            return abs(a - A) + abs(b - B) + abs(c - C) - 30
        else:
            return inf
    
    ret1 = dfs(pos + 1, a, b, c)
    ret2 = dfs(pos + 1, a + length[pos], b, c) + 10
    ret3 = dfs(pos + 1, a, b + length[pos], c) + 10
    ret4 = dfs(pos + 1, a, b, c + length[pos]) + 10
    return min(ret1, ret2, ret3, ret4)
    
dfs(0, 0, 0, 0)

Submission status: Wrong Answer

Solution code:
####C-bamboo
N,A,B,C = map(int,input().split())
l =[]
for _ in range(N):
    l.append(int(input()))
INF = 10**9

def dfs(cur,a,b,c):
    if cur == N:
        if min(a,b,c) > 0:
            ans = abs(a-A) + abs(b-B) + abs(c-C) -30 
            #接合は各一回ずつ余分
        else:
            ans = INF
        return ans
    ret0 = dfs(cur+1,a+l[cur],b,       c)        + 10
    ret1 = dfs(cur+1,a,       b+l[cur],c)        + 10
    ret2 = dfs(cur+1,a,       b,       c+l[cur]) + 10
    ret3 = dfs(cur+1,a,       b,       c)        + 10
    #a,b,c,どこにも入れないの4通り
    #分けて接合 -> 10
    return min(ret0,ret1,ret2,ret3)
print(dfs(0,0,0,0))

Submission status: Wrong Answer

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

def base2n(Num,n):
    if Num<n:
        return str(Num)
    else:
        return base2n(Num//n,n)+str(Num%n)

ans=10**6
    
for i in range(4**N):
    i_4=("0"*N+base2n(i,4))[-N:]
    Group=[[] for i in range(4)]
    for j in range(N):
        Group[int(i_4[j])-1].append(l[j])
    if len(Group[0])*len(Group[1])*len(Group[2])==0:
        pass
    else:
        ans=min(ans,(2-len(Group[3]))*10+sum([abs(ABC[j] - sum(Group[j])) for j in range(3)]))
        
print(ans)

Submission status: Wrong Answer

Solution code:
# @hitstales 
n, *goals = list(map(int, input().split()))
bamboos = list(map(int, [input().rstrip() for _ in range(n)]))


def get_best(li, goal):
    bam_mp_lis = {0: (-10, tuple())}
    li = sorted(li)
    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:
        if len(bambooo) == 0:
            tmp = 10 ** 9
            break
        be, beli = get_best(bambooo, ju)
        beli = sorted(beli, reverse=True)
        for beli0 in beli:
            del bambooo[beli0]
        tmp += be
    if tmp < ans:
        ans = tmp

print(ans)

Submission status: Wrong Answer

Solution code:
n,a,b,c = map(int, input().split())
l = [int(input()) for _ in range(n)]
def dfs(k,p,q,r):
  if k == n:
    if min(a,b,c) > 0:
      return abs(p-a)+abs(q-b)+abs(r-c)-30
    else:
      return 10**7
  t1 = dfs(k+1,p,q,r)
  t2 = dfs(k+1,p+l[k],q,r)+10
  t3 = dfs(k+1,p,q+l[k],r)+10
  t4 = dfs(k+1,p,q,r+l[k])+10
  return min(t1,t2,t3,t4)

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

Submission status: Wrong Answer

Solution code:
n,a,b,c=map(int,input().split())
l=sorted([int(input()) for i in range(n)])
def kad(w,x,y,z):
    if w==n:
        if min(x,y,z)>0:
            return(abs(x-a)+abs(y-b)+abs(z-c)-30)
        else:
            return a
    c0=kad(w+1,x,y,z)
    c1=kad(w+1,x+l[w],y,z)+10
    c2=kad(w+1,x,y+l[w],z)+10
    c3=kad(w+1,x,y,z+l[w])+10
    return min(c0,c1,c2,c3)

print(kad(0,0,0,0))

Submission status: Wrong Answer

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

def solve(w,x,y,z):
    if w==n:
        if min(x,y,z)>0:
            return abs(x-a)+abs(y-b)+abs(z-c)-30
        else:
            return 10e4
        can0=solve(w+1,x,y,z)
        can1=solve(w+1,x+l[w],y,z)+10
        can2=solve(w+1,x,y+l[w],z)+10
        can3=solve(w+1,x,y,z+l[w])+10
        return min(can0,can1,can2,can3)

print(solve(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)]

K = 4

minmp = 1e8
for x in range(123, pow(4, N)):
    v = x
    c = [0] * K
    s = [0] * K
    i = 0
    while v > 0:
        m = v % K
        c[m] += 1
        s[m] += L[i]
        i += 1
        v //= K

    if c[1] > 0 and c[2] > 0 and c[3] > 0:
        minmp = min(minmp, (c[1] + c[2] + c[3] - 3) * 10 + abs(s[1] - A) + abs(s[2] - B) + abs(s[3] - C))

print(minmp)

Submission status: Wrong Answer

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

res = float('inf')
    
l = [int(input()) for _ in range(n)]
for i in range(1, 4 ** n):
    j, x = 0, [a, b, c]
    ans = 0
    while i:
        k = i % 4
        if k: 
            x[k - 1] -= l[j]
            ans += 10
        j += 1
        i //= 4
    ans += sum(map(abs, x))
    res = min(res, ans)
print(res)

Submission status: Wrong Answer

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

l = [0 for _ in range(n)]

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


def dfs(count, a, b, c):
    if count == n:
        if a == 0 or b == 0 or c == 0:
            return 1000
        return abs(A - a) + abs(B - b) + abs(C - c) - 30
    else:
        return min(dfs(count + 1, a, b, c),
                   dfs(count + 1, a + l[count], b, c) + 10,
                   dfs(count + 1, a, b + l[count], c) + 10,
                   dfs(count + 1, a, b, c + l[count]) + 10,
                   )


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

Submission status: Wrong Answer

Solution code:
print(1)

Submission status: Wrong Answer

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

def dfs(count,x,y,z):
    if count == n:
        if a>0 and b>0 and c>0:
            return abs(x-a)+abs(y-b)+abs(z-c)-30 
        else:
            return 10^9
    ret0 = dfs(count+1,x,y,z)
    ret1 = dfs(count+1,x+L[count],y,z)+10
    ret2 = dfs(count+1,x,y+L[count],z)+10
    ret3 = dfs(count+1,x,y,z+L[count])+10
    
    return min(ret0,ret1,ret2,ret3)

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

Submission status: Wrong Answer

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

def dfs(count,x,y,z):
    if count == n:
        if a>0 and b>0 and c>0:
            return abs(x-a)+abs(y-b)+abs(z-c)-30 
        else:
            return 0
    ret0 = dfs(count+1,x,y,z)
    ret1 = dfs(count+1,x+L[count],y,z)+10
    ret2 = dfs(count+1,x,y+L[count],z)+10
    ret3 = dfs(count+1,x,y,z+L[count])+10
    
    return min(ret0,ret1,ret2,ret3)

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

Submission status: Wrong Answer

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

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())
L = [int(input()) for _ in range(N)]
INF = 1000


def dfs(lances, d, len_a, len_b, len_c):
    if d == N:
        if min(len_a, len_b, len_c) == 0:
            return INF
        else:
            return abs(A-len_a) + abs(B-len_b) + abs(C-len_c) - 30
    use_no = dfs(lances, d+1, len_a, len_b, len_c)
    use_for_a = dfs(lances, d+1, len_a + lances[d], len_b, len_c) + 10
    use_for_b = dfs(lances, d+1, len_a, len_b + lances[d], len_c) + 10
    use_for_c = dfs(lances, d+1, len_a, len_b, len_c + lances[d]) + 10
    return min(use_no, use_for_a, use_for_b, use_for_c)


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

Submission status: Wrong Answer

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


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) else 1000000000

    path_null = dfs(cur+1, a, b, c)
    path_a = dfs(cur+1, a+L[cur], b, c) + 10
    path_b = dfs(cur+1, a, b+L[cur], c) + 10
    path_c = dfs(cur+1, a, b, c+L[cur]) + 10

    return min(path_null, path_a, path_b, path_c)

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

Submission status: Wrong Answer

Solution code:
n, a, b, c = map(int, input().split())
l = sorted([int(input()) for i in range(n)])
ans = 10**10
for order in [[a, b, c],[b, c, a],[c, a, b],[a, c, b],[b, a, c],[c, b, a]]:
    mp = 0
    l_copy = l.copy()
    for goal in order:
        tem_mp = 10**10
        used = []
        for bit in range(1, 2 ** len(l_copy)):
            length = 0
            cost = 0
            count = 0
            tem_used = []
            for j in range(len(l_copy)):
                if (bit >> j) & 1:
                    length += l[j]
                    count += 1
                    if count > 1:
                        cost += 10
                    tem_used.append(j)
            if tem_mp  > abs(goal - length) + cost:
                used = tem_used
                tem_mp = abs(goal - length) + cost
        mp += tem_mp
        for u in used[::-1]:
            l_copy.pop(u)
    ans = min(ans, mp)

print(ans)

Submission status: Wrong Answer

Solution code:
n, a, b, c = map(int, input().split())
l = sorted([int(input()) for i in range(n)])
mp = 0
for goal in [c, a, b]: #条件が厳しい順に作っていく
    tem_mp = 10**10
    used = []
    for bit in range(1, 2 ** len(l)): #何も使わないものを除く
        length = 0
        cost = 0
        count = 0
        tem_used = []
        for j in range(len(l)):
            if (bit >> j) & 1:
                length += l[j]
                count += 1
                if count > 1:
                    cost += 10
                tem_used.append(j)
        if tem_mp  > abs(goal - length) + cost:
            used = tem_used
            tem_mp = abs(goal - length) + cost
    mp += tem_mp
    for u in used[::-1]:
        l.pop(u)

print(mp)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
from math import *
from itertools import *
from collections import *
from functools import *
try:
    from math import gcd
except Exception:
    from fractions import gcd


def solve(N: int, A: int, B: int, C: int, l: "List[int]"):
    e = [A, B, C]

    ret = float('inf')
    for i in range(4 ** N):
        ls = [0, 0, 0, 0]
        ns = [0, 0, 0, 0]
        for j, ll in enumerate(l):
            key = (i >> (2 * j)) & 0x03
            ls[key] += ll
            ns[key] += 1

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

        cs = []
        for n in ns:
            if 0 < n:
                cs.append(10 * int(ceil(log(n) / log(2))))
            else:
                cs.append(0)

        for i0, i1, i2 in permutations([0, 1, 2], 3):
            cc = cs[0] + abs(e[i0] - ls[0])
            cc += cs[1] + abs(e[i1] - ls[1])
            cc += cs[2] + abs(e[i2] - ls[2])
            if cc < ret:
                ret = cc
    return ret


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = int(next(tokens))  # type: int
    B = int(next(tokens))  # type: int
    C = int(next(tokens))  # type: int
    l = [ int(next(tokens)) for _ in range(N) ]  # type: "List[int]"
    result = solve(N, A, B, C, l)
    print(result)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
from math import *
from itertools import *
from collections import *
from functools import *
try:
    from math import gcd
except Exception:
    from fractions import gcd


def solve(N: int, A: int, B: int, C: int, l: "List[int]"):
    e = [A, B, C]

    ret = float('inf')
    for i in range(4 ** N):
        ls = [0, 0, 0, 0]
        ns = [0, 0, 0, 0]
        for j, ll in enumerate(l):
            key = (i >> (2 * j)) & 0x03
            ls[key] += ll
            ns[key] += 1

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

        cs = []
        for n in ns:
            if 0 < n:
                cs.append(10 * int(ceil(log(n) / log(2))))
            else:
                cs.append(0)

        c0 =  cs[0] + abs(e[0] - ls[0])
        c1 = c0
        c1 += cs[1] + abs(e[1] - ls[1])
        c1 += cs[2] + abs(e[2] - ls[2])
        if c1 < ret:
            ret = c1

        c2 = c0
        c2 += cs[1] + abs(e[2] - ls[1])
        c2 += cs[2] + abs(e[1] - ls[2])
        if c2 < ret:
            ret = c2
    return ret


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = int(next(tokens))  # type: int
    B = int(next(tokens))  # type: int
    C = int(next(tokens))  # type: int
    l = [ int(next(tokens)) for _ in range(N) ]  # type: "List[int]"
    result = solve(N, A, B, C, l)
    print(result)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
## 間違ってるけどコード保存用

import numpy as np
from copy import deepcopy

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

L = np.array(L)
max_merge = N-3
#min_MP = float('Inf')

## 合成なし増減させるのみで達成されるうちの最小MPを出力
## これだと自明に違うとわかった...
def bamboo2MP_nomerge(bamboo, A, B, C):
  arr = deepcopy(bamboo)
  n_bamboo = len(arr)
  bamboo_A = np.argmin(np.abs(arr-A))
  MP_A = np.abs(arr[bamboo_A]-A)
  np.delete(arr, MP_A)
  bamboo_B = np.argmin(np.abs(arr-B))
  MP_B = np.abs(arr[bamboo_B]-B)
  np.delete(arr, MP_B)
  bamboo_C = np.argmin(np.abs(arr-C))
  MP_C = np.abs(arr[bamboo_C]-C)
  #np.delete(arr, MP_C)
  return int(MP_A + MP_B + MP_C)
  

## 合成0回の時の最小MPを取得
min_MP = bamboo2MP_nomerge(L, A, B, C)
  
## 合成ありの時の最小MPを取得  
#for i in range(max_merge):
  
print(min_MP)

Submission status: Wrong Answer

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

#lst.sort(reverse =True) #大きいものが先に来ている

INF = 10 ** 9

def dfs(count, a, b, c): #count: 今回考える棒の番号, a, (b, c): A, (B,C) の棒を作るのに使う棒の長さの合計
    if count == N: #最後まで各竹をa, b, cに割り振り終わったら…
        if min(a,b,c) < 0: #A, B, C用の竹が少なくともそれぞれ1本以上必要、それが満たされていないとき
            return INF #とりあえず大きな数を返しとく
        else:
            return abs(a-A)+abs(b-B)+abs(c-C)-30 
        #使う棒の合計の長さ(a, b, c)と目標とする長さ(A, B, C)の差の絶対値-各棒1本目のつなぐのに必要なMP(10 * 3)を除く
    ret0 = dfs(count+1, a, b, c) #count番目の竹を使わない-->countのみ1つ進める
    ret1 = dfs(count+1, a+lst[count], b, c) + 10 #count番目の竹をAに使う。つなぐ文、必要なMPを+10する
    ret2 = dfs(count+1, a, b+lst[count], c) + 10 #count番目の竹をBに使う。つなぐ文、必要なMPを+10する
    ret3 = dfs(count+1, a, b, c+lst[count]) + 10 #count番目の竹をCに使う。つなぐ文、必要なMPを+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())
bb = [int(input()) for i in range(n)]
res = []
for j in range (4**n-1) :
  t = str(bin(j))
  tt = t[2:]
  ttt = "0" * (2*n-len(tt)) + tt
  tttt = tuple(ttt)
  ttttt = tuple(map (int,tttt))
  mp = 0
  ans = 10000000
  lg = [0,0,0,0]
  for take in range(n) :
      use = ttttt[take*2]*2 + ttttt[take*2+1]
      mp += 10
      if lg[use] == 0:
          mp -= 10
      lg[use] += bb[take]
      lg[3] = 0 
  mp += abs(a-lg[0])
  mp += abs(b-lg[1])
  mp += abs(c-lg[2])
  if ans > mp :
      ans = mp
  res.append(ans)
print (min(res))

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**9)

def search(arr,arr2,cost,flag):
  global mincost
  if cost>mincost:
    return 0
  if flag==0:
    if len(arr2)==0:
      ans.add(cost)
      if mincost>cost:
        mincost=cost
      return 0
    tmp=10**9
    tpos=0
    for i in range(len(arr)):
      if tmp>cost+abs(arr2[0]-arr[i]):
        tmp=cost+abs(arr2[0]-arr[i])
        tpos=i
    search(arr[:tpos]+arr[tpos+1:],arr2[1:],tmp,0)
  else:
    if len(arr)>=len(arr2)+1:
      for i in range(len(arr)):
        for j in range(i+1,len(arr)):
          search(arr[:i]+arr[i+1:j]+arr[j+1:]+[arr[i]+arr[j]],arr2,cost+10,0)
          search(arr[:i]+arr[i+1:j]+arr[j+1:]+[arr[i]+arr[j]],arr2,cost+10,1)

n,a,b,c=map(int,input().split())
arr=[int(input()) for _ in range(n)]
arr2=[a,b,c]
ans=set()
mincost=10**9

search(arr,arr2,0,0)
search(arr,arr2,0,1)

ans=sorted(list(ans))
print(ans[0])

Submission status: Wrong Answer

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

def dfs(num, a, b, c):
    if N == num:
        print(a, b, c)
        return abs(A-a)+abs(B-b)+abs(C-c) - 30 if min(a,b,c)>=1 else 10**10
    ret = []
    ret.append(dfs(num+1, a, b, c))
    ret.append(dfs(num+1, a+l[num], b, c)+10)
    ret.append(dfs(num+1, a, b+l[num], c)+10)
    ret.append(dfs(num+1, a, b, c+l[num])+10)
    return min(ret)

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)]
#L1～LNはL[0]~L[N-1]で取り出す。よってこれから竹1~Nのことを竹0~N-1と呼ぶ。

#作られるべきA,B,Cが自らの材料として選んだ竹を、集合memA, memB, memCに格納？

#メンバーの選び方[0]~[255]の「[合計初期サイズ, 本数]」を格納するリスト
initialSum = []
for i in range(2**N): #全てのフラグパターンについて計算する
    nowsum = 0
    nowbamboocnt = 0
    for j in range(N): #2^(j+1)乗で割った余りが2^j以上なら、フラグjがTrue
        if i % (2**(j+1)) >= 2**j:
            nowbamboocnt += 1
            nowsum += L[j] #j(0-N-1)本目の竹について見ているので、L[j]が今見ている竹の長さ
    initialSum.append([nowsum, nowbamboocnt])

#テスト用→ print(initialSum)

#A,B,Cが竹を選んだ場合の所要MPを出し、最低値とその時の組み合わせをminに格納する
bambooset = {i for i in range(N)} #存在する竹の組み合わせ
min = 3000 #最低値の初期化

lestbamboo = bambooset.copy() #残っている竹

for i, initSumA in enumerate(initialSum): #i:0~255。initsumA[0]は初期合計サイズ, initsumA[1]は本数
#Aはこのfor文で既に選ばれていると考えればいい。
    if initSumA[1] ==0 or N - initSumA[1] < 2: continue #竹が0とか、残り竹が0,1なら飛ばす
    lestbamboo = bambooset.copy() #再初期化
    for j in range(N): #使った竹はlestbambooからremove
        if i % (2**(j+1)) >= 2**j: #j(0-N-1)本目の竹について考えている
            lestbamboo.remove(j)
    necesMPforA = abs(A - initSumA[0]) + (initSumA[1]-1)*10 #abs(目標-合計)-合成必要MP

    #次はBを計算する。Bが取らなかったものの中からCが取る
    for b in range(len(lestbamboo)**2): #残った竹それぞれにつき取るか取らないか選ぶ
    #2*残り本数通りある。ex.3本残っているとして、b(000とか101とか)で、Bの選び方を示す
        lestbambooList = list(lestbamboo) #リスト化する [0]~[本数-1]。値は竹番号[0-N]
        #lestbambooListは自動的に昇順になっている
        bamboocntB = 0 #Bの本数
        nowsumB = 0
        lestbambooforC = lestbamboo.copy() #Cのための残り竹を入れる
        for k in range(len(lestbambooList)): #2^(k+1)乗で割った余りが2^k以上なら、フラグkがTrue
            if b % (2**(k+1)) >= 2**k: #Bがとる場合
                bamboocntB += 1
                nowsumB += lestbambooList[k] #竹番号はlestbambooList[k]に入っている
                lestbambooforC.remove(lestbambooList[k])
        if bamboocntB == 0: continue #0本だと駄目なので飛ばす

        for m in range(len(lestbambooforC)**2): #残った竹それぞれにつき取るか取らないか
        #このfor文終了1回につき、ABCそれぞれの組み合わせが1つ終了！
        # →このループの終了時にminの判定を行う
            lestbambooforCList = list(lestbambooforC) #やっぱりリスト化
            bamboocntC = 0
            nowsumC = 0
            for n in range(len(lestbambooforCList)):
                if m % (2**(n+1)) >= 2**n: #Cがとる場合
                    bamboocntC += 1
                    nowsumC += lestbambooforCList[n] #竹番号はlestbambooListforC[n]に入っている
            if bamboocntC == 0: continue #0本だと駄目なので飛ばす

            nowTotalSum = necesMPforA + abs(B - nowsumB) + (bamboocntB-1)*10 + abs(C - nowsumC) + (bamboocntC-1)*10
            #minの判定！
            if nowTotalSum < min: min = nowTotalSum

print(min)

Submission status: Wrong Answer

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

def dfs(i, a, b, c):
    if i == N:
        return abs(A - a) + abs(B - b) + abs(C - c) - 30
    
    tmp0 = dfs(i + 1, a, b, c)
    tmpa = dfs(i + 1, a + L[i], b, c) + 10
    tmpb = dfs(i + 1, a, b + L[i], c) + 10
    tmpc = dfs(i + 1, a, b, c + L[i]) + 10
    ans = min(tmp0, tmpa, tmpb, tmpc)
    
    return ans

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

Submission status: Wrong Answer

Solution code:
def rec (i,a,b,c):
    if i==N:
        if a==0 and b==0 and c==0:
            return INF
        else:
            return abs(a-A)+abs(b-B)+abs(c-C)
    res = rec(i+1,a,b,c)
    
    res = min(res,rec(i+1,a+L[i],b,c)+(10 if a>0 else 0))
    res = min(res,rec(i+1,a,b+L[i],c)+(10 if b>0 else 0))
    res = min(res,rec(i+1,a,b,c+L[i])+(10 if c>0 else 0))
    
    return res
N,A,B,C = list(map(int,input().split()))
INF=10**8
L = [int(input()) for _ in range(N)]
print(rec(0,0,0,0))

Submission status: Wrong Answer

Solution code:
from itertools import*
import numpy as np
n,a,b,c,*l=map(int,open(0).read().split())
d=np.array([a,b,c])
l=np.array(l)
ansl=[]
for s in product(range(4),repeat=n):
    if sum(i!=0for i in s)<3:
        continue
    else:
        ans=0
        s=np.array(s)
        x=s==1
        y=s==2
        z=s==3
        ans+=sum(abs(np.array([sum(l*x),sum(l*y),sum(l*z)])-d))+((sum(x)-1)+(sum(y)-1)+(sum(z)-1))*10
        ansl.append(ans)
print(min(ansl))

Submission status: Wrong Answer

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

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

ans = 2 ** 20 -1
for i in range(N):
    for j in range(N):
        if i == j: continue
        for k in range(N):
            if i == k or j == k : continue
        
            x,y,z = l[i],l[j],l[k]
            cnt = 0
            
            for t in range(N):
                if t == i or t == j or t == k : continue

                if l[t] > 10:
                    cnt += l[t]
                    x += l[t]
                if l[t] > 10:
                    cnt += l[t]
                    y += l[t]
                if l[t] > 10:
                    cnt += l[t]
                    z += l[t]
            cnt += abs(x-A) 
            cnt += abs(y-B) 
            cnt += abs(z-C) 
            ans = min(ans,cnt)
                
                
print(ans)

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

Solution code:
import itertools
import numpy as np

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

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

min_loss = 9999999999999999
for pat in itertools.product(["A", "B", "C", "D"], repeat=N):
    if pat.count("A") > 0 and pat.count("B") > 0 and pat.count("C") > 0:
        # pat = np.array(pat)
        pat_A = l[[p == "A" for p in pat]]
        pat_B = l[[p == "B" for p in pat]]
        pat_C = l[[p == "C" for p in pat]]

        loss_A = abs(A-pat_A.sum())+10*(len(pat_A)-1)
        loss_B = abs(B-pat_B.sum())+10*(len(pat_B)-1)
        loss_C = abs(C-pat_C.sum())+10*(len(pat_C)-1)

        loss = loss_A+loss_B+loss_C
        min_loss = min(min_loss, loss)

print(min_loss)

Submission status: Wrong Answer

Solution code:
from itertools import product
n,a,b,c = map(int,input().split())
l = [int(input()) for i in range(n)]
x = list(product([0, 1, 2, 3], repeat=n))
ans = 10**10
for i in range(len(x)):
    tmpa = 0
    tmpb = 0
    tmpc = 0
    for j in range(n):
        if x[i][j] == 1:
            tmpa += l[j]
        if x[i][j] == 2:
            tmpb += l[j]
        if x[i][j] == 3:
            tmpc += l[j]
    tmpans = abs(a-tmpa)+abs(b-tmpb)+abs(c-tmpc)
    tmpans += (x[i].count(1)-1)*10 + (x[i].count(2)-1)*10 + (x[i].count(3)-1)*10
    ans = min(ans,tmpans)
print(ans)

Submission status: Wrong Answer

Solution code:
N, A, B, C = map(int, input().split(' '))
L = [int(input()) for i in range(N)]
# 上限
MAX = 10 ** 9
#再帰関数
def dfs(cur, a, b, c):
  if cur == N:
    return abs(a - A) + abs(b - B) + abs(c - C) - 30 if min(a, b, c) > 0 else MAX
  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
  print(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()))
ls = [int(input()) for _ in range(n)]

min_ans = 10 ** 10
for i in range(4 ** n):
	mm = [[], [], [],[]]
	tmp = i
	for i2 in range(n):
		g = tmp // (4 ** (n - i2 - 1))
		tmp = tmp % (4 ** (n - i2 - 1))
		mm[g%4].append(ls[i2])
	
	if len(mm[0]) * len(mm[1]) * len(mm[2]) == 0:
		continue

	print(mm)
	ans = abs(sum(mm[0]) - a) + abs(sum(mm[1]) - b) + abs(sum(mm[2]) - c) + (len(mm[0]) - 1) * 10 + (len(mm[1]) - 1) * 10 + (len(mm[2]) - 1) * 10
	min_ans = min(ans, min_ans)

print(min_ans)

Submission status: Wrong Answer

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

min_ans = 10 ** 10
for i in range(4 ** n):
	mm = [[], [], [],[]]
	tmp = i
	for i2 in range(n):
		g = tmp // (4 ** (n - i2))
		tmp = tmp % (4 ** (n - i2))
		mm[g%4].append(ls[i2])
	
	if len(mm[0]) * len(mm[1]) * len(mm[2]) == 0:
		continue

	ans = abs(sum(mm[0]) - a) + abs(sum(mm[1]) - b) + abs(sum(mm[2]) - c) + (len(mm[0]) - 1) * 10 + (len(mm[1]) - 1) * 10 + (len(mm[2]) - 1) * 10
	min_ans = min(ans, min_ans)

print(min_ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys


def solve(N: int, A: int, B: int, C: int, l: "List[int]"):
    result = 10000000000
    for i in range(2**(N*2) -1):
        bitBase = i | 2**(N*2)
        debug = False
        # if i == 4626:
        #     debug = True
        mergeCost = 0
        a = 0
        b = 0
        c = 0
        debugStr = ""
        for j in range(N):
            flag = ((3 << (j * 2)) & bitBase) >> (j * 2)
            if flag == 1:
                if a != 0:
                    mergeCost+= 10
                if debug:
                    debugStr += "A"
                a += l[j]
            elif flag == 2:
                if b != 0:
                    mergeCost += 10
                if debug:
                    debugStr += "B"
                b += l[j]
            elif flag == 3:
                if c != 0:
                    mergeCost += 10 
                if debug:
                    debugStr += "C"               
                c+= l[j]
            else:
                if debug:
                    debugStr += "X"
        cost = mergeCost + abs(A-a) + abs(B - b) + abs(C - c)
        if debug:
            print(debugStr)
        # if cost == 120:
        #     print(i)
        result = min(cost,result)
    print(result)
    return


# Generated by 1.1.3 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = int(next(tokens))  # type: int
    B = int(next(tokens))  # type: int
    C = int(next(tokens))  # type: int
    l = [ int(next(tokens)) for _ in range(N) ]  # type: "List[int]"
    solve(N, A, B, C, l)

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)]
inf=10**9

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

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