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: Accepted

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

def dfs(now, a, b, c):
    if now == N:
        if a == 0 or b == 0 or c == 0:
            return 100000
        return abs(A-a) + abs(B-b) + abs(C-c)
    d1 = dfs(now+1, a+ll[now], b, c) + min(10, a*10)
    d2 = dfs(now+1, a, b+ll[now], c) + min(10, b*10)
    d3 = dfs(now+1, a, b, c+ll[now]) + min(10,c*10)
    d4 = dfs(now+1, a, b, c)
    return min(d1, d2, d3, d4)

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

Submission status: Accepted

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

def dfs(i, a, b, c, N):
    global ls,A,B,C

    if i == N:
        mp = 0

        for ans_item, item in zip([A,B,C], [a,b,c]):
            if len(item) == 0:
                return float("inf")
            elif len(item) > 1:
                mp += (len(item) - 1) * 10
            total = sum(item)
            if ans_item == total:
                pass
            else:
                mp += abs(ans_item-total)
        
        return mp

    l = ls[i]
    mps = []
    mps.append(dfs(i+1, a+[l], b, c, N))
    mps.append(dfs(i+1, a, b+[l], c, N))
    mps.append(dfs(i+1, a, b, c+[l], N))
    mps.append(dfs(i+1, a, b, c, N))
    return min(mps)

print(dfs(i=0, a=[], b=[], c=[], N=N))

Submission status: Accepted

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

bumboos = [int(input()) for _ 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+bumboos[cur], b, c) + 10
    ret2 = dfs(cur + 1, a, b+bumboos[cur], c) + 10
    ret3 = dfs(cur + 1, a, b, c+bumboos[cur]) + 10
    return min(ret0, ret1, ret2, ret3)

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


"""
1, 2, 3, 4, 5, 6  7, 8
A  A  A  A  A  A  A  A
B  B  B  B  B  B  B  B
C  C  C  C  C  C  C  C
X  X  X  X  X  X  X  X

example
5 100 90 80
98
40
30
21
80

1. dfs(0,0,0,0)
  ret0 = dfs(1,0,0,0) + 10
  ret1 = dfs(1,98,0,0) + 10 <- will be chosen
  ret2 = dfs(1,0,98,0) + 10
  ret3 = dfs(1,0,0,98) + 10

2. dfs(1,98,0,0)
  ret0 = dfs(2,98,0,0) + 10
  ret1 = dfs(2,138,0,0) + 10
  ret2 = dfs(2,98,40,0) + 10 <- will be chosen
  ret3 = dfs(2,98,0,40) + 10

3. dfs(2,98,40,0)
  ret0 = dfs(3,98,40,0) + 10
  ret1 = dfs(3,128,40,0) + 10
  ret2 = dfs(3,98,70,0) + 10 <- will be chosen
  ret3 = dfs(3,98,40,30) + 10

4. dfs(3,98,70,0)
  ret0 = dfs(4,98,70,0) + 10
  ret1 = dfs(4,119,70,0) + 10
  ret2 = dfs(4,98,91,0) + 10 <- will be chosen
  ret3 = dfs(4,98,70,21) + 10

5. dfs(4,98,91,0)
  ret0 = dfs(5,98,91,0) + 10
  ret1 = dfs(5,178,91,0) + 10
  ret2 = dfs(5,98,171,0) + 10
  ret3 = dfs(5,98,91,80) + 10 <- will be chosen

6. dfs(5,98,91,80)
  return abs(98 - 100) + abs(91 - 90) + abs(80 - 80) - 30 if min(98,91,80) > 0 else INF
equals
  return 2 + 1 + 0 - 30
equals
  return -27

then,

5. dfs(4,98,91,0)
  ret3 = -27 + 10
  it returns -17

4. dfs(3,98,70,0)
  ret2 = -17 + 10
  it returns -7

3. dfs(2,98,40,0)
  ret2 = -7 + 10
  it returns 3

2. dfs(1,98,0,0)
  ret2 = 3 + 10
  it returns 13

1. dfs(0,0,0,0)
  ret1 = 13 + 10
  it returns 23

therefore, the answer is 23.
"""

Submission status: Accepted

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

