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

Solution (Python):
from itertools import permutations


def solve(string):
    n, a, b, c, *l = map(int, string.split())
    ans = 3000

    for _l in permutations(l):
        for i in range(1, len(l) - 1):
            for j in range(i + 1, len(l)):
                for k in range(j + 1, len(l) + 1):
                    tmp = 10 * (i - 1) + abs(a - sum(_l[:i])) + 10 * (j - i - 1) + abs(
                        b - sum(_l[i:j])) + 10 * (k - j - 1) + abs(c - sum(_l[j:k]))
                    if tmp < ans:
                        ans = tmp
    return str(ans)


if __name__ == '__main__':
    n, a, b, c = map(int, input().split())
    print(solve('{} {} {} {}\n'.format(n, a, b, c) + '\n'.join([input() for _ in range(n)])))

Solution (Python):
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))

Solution (Python):
import itertools
n,a,b,c = map(int,input().split())

remain = []
res = []
for i in range(n):
    remain.append(int(input()))
x =tuple([ [0,1,2,3]]*n)
for t in list(itertools.product(*x)):
    l = [0,0,0,0]
    s = [0,0,0,0]
    for i in range(n):
        l[t[i]] += remain[i]
        s[t[i]] += 1
    if l[1]*l[2]*l[3] == 0:
        continue
    else:
        magic = (s[1]+s[2]+s[3]-3)*10
        magic += abs(l[1]-a)
        magic += abs(l[2]-b)
        magic += abs(l[3]-c)
        res.append(magic)
print(min(res))

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

def dfs(i, a_cur, b_cur, c_cur):
    if i == n:
        return abs(a_cur-a) + abs(b_cur-b) + abs(c_cur-c) - 30 if min(a_cur, b_cur, c_cur) > 0 else inf
    ans0 = dfs(i+1, a_cur+l[i], b_cur, c_cur) + 10
    ans1 = dfs(i+1, a_cur, b_cur+l[i], c_cur) + 10
    ans2 = dfs(i+1, a_cur, b_cur, c_cur+l[i]) + 10
    ans3 = dfs(i+1, a_cur, b_cur, c_cur)
    return min(ans0, ans1, ans2, ans3)

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

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

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

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

Solution (Python):
def main():
    N, A, B, C = map(int, input().split())
    l_list = [int(input()) for i in range(N)]

    INF = 10 ** 9

    def dfs(current_bamboo_index, a, b, c):
        if current_bamboo_index == N:
            return abs(a - A) + abs(b - B) + abs(c - C) - 30 if a * b * c > 0 else INF
        stuff_a = dfs(current_bamboo_index + 1, a + l_list[current_bamboo_index], b, c) + 10
        stuff_b = dfs(current_bamboo_index + 1, a, b + l_list[current_bamboo_index], c) + 10
        stuff_c = dfs(current_bamboo_index + 1, a, b, c + l_list[current_bamboo_index]) + 10
        unused = dfs(current_bamboo_index + 1, a, b, c)
        return min(stuff_a, stuff_b, stuff_c, unused)

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


if __name__ == "__main__":
    main()

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

ans = 10**9
for m in range(4**n):
    length = [0, 0, 0, 0]
    counts = [0, 0, 0, 0]
    for d in range(n-1, -1, -1):
        i = m//(4**d)
        length[i] += l[d]
        counts[i] += 1
        m %= 4**d
        
    if 0 in length[:3]:
        continue
    ans_tmp = 0
    for i in range(3):
        ans_tmp += abs([a,b,c][i] - length[i]) + 10*(counts[i]-1)
    ans = min(ans, ans_tmp)
print(ans)

Solution (Python):
N,A,B,C=list(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 10**9+7
    else:
        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))

Solution (Python):
N, A, B, C = map(int, input().split())
l = [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 + 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))

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

