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:
# -*- coding: utf-8 -*-

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
#from math import gcd
import bisect
from collections import defaultdict
from collections import deque
from functools import lru_cache


#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')

#############
# Functions #
#############

######INPUT######
def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]

######OUTPUT######
def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()

#####Shorten#####
def DD(arg): return defaultdict(arg)


#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1:
    arr.append([temp, 1])
  if arr==[]:
    arr.append([n, 1])
  return arr

#####MakeDivisors######
def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors

#####MakePrimes######
def make_primes(N):
  max = int(math.sqrt(N))
  seachList = [i for i in range(2,N+1)]
  primeNum = []
  while seachList[0] <= max:
    primeNum.append(seachList[0])
    tmp = seachList[0]
    seachList = [i for i in seachList if i % tmp != 0]
  primeNum.extend(seachList)
  return primeNum

#####GCD#####
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#####BitCount#####
def count_bit(n):
  count = 0
  while n:
    n &= n -1
    count += 1
  return count

#####ChangeBase#####
def base_10_to_n(X, n):
  if X//n:
    return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i-1])*n**i for i in range(len(str(X))))

#####IntLog#####
def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count

#############
# Main Code #
#############
N,A,B,C = IL()
L = ILs(N)

def make_take(T,x,b):
  n = len(T)
  Tset = set(T)
  if n == 0:
    return INF,T
  choose = 0
  number = 0
  for i in range(n):
    if (b>>i)&1 == 1:
      Tset -= {T[i]}
      choose += T[i]
      number += 1
  MP = abs(choose-x)+(number-1)*10
  return MP,list(Tset)

MIN = INF
n1 = len(L)
for b1 in range(1,1<<n1):
  MP1,L1 = make_take(L,A,b1)
  n2 = len(L1)
  for b2 in range(1,1<<n2):
    MP2,L2 = make_take(L1,B,b2)
    n3 = len(L2)
    for b3 in range(1,1<<n3):
      MP3,L3 = make_take(L2,C,b3)
      MIN = min(MIN,MP1+MP2+MP3)
print(MIN)

Submission status: Wrong Answer

Solution code:
def dfs(i,a,b,c):
  if i == N:
    if a == 0 or b == 0 or c == 0:
      return float('inf')
    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

N, A, B, C = list(map(int,input().split()))
l = [0] * N
for i in range(N):
  l[i] = int(input())
  
print(dfs(0,0,0,0))

Submission status: Wrong Answer

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


def main():
    N, A, B, C = map(int, input().split())
    l = [int(input()) for _ in range(N)]
    ans = 10**9
    for i in range(4**N):
        # N本の竹がA,B,Cのどれに使われるか
        cost = [[] for _ in range(3)]
        tmp = i
        j = 0
        while tmp > 0:
            if tmp % 4 < 3:
                cost[tmp % 4].append(l[j])
            j += 1
            tmp //= 4

        if len(cost[0]) == 0 or len(cost[1]) == 0 or len(cost[2]) == 0:
            continue

        tmpa = 0
        D = [A, B, C]
        # MPの計算
        for i in range(3):
            tmpa += abs(sum(cost[i]) - D[i]) + 10*(len(cost[i])-1)
        ans = min(ans, tmpa)

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from itertools import product

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

ans = 10**18
for state in product(range(4), repeat=N):
    K = [[] for _ in range(4)]
    for i, l in enumerate(L):
        K[state[i]].append(l)

    cost = 0
    for d, D in zip((A, B, C), K):
        cost += (len(D) - 1) * 10
        cost += abs(d - sum(D))
    ans = min(ans, cost)

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
from copy import deepcopy
def input(): return sys.stdin.readline().strip()

def func(n): # 和がnになるような３つの数字の組を全列挙
    arr = []
    for i in range(1, n - 1):
        for j in range(1, n - i):
            arr.append((i, j, (n - i - j)))
    return arr

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

    """
    どの竹をくっつけるかをビット全探索
    """

    ans = 10**100
    for n in range(1 << N):
        bumboo_num = [i for i in range(N) if n & (1 << i)]
        l = len(bumboo_num)
        for p, q, r in func(l):
            near_a = 0
            near_b = 0
            near_c = 0
            for i in range(p): near_a += L[bumboo_num[i]]
            for i in range(p, p + q): near_b += L[bumboo_num[i]]
            for i in range(p + q, l): near_c += L[bumboo_num[i]]
            val = 10 * (max(0, p - 1) + max(0, q - 1) + max(0, r - 1))
            val += abs(A - near_a) + abs(B - near_b) + abs(C - near_c)
            #if ans > val: print("n={}, p={}, q={}, r={}".format(n, p, q, r))
            ans = min(ans, val)
    print(ans)