def dfs(cnt,a,b,c):
    if cnt == N:
        return abs(A-a)+abs(B-b)+abs(C-c) if min(a,b,c) > 0 else float('inf')
    l0 = dfs(cnt+1,a,b,c)
    la = dfs(cnt+1,a+list[cnt],b,c) +(10 if a != 0 else 0)
    lb = dfs(cnt+1,a,b+list[cnt],c) +(10 if b != 0 else 0)
    lc = dfs(cnt+1,a,b,c+list[cnt]) +(10 if c != 0 else 0)
    return min(l0,la,lb,lc)

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

Submission status: Accepted

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
    return min(ret0, ret1, ret2, ret3)

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

Submission status: Accepted

Solution code:
import itertools

[n,a,b,c] = list(map(int, input().strip().split(' ')))
lst = []
for i in range(n):
    lst.append(int(input().strip()))

x = [0,1,2,3]
y = list(itertools.product(x,x,x,x,x,x,x,x))
minimum = 1000000
for pattern in y:
    aa = 0
    bb = 0
    cc = 0
    mp = 0
    for i,num in enumerate(pattern):
        if i > n - 1:
            break
        if num == 1:
            if aa != 0:
                mp += 10
            aa += lst[i]
        if num == 2:
            if bb != 0:
                mp += 10
            bb += lst[i]
        if num == 3:
            if cc!= 0:
                mp += 10
            cc += lst[i]
    if aa == 0 or bb == 0 or cc == 0:
        continue
    mp += abs(a-aa) + abs(b-bb) + abs(c-cc)
    if minimum > mp:
        minimum = mp
print(minimum)

Submission status: Accepted

Solution code:
N, A, B, C = map(int, input().split())
l_list = [0]*8
for i in range(N):
    l_list[i] = int(input())
INF = 1e9

target_length = {'A':A, 'B':B, 'C':C}
# print(l_list)
cost_list = []

all_combination = []
base = ['A', 'B', 'C', 'O']

def comb_to_cost(combination):
    cost = 0
    for char in ['A', 'B', 'C']:
        base_length = 0
        for i in range(8):
            c = combination[i]
            if c == char:
                base_length += l_list[i]
                cost += 10
        cost -= 10
        if base_length == 0:
            return INF
        cost += abs(base_length-target_length[char])
    return cost


for char1 in base:
    for char2 in base:
        for char3 in base:
            for char4 in base:
                for char5 in base:
                    for char6 in base:
                        for char7 in base:
                            for char8 in base:
                                all_combination.append(char1+char2+char3+char4+char5+char6+char7+char8)

for comb in all_combination:
    cost_list.append(comb_to_cost(comb))

# print(cost_list)
print(min(cost_list))

Submission status: Accepted

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

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


def dfs(cur, a_sum, b_sum, c_sum):
    if cur == N:
        return abs(A-a_sum)+abs(B-b_sum)+abs(C-c_sum)-30 if a_sum*b_sum*c_sum > 0 else 100000000

    pat0 = dfs(cur+1, a_sum, b_sum, c_sum)
    pat1 = dfs(cur+1, a_sum+l_in[cur], b_sum, c_sum)+10
    pat2 = dfs(cur+1, a_sum, b_sum+l_in[cur], c_sum)+10
    pat3 = dfs(cur+1, a_sum, b_sum, c_sum+l_in[cur])+10

    return min(pat0, pat1, pat2, pat3)


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

Submission status: Accepted

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

def dfs(x, y, z, now):

    if now == n:
        return abs(x - a) + abs(y - b) + abs(z - c) - 30 if not 0 in (x, y, z) else 3000

    usea = dfs(x + l[now], y, z, now + 1) + 10
    useb = dfs(x, y + l[now], z, now + 1) + 10
    usec = dfs(x, y, z + l[now], now + 1) + 10
    nouse = dfs(x, y, z, now + 1)
    return min(usea, useb, usec, nouse)

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

Submission status: Accepted

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

lList = []
for _ in range(N):
    lList.append(int(input()))

def dfs(idx, i, j, k):
    if idx == N:
        if min(i,j,k) > 0:
            return abs(A-i) + abs(B- j) + abs(C - k)
        else:
            return 10**9
    re4 = dfs(idx+1, i, j, k)
    re1 = dfs(idx+1, i+lList[idx], j, k)+10
    re2 = dfs(idx+1, i, j+lList[idx], k)+10
    re3 = dfs(idx+1, i, j, k+lList[idx])+10

    return min(re1,re2,re3,re4)