def find(idx, a1, b1, c1):
    if idx == n:
        if min(a1, b1, c1) == 0:
            return 100000000
        return abs(a - a1) + abs(b - b1) + abs(c - c1) - 30
    r0 = find(idx + 1, a1, b1, c1)
    r1 = find(idx + 1, a1 + l[idx], b1, c1) + 10
    r2 = find(idx + 1, a1, b1 + l[idx], c1) + 10
    r3 = find(idx + 1, a1, b1, c1 + l[idx]) + 10
    return min(r0, r1, r2, r3)

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

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

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

	ret0 = dfs(depth + 1, a, b, c)
	ret1 = dfs(depth + 1, a + L[depth], b, c) + 10
	ret2 = dfs(depth + 1, a, b + L[depth], c) + 10
	ret3 = dfs(depth + 1, a, b, c + L[depth]) + 10

	return min(ret0, ret1, ret2, ret3)

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

Solution (Python):
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))

Solution (Python):
n, ao, bo, co = map(int, input().split())
l = [int(input()) for _ in range(n)]

def solve(ln, a, b, c):
    if len(l) <= ln:
        if a * b * c > 0:
            return abs(ao - a) + abs(bo - b) + abs(co - c)
        else:
            return float("inf")
    
    x = l[ln]
    return min([
        solve(ln + 1, a, b, c),
        solve(ln + 1, a + x, b, c) + (10 if a > 0 else 0),
        solve(ln + 1, a, b + x, c) + (10 if b > 0 else 0),
        solve(ln + 1, a, b, c + x) + (10 if c > 0 else 0)
    ])
print(solve(0, 0, 0, 0))

Solution (Python):
n, a, b, c = map(int, input().split(' '))
l = []
for i in range(n):
  l.append(int(input()))
  
def func(k, x, y, z):
  if k == n:
    if not x or not y or not z:
      return 10**9
    else:
      return abs(x-a) + abs(y-b) + abs(z-c) - 30
  else:
    p = func(k+1, x, y, z)
    q = func(k+1, x + l[k], y, z) + 10
    r = func(k+1, x, y + l[k], z) + 10
    s = func(k+1, x, y, z + l[k]) + 10
    return min(p, q, r, s)

print(func(0, 0, 0, 0))

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

INF = 10 ** 5






def cost(lres, a, b, c):
    if len(lres) == 0:
        if a * b * c > 0:
            return abs(a - A) + abs(b - B) + abs(c - C)
        else:
            return INF

    x = lres[0]
    return min(
        cost(lres[1:], a, b, c),
        cost(lres[1:], a + x, b, c) + (10 if a > 0 else 0),
        cost(lres[1:], a, b + x, c) + (10 if b > 0 else 0),
        cost(lres[1:], a, b, c + x) + (10 if c > 0 else 0),
    )

print(cost(l, 0, 0, 0))

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 9)
N, A, B, C = map(int, input().split())
l_list = [int(input()) for _ in range(N)]


def dfs(now, a, b, c):
    if now == N:
        return abs(a - A) + abs(b - B) + abs(c - C) - 30 if min(a, b, c) > 0 else float("inf")
    res0 = dfs(now + 1, a, b, c)
    res1 = dfs(now + 1, a + l_list[now], b, c) + 10
    res2 = dfs(now + 1, a, b + l_list[now], c) + 10
    res3 = dfs(now + 1, a, b, c + l_list[now]) + 10
    return min(res0, res1, res2, res3)


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

Solution (Python):
N,A,B,C=map(int,input().split())
l=[int(input()) for _ in range(N)]
INF=10**9
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 INF
    res0=dfs(cur+1,a,b,c)
    res1=dfs(cur+1,a+l[cur],b,c)+10
    res2=dfs(cur+1,a,b+l[cur],c)+10
    res3=dfs(cur+1,a,b,c+l[cur])+10
    return min(res0,res1,res2,res3)
print(dfs(0,0,0,0))

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