if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n, a, b, c = map(int, input().split())
l = [int(input()) for _ in range(n)]
INF = 10*9+7


def dfs(pos, a_tmp, b_tmp, c_tmp):
    if pos == n:
        if min(a_tmp, b_tmp, c_tmp) > 0:
            return abs(a-a_tmp)+abs(b-b_tmp)+abs(c-c_tmp)-30
        else:
            return INF
    res0 = dfs(pos+1, a_tmp, b_tmp, c_tmp)
    res1 = dfs(pos+1, a_tmp+l[pos], b_tmp, c_tmp)+10
    res2 = dfs(pos+1, a_tmp, b_tmp+l[pos], c_tmp)+10
    res3 = dfs(pos+1, a_tmp, b_tmp, c_tmp+l[pos])+10
    return min(res0, res1, res2, res3)


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 = 10*9+7


def dfs(pos, a_tmp, b_tmp, c_tmp):
    if pos == n:
        if min(a_tmp, b_tmp, c_tmp) > 0:
            return abs(a-a_tmp)+abs(b ^ b_tmp)+abs(c-c_tmp)-30
        else:
            return INF
    res0 = dfs(pos+1, a_tmp, b_tmp, c_tmp)
    res1 = dfs(pos+1, a_tmp+l[pos], b_tmp, c_tmp)+10
    res2 = dfs(pos+1, a_tmp, b_tmp+l[pos], c_tmp)+10
    res3 = dfs(pos+1, a_tmp, b_tmp, c_tmp+l[pos])+10
    return min(res0, res1, res2, res3)


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

Submission status: Wrong Answer

Solution code:
import itertools
N, A, B, C = map(int, input().split())
ln = [int(input()) for _ in range(N)]
bit = itertools.product([0, 1, 2, 3], repeat=N)
ans = 1000
for bi in bit:
    cnt = 0
    a = []
    b = []
    c = []
    for i in range(N):
        if bi[i] == 1:
            a.append(ln[i])
        elif bi[i] == 2:
            b.append(ln[i])
        elif bi[i] == 3:
            c.append(ln[i])
    if len(a) == 0 or len(b) == 0 or len(c) == 0:
        continue
    cnt += 10*(max(len(a)-1, 0)+max(len(b)-1, 0)+max(len(c)-1, 0))
    temp = [sum(a), sum(b), sum(c)]
    temp.sort()
    cnt += abs(temp[0]-C) + abs(temp[1]-B) + abs(temp[2]-A)
    ans = min(ans, cnt)
print(ans)

Submission status: Wrong Answer

Solution code:
def min_mp(bls, A, B, C, a_used=False, b_used=False, c_used=False):
    if sum([a_used, b_used, c_used]) == 3:
        return 0
    else:
        min_val = 10**9  # large number
        if len(bls) - 1 >= sum([a_used, b_used, c_used]):
            min_val = min([
                min_val,
                min_mp(bls[1:], A, B, C, a_used, b_used, c_used)
            ])
        if len(bls) > 0:
            if not a_used:
                min_val = min([
                    min_val,
                    abs(bls[0] - A) + min_mp(bls[1:], A, B, C, not a_used, b_used, c_used),
                    10 + min_mp(bls[1:], A - bls[0], B, C, a_used, b_used, c_used)
                ])
            if not b_used:
                min_val = min([
                    min_val,
                    abs(bls[0] - B) + min_mp(bls[1:], A, B, C, a_used, not b_used, c_used),
                    10 + min_mp(bls[1:], A, B - bls[0], C, a_used, b_used, c_used)
                ])
            if not c_used:
                min_val = min([
                    min_val,
                    abs(bls[0] - C) + min_mp(bls[1:], A, B, C, a_used, b_used, not c_used),
                    10 + min_mp(bls[1:], A, B, C - bls[0], a_used, b_used, c_used)
                ])
        return min_val