print(dfs(0,0,0,0)-30)

Submission status: Accepted

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


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


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

Submission status: Accepted

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

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


def calc(q):
    score = 0
    x = y = z = 0
    xflag = yflag = zflag = False
    for i in range(n):
        if q[i] == 0:
            x += l[i]
            if xflag:
                score += 10
            xflag = True
        elif q[i] == 1:
            y += l[i]
            if yflag:
                score += 10
            yflag = True
        elif q[i] == 2:
            z += l[i]
            if zflag:
                score += 10
            zflag = True
    if xflag and yflag and zflag:
        return abs(a-x) + abs(b-y) + abs(c-z) +score
    else:
        return 100000000000000


ans = 100000000000000
def dfs(q):
    global ans
    if len(q) >= n:
        res = calc(q)
        ans = min(ans,res)
        return
    else:
        for i in range(4):
            q.append(i)
            dfs(q)
            q.pop(-1)
    return ans


dfs([])

print(dfs([]))

Submission status: Accepted

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

bamboos = []
for _ in range(N):
    l = int(input())
    bamboos.append(l)
    
import copy

def dfs(i, bs):
    if i == len(bamboos):
        #print(bs)
        if not bs['A'] or not bs['B'] or not bs['C']:
            return []
        return [bs]
    ret = []
    tmp = copy.deepcopy(bs)
    tmp['A'].append(bamboos[i])
    ret.extend(dfs(i+1, tmp))
    tmp = copy.deepcopy(bs)
    tmp['B'].append(bamboos[i])
    ret.extend(dfs(i+1, tmp))
    tmp = copy.deepcopy(bs)
    tmp['C'].append(bamboos[i])
    ret.extend(dfs(i+1, tmp))
    tmp = copy.deepcopy(bs)
    tmp['D'].append(bamboos[i])
    ret.extend(dfs(i+1, tmp))
    
    return ret

ABC = {'A': A, 'B': B, 'C': C}
bs_comb = dfs(0, {'A':[],'B':[],'C':[],'D':[]})

import sys
ans = sys.maxsize
for bs in bs_comb:
    cost = 0
    for abc in ['A', 'B', 'C']:
        cost += (len(bs[abc]) - 1) * 10
        cost += abs(sum(bs[abc]) - ABC[abc])
    ans = min(ans, cost)
print(ans)

Submission status: Accepted

Solution code:
n,A,B,C=map(int,input().split())
l=[int(input()) for _ in range(n)]
t=10**9
def dfs(level,a,b,c):
  if level==n:
    return abs(a-A)+abs(b-B)+abs(c-C)-30 if min(a,b,c)>0 else t
  k1=dfs(level+1,a,b,c)
  k2=dfs(level+1,a+l[level],b,c)+10
  k3=dfs(level+1,a,b+l[level],c)+10
  k4=dfs(level+1,a,b,c+l[level])+10
  return min(k1,k2,k3,k4)
print(dfs(0,0,0,0))

Submission status: Accepted

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

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

print(search(0, 0, 0, 0))

Submission status: Accepted

Solution code:
########関数部分##############
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)
############################

def scorecalc(a,target):
    if len(a) == 0:
        ans = 10**9
    else:
        ans = 10*(len(a)-1)
        ans += abs(sum(a)-target)
    return ans

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

mnm = 10**9

for i in range(1,4**n+1):
    test = Base_10_to_n(i,4)
    test = list(test)
    for k in range(n-len(test)):
        test.insert(0,"0")
    fora = []
    forb = []
    forc = []
    for k in range(n):
        if test[k] == "0":
            fora.append(l[k])
        elif test[k] == "1":
            forb.append(l[k])
        elif test[k] == "2":
            forc.append(l[k])
    temp = scorecalc(fora,a) + scorecalc(forb,b) + scorecalc(forc,c)
    if temp < mnm:
        mnm = temp
    
print(mnm)

Submission status: Accepted

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
	return min(ret0, ret1, ret2, ret3)

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

Submission status: Accepted

Solution code:
import itertools

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

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

ans = 999999999
count = 0