def recursiv(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 9999
    r0 = recursiv(cur + 1, a, b, c)
    r1 = recursiv(cur + 1, a + vectors[cur], b, c) + 10
    r2 = recursiv(cur + 1, a, b + vectors[cur], c) + 10
    r3 = recursiv(cur + 1, a, b, c + vectors[cur]) + 10
    return min(r0, r1, r2, r3)

print(recursiv(0,0,0,0))

Solution (Python):



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

    
    
    
    
    def dfs(i, cur_a, cur_b, cur_c):
        if i == n:
            if cur_a == 0 or cur_b == 0 or cur_c == 0:
                return float('inf')
            return abs(a - cur_a) + abs(b - cur_b) + abs(c - cur_c)

        w = dfs(i + 1, cur_a, cur_b, cur_c)
        x = dfs(i + 1, cur_a + l[i], cur_b, cur_c) + 10 * (cur_a != 0)
        y = dfs(i + 1, cur_a, cur_b + l[i], cur_c) + 10 * (cur_b != 0)
        z = dfs(i + 1, cur_a, cur_b, cur_c + l[i]) + 10 * (cur_c != 0)

        return min(w, x, y, z)

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


if __name__ == '__main__':
    main()

Solution (Python):



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

    
    
    
    def dfs(i, cur_a, cur_b, cur_c):
        if i == n:
            if cur_a == 0 or cur_b == 0 or cur_c == 0:
                return float('inf')
            return abs(a - cur_a) + abs(b - cur_b) + abs(c - cur_c) - 30

        result0 = dfs(i + 1, cur_a, cur_b, cur_c)
        result1 = dfs(i + 1, cur_a + l[i], cur_b, cur_c) + 10
        result2 = dfs(i + 1, cur_a, cur_b + l[i], cur_c) + 10
        result3 = dfs(i + 1, cur_a, cur_b, cur_c + l[i]) + 10

        return min(result0, result1, result2, result3)

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


if __name__ == '__main__':
    main()

Solution (Python):
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))

Solution (Python):
N, A, B, C = list(map(int, raw_input().split()))
length = list()
for n in xrange(N): length.append(int(raw_input()))

INF = 10 ** 16

def cost(idx, a, b, c):
    if idx == N: return abs(a-A)+abs(b-B)+abs(c-C)-30 if min(a,b,c) > 0 else INF
    val1 = cost(idx+1, a, b, c)
    val2 = cost(idx+1, a+length[idx], b, c) + 10
    val3 = cost(idx+1, a, b+length[idx], c) + 10
    val4 = cost(idx+1, a, b, c+length[idx]) + 10
    return min(val1, val2, val3, val4)

print cost(0, 0, 0, 0)

Solution (Python):
from bisect import bisect, bisect_left
from sys import stdin

def dfs(n):
    l = [0] * n
    ret = []
    def dfs_(n):
        if not n:
            ret.append(l[:])
            return
        for i in ('a', 'b', 'c', '_'):
            l[n-1] = i
            dfs_(n-1)
    dfs_(n)
    return ret

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

    ans = float('inf')
    for s in dfs(N):
        banboo = {'a': 0, 'b': 0, 'c':0, '_': 0}
        for i, t in enumerate(s):
            banboo[t] += L[i]
        if all((banboo['a'] > 0,
                banboo['b'] > 0,
                banboo['c'] > 0)):
            mp = (s.count('a') + s.count('b') + s.count('c') - 3) * 10
            mp += (abs(banboo['a'] - A) +
                   abs(banboo['b'] - B) +
                   abs(banboo['c'] - C))
            ans = min(ans, mp)
    print(ans)

input = lambda: stdin.readline()
main()

Solution (Python):
N , A , B , C = map(int , input().split())
l = []
Takesakusen = []

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

m = 4
K = 4**N-1

a = 0
b = 0
c = 0
jointmagic = 0
magic = 2**20

def dec(m , k):
	k_m = []
	for count in range(N-1):
		k_m.append(k%m)
		k = k // m
	k_m.append(k)
	return(k_m)

for k in range(K):
	Takesakusen.append(dec(m , k))