def main():
    N, A, B, C = list(map(int, input().split(' ')))
    bls = [int(input()) for _ in range(N)]  # bamboo length
    print(min_mp(bls, A, B, C))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def min_mp(bls, A, B, C, a_used=False, b_used=False, c_used=False):
    if sum([a_used, b_used, c_used]) == 3:
        return 0
    else:
        min_val = 10**9  # large number
        if len(bls) - 1 >= sum([a_used, b_used, c_used]):
            min_val = min([
                min_val,
                min_mp(bls[1:], A, B, C, a_used, b_used, c_used),
                10 + min_mp(bls[1:], A - bls[0], B, C, a_used, b_used, c_used),
                10 + min_mp(bls[1:], A, B - bls[0], C, a_used, b_used, c_used),
                10 + min_mp(bls[1:], A, B, C - bls[0], a_used, b_used, c_used)
            ])
        if len(bls) > 0:
            if not a_used:
                min_val = min([min_val, abs(bls[0] - A) + min_mp(bls[1:], A, B, C, not a_used, b_used, c_used)])
            if not b_used:
                min_val = min([min_val, abs(bls[0] - B) + min_mp(bls[1:], A, B, C, a_used, not b_used, c_used)])
            if not c_used:
                min_val = min([min_val, abs(bls[0] - C) + min_mp(bls[1:], A, B, C, a_used, b_used, not c_used)])
        return min_val


def main():
    N, A, B, C = list(map(int, input().split(' ')))
    bls = [int(input()) for _ in range(N)]  # bamboo length
    print(min_mp(bls, A, B, C))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

res2=1000000000

for i in range(2**n-1):
	for j in range(2**n-1):
		x=[]
		y=[]
		z=[]
		for k in range(n):
			
			if i>>k&1:
				if j>>k&1:
					x.append(L[k])
				else:
					y.append(L[k])
			else:
				if j>>k&1:
					z.append(L[k])
			res1=abs(sum(x)-a)+abs(sum(y)-b)+abs(sum(z)-c)
			
			if len(x)>1:
				res1+=10*(len(x)-1)
			if len(y)>1:
				res1+=10*(len(y)-1)
			if len(z)>1:
				res1+=10*(len(z)-1)				
			
			res2=min(res1,res2)

print(res2)

Submission status: Wrong Answer

Solution code:
n, a, b, c = map(int, input().split())
l = []
for i in range(n):
    l.append(int(input()))
from itertools import product
bamboo = [0, a, b, c]
min_num = float('inf')
for i in product([0, 1, 2, 3], repeat=8):
    if set(i) >= set([1, 2, 3]):
        sums = [0, 0, 0, 0]
        tmp = -30
        for j in range(n):
            if i[j] != 0:
                sums[i[j]] += l[j]
                tmp += 10
        min_num = min(min_num,tmp+sum(abs(sums[k] - bamboo[k]) for k in range(1, 4)))
print(min_num)

Submission status: Wrong Answer

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

def dfs(i, cnt, a, b, c):
    if i == N:
        if a == 0 or b == 0 or c == 0:
            return INF
        return abs(a - A) + abs(b - B) + abs(c - C) + 10 * cnt
    MP = INF
    if a == 0:
        MP = min(MP, dfs(i+1, cnt, a+L[i], b, c))
    else:
        MP = min(MP, dfs(i+1, cnt+1, a+L[i], b, c))
    if b == 0:
        MP = min(MP, dfs(i+1, cnt, a, b+L[i], c))
    else:
        MP = min(MP, dfs(i+1, cnt+1, a, b+L[i], c))
    if c == 0:
        MP = min(MP, dfs(i+1, cnt, a, b, c+L[i]))
    else:
        MP = min(MP, dfs(i+1, cnt+1, a, b, c+L[i]))
    return MP

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

Submission status: Wrong Answer

Solution code:
import math
import sys
sys.setrecursionlimit(1000000)
N, A, B, C = 5, 100, 90, 80
ARR = [
    98,
    40,
    30,
    21,
    80
]

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


def dfs(deep, crr):
    if deep == N:
        ok, res = calculate(crr)
        if ok:
            result.append(int(res))
    else:
        for i in range(4):
            crr[deep] = i
            dfs(deep + 1, crr)