products = list(itertools.product([0, 1, 2, 3], repeat=N))
# [0, 1, 2, 3]の中からN個（重複可）とってきた時の総当たりの組み合わせ
for p in products:
    scores_g = [[0, 0], [0, 0], [0, 0], [0, 0]]
    for i in range(len(p)):
        scores_g[p[i]][0] += l[i]
        scores_g[p[i]][1] += 1
    for g in range(3):
        if scores_g[g][1] == 0:
            count = 999999999
            continue
        else:
            count += abs(scores_g[g][0] - t[g]) + (scores_g[g][1] - 1) * 10
    if ans > count:
        ans = count
    count = 0

print(ans)

Submission status: Accepted

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
  return min(ret0, ret1, ret2, ret3)
print(dfs(0, 0, 0, 0))

Submission status: Accepted

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

def dfs(cnt,a,b,c):
    if cnt == N:
        return abs(A-a)+abs(B-b)+abs(C-c) if min(a,b,c) > 0 else 10**9

    l  = dfs(cnt+1,a,b,c)
    la = dfs(cnt+1,a+list[cnt],b,c) + (10 if a != 0 else 0)
    lb = dfs(cnt+1,a,b+list[cnt],c) + (10 if b != 0 else 0)
    lc = dfs(cnt+1,a,b,c+list[cnt]) + (10 if c != 0 else 0)
    return min(l,la,lb,lc)

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

Submission status: Accepted

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

    print(solve(a, b, c, l))

def solve(a, b, c, l):
    m = float('inf')
    for x in range(1, 4 ** len(l)):
        m = min(m, eval_cost(x, a, b, c, l))
    return m

def eval_cost(x, a, b, c, l):
    cnt = [0, 0, 0]
    y = [0, 0, 0]

    for i in range(len(l)):
        n = (x >> (i * 2)) & 0x03
        if n < 3:
            cnt[n] += 1
            y[n] += l[i]

    if cnt.count(0) != 0:
        return float('inf')
    
    return (sum(cnt) - 3) * 10 + abs(y[0] - a) + abs(y[1] - b) + abs(y[2] - c)

main()

Submission status: Accepted

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)):
    if x[i].count(1) == 0 or x[i].count(2) == 0 or x[i].count(3) == 0:
        continue
    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: Accepted

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
  return min(ret0,ret1,ret2,ret3)
print(dfs(0, 0, 0, 0))

Submission status: Accepted

Solution code:
N, A, B, C = [int(i) for i in input().split()]
L = [int(input()) for i in range(N)]
 
def dfs(idx, a, b, c):
 
    if idx == N:
        if a == 0 or b == 0 or c == 0:
            return 1e9
        
        return abs(A - a) + abs(B - b) + abs(C - c)
    
    x = dfs(idx + 1, a, b, c)
    y = dfs(idx + 1, a + L[idx], b, c) + 10*(a != 0)
    z = dfs(idx + 1, a, b + L[idx], c) + 10*(b != 0)
    w = dfs(idx + 1, a, b, c + L[idx]) + 10*(c != 0)
    return min(x, y, z, w);
 
ans = dfs(0, 0, 0, 0);
print(ans)

Submission status: Accepted

Solution code:
N, A, B, C = map(int, input().split())
l = []
for i in range(N):
	l.append(int(input()))
X = []
for i in range(4 ** N):
	num = []
	for j in range(N):
		num.append(i % 4)
		i //= 4
	a, b, c, x = 0, 0, 0, -30
	for k in range(N):
		if num[k] == 0:
			a += l[k]
			x += 10
		elif num[k] == 1:
			b += l[k]
			x += 10
		elif num[k] == 2:
			c += l[k]
			x += 10
	if a * b * c != 0:
		x += abs(A - a) + abs(B - b) + abs(C - c)
		X.append(x)
print(min(X))

Submission status: Accepted

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

def find(i, A, B, C):
	if i == n:
		return abs(A-a) + abs(B-b) + abs(C-c) if min(A, B, C) > 0 else 9999999999999999999999999999
	else:
		return min(
			find(i + 1, A       , B       , C       ),
			find(i + 1, A + l[i], B       , C       ) + (10 if A > 0 else 0),
			find(i + 1, A       , B + l[i], C       ) + (10 if B > 0 else 0),
			find(i + 1, A       , B       , C + l[i]) + (10 if C > 0 else 0),
		)