for T in Takesakusen:
	if 1 in T and 2 in T and 3 in T:
		for s in range(N):
			if T[s] == 1:
				a += l[s]
				jointmagic += 10
			if T[s] == 2:
				b += l[s]
				jointmagic += 10
			if T[s] == 3:
				c += l[s]
				jointmagic += 10
		if magic > abs(a - A) + abs(b - B) + abs(c - C) + jointmagic - 30 :
			magic = abs(a - A) + abs(b - B) + abs(c - C) + jointmagic - 30








	a = 0
	b = 0
	c = 0
	jointmagic = 0

print(magic)

Solution (Python):
N, A, B, C = [int(_) for _ in input().split()]
H = [int(input()) for _ in range(N)]
INF = 1000 * 1000

def dfs(h, a, b, c):
    if not h:
        return INF if a * b * c == 0 else abs(A - a)+  abs(B - b) + abs(C - c)

    k = h[0]
    return min(dfs(h[1:], a, b, c),
               dfs(h[1:], a + k, b, c) + 10,
               dfs(h[1:], a, b + k, c) + 10,
               dfs(h[1:], a, b, c + k) + 10)

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

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

import itertools
import copy

MP = []
l_temp=[]

for abc_perm in list(itertools.permutations([A,B,C])):
    l_temp = copy.deepcopy(l)
    MP_consumption = 0
    for i,j in enumerate(abc_perm):
        c_list = []
        MP_consumption_list = []
        min_index = 0
        for k in range(1,len(l_temp)-(2-i)+1):
            for c in list(itertools.combinations(l_temp,k)):
                c_list.append(c)
                MP_consumption_list.append( abs(j - sum(c)) + 10*(len(c)-1 ) )
        MP_consumption += min(MP_consumption_list)
        min_index = MP_consumption_list.index(min(MP_consumption_list))
        [l_temp.remove(a) for a in c_list[min_index]]
    MP.append(MP_consumption)
print(min(MP))

Solution (Python):


















import math
import numpy as np
import copy
from collections import defaultdict, Counter
from itertools import product
from bisect import bisect_left, bisect_right
def inpl(): return list(map(int, input().split()))

li = list(map(int, input().split()))
N, A, B, C = li[0], li[1], li[2], li[3]
l = sorted([ int(input()) for i in range(N) ])

INF = 1e9

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

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

Solution (Python):


















import math
import numpy as np
import copy
from collections import defaultdict, Counter
from itertools import product
from bisect import bisect_left, bisect_right
def inpl(): return list(map(int, input().split()))

li = list(map(int, input().split()))
N, A, B, C = li[0], li[1], li[2], li[3]
l = sorted([ int(input()) for i in range(N) ])

ans = 1e9



for X in product(range(4), repeat=N):
    
    
    P = [[] for _ in range(4)]
    for i, x in enumerate(X):
        P[x].append(l[i])

    
    if min(map(len, P[1:])) == 0:
        continue

    tmp = 0
    for q, d in zip(P[1:], [A, B, C]):
        tmp += 10*(len(q)-1) + abs(d-sum(q))
    if tmp < ans:
        ans = min(tmp, ans)

print(ans)

Solution (Python):

import copy
import numpy as np
import random



def to_4digit(i,n):
    digits = [0 for i in range(n)]
    digit = 0
    while(i>=1):
        digits[digit] = i%4
        digit += 1
        i//=4
    return digits

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

def solve(n,a,b,c,l):
    ans = 10**20
    for i in range(4**n-1):
        choice = to_4digit(i,n)
        abc = [0 for i in range(3)]
        mp = 0
        
        for j in range(len(choice)):
            if choice[j]==3:
                pass
            else:
                if abc[choice[j]]!=0:
                    mp += 10
                abc[choice[j]] += l[j]
        if 0 in abc:
            pass
        else:
            mp += abs(abc[0]-a)+abs(abc[1]-b)+abs(abc[2]-c)
            if ans>mp:
                ans = mp


        
    return ans


if __name__ == "__main__":
    main()

Solution (Python):