def calculate(crr):
    sA = set()
    sB = set()
    sC = set()
    sN = set()
    for index, cr in enumerate(crr):
        if cr == 0:
            sN.add(ARR[index])
        if cr == 1:
            sA.add(ARR[index])
        if cr == 2:
            sB.add(ARR[index])
        if cr == 3:
            sC.add(ARR[index])

    if len(sN) == N:
        return False,-1
    if len(sA) == 0:
        return False, -1
    if len(sB) == 0:
        return False, -1
    if len(sC) == 0:
        return False, -1


    s1 = (len(sA) - 1) * 10 + math.fabs(sum(sA) - A)
    s2 = (len(sB) - 1) * 10 + math.fabs(sum(sB) - B)
    s3 = (len(sC) - 1) * 10 + math.fabs(sum(sC) - C)

    return True, s1 + s2 + s3


dfs(0, [0 for i in range(N)])

print(min(result))

Submission status: Wrong Answer

Solution code:
import math

N, A, B, C = 5, 100, 90, 80
ARR = [
    98,
    40,
    30,
    21,
    80
]

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


def dfs(deep, crr):
    if deep == N:
        ok, res = calculate(crr)
        if ok:
            result.append(int(res))
    else:
        for i in range(4):
            crr[deep] = i
            dfs(deep + 1, crr)


def calculate(crr):
    sA = set()
    sB = set()
    sC = set()
    sN = set()
    for index, cr in enumerate(crr):
        if cr == 0:
            sN.add(ARR[index])
        if cr == 1:
            sA.add(ARR[index])
        if cr == 2:
            sB.add(ARR[index])
        if cr == 3:
            sC.add(ARR[index])

    if len(sN) == N:
        return False,-1
    if len(sA) == 0:
        return False, -1
    if len(sB) == 0:
        return False, -1
    if len(sC) == 0:
        return False, -1


    s1 = (len(sA) - 1) * 10 + math.fabs(sum(sA) - A)
    s2 = (len(sB) - 1) * 10 + math.fabs(sum(sB) - B)
    s3 = (len(sC) - 1) * 10 + math.fabs(sum(sC) - C)

    return True, s1 + s2 + s3


dfs(0, [0 for i in range(N)])

print(min(result))

Submission status: Wrong Answer

Solution code:
import math

N, A, B, C = 5, 100, 90, 80
ARR = [
    98,
    40,
    30,
    21,
    80
]

# N, A, B, C = map(int, input().split())
# ARR = []
# for i in range(N):
#     ARR.append(int(input()))
result = []


def dfs(deep, crr):
    if deep == N:
        ok, res = calculate(crr)
        if ok:
            result.append(int(res))
    else:
        for i in range(4):
            crr[deep] = i
            dfs(deep + 1, crr)


def calculate(crr):
    sA = set()
    sB = set()
    sC = set()
    sN = set()
    for index, cr in enumerate(crr):
        if cr == 0:
            sN.add(ARR[index])
        if cr == 1:
            sA.add(ARR[index])
        if cr == 2:
            sB.add(ARR[index])
        if cr == 3:
            sC.add(ARR[index])

    if len(sN) == N:
        return False,-1
    if len(sA) == 0:
        return False, -1
    if len(sB) == 0:
        return False, -1
    if len(sC) == 0:
        return False, -1


    s1 = (len(sA) - 1) * 10 + math.fabs(sum(sA) - A)
    s2 = (len(sB) - 1) * 10 + math.fabs(sum(sB) - B)
    s3 = (len(sC) - 1) * 10 + math.fabs(sum(sC) - C)

    return True, s1 + s2 + s3


dfs(0, [0 for i in range(N)])

print(min(result))

Submission status: Wrong Answer

Solution code:
import math

N, A, B, C = 5, 100, 90, 80
ARR = [
    98,
    40,
    30,
    21,
    80
]

# N, A, B, C = map(int, input().split())
# ARR = []
# for i in range(N):
#     ARR.append(int(input()))
result = []


def dfs(deep, crr):
    if deep == N:
        ok, res = calculate(crr)
        if ok:
            result.append(int(res))
    else:
        for i in range(4):
            crr[deep] = i
            dfs(deep + 1, crr)


def calculate(crr):
    sA = set()
    sB = set()
    sC = set()
    sN = set()
    for index, cr in enumerate(crr):
        if cr == 0:
            sN.add(ARR[index])
        if cr == 1:
            sA.add(ARR[index])
        if cr == 2:
            sB.add(ARR[index])
        if cr == 3:
            sC.add(ARR[index])

    if len(sN) == N:
        return False,-1
    if len(sA) == 0:
        return False, -1
    if len(sB) == 0:
        return False, -1
    if len(sC) == 0:
        return False, -1

    print(crr)

    s1 = (len(sA) - 1) * 10 + math.fabs(sum(sA) - A)
    s2 = (len(sB) - 1) * 10 + math.fabs(sum(sB) - B)
    s3 = (len(sC) - 1) * 10 + math.fabs(sum(sC) - C)

    return True, s1 + s2 + s3