print(find(0, 0, 0, 0))

Submission status: Accepted

Solution code:
N,A,B,C = list(map(int,input().split()))
L = [int(input()) for i in range(N)]
MIN = 99999999999
def solve(a,b,c,an,bn,cn,k):
    global MIN
    if k == N:
        if not (an == 0 or bn == 0 or cn == 0):
            MIN = min(MIN,abs(A-a)+abs(B-b)+abs(C-c)+10*(an+bn+cn-3))
        return
    else:
        solve(a+L[k],b,c,an+1,bn,cn,k+1)
        solve(a,b+L[k],c,an,bn+1,cn,k+1)
        solve(a,b,c+L[k],an,bn,cn+1,k+1)
        solve(a,b,c,an,bn,cn,k+1)
solve(0,0,0,0,0,0,0)
print(MIN)

Submission status: Accepted

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: Accepted

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
        invalid = False
        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"
        if a == 0 or b == 0 or c == 0:
            continue
        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: Accepted

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


nabc = list(map(int, input().split()))
n = nabc[0]
a = nabc[1]
b = nabc[2]
c = nabc[3]
l_list = []
for i in range(n):
    l_list.append(int(input()))
min_MP = 3000
for i in range(4 ** n):
    mix = "00000000" + base10to(i, 4)
    if mix.count("1") * mix.count("2") * mix.count("3") == 0:
        continue
    MP = (mix.count("1")  + mix.count("2") + mix.count("3") - 3) * 10
    b_a = 0
    b_b = 0
    b_c = 0
    for j in range(n):
        if mix[- j - 1] == "1":
            b_a += l_list[j]
        elif mix[- j - 1] == "2":
            b_b += l_list[j]
        elif mix[- j - 1] == "3":
            b_c += l_list[j]
    MP += abs(b_a - a) + abs(b_b - b) + abs(b_c - c)
    if MP < min_MP:
        min_MP = MP
print(min_MP)

Submission status: Accepted

Solution code:
N, A, B, C = map(int, (input().split(' ')))
a_list = list(map(int, [input() for _ in range(N)]))
kado = [0] * 3
calc = []

def calc_mp(kado, score):
    calc.append(abs(A - kado[0]) + abs(B - kado[1]) + abs(C - kado[2]) + score)


def solve(kado, a_list, score):
    if all([k != 0 for k in kado]):
        calc_mp(kado, score)
    if a_list == []:
        return
    for i in range(4):
        if i == 3:
            solve(kado, a_list[1:], score)
        else:
            empty = [0, 0, 0]
            empty[i] += a_list[0]
            solve([i + j for i, j in zip(kado, empty)], a_list[1:], score + 10)

solve(kado, a_list, 0)
print(min(calc) - 30)

Submission status: Accepted

Solution code:
magic = 10**4
N,a,b,c = map(int,input().split())
src = [int(input()) for _ in range(N)]
count = [0]*N

for i in range(4**N):
    a_num = [0,0]
    b_num = [0,0]
    c_num = [0,0]
    tmpi = i
    for j in range(N)[::-1]:
        num = src[j]
        abc = tmpi//(4**j)
        tmpi %= 4**j
        if abc == 0:
            a_num[0] += num
            a_num[1] += 1
        elif abc == 1:
            b_num[0] += num
            b_num[1] += 1
        elif abc == 2:
            c_num[0] += num
            c_num[1] += 1
    if a_num[1] > 0 and b_num[1] > 0 and c_num[1] > 0:
        magic = min(abs(a-a_num[0]) + abs(b-b_num[0]) + abs(c-c_num[0]) + (a_num[1]+b_num[1]+c_num[1]-3)*10,magic)
        
print(magic)

Submission status: Accepted

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

def magic(cur,a,b,c):
  if cur==N:
    if a*b*c!=0:
      return abs(a-A)+abs(b-B)+abs(c-C)-30
    else:
      return 10**9
  r0=magic(cur+1,a,b,c)
  r1=magic(cur+1,a+l[cur],b,c)+10
  r2=magic(cur+1,a,b+l[cur],c)+10
  r3=magic(cur+1,a,b,c+l[cur])+10
  return min(r0,r1,r2,r3)