from itertools import product
def inpl(): return list(map(int, input().split()))

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

ans = float("INF")

for X in product(range(4), repeat=N):
    P = [[] for _ in range(4)]
    for i, x in enumerate(X):
        P[x].append(L[i])

    if min(map(len, P[1:])) == 0:
        continue

    tmp = 0
    for Q, D in zip(P[1:], [A, B, C]):
        tmp += 10*(len(Q)-1) + abs(D-sum(Q))
    if tmp < ans:
        ans = min(tmp, ans)

print(ans)

Solution (Python):
def dfs(i, A, B, C):
    global ans
    if i == n:
        if A and B and C:
            ans = min(ans, abs(a - sum(A)) + abs(b - sum(B)) + abs(c - sum(C)) + (len(A) + len(B) + len(C) - 3) * 10)
    else:
        dfs(i + 1, A, B, C)
        dfs(i + 1, A + [L[i]], B, C)
        dfs(i + 1, A, B + [L[i]], C)
        dfs(i + 1, A, B, C + [L[i]])

n, a, b, c = map(int, input().split())
L = [int(input()) for i in range(n)]
ans = float("inf")
dfs(0, [], [], [])
print(ans)

Solution (Python):
def dfs(i, A, B, C):
    global ans
    if i == n:
        if A and B and C:
            ans = min(ans, abs(a - sum(A)) + abs(b - sum(B)) + abs(c - sum(C)) + (len(A) + len(B) + len(C) - 3) * 10)
    else:
        
        dfs(i + 1, A, B, C)
        dfs(i + 1, A + [L[i]], B, C)
        dfs(i + 1, A, B + [L[i]], C)
        dfs(i + 1, A, B, C + [L[i]])

n, a, b, c = map(int, input().split())
L = [int(input()) for i in range(n)]
ans = float("inf")
dfs(0, [], [], [])
print(ans)

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

INF = 10 ** 5

def dfs(current, a, b, c):
    if current == N:
        if min(a, b, c) > 0:
            return abs(a - A) + abs(b - B) + abs(c - C) - 30
        else:
            return INF

    ret0 = dfs(current + 1, a, b, c)
    ret1 = dfs(current + 1, a + l_list[current], b, c) + 10
    ret2 = dfs(current + 1, a, b + l_list[current], c) + 10
    ret3 = dfs(current + 1, a, b, c + l_list[current]) + 10
    return min(ret0, ret1, ret2, ret3)

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

Solution (Python):
N,A,B,C = map(int,input().split())
L = []
for i in range(N):
  L.append(int(input()))
MP = float('inf')
n = 4  
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)

for X in range(4**N):
  STR = Base_10_to_n(X,n).zfill(N)
  nU = []
  uA = []
  uB = []
  uC = []
  SUBMP = 0
  for l in range(N):
    if STR[l] == '0':
      nU.append(L[l])
    elif STR[l] == '1':
      uA.append(L[l])
    elif STR[l] == '2':
      uB.append(L[l])
    else:
      uC.append(L[l])
    if len(uA) != 0 and len(uB) != 0 and len(uC) != 0:
      SUBMP = (len(uA)+len(uB)+len(uC)-3)*10+abs(sum(uA)-A)+abs(sum(uB)-B)+abs(sum(uC)-C)
      if SUBMP <= MP:
        MP = SUBMP
print(MP)

Solution (Python):
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))

Solution (Python):
N, A, B, C = map(int, input().split())
l = [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 + 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))

Solution (Python):


"""
参考：https://img.atcoder.jp/abc119/editorial.pdf
・公式解の再帰
"""