dfs(0, [0 for i in range(N)])

print(min(result))

Submission status: Wrong Answer

Solution code:
import math

N, A, B, C = 5, 100, 90, 80
ARR = [
    98,
    40,
    30,
    21,
    80
]

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

result = []

def dfs(deep, crr):
    if deep == N:
        ok,res = calculate(crr)
        if ok:
            result.append(int(res))
    else:
        for i in range(4):
            crr[deep] = i
            dfs(deep + 1, crr)


def calculate(crr):
    sA = set()
    sB = set()
    sC = set()
    sN = set()
    for index, cr in enumerate(crr):
        if cr == 0:
            sN.add(ARR[index])
        if cr == 1:
            sA.add(ARR[index])
        if cr == 2:
            sB.add(ARR[index])
        if cr == 3:
            sC.add(ARR[index])

    if len(sA) == 0:
        return False, -1
    if len(sB) == 0:
        return False, -1
    if len(sC) == 0:
        return False, -1

    s1 = (len(sA) - 1) * 10 + math.fabs(sum(sA) - A)
    s2 = (len(sB) - 1) * 10 + math.fabs(sum(sB) - B)
    s3 = (len(sC) - 1) * 10 + math.fabs(sum(sC) - C)

    return True, s1 + s2 + s3


dfs(0, [0 for i in range(N)])

print(min(result))

Submission status: Wrong Answer

Solution code:
from sys import setrecursionlimit
setrecursionlimit(10 ** 7)

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

def Base_10_to_n(X, n):
    if (int(X/n)):
        return Base_10_to_n(int(X/n), n)+str(X%n)
    return str(X%n)

min_mp = 10**10

for i in range(4**n):
    mp = 0
    li = [0]*4
    count_li = [0]*4
    s = Base_10_to_n(i,4).zfill(n)
    for j,k in enumerate(s):
        li[int(k)] += l[j]
        count_li[int(k)] += 1
    if 0 in li[:-1]:
        continue
    mp += abs(a-li[0])
    mp += abs(b-li[1])
    mp += abs(c-li[2])
    for item in count_li:
        if item >= 2:
            mp += 10*(item-1)
    min_mp = min(min_mp,mp)
    
print(min_mp)

Submission status: Wrong Answer

Solution code:
n, *abc = [int(x) for x in input().split()]
l = [int(input()) for i in range(n)]
A = 0; B = 1; C = 2;

minmp = 10 ** 9
for k in range(2 ** (2 * n)):
    mat = [[], [], [], []]  # mat[0]:= Aの材料, mat[1]:=Bの材料, mat[2]:=Cの材料
    # for i = 0, ..., N-1
    # kの 2*i & 2*i+1ビット目が 0b00: l[i]はAに使う
    #     ↑lsbから             0b01: l[i]はBに使う
    #                         0b10: l[i]はCに使う
    #                         0b11: l[i]は使わない
    for i in range(1, n):
        mat[k & (0b11)].append(l[i])
        k >>= 2

    if len(mat[0]) == 0 or len(mat[1]) == 0 or len(mat[2]) == 0:
        continue

    mp = 0
    for j in range(3):
        mp += 10 * (len(mat[j]) - 1)
        mp += 1 * abs(abc[j] - sum(mat[j]))

    minmp = min(minmp, mp)

print(minmp)

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

# idx: current index
# a: length of bamboo for A
# b: length of bamboo for B
# c: length of bamboo for C
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
  none = dfs(cur + 1, a, b, c)
  fora = dfs(cur + 1, a + l[cur], b, c) + 10
  forb = dfs(cur + 1, a, b + l[cur], c) + 10
  forc = dfs(cur + 1, a, b, c + l[cur]) + 10
  print(none,fora,forb,forc)
  return min(none, fora, forb, forc)

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

Submission status: Wrong Answer

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

def base10to(n,b):
    if int(n/b):
        return base10to(n/b,b)+str(n%b)
    return str(n%b)

for i in range(n):
    L.append(int(input()))