print(magic(0,0,0,0))

Submission status: Accepted

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

def dfs(depth):
    if depth == N:
        a, b, c = 0, 0, 0
        flag_a, flag_b, flag_c  = False, False, False
        cost = 0
        for i in range(N):
            if used[i] == 0:
                if flag_a:
                    cost += 10
                a += l[i]
                flag_a = True
            elif used[i] == 1:
                if flag_b:
                    cost += 10
                b += l[i]
                flag_b = True
            elif used[i] == 2:
                if flag_c:
                    cost += 10
                c += l[i]
                flag_c = True
            else:
                pass
        if min(a, b, c) > 0:
            cost += abs(a - A) + abs(b - B) + abs(c - C)
            return cost
        else:
            return 10**9

    for i in range(4):
        used[depth] = i
        cost_list.append(dfs(depth + 1))

    return 10**9

dfs(0)

print(min(cost_list))

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LS()
    return l
sys.setrecursionlimit(1000000)
mod = 1000000007

#A
def A():
    n = I()
    ans = float("inf")
    for i in range(n+1):
        m = 0
        k = i
        while k > 0:
            m += k%6
            k //= 6
        k = n-i
        while k > 0:
            m += k%9
            k //= 9
        if m < ans:
            ans = m
    print(ans)
    return

#B
def B():
    n = I()
    x = [None for i in range(n)]
    y = [None for i in range(n)]
    h = [None for i in range(n)]
    for i in range(n):
        x[i],y[i],h[i] = LI()
    if n == 1:
        print(x[0],y[0],h[0])
        quit()
    for cy in range(101):
        for cx in range(101):
            for i in range(n):
                if h[i] > 0:break
            ch = h[i]+abs(x[i]-cx)+abs(y[i]-cy)
            f = 1
            for i in range(n):
                if max(ch-abs(x[i]-cx)-abs(y[i]-cy),0) != h[i]:f = 0
            if f:
                print(cx,cy,ch)
                quit()
    return

#C
def C():
    def dfs(m,k):
        if m == n:
            lis.append(k)
        else:
            for i in range(4):
                dfs(m+1,k+[i])
    n,a,b,c = LI()
    an = [a,b,c]
    l = IR(n)
    lis = []
    dfs(0,[])
    ans = float("inf")
    for k in lis:
        s = [0,0,0,0]
        d = [0,0,0,0]
        for i in range(n):
            s[k[i]] += l[i]
            d[k[i]] += 1
        m = 0
        for i in range(3):
            m += 10*max(0,d[i]-1)
            if s[i] == 0:
                m = float("inf")
                break
            m += abs(an[i]-s[i])
        if m < ans:
            ans = m
    print(ans)
    return

#D
def D():
    return

#E
def E():
    return

#F
def F():
    return

#G
def G():
    return

#H
def H():
    return

#I
def I_():
    return

#J
def J():
    return

#Solve
if __name__ == "__main__":
    C()

Submission status: Accepted

Solution code:
INF = float('inf')
def dfs(a=0,b=0,c=0,d=0):
    if d==N:
        return abs(A-a)+abs(B-b)+abs(C-c)-30 if min(a,min(b,c)) else INF
    ans = dfs(a,b,c,d+1)
    ans = min(ans,dfs(a+l[d],b,c,d+1)+10)
    ans = min(ans,dfs(a,b+l[d],c,d+1)+10)
    ans = min(ans,dfs(a,b,c+l[d],d+1)+10)
    return ans

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

Submission status: Accepted

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

l = []

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

bit = {}
bitlen = {}

for i in range(1<<n):
    cnt = 0
    leng = 0
    for j in range(n):
        if((1<<j) & i): 
            cnt += 1
            leng += l[j]
    bit[i] = cnt
    bitlen[i] = leng


ans = float('inf')

for i in range(1,1<<n):
    for j in range(1,1<<n):
        if(i&j):continue
        for k in range(1,1<<n):
            if(i&k):continue
            if(j&k):continue
            
            t = 0
            t += (bit[i]-1)*10
            t += (bit[j]-1)*10
            t += (bit[k]-1)*10
            t += abs(bitlen[i]-a)
            t += abs(bitlen[j]-b)
            t += abs(bitlen[k]-c)
            if(ans > t):ans = t

print(ans)