import sys, re
from collections import deque, defaultdict, Counter
from math import sqrt, hypot, factorial, pi, sin, cos, radians, log10
if sys.version_info.minor >= 5: from math import gcd
else: from fractions import gcd 
from heapq import heappop, heappush, heapify, heappushpop
from bisect import bisect_left, bisect_right
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul, xor
from copy import copy, deepcopy
from functools import reduce, partial
from fractions import Fraction
from string import ascii_lowercase, ascii_uppercase, digits
from datetime import date

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def round(x): return int((x*2+1) // 2)
def fermat(x, y, MOD): return x * pow(y, MOD-2, MOD) % MOD
def lcm(x, y): return (x * y) // gcd(x, y)
def lcm_list(nums): return reduce(lcm, nums, 1)
def gcd_list(nums): return reduce(gcd, nums, nums[0])
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N,A,B,C=MAP()
L=[INT() for i in range(N)]
ABC=[A,B,C]

def rec(i, a, b, c):
    if i==N:
        
        if min(a, b, c)==0:
            return INF
        
        a=abs(A-a)
        b=abs(B-b)
        c=abs(C-c)
        
        return a+b+c-30
    res=INF
    
    res=min(res, rec(i+1, a, b, c))
    
    res=min(res, rec(i+1, a+L[i], b, c)+10)
    res=min(res, rec(i+1, a, b+L[i], c)+10)
    res=min(res, rec(i+1, a, b, c+L[i])+10)
    return res

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

Solution (Python):
INF = 1000000007


def dfs(cur, a, b, c):
    if cur == N:
        return abs(a - A) + abs(b - B) + abs(c - C) - 30\
            if 0 < min(a, b, c) else INF

    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
    ret4 = dfs(cur + 1, a, b, c)

    return min(ret1, ret2, ret3, ret4)


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

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

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

Solution (Python):

"""
Created on Sat Mar  2 11:07:19 2019

@author: Yamazaki Kenichi
"""

N,A,B,C = map(int,input().split())
l = [int(input()) for i in range(N)]
ABC = [0,A,B,C]
def basen(x,n):
    if x//n >0:
        return basen(x//n,n)+str(x%n)
    return str(x%n)

x = []
ans = 10**5
for i in range(4**N):
    k = basen(i,4).zfill(N)
    if any(k.count(c)<1 for c in '123'):
        continue
    abc = ABC[:]
    for j in range(N):
        abc[int(k[j])] -= l[j]
    ans = min(ans,
              (N-k.count('0')-3)*10
              +abs(abc[1])+abs(abc[2])+abs(abc[3]))
print(ans)

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

def ans(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 = ans(cur + 1,a,b,c) 
    ret1 = ans(cur + 1,a + l[cur],b,c) + 10
    ret2 = ans(cur + 1,a,b + l[cur],c) + 10
    ret3 = ans(cur + 1,a,b,c + l[cur]) + 10

    return min(ret0,ret1,ret2,ret3)

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

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

INF = pow(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))

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

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

INF = 10**9
  
def dfs(depth, a, b, c): 
  if depth == N:
    if min(a, b, c) > 0:
      return abs(A-a) + abs(B-b) + abs(C-c) - 30
      
    else:
      return INF 
  else:
    
    case_a = dfs(depth+1, a+L[depth], b, c) + 10
    
    case_b = dfs(depth+1, a, b+L[depth], c) + 10
    
    case_c = dfs(depth+1, a, b, c+L[depth]) + 10
    
    case_x = dfs(depth+1, a, b, c) 

    return min(case_a, case_b, case_c, case_x)
  
print(dfs(0,0,0,0))

Solution (Python):
def solve(V, take, A, B, C):
    make = [0, 0, 0] 
    cnt = [0, 0, 0] 
    cost = 0
    for i in range(N):
        if V[i] == 0:
            continue
        elif V[i] == 1:
            make[0] += take[i]
            if cnt[0] != 0:
                cost += 10
            cnt[0] += 1
        elif V[i] == 2:
            make[1] += take[i]
            if cnt[1] != 0:
                cost += 10
            cnt[1] += 1
        else:
            make[2] += take[i]
            if cnt[2] != 0:
                cost += 10
            cnt[2] += 1
    
    
    if 0 in make:
        return 100000
    cost += (abs(make[0]-A) + abs(make[1]-B) + abs(make[2]-C))
    return cost


        


temp = list(map(int, input().split()))
N = temp[0]
A = temp[1]
B = temp[2]
C = temp[3]
take = []
min = 100000
for i in range(N):
    take.append(int(input()))
zenbu = 4**N
for i in range(zenbu):
    bit = i
    vec = []
    for j in range(N):
        vec.append(bit % 4)
        bit //= 4
    cost = solve(vec, take, A, B, C)
    if cost < min:
        min = cost
print(str(min))

Solution (Python):
N, A, B, C = list(map(int, input().split(' ')))
bam_list = []

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

INF = 10000000

def enumerate(bam_list, a, b, c):
  if len(bam_list) == 0:
    return abs(A - a) + abs(B - b) + abs(C - c) - 30 if min(a, b, c) > 0 else  INF
  cur = bam_list[0]
  patA = enumerate(bam_list[1:], a + cur , b, c) + 10
  patB = enumerate(bam_list[1:], a, b+cur, c) + 10
  patC = enumerate(bam_list[1:], a, b, c+cur) + 10
  patD = enumerate(bam_list[1:], a, b, c)
  return min(patA, patB, patC, patD)

print(enumerate(bam_list, 0, 0, 0))

Solution (Python):
N, A, B, C = list(map(int, input().split(' ')))
bam_list = []

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

INF = 10000000

def enumerate(bam_list, a, b, c):
  if len(bam_list) == 0:
    return abs(A - a) + abs(B - b) + abs(C - c) - 30 if min(a, b, c) > 0 else  INF
  cur = bam_list[0]
  patA = enumerate(bam_list[1:], a + cur , b, c) + 10
  patB = enumerate(bam_list[1:], a, b+cur, c) + 10
  patC = enumerate(bam_list[1:], a, b, c+cur) + 10
  patD = enumerate(bam_list[1:], a, b, c)
  return min(patA, patB, patC, patD)

print(enumerate(bam_list, 0, 0, 0))

Solution (Python):
import itertools
n, a, b, c = map(int ,input().split())
ln = [int(input()) for _ in range(n)]

ans = 10**10
for l in itertools.product([0, 1, 2, 3], repeat = n):
    if 1 in l and 2 in l and 3 in l:
        count = [0] * 3
        total = [0] * 3
        for i, li in enumerate(l):
            if li != 0:
                count[li-1] += 1
                total[li-1] += ln[i]
        cost = 0
        cost += abs(a - total[0]) + 10 * (count[0] - 1)
        cost += abs(b - total[1]) + 10 * (count[1] - 1)
        cost += abs(c - total[2]) + 10 * (count[2] - 1)
        if ans > cost:
            ans = cost
print(ans)

Solution (Python):
ri = lambda: int(input())
rl = lambda: list(map(int,input().split()))

N,A,B,C=rl()
l=[ri() for _ in range(N)]
ans = 10**18
import itertools
for seq in itertools.product(range(4),repeat=N):
    a,b,c=0,0,0
    rst = 0
    for i in range(N):
        if seq[i]==1:
            a+=l[i]
            rst+=10
        elif seq[i]==2:
            b+=l[i]
            rst+=10
        elif seq[i]==3:
            c+=l[i]
            rst+=10
    if a*b*c != 0:
        rst += abs(A-a)+abs(B-b)+abs(C-c)-30
        if ans > rst:
            ans = rst

print(ans)

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

def dfs(depth,aa,bb,cc):
        if depth==n:
                
                ret=abs(a-aa)+abs(b-bb)+abs(c-cc)-30
                if min(aa,bb,cc)>0:
                        return ret
                else:
                        return 10**9+7
        ause=dfs(depth+1,aa+l[depth],bb,cc)+10
        buse=dfs(depth+1,aa,bb+l[depth],cc)+10
        cuse=dfs(depth+1,aa,bb,cc+l[depth])+10
        nonuse=dfs(depth+1,aa,bb,cc)
        return min(ause,buse,cuse,nonuse)

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