ans=10**10
for i in range(4**n):
    S=base10to(i,4)
    S=S.zfill(n)
    A=[]
    B=[]
    C=[]
    for j in range(n):
        if S[j]=='0':
            continue
        elif S[j]=='1':
            A.append(L[j])
        elif S[j]=='2':
            B.append(L[j])
        else:
            C.append(L[j])
    if A and B and C:
        ans=min(ans,abs(a-sum(A))+(len(A)-1)*10+abs(b-sum(B))+(len(B)-1)*10+abs(c-sum(C))+(len(C)-1))
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(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 10**9
    ret0 = dfs(cur+1,a,b,c)
    ret1 = dfs(cur+1,a+l[cur],b,c)
    ret2 = dfs(cur+1,a,b+l[cur],c)
    ret3 = dfs(cur+1,a,b,c+l[cur])
    return min(ret0,ret1,ret2,ret3)
print(dfs(0,0,0,0))

Submission status: Wrong Answer

Solution code:
# coding: utf-8
N,A,B,C=map(int,input().split())
L=[]
INF=10**6

ans=INF

def base10to(n, b):
    if (int(n/b)):
        return base10to(int(n/b), b) + str(n%b)
    return str(n%b)

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

for i in range(3**N):
    S=base10to(i,3).zfill(N)
    LA=[]
    LB=[]
    LC=[]
    for j in range(N):
        if S[j]=="0":
            LA.append(L[j])
        elif S[j]=="1":
            LB.append(L[j])
        elif S[j]=="2":
            LC.append(L[j])
    
    if not(LA) or not(LB) or not(LC):
        continue
    
    dpA=[[INF for b in range(1500)] for a in range(len(LA))]
    dpB=[[INF for b in range(1500)] for a in range(len(LB))]
    dpC=[[INF for b in range(1500)] for a in range(len(LC))]
    
    for a in range(len(LA)):
        l=LA[a]
        dpA[a][l]=0
        dpA[a][0]=0
        for b in range(1500):
            if a>0 and b-l>=0:
                dpA[a][b]=min(dpA[a][b],dpA[a][b-1]+1,dpA[a-1][b-l]+10)
                
        for b in range(1500):
            if b>0:
                dpA[a][-(b+1)]=min(dpA[a][b],dpA[a][-b]+1)
    
    for a in range(len(LB)):
        l=LB[a]
        dpB[a][l]=0
        dpB[a][0]=0
        for b in range(1500):
            if a>0 and b-l>=0:
                dpB[a][b]=min(dpB[a][b],dpB[a][b-1]+1,dpB[a-1][b-l]+10)
                
        for b in range(1500):
            if b>0:
                dpB[a][-(b+1)]=min(dpB[a][b],dpB[a][-b]+1)
                
    for a in range(len(LC)):
        l=LC[a]
        dpC[a][l]=0
        dpC[a][0]=0
        for b in range(1500):
            if a>0 and b-l>=0:
                dpC[a][b]=min(dpC[a][b],dpC[a][b-1]+1,dpC[a-1][b-l]+10)
                
        for b in range(1500):
            if b>0:
                dpC[a][-(b+1)]=min(dpC[a][b],dpC[a][-b]+1)
    
    ans=min(ans,dpA[len(LA)-1][A]+dpB[len(LB)-1][B]+dpC[len(LC)-1][C])

print(ans)

Submission status: Wrong Answer

Solution code:
N,a,b,c=map(int,input().split())
L=[int(input()) for i in range(N)]
import itertools 
ans=300000
for i in itertools.product('ABCD',repeat=N):
  cst=0
  A=[]
  B=[]
  C=[]
  for j in range(N):
    if i[j]=='A':
      A.append(L[j])
    elif i[j]=='B':
      B.append(L[j]) 
    elif i[j]=='C':
      C.append(L[j])
  if len(A)>0 and len(B)>0 and len(C)>0:
    if len(A)>1:
      cst+=len(A)-1
    cst+=abs(sum(A)-a)
    if len(B)>1:
      cst+=len(B)-1
    cst+=abs(sum(B)-b)
    if len(C)>1:
      cst+=len(C)-1
    cst+=abs(sum(C)-c)
    ans=min(ans,cst)
 
print(ans)

Submission status: Wrong Answer

Solution code:
n,a,b,c = map(int,input().split())
l = [int(input()) for _ in range(n)]
ans = 10000
for i in range(2 ** (2*n)):
  v = [0,0,0,0]
  assign = [(i>>(2*j)) & 3 for j in range(n)]
  cost = 0
  for j in range(n):
    if v[assign[j]] > 0:
      cost += 10
    v[assign[j]] += l[j]
  if v[1] == 0 or v[2] == 0 or v[3] == 0:
    continue
  ans = min(ans, cost + abs(v[1]-a) + abs(v[2]-b) + abs(v[3]-c))
print(ans)

Submission status: Wrong Answer

Solution code:
n,a,b,c = map(int,input().split())
l = [int(input()) for _ in range(n)]
ans = 10000
for i in range(2 ** (2*n)):
  v = [0,0,0,0]
  assign = [(i>>(2*j)) & 3 for j in range(n)]
  for j in range(n):
    v[assign[j]] += l[j] + (v[assign[j]] > 0) * 10
  if v[1] == 0 or v[2] == 0 or v[3] == 0:
    continue
  ans = min(ans, abs(v[1]-a) + abs(v[2]-b) + abs(v[3]-c))
print(ans)

Submission status: Wrong Answer

Solution code:
n,a,b,c = map(int,input().split())
l = [int(input()) for _ in range(n)]
ans = 10000
for i in range(2 ** (2*n)):
  v = [0,0,0,0]
  assign = [(i>>(2*j)) & 3 for j in range(n)]
  for j in range(n):
    v[assign[j]] += l[j]
  if v[1] == 0 or v[2] == 0 or v[3] == 0:
    continue
  ans = min(ans, abs(v[1]-a) + abs(v[2]-b) + abs(v[3]-c))
print(ans)

Submission status: Wrong Answer

Solution code:

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

#rec part
def rec(a,b,c,gousei,now):
    global ans

    if(a != 0 or b != 0 or c != 0):
        ans = min(ans,(abs(a-A)+abs(b-B)+abs(c-C)+gousei*10))
        #print(a,b,c,ans) 

    if now == N:
        return 0


    #A
    rec(a,b,c, gousei,now+1)
    rec(a+l[now],b,c,gousei+1 if a!= 0 else gousei,now+1)
    rec(a,b+l[now],c,gousei+1 if b!= 0 else gousei,now+1)
    rec(a,b,c+l[now],gousei+1 if c!= 0 else gousei,now+1)


    
rec(0,0,0,0,0)
print(ans)

Submission status: Wrong Answer

Solution code:

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

#rec part
def rec(a,b,c,gousei,now):
    global ans

    if(a != 0 | b != 0 | c != 0):
        ans = min(ans,(abs(a-A)+abs(b-B)+abs(c-C)+gousei*10))

    if now == N:
        return 0
    


    #A
    rec(a+l[now],b,c,gousei+1 if a!= 0 else gousei,now+1)
    rec(a,b+l[now],c,gousei+1 if b!= 0 else gousei,now+1)
    rec(a,b,c+l[now],gousei+1 if c!= 0 else gousei,now+1)
    rec(a,b,c, gousei,now+1)


    
rec(0,0,0,0,0)
print(ans)

Submission status: Wrong Answer

Solution code:

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

#rec part
def rec(a,b,c,gousei,now):
    global ans

    ans = min(ans,(abs(a-A)+abs(b-B)+abs(c-C)+gousei*10))

    if now == N:
        return 0

    #A
    rec(a+l[now],b,c,gousei+1 if a!= 0 else gousei,now+1)
    rec(a,b+l[now],c,gousei+1 if b!= 0 else gousei,now+1)
    rec(a,b,c+l[now],gousei+1 if c!= 0 else gousei,now+1)
    rec(a,b,c, gousei,now+1)


    
rec(0,0,0,0,0)
print(ans)

Submission status: Wrong Answer

Solution code:
N,A,B,C=map(int, input().split())
l=[int(input()) for i in range(N)]
from itertools import product
p=list(product([0,1,2,3],repeat=N))
t=1000
for i in p:
    a=b=c=ac=0
    for j in range(N):
        if i[j]==1:
            a+=l[j]
            ac+=1
        elif i[j]==2:
            b+=l[j]
            ac+=1
        elif i[j]==3:
            c+=l[j]
            ac+=1
    if a and b and c:
        tt=abs(a-A)+abs(b-B)+abs(c-C)+(ac-3)*10
        if t>tt:
            t=tt
print(